简体   繁体   English

OpenGL中的三角形

[英]Triangle in OpenGL

I'm trying to create a triangle with OpenGL but I see nothing but a white board on my screen.我正在尝试使用 OpenGL 创建一个三角形,但我在屏幕上只看到一块白板。

Here's my code:这是我的代码:

#include <GL/glut.h>

extern float vertices[3][3] = 
{
    { -0.5f, -0.5f, 0.0f },
    {  0.5f, -0.5f, 0.0f },
    {  0.0f,  0.5f, 0.0f }
};


void display()
{
    glClearColor(0, 0, 0, 1);
    glBegin(GL_TRIANGLES);
    glColor3fv(1, 0, 0);
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[2]);
    glEnd();
    glFlush();
}


int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(1000, 1000);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Hello Gult");
    glutDisplayFunc(display);
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
    return 0;
}

What could it be and how can I solve this?它可能是什么,我该如何解决这个问题?

You need to call glutSwapBuffers when using a double-buffered window ( GLUT_DOUBLE ).使用双缓冲 window ( GLUT_DOUBLE ) 时需要调用glutSwapBuffers Also, you must clear the depth buffer when the Depth Test is enabled.此外,您必须在启用深度测试时清除深度缓冲区。 Use glColor3f instead of glColor3fv when specifying a color with 3 separate arguments:当使用 3 个单独的glColor3f指定颜色时,使用 glColor3f 而不是glColor3fv

void display()
{
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glColor3f(1, 0, 0);
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[2]);
    glEnd();
    glutSwapBuffers();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM