前言

前面已经用Visual Studio成功运行了OpenGL了,但是VS的界面比较丑,而且代码提示和补全等各方面不够人性化(主要是我不习惯),相比之下,JB家的CLion虽然运行上慢一点,bug多一点,配置麻烦一点(喂!这缺点太多了吧),但是为了颜值和流畅的编程体验,用一下也不是不可以。

配置

安装什么的就不说了,新建一个项目,Clion会自动生成一个CMakeLists.txtmain.cpp,点击运行没毛病,那就进入opengl的环境配置。
我这里用的是glut库,别的库参照着配置问题应该不大。在vs里我们是通过右键项目属性设置“三大件”的,但CLion是通过cmake来管理编译和链接的,只能通过手写CMakelists来进行配置。其实配置思路都差不多,手写配置还可以更清晰。
以下是CMakeLists.txt内容:

cmake_minimum_required(VERSION 3.16)
project(gl_demo)

set(CMAKE_CXX_STANDARD 14)

include_directories(C:/dev/glut/include) # 添加include头文件目录
link_directories(C:/dev//glut/lib) # 添加lib库目录

add_executable(gl_demo main.cpp)
target_link_libraries(gl_demo glut32.lib opengl32.lib) # 添加链接的具体lib项

以下是demo

// Lines.c
// OpenGL SuperBible, Chapter 4
// Demonstrates primitive GL_LINES
// Program by Richard S. Wright Jr.

#include <gl/glut.h>
#include <math.h>

// Define a constant for the value of PI
#define GL_PI 3.1415f

// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;


// Called to draw scene
void RenderScene(void) {
    GLfloat x, y, z, angle; // Storage for coordinates and angles

    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT);

    // Save matrix state and do the rotation
    glPushMatrix();
    glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    glRotatef(yRot, 0.0f, 1.0f, 0.0f);

    // Call only once for all remaining points
    glBegin(GL_LINES);

    z = 0.0f;
    for (angle = 0.0f; angle <= GL_PI * 3.0f; angle += 0.5f) {
        // Top half of the circle
        x = 50.0f * cos(angle);
        y = 50.0f * sin(angle);
        glVertex3f(x, y, z);

        // Bottom half of the circle
        x = 50.0f * cos(angle + 3.1415f);
        y = 50.0f * sin(angle + 3.1415f);
        glVertex3f(x, y, z);
    }

    // Done drawing points
    glEnd();

    // Restore transformations
    glPopMatrix();

    // Flush drawing commands
    glutSwapBuffers();
}

// This function does any needed initialization on the rendering
void SetupRC() {
    // Black background
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    // Set drawing color to green
    glColor3f(0.0f, 1.0f, 0.0f);
}

void SpecialKeys(int key, int x, int y) {
    if (key == GLUT_KEY_UP)
        xRot -= 5.0f;

    if (key == GLUT_KEY_DOWN)
        xRot += 5.0f;

    if (key == GLUT_KEY_LEFT)
        yRot -= 5.0f;

    if (key == GLUT_KEY_RIGHT)
        yRot += 5.0f;

    if (key > 356.0f)
        xRot = 0.0f;

    if (key < -1.0f)
        xRot = 355.0f;

    if (key > 356.0f)
        yRot = 0.0f;

    if (key < -1.0f)
        yRot = 355.0f;

    // Refresh the Window
    glutPostRedisplay();
}

void KeyPressFunc(unsigned char key, int x, int y) {
    if (key == 32)
        xRot -= 5.0f;

    if (key == 97)
        xRot += 5.0f;

    if (key == 98)
        yRot -= 5.0f;

    if (key == 99)
        yRot += 5.0f;

    if (key > 356.0f)
        xRot = 0.0f;

    if (key < -1.0f)
        xRot = 355.0f;

    if (key > 356.0f)
        yRot = 0.0f;

    if (key < -1.0f)
        yRot = 355.0f;

    // Refresh the Window
    glutPostRedisplay();
}

void ChangeSize(int w, int h) {
    GLfloat nRange = 100.0f;

    // Prevent a divide by zero
    if (h == 0)
        h = 1;

    // Set Viewport to window dimensions
    glViewport(0, 0, w, h);

    // Reset coordinate system
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Establish clipping volume (left, right, bottom, top, near, far)
    if (w <= h)
        glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange);
    else
        glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc, char *argv[]) {

    GLsizei h = 500;
    GLsizei w = 500;
    GLsizei x = 200, y = 100;

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

    glutInitWindowSize(h, w);
    glutInitWindowPosition(x, y);
    glutCreateWindow("Lines Example");

    glutDisplayFunc(RenderScene);
    glutReshapeFunc(ChangeSize);
    glutSpecialFunc(SpecialKeys);
    glutKeyboardFunc(KeyPressFunc);

    SetupRC();
    glutMainLoop();

    return 0;
}
Last modification:June 4th, 2020 at 09:31 pm