user2649681 user2649681 - 4 months ago 21
Java Question

LWJGL rendering nothing

I've looked over the following program over and over and cannot find why nothing renders to the screen when I run it:

Game.java:

public class Game {

private int fbid, fbid2;

public void start(){
DisplayManager.init();

glViewport(0, 0, DisplayManager.SCR_WIDTH, DisplayManager.SCR_HEIGHT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 0, DisplayManager.SCR_WIDTH, DisplayManager.SCR_HEIGHT, 1, -1);

glMatrixMode(GL_MODELVIEW);

FloatBuffer fb = BufferUtils.createFloatBuffer(6);
fb.put(100).put(100).put(100).put(200).put(200).put(200);
fb.flip();

fbid = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, fbid);
glBufferData(GL_ARRAY_BUFFER, fb, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

FloatBuffer fb2 = BufferUtils.createFloatBuffer(9);
fb2.put(1).put(1).put(1).put(1).put(1).put(1).put(1).put(1).put(1);
fb2.flip();

fbid2 = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, fbid2);
glBufferData(GL_ARRAY_BUFFER, fb2, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

while(!DisplayManager.shouldClose()){
render();
}

DisplayManager.destroy();
}

public void render(){
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 0, 0, 0);

glColor3f(1, 1, 1);

glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, fbid);
glVertexPointer(2, GL_FLOAT, 0, 0);

glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, fbid2);
glColorPointer(3, GL_FLOAT, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, 3);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

DisplayManager.update();
}

}


DisplayManager.java:

public class DisplayManager {

public static final int SCR_WIDTH = 640;
public static final int SCR_HEIGHT = 480;

public static final int FPS = 30;

public static void init(){
try {
Display.setDisplayMode(new DisplayMode(SCR_WIDTH, SCR_HEIGHT));
Display.setTitle("StepBattler");
Display.setResizable(false);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
}

public static void update(){
Display.update();
Display.sync(FPS);
}

public static void destroy(){
Display.destroy();
System.exit(0);
}

public static boolean shouldClose(){
return Display.isCloseRequested();
}

public static boolean shouldResize(){
return Display.wasResized();
}

}


When I run the screen, I just get a window filled with black, and I cannot understand why a white triangle won't render to the window. I've messed around with
glClearColor()
, and changing its values does succeed in changing the window background color, but I can't make a white triangle appear in the window. Also, I tried using
glBegin()...glEnd()
to render in immediate mode, with none of the code having to do with buffers in there, and it still didn't render anything. I'm really confused about this, what am I missing?

Answer

The arguments in you glOrtho() call look to be in the wrong order:

glOrtho(0, 0, DisplayManager.SCR_WIDTH, DisplayManager.SCR_HEIGHT, 1, -1);

The signature of glOrtho(), based on the man page, is:

void glOrtho(GLdouble left, GLdouble right,
             GLdouble bottom, GLdouble top,
             GLdouble nearVal,  GLdouble farVal);

Since you're passing 0 for both left and right, the result would be an invalid projection matrix.

The correct call for your case is:

glOrtho(0, DisplayManager.SCR_WIDTH, 0, DisplayManager.SCR_HEIGHT, 1, -1);
Comments