问题
When I call "GL11.glEnd" the polygon disappears. If I leave that one line "GL11.glEnd" out, then it stays on the screen, but when I put it in, the polygon I was drawing disappears.
package package01;
import org.lwjgl.opengl.GL11;
public class Graph {
...
void initGraph(){
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glOrtho(-500, 500, -281, 281, -1, 1);
GL11.glColor3f(0.8f, 0.8f, 1.0f);
GL11.glBegin(GL11.GL_POLYGON);
GL11.glVertex3f(-60, 110, 0);
GL11.glVertex3f(60, 110, 0);
GL11.glVertex3f(120, 0, 0);
GL11.glVertex3f(60, -110, 0);
GL11.glVertex3f(-60, -110, 0);
GL11.glVertex3f(-120, 0, 0);
GL11.glEnd();
GL11.glFlush();
}
}
And here is the second class if it would help.
package package01;
...
public class DisplayScreen {
Graph g = new Graph();
void start(){
try {
Display.setDisplayMode(new DisplayMode(1000,562));
Display.create();
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
} catch(LWJGLException e) {
e.printStackTrace();
}
while(!Display.isCloseRequested()){
Display.update();
g.initGraph();
}
Display.destroy();
}
public static void main(String[] args){
DisplayScreen ds = new DisplayScreen();
ds.start();
}
}
回答1:
remove GL11.glOrtho(-500, 500, -281, 281, -1, 1); from the initGraph() method.
add
glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
GL11.glOrtho(-500, 500, -281, 281, -1, 1);
glMatrixMode(GL11.GL_MODELVIEW);
before the game loop.
update needs to be called after initGraph g.initGraph(); Display.update();
来源:https://stackoverflow.com/questions/8950138/java-lwjgl-when-i-call-gl11-glend-the-polygon-disappears