OpenGL:Windows+Clion+MinGW+freeglut配置教程

Clion配置OpenGL+freeglut参考教程

用CLion配置OpenGL的相关操作看的我非常乱,用了很长时间,结果发现其实配置这个只需要几个步骤,希望对大家有所帮助吧!

  1. 下载freeglut 下载链接

  2. 解压后将压缩包里的lib文件夹和include文件夹复制到自己建的工程中
    OpenGL:Windows+Clion+MinGW+freeglut配置教程_第1张图片

  3. 修改cmakelists.txt文件,我的cmakelists.txt文件如下,Demo1更改为自己的工程名即可

cmake_minimum_required(VERSION 3.17)
project(Demo1 C)

set(CMAKE_C_STANDARD 99)

include_directories(include)
link_directories(lib/x64)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin) //指定输出的位置为bin文件夹

add_executable(Demo1 main.c)

target_link_libraries(Demo1 libfreeglut.a opengl32.dll libglu32.a)
  1. 将freeglut中的bin/x64文件夹打开,将里面的freeglut.dll文件复制到自己建立的工程中cmake-build-debug\bin文件夹中即可。

我在完成上述步骤后就可以运行OpenGL程序了,希望对大家有所帮助,最后附上测试用的源代码(C语言)

#include 
#include 
#include 
#include 

#define PI 3.1415926

GLenum errorCheck(){
     
    GLenum  code;
    const GLubyte* string;
    code = glGetError();
    if(code != GL_NO_ERROR){
     
        string = gluErrorString(code);
        fprintf(stderr , "OpenGL error:%s\n", string);
    }
    return code;
}

void init(void){
     
    glClearColor(0.0,0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0,400.0,0.0,400.0);
}

void Five_Pointed_Star(void){
     
    glClear(GL_COLOR_BUFFER_BIT);

    double r = 200 ;

    int pointA_x = r*(1-cos(18*PI/180)) ,pointA_y = r*(1+sin(18*PI/180));
    int pointB_x = 200 ,pointB_y = 400;
    int pointC_x = r*(1+cos(18*PI/180)), pointC_y = r*(1+sin(18*PI/180));
    int pointD_x = r*(1+cos(54*PI/180)), pointD_y = r*(1-sin(54*PI/180));
    int pointE_x = r*(1-cos(54*PI/180)), pointE_y = r*(1-sin(54*PI/180));


    glColor3f(0.0,0.4,0.2);
    glBegin(GL_LINES);
    glVertex2i(pointA_x,pointA_y);
    glVertex2i(pointC_x,pointC_y);
    glVertex2i(pointB_x,pointB_y);
    glVertex2i(pointE_x,pointE_y);
    glVertex2i(pointB_x,pointB_y);
    glVertex2i(pointD_x,pointD_y);
    glVertex2i(pointA_x,pointA_y);
    glVertex2i(pointD_x,pointD_y);
    glVertex2i(pointC_x,pointC_y);
    glVertex2i(pointE_x,pointE_y);
    glEnd();

    glFlush();
}

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

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(75,50);
    glutInitWindowSize(400,400);
    glutCreateWindow("An Example OpenGL Program");

    init();
    glutDisplayFunc(Five_Pointed_Star);
    glutMainLoop();
    return 0;
}

运行结果是一颗五角星

OpenGL:Windows+Clion+MinGW+freeglut配置教程_第2张图片

你可能感兴趣的:(OpenGL,opengl)