gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 00:09:22 26 4
gpt4 key购买 nike

我正在尝试在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com