现在应该去学glut 还是glfw知乎

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了现在应该去学glut 还是glfw知乎相关的知识,希望对你有一定的参考价值。

使用glew和glfw进行opengl编程freeglut和glut是很多教程使用的,不过现在glfw明显好多了,还有glew是windows下面使用opengl1.1以上版本api的比较好的法,更重要的是这两个都是跨平台的,用起来真的是很方便的说,[cpp]viewplaincopy#defineGLEW_STATIC #include #include #include #include charszTitle[64]="opengltutorial001-colortriangle"; staticvoiderror_callback(interror,constchar*description) fputs(description,stderr); staticvoidkey_callback(GLFWwindow*window,intkey,intscancode,intaction,intmods) if(key==GLFW_KEY_ESCAPE&&action==GLFW_PRESS) glfwSetWindowShouldClose(window,GL_TRUE); intmain(void) GLFWwindow*window; glfwSetErrorCallback(error_callback); if(!glfwInit())return-1; window=glfwCreateWindow(512,400,szTitle,NULL,NULL); if(!window) glfwTerminate(); exit(EXIT_FAILURE); glfwMakeContextCurrent(window); glfwSetKeyCallback(window,key_callback); glewExperimental=GL_TRUE; glewInit(); while(!glfwWindowShouldClose(window)) floatratio; intwidth,height; glfwGetFramebufferSize(window,&width,&height); ratio=width/(float)height; glViewport(0,0,width,height); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-ratio,ratio,-1.f,1.f,1.f,-1.f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef((float)glfwGetTime()*50.f,1.f,0.f,0.f); glBegin(GL_TRIANGLES); glColor3f(1.f,0.f,0.f); glVertex3f(-0.6f,-0.4f,0.f); glColor3f(0.f,1.f,0.f); glVertex3f(0.6f,-0.4f,0.f); glColor3f(0.f,0.f,1.f); glVertex3f(0.f,0.6f,0.f); glEnd(); glfwSwapBuffers(window); glfwPollEvents(); glfwDestroyWindow(window); glfwTerminate(); return0; 简单的一个例子 参考技术A 百度已经很让人无语了,去采集知乎上的数据,采集也得专业点呗。完全没有一点创新。连AI都不是,是赤裸裸的剽窃。玩你的莆田吧。 参考技术B 一看就是百度AI自动生成的问题,真服了你们百度了,天天搞这些没有意思~glfw吧

如何在 glfw 中 glutDisplayFunc、glutMainLoop?

【中文标题】如何在 glfw 中 glutDisplayFunc、glutMainLoop?【英文标题】:How to glutDisplayFunc, glutMainLoop in glfw? 【发布时间】:2019-08-03 09:08:22 【问题描述】:

我有一个使用 glut 的小程序,出于许多原因,我现在需要使用 glfw。由于我从未使用过 glfw,我遇到了很多问题。 主要是函数:glutDisplayFuncglutReshapeFuncglutIdleFuncglutMainLoop。我刚刚发现glfw中没有等效的功能。我应该如何修改我的程序?

我的程序是关于一个 3 维旋转的圆锥体

我有一个函数displaycone

