I made some code to produce boxes every time my code encounters a white pixel in my level image. However, it doesn’t seem to work. It only works if I comment out the actual loading from image parts.
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(-5, 5, -5, 5, -20, 20);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
float camPosX=0, camPosY=0;
for(int i=0;i<level.getWidth();i++){
for(int j=0;j<level.getHeight();j++){
if(level.getRGB(i, j)==Color.red.getRGB()){camPosX=i;camPosY=j;}
}
}
System.out.println("Camera position is "+camPosX+", "+camPosY);
int x=0;
while (!Display.isCloseRequested()) {
Display.sync(60);
//poll for keypresses first, default key is 'forward'
if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD8));
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT|GL11.GL_COLOR_BUFFER_BIT);
//GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glColor3f(0, 0, 0);
for(int i=0;i<level.getWidth();i++){
for(int j=0;j<level.getHeight();j++){
if(level.getRGB(i, j)==Color.WHITE.getRGB()){
GL11.glTranslatef(-i, 0, -j);
GL11.glRotatef(45, 1, 1, 1);
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
GL11.glVertex3f( 0.5f, 0.5f, 0.5f); // Top Right Of The Quad (Front)
GL11.glVertex3f(-0.5f, 0.5f, 0.5f); // Top Left Of The Quad (Front)
GL11.glVertex3f(-0.5f,-0.5f, 0.5f); // Bottom Left Of The Quad (Front)
GL11.glVertex3f( 0.5f,-0.5f, 0.5f); // Bottom Right Of The Quad (Front)
GL11.glVertex3f( 0.5f,-0.5f,-0.5f); // Bottom Left Of The Quad (Back)
GL11.glVertex3f(-0.5f,-0.5f,-0.5f); // Bottom Right Of The Quad (Back)
GL11.glVertex3f(-0.5f, 0.5f,-0.5f); // Top Right Of The Quad (Back)
GL11.glVertex3f( 0.5f, 0.5f,-0.5f); // Top Left Of The Quad (Back)
GL11.glVertex3f(-0.5f, 0.5f, 0.5f); // Top Right Of The Quad (Left)
GL11.glVertex3f(-0.5f, 0.5f,-0.5f); // Top Left Of The Quad (Left)
GL11.glVertex3f(-0.5f,-0.5f,-0.5f); // Bottom Left Of The Quad (Left)
GL11.glVertex3f(-0.5f,-0.5f, 0.5f); // Bottom Right Of The Quad (Left)
GL11.glVertex3f( 0.5f, 0.5f,-0.5f); // Top Right Of The Quad (Right)
GL11.glVertex3f( 0.5f, 0.5f, 0.5f); // Top Left Of The Quad (Right)
GL11.glVertex3f( 0.5f,-0.5f, 0.5f); // Bottom Left Of The Quad (Right)
GL11.glVertex3f( 0.5f,-0.5f,-0.5f); // Bottom Right Of The Quad (Right)
GL11.glEnd();
GL11.glLoadIdentity();
}
}
}
GL11.glTranslatef(-camPosX, 0, -camPosY);
Display.update();
}
In the main loop where you generate the boxes, you probably want to do
The line
probably does not what you think it does. It does not have any effect on the following call
Display.update(). However, it will affect the next run of your main loop, because you do not callGL11.glLoadIdentity()at the beginning of the main loop. I suggest moving it to the beginning of the main loop and callingglLoadIdentity()before.