Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: No graphics

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2012
    Posts
    3

    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.
    Code :
    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.

  2. #2
    Junior Member Newbie
    Join Date
    Nov 2011
    Location
    Surrey
    Posts
    15
    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

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2012
    Posts
    3
    Still nothing.

  4. #4
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    456
    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 http://lwjgl.org/javadoc/org/lwjgl/opengl/Display.html 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.
    Last edited by Dan Bartlett; 08-20-2012 at 10:32 AM.

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    What do you think this code does:

    Code :
    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:

    Code :
     
    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:

    Code :
     
       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.
    Last edited by thokra; 08-20-2012 at 10:50 AM.

  6. #6
    Junior Member Newbie
    Join Date
    Aug 2012
    Posts
    3
    I have copied the code, but I still get a black screen with nothing.

  7. #7
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    If you change the clear color to something non-black, do you see that color then?

  8. #8
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    456
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •