OpenGL lwjgl不会绘制多边形

时间:2013-06-02 02:09:21

标签: java lwjgl

我在lwjgl openGL中创建并创建了一个三角形,但它没有显示我指定的方形和三角形。我被卡住了,我似乎无法让它工作,我是openGL lwjgl的新手。为什么不在画面上画画?

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

1 个答案:

答案 0 :(得分:1)

花了一点时间,但我找到了问题。第一个是如何初始化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();

使用:

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

使用窗口宽度替换800,使用高度替换600,您应该更改分辨率。

并在你的while循环中将这两行放在开头:

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