为什么我的OpenGL代码不起作用 [英] Why is my OpenGL code not working

查看:109
本文介绍了为什么我的OpenGL代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OpenGL的完全新手,已经开始关注有关此主题的书.他们演示的第一个程序是Sierpinski Gasket程序.这是本书中的代码:

I'm a complete novice in OpenGL and have started following a book on this topic. The first program that they demonstrate is the Sierpinski Gasket program. Here's what the code looks like in the book:

#include<GL/glut.h>
#include<stdlib.h>
void myinit()
{
    //attributes
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,0.0,0.0); //draw in red

    //set up viewing
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,50.0,0.0,50.0);
    glMatrixMode(GL_MODELVIEW);
}
void display()
{
    GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}};  //triangle
    GLfloat p[2]={7.5,5.0};     //arbitrary point
    glClear(GL_COLOR_BUFFER_BIT);   //clear the window
    glBegin(GL_POINTS);
    //Gasket Program
    for(int i=0;i<5000;i++){
        int j=rand()%3;
        //compute points halfway between random vertex and arbitrary point
        p[0]=(vertices[j][0]+p[0])/2;
        p[1]=(vertices[j][1]+p[1])/2;
        //plot the point
        glVertex2fv(p);
    }
    glEnd();
    glFlush();
}
int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Sierpinski Gasket");
    glutDisplayFunc(display);
    myinit();
    glutMainLoop();
    return 0;
}

但是,当我编译程序时,它只显示一个完全用白色填充的窗口,而没有其他内容.上面的代码为什么不按应有的方式工作?

However, when I compile the program it only displays a window completely filled with white and nothing else. Why isn't the above code working the way it should?

推荐答案

glFlush()替换为glutSwapBuffers():

#include<GL/glut.h>

void display()
{
    glClearColor(1.0,1.0,1.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);   //clear the window

    //set up viewing
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,50.0,0.0,50.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}};  //triangle
    GLfloat p[2]={7.5,5.0};     //arbitrary point
    glColor3f(1.0,0.0,0.0); //draw in red
    glBegin(GL_POINTS);
    //Gasket Program
    for(int i=0;i<5000;i++)
    {
        int j=rand()%3;
        //compute points halfway between random vertex and arbitrary point
        p[0]=(vertices[j][0]+p[0])/2;
        p[1]=(vertices[j][1]+p[1])/2;
        //plot the point
        glVertex2fv(p);
    }
    glEnd();

    glutSwapBuffers();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(500,500);
    glutCreateWindow("Sierpinski Gasket");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

glFlush()实际上不会交换GLUT_DOUBLE请求的后/前缓冲区.为此,您需要glutSwapBuffers().

glFlush() won't actually swap the back/front buffers requested by GLUT_DOUBLE. You need glutSwapBuffers() for that.

这篇关于为什么我的OpenGL代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