void displayCone(void)

    // clear the drawing buffer.
    glClear(GL_COLOR_BUFFER_BIT);  //

    // set matrix mode
    glMatrixMode(GL_MODELVIEW);
    // clear model view matrix
    glLoadIdentity();
    // multiply view matrix to current matrix
    gluLookAt(3.0, 3.0, 3.0-4.5, 0.0, 0.0,-4.5,0,1,0);

    // ******
    glPushMatrix();


    glTranslatef(0.0, 0.0, -4.5);

    glBegin(GL_LINES);

    glColor3f (1.0, 1.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(2.0, 0.0, 0.0);

    glColor3f (1.0, 1.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 2.0, 0.0);

    glColor3f (1.0, 1.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 2.0);
    glEnd();

    glPopMatrix();

    // clear the drawing buffer.


    // traslate the draw by z = -4.0
    // Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
    glTranslatef(0.0,0.0,-4.5);
    // Red color used to draw.
    glColor3f(0.8, 0.2, 0.1);
    // changing in transformation matrix.
    // rotation about X axis
    glRotatef(xRotated,1.0,0.0,0.0);
    // rotation about Y axis
    glRotatef(yRotated,0.0,1.0,0.0);
    // rotation about Z axis
    glRotatef(zRotated,0.0,0.0,1.0);

    // scaling transfomation
    glScalef(1.0,1.0,1.0);
    // built-in (glut library) function , draw you a Cone.

    // move the peak of the cone to the origin
    glTranslatef(0.0, 0.0, -height);

    glutSolidCone(base,height,slices,stacks);
    // Flush buffers to screen
    // gluLookAt(3,3,3,0,0,-4.5,0,1,0); <----------------------- delete

    glFlush();
    // sawp buffers called because we are using double buffering
    // glutSwapBuffers();


一个函数reshapecone:

void reshapeCone(int x, int y)

    if (y == 0 || x == 0) return;  //Nothing is visible then, so return
    //Set a new projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //Angle of view:40 degrees
    //Near clipping plane distance: 0.5
    //Far clipping plane distance: 20.0

    gluPerspective(35.0,(GLdouble)x/(GLdouble)y,0.5,20.0);

    glViewport(0,0,x,y);  //Use the whole window for rendering

还有一个函数idleCone

void idleCone(void)

    for(int j = 1; j<10000 ; j++)
        double i = dati[j+1][0];

        int win = glfwGetWindow();

        if(i == 0.)      break; 

        xRotated = 180/M_PI*(dati[j][0]);
        yRotated = 180/M_PI*(dati[j][1]);
        zRotated = 180/M_PI*(dati[j][2]);

        displayCone();
        xRotated += 0.;
        yRotated += 0.;
        zRotated += 0.;
        displayCone();

    

在我之前的程序中,我在main

glfwInit(&argc, argv);
//double buffering used to avoid flickering problem in animation
glutInitDisplayMode(GLUT_SINGLE | GL_RGB);
//glfwInitWindowSize(800,700);

glfwCreateWindow(800,700,"Rotation of the top",NULL,NULL);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
xRotated = yRotated = zRotated = 0.0;
xRotated=0.0;
yRotated=0.0;
glClearColor(0.0,0.0,0.0,0.0);

glutDisplayFunc(displayCone);
glutReshapeFunc(reshapeCone);
glutIdleFunc(idleCone);

glutMainLoop();  

【问题讨论】:

【参考方案1】:

使用glfw 时,您必须创建自己的应用程序循环。请注意,在调用任何 OpenGL 指令之前,通过glfwMakeContextCurrent 使 OpenGL 上下文成为当前非常重要。例如:

GLFWwindow *wnd = glfwCreateWindow(800,700,"Rotation of the top",NULL,NULL);
glfwMakeContextCurrent(wnd);

// do the OpenGL initialization
// [...]

while (!glfwWindowShouldClose(wnd))

    // do the drawing
    displayCone();

    glfwSwapBuffers(wnd);
    glfwPollEvents();

您可以通过glfwSetWindowSizeCallback 设置大小回调,而不是glutReshapeFunc:例如:

glfwSetWindowSizeCallback(wnd, reshapeCone);
void reshapeCone(GLFWwindow* window, int x, int y)

    // [...]
  

【讨论】:

当我编译时我得到:注意:交叉初始化 'GLFWwindow* wnd' @Jatos 抱歉,我看不到您的实际代码,也不知道错误发生在哪一行。我在switch 语句中的GLFWwindow *wnd = glfwCreateWindow(...) 行? 现在我通过将 GLFWwindow 排除在 switch 语句之外来解决它。当我在 mac 终端上编译代码时说架构 x86_64 的未定义符号:“_glfwCreateWindow”,引用自:_main in ccR3zGkL.o @Jatos 可能OpenGL 3.3/4.1 on Mac OSX 10.9 using GLFW library 可以帮忙

以上是关于现在应该去学glut 还是glfw知乎的主要内容,如果未能解决你的问题,请参考以下文章

Haskell openGL 和 GLUT 在 Mac OS X 上冻结?我可以在 GLUT 上使用 GLFW 吗?

OpenGL2.0及以上版本中gl,glut,glew,glfw,mesa等部件的关系

opengl关于glut, freeglut, glfw, glew, glad, gl3w库的说明

学GO语言好还是java语言好??

如何在 glfw 中 glutDisplayFunc、glutMainLoop?

现在还值得学Python吗?