1

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

1 回答 1

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();
于 2013-06-02T02:56:58.337 回答