简体   繁体   中英

OpenGL lwjgl won't draw polygon

I was doing and creating a triangle in the lwjgl openGL but it doesnt display the square and the triangle that I specify. I am stuck and I cant seemingly make it work, I am new to openGL lwjgl. why is it not drawing on the screen?

public Cube3D() {
    try {
        Display.setDisplayMode(new DisplayMode(640,480));
        Display.setTitle("Gaming");

        Display.create();
    } catch (LWJGLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //initiallized code OPENGL
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST); 
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glViewport(0, 0, 640, 480); 

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); 

    while(!Display.isCloseRequested()) {    //Reset The View
         glTranslatef(-1.5f,0.0f,-8.0f);    // Move Left 1.5 Units And Into
                                            // The Screen 8 (not 6.0 like
                                            // VC../ not sure why)
         glBegin(GL_TRIANGLES);             // Drawing Using Triangles
         glVertex3f( 0.0f, 1.0f, 0.0f);     // Top
         glVertex3f(-1.0f,-1.0f, 0.0f);     // Bottom Left
         glVertex3f( 1.0f,-1.0f, 0.0f);     // Bottom Right
         glEnd();                           // Finished Drawing The Triangle
         glTranslatef(3.0f,0.0f,0.0f);      // Move Right 3 Units
         glBegin(GL_QUADS);                 // Draw A Quad
         glVertex3f(-1.0f, 1.0f, 0.0f);     // Top Left
         glVertex3f( 1.0f, 1.0f, 0.0f);     // Top Right
         glVertex3f( 1.0f,-1.0f, 0.0f);     // Bottom Right
         glVertex3f(-1.0f,-1.0f, 0.0f);     // Bottom Left
         glEnd();

         Display.update();
         Display.sync(60);    
    }
    Display.destroy();
}
}

It took a little bit of time but I manage to find the problem. The first is how you initialize the opengl.

Replace:

glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST); 
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glViewport(0, 0, 640, 480); 

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

with:

glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
GLU.gluPerspective(45.0f, ((float) 800) / ((float) 600), 0.1f, 100.0f);
glMatrixMode(GL11.GL_MODELVIEW);
glLoadIdentity();
glEnable(GL11.GL_DEPTH_TEST);

replacing 800 with the width of the window and 600 with the height should you change the resolution.

and in your while loop put these two lines at the beginning:

GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

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