行不会出现在opengl中

时间:2017-04-27 13:26:44

标签: c++ opengl

我正在尝试在opengl中绘制一个简单的行,并且我已正确设置了environemt,并且在执行代码时,我得到一个空白屏幕而不是行。

这是我的代码

#include "stdafx.h"
#include "freeglut.h"
#include <Windows.h> 
#include <iostream>

using namespace std;

void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();

glColor3f(1.0, 0.0, 0.0);
glPointSize(5.0);
glLineWidth(5.0);
glBegin(GL_LINES);
glVertex2d(0.0, 0.0);
glVertex2d(0.5,0.5);
glEnd();

}



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

// Initialize GLUT
glutInit(&argc, argv);
// Set up some memory buffers for our display
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
// Set the window size
glutInitWindowSize(800, 600);
// Create the window with the title "Hello,GL"
glutCreateWindow("Hello, GL");
// Bind the two functions (above) to respond when necessary
glutReshapeFunc(reshape);
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

1 个答案:

答案 0 :(得分:6)

在显示功能中,您在绘制线之前交换了缓冲区。在绘制线之后你必须交换缓冲区。所以你的代码应如下所示:

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glColor3f(1.0, 0.0, 0.0);
glPointSize(5.0);
glLineWidth(5.0);
glBegin(GL_LINES); 
glVertex2d(0.0, 0.0);
glVertex2d(0.5,0.5);
glEnd();

glutSwapBuffers();
}