在OpenGL

时间:2017-08-08 17:23:47

标签: c++ opengl glut

该线未显示。代码有什么问题?

#include<windows.h>
 //#ifdef __APPLE__
 //#include <GLUT/glut.h>
 //#else
#include <GL/glut.h>
//#endif
//#include <stdlib.h>

void init(void){
  glClearColor(1.0, 1.0,1.0,0.0);
  glMatrixMode(GL_PROJECTION);
  gluOrtho2D(0.0, 200.0, 0.0,150.0);
 }

 void lineSegment(void){
   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(1.0f, 0.0f, 0.0f);     // Red

  //glColor3f(0.2, 0.4, 0.2);
  glBegin(GL_LINE);
  glVertex2i(180,15);
  glVertex2i(10,145);
  glEnd();
  glFlush();
}


int main(int argc, char* argv[]){
   glutInit(&argc,argv);
   glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
   //glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
   glutInitWindowPosition(50,100);
   glutInitWindowSize(400,300);
   glutCreateWindow("An example OpenGL Program");
   init();
    glutDisplayFunc(lineSegment);
   glutMainLoop();
 return 0;
}

1 个答案:

答案 0 :(得分:0)

  • 您正在申请GLUT_DOUBLE - 缓存的窗口,但在glutSwapBuffers()回调结束时未能致电glutDisplayFunc()glFlush()还不够。
  • GL_LINE不是glBegin()的有效参数。您正在考虑GL_LINES

所有在一起:

#include <GL/glut.h>

void init()
{
    glClearColor( 1.0, 1.0, 1.0, 0.0 );
    glMatrixMode( GL_PROJECTION );
    gluOrtho2D( 0.0, 200.0, 0.0, 150.0 );
}

void lineSegment()
{
    glClear( GL_COLOR_BUFFER_BIT );
    glColor3f( 1.0f, 0.0f, 0.0f );     // Red
    glBegin( GL_LINES );
    glVertex2i( 180, 15 );
    glVertex2i( 10, 145 );
    glEnd();
    glutSwapBuffers();
}

int main( int argc, char* argv[] )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE ); // Enable double buffered mode
    glutInitWindowSize( 400, 300 );
    glutCreateWindow( "An example OpenGL Program" );
    init();
    glutDisplayFunc( lineSegment );
    glutMainLoop();
    return 0;
}