c++ - 线条不会出现在 opengl 中

标签 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;
}

最佳答案

在您的显示函数中,您已经在绘制线条之前交换了缓冲区。您必须在绘制线条后交换缓冲区。所以你的代码应该如下所示:

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();
}

关于c++ - 线条不会出现在 opengl 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43659262/

相关文章:

php - 调用正在运行的程序并在 php 中获得响应

c++ - 无法在 OpenGL 中随时间改变颜色

opengl - 使用光线转换时在深度缓冲区中写入正确的值

c++ - 在 CString.Format() 的区域设置中使用小数点分隔符

c++ - C++中如何定义从整数到 float 的精度损失?

c++ - 无法链接 GLFW3 : undefined references

c++ - 如何使用 STL 算法找到分隔字符串中两个不同字母的最短星号序列?

c++ - 没有累积缓冲区的 OpenGL 运动模糊

c++ - 投影和 View 矩阵

c++ - GL_TRIANGLE_STRIP 的 glDrawRangeElements 无法正常工作(使用 glMapBuffer)