简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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