noobs question

Started by jp, October 24, 2004, 22:33:27

Previous topic - Next topic

jp

I can not succeed in having something else than a black window with the following code, the simplest I found javascript:emoticon(':(')
Sad.
Maybe I am confused with the coordinates system but I tried many combinations without getting more javascript:emoticon(':?')
Confused. There could be a deeper reason that I can not catch.

Thank you for helping.

/*
* Created on 24 oct. 2004
*
* To change the template for this generated file go to
* Window&Preferences&Java&Code Generation&Code and Comments
*/
package jpa.airground;

import org.lwjgl.opengl.*;
import org.lwjgl.input.*;
import org.lwjgl.opengl.glu.*;

/**
* @author jpa
*
* To change the template for this generated type comment go to
* Window&Preferences&Java&Code Generation&Code and Comments
*/
public class Test {
   public static void main(String[] args) {
      System.out.println("Program started...");

      init();
      while (!Display.isCloseRequested()){
         render();   
      }
      System.out.println("Close requested : cleaning display...");
      destroy();
      System.out.println("...Program ended.");
   }
   
   public static void init() {
      try
      {
         int mode = -1;
         DisplayMode modes[] = Display.getAvailableDisplayModes();

         // Loop through all display mode available to find the requested one.
         for(int i = 0; i < modes.length; i++)
         {
            if(modes.getWidth() == 800
               && modes.getHeight() == 600
               && modes.getBitsPerPixel() >= 16)
            {
               mode = i;
               break;
            }
         }
          // Wanted display mode has not been found...
         if(mode != -1)
         {
            System.out.println("Setting display mode to " + modes[mode]);
            Display.setDisplayMode(modes[mode]);
         }
      }
      catch(Exception e)
      {
         e.printStackTrace(System.err);
      }
      
      try {
      
         Display.setFullscreen(false);
         Display.setVSyncEnabled(true);
         Display.setTitle("Test");
         Display.create(new PixelFormat(0, 16, 0));

         Mouse.create();
         Keyboard.create();
         Keyboard.enableBuffer();
         
         GL11.glMatrixMode(GL11.GL_PROJECTION);
         GL11.glLoadIdentity();
         GLU.gluPerspective(45.0f, 1.33f, 1.0f, 200.0f);
         GL11.glFlush();
         
         System.out.println("Engine initialized");
     
      } catch (Exception e) {
         e.printStackTrace(System.err);
      }
   }
   
   public static void render() {
      try {
         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
         GL11.glMatrixMode(GL11.GL_MODELVIEW);
         GL11.glLoadIdentity();
         GL11.glTranslatef(0.0f,0.0f,10.0f);
         GL11.glColor3f(1.0f,1.0f,1.0f);
         GL11.glBegin(GL11.GL_TRIANGLES);               // Drawing Using Triangles
            GL11.glVertex3f( 0.0f, 1.0f, 0.0f);            // Top
            GL11.glVertex3f(-1.0f,-1.0f, 0.0f);            // Bottom Left
            GL11.glVertex3f( 1.0f,-1.0f, 0.0f);            // Bottom Right
         GL11.glEnd();
         GL11.glFlush();
      } catch (Exception e) {
         e.printStackTrace(System.err);
      }
   }
      
   public static void destroy() {
      // Close the window
      Display.destroy();
   }
   
}

:(  :(

CaptainJester

Your line:

GL11.glTranslatef(0.0f,0.0f,10.0f);


should be:

GL11.glTranslatef(0.0f,0.0f,-10.0f);

Positive z is towards the viewer, negative into the screen.  You need to be more negative than 0.0 to see anything.(Depending on your settings of course.)

Check out http://nehe.gamedev.net/ for a lot of good OpenGL tutorials.  More than half have been converted to LWJGL.
The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities.  We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)
8)

princec

Also, as a friendly default, LWJGL sets up the OpenGL context with ordinary pixel-perfect 2D drawing - so you don't want to change Z anyway as it has no effect other than to unexpectedly clip your graphics to nothing!

And furthermore as it's 1:1 with screen coordinates your triangle would be 1 pixel big, if it is displayed at all!

Cas :)

jp

:D Thanks for your answers ! I changed the Z translation to -10, as the Z axis is oriented toward the viewer.
It is still black and I try to figure out what you said, princec, about the projection size. Are you saying than one unit in the 3D world match with 1 pixel on the 2D screen ?
I am surprised because I thought that the 3D world was projected on the near plane of the frustum (Z=1), which is spread by the projection on the whole viewport (the window size by default I suppose). Even at a 10 units distance, it should be larger than 1 pixel, shoudn't it ?
Maybe were you rather talking about the wireframe thickness ? Am I displaying a wireframe or colored surfaces in the default settings ?
I am gonna give a look to the NeHe tutorials anyway, thanks for the advice.

princec

By default LWJGL just sets it up so that drawing on the Z=0 plane is just like ordinary 2D drawing in any other 2D API (except of course (0,0) is the bottom left rather than the top left). So 1 point in the 3D world = 1 pixel on the screen. In orthographic projection it doesn't matter what Z coordinate you specify as it renders the same no matter.

Cas :)

cfmdobbie

Quote from: "jp"Are you saying than one unit in the 3D world match with 1 pixel on the 2D screen ?

As default, LWJGL sets up an orthogonal projection with a 1:1 screen-to-world coordinates perspective and a Z-range of -1 to 1.  If you want to display 3D, you'll likely want to call gluPerspective or glFrustum to set up the viewing transformation you want, but if you're drawing 2D you're able to use screen pixel coordinates - on a 1024x768 screen, a line from (0,0) to (1024,768) will go from corner to corner of the screen.

However, you're specifying your own viewing transformation, so that shouldn't affect you.
ellomynameis Charlie Dobbie.

jp

Thanks again for all your explanations. I think I catched the fact that one can mix 2D with 3D using Z between -1 and 1, with an exact pixel projection.
By the way, I found the reason of my black window in the NeHe lesson 04 (the translation for LWJGL, which is a great job, CaptainJester, and is very helpful :P).
I had just forgotten the Display.update() after render(), so that the Display was never refreshed :oops:.
Now I can try to go further, until the next silly mistake.