简体   繁体   English

行不会出现在opengl中

[英]Line won't appear in opengl

I am trying to draw a simple line in opengl and i have set up the environemt properly and when executing the code i get a blank screen rather than the line. 我试图在opengl中画一条简单的线,并且我已经正确设置了environemt,并且在执行代码时我得到了黑屏,而不是这条线。

This is my code 这是我的代码

#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;
}

In your display function you have swapped the buffers before the line is drawn. 在显示功能中,您已在绘制线条之前交换了缓冲区。 you have to swap the buffers after the line is drawn . 您必须在绘制线条后交换缓冲区。 so your code should appear as follows: 因此您的代码应如下所示:

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

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

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