No graphics

Hello,

I have just started with OpenGl and I am using the LWJGL for it. I have created this code with the help of the OpenGl superbible.

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
 
public class Main {
 
   public Main(String title){	   
	   try {
		Display.setDisplayMode(new DisplayMode(600,400));
		Display.setTitle(title);
		Display.create();
	   }catch (LWJGLException e) {	
		e.printStackTrace();
	   }
	   
	   //Initialization
	  init();
	   
	   while(!Display.isCloseRequested()){
		
		   //Render
		  render();
		   
		   Display.update();
		   Display.sync(60);
		}
	   Display.destroy();
   	}
   
   public static void main(String[] args){
	  new Main("OpenGl tests");
   }
   
   private void init(){
	   
	   GL11.glMatrixMode(GL11.GL_PROJECTION); 
	   GL11.glLoadIdentity();   
	   GL11.glDisable(GL11.GL_DEPTH_TEST);
	   GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
	   GL11.glOrtho(0, 640, 0, 480, 1, -1);
	   GL11.glClearColor(.0f, .0f, .0f, .0f);
   }
   
   private void render(){
	   GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
	   
	   GL11.glRectf(20, 20, 20, 20);
	   
	   GL11.glFlush();
   }
}

But I got a problem there are no graphics in the window.

You set the clear color to black and never set the color of your rectangle so you are drawing a black rectangle on a black background. Try setting your rectangle to red or something

Still nothing.

The initial color is (R,G,B,A) = (1,1,1,1), so that shouldn’t be the problem. If your window is double buffered, then you will need to swap buffers to get what you have drawn into the back buffer to be displayed. To swap buffers, you’d call SwapBuffers(hdc) on Windows. Try clearing to a different color than black first, as when clearing to black it makes it hard to tell whether nothing is being drawn, or the clear is happening. You may also want to switch back to GL_MODELVIEW matrix mode after setting the GL_PROJECTION matrix.

Edit:
Looking up the docs at LWJGL - Lightweight Java Game Library it seems that SwapBuffers occurs in the update() function so that’s probably not a problem, but you don’t seem to have a Display.makeCurrent() call before issuing your OpenGL calls.

What do you think this code does:

glRectf(20, 20, 20, 20);

At most, you’ll get one vertex out of this since glRect is implemented to simply draw a quad using two corners, (x1,y1) and (x2,y2). Play around with the values. If still nothing shows afterwards there is something wrong in some other part of your code.

Apart from that, glRect defines the plane in the z=0 plane, which, given that you don’t translate the plane in any way, will simply have it cut right through the origin and thus the camera, so will see nothing. The projection alone won’t help as your current near and far plane distances will simply cancel each other out.

Try the following. In your init() method:



private void init(){        
     GL11.glEnable(GL11.GL_DEPTH_TEST); // there's no reason to not enable  the depth buffer test, as soon as your scene gets more complex you'll  need it

    // setup projection
    GL11.glMatrixMode(GL11.GL_PROJECTION);         
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 640, 0, 480, 1, 10); 
    GL11.glMatrixMode(GL11.GL_MODELVIEW);        
    
    // ok to do that, but remember that color vertex attributes default to black as well
    GL11.glClearColor(.0f, .0f, .0f, .0f); 
    
    // you can't render anything without a viewport since the last step in the transformation pipeline
    // is [i]viewport mapping [/i]- if no viewport is set there'll be no image
    GL11.glViewport(0, 0, 640, 480);
  }

And in your render function:



   private void render(){        
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    
    // this will first set the MODELVIEW matrix to the identity
    GL11.glLoadIdentity();

    // then we'll translate the rect back 5 units
    GL11.glTranslatef(0.f, 0.f, -5.f);

    // we set the color attribute of the vertices to red
    GL11.glColor3f(1.f, 0.f, 0.f);

    // and draw a rectangle around the origin
    GL11.glRectf(-1.f,  -1.f, 1.f, 1.f);
    GL11.glFlush();    
}


WARNING: This code is from the top of my head and untested. Please get back with your progress.

I have copied the code, but I still get a black screen with nothing.

If you change the clear color to something non-black, do you see that color then?

Did you try adding Display.makeCurrent() before issuing any OpenGL calls? You have OpenGL calls in init() so it would have to be before then.