Basic OpenGL Tutorial doesn`t work

Started by gonzo, October 31, 2008, 22:15:41

Previous topic - Next topic

gonzo

I tried the "Basic OpenGL Rendering" Tutorial of the wiki-page, but instead of a rotating square there is only a black screen. I tried it on Mac OSX and WindowsXP. This is the Code:

package lwjgl;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

public class Main {

  public static final String GAME_TITLE = "My Game";
  private static final int FRAMERATE = 60;
  private static boolean finished;
  private static float angle;

  public static void main(String[] args) {
    boolean fullscreen = (args.length == 1 && args[0].equals("-fullscreen"));

    try {
      init(fullscreen);
      run();
    } catch (Exception e) {
      e.printStackTrace(System.err);
      Sys.alert(GAME_TITLE, "An error occured and the game will exit.");
    } finally {
      cleanup();
    }
    System.exit(0);
  }

  private static void init(boolean fullscreen) throws Exception {
    Display.setTitle(GAME_TITLE);
    Display.setFullscreen(fullscreen);

    Display.setVSyncEnabled(false);

    Display.create();
  }

  private static void run() {

    while (!finished) {
      Display.update();

      if (Display.isCloseRequested()) {
   finished = true;
      }
      else if (Display.isActive()) {
        logic();
        render();
        Display.sync(FRAMERATE);
      }
      else {
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
        }
        logic();

        if (Display.isVisible() || Display.isDirty()) {
          render();
        }
      }
    }
  }

  private static void cleanup() {
    Display.destroy();
  }

  private static void logic() {
    if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
      finished = true;
    }

    angle += 2.0f % 360;
  }

  private static void render() {
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);

    GL11.glPushMatrix();
    GL11.glTranslatef(Display.getDisplayMode().getWidth() / 2, Display.getDisplayMode().getHeight() / 2, 0.0f);

    GL11.glRotatef(angle, 0, 0, 1.0f);

    GL11.glBegin(GL11.GL_QUADS);
      GL11.glVertex2i(-50, -50);
      GL11.glVertex2i(50, -50);
      GL11.glVertex2i(50, 50);
      GL11.glVertex2i(-50, 50);
    GL11.glEnd();

    GL11.glPopMatrix();
  }
}

Can someone help me please???

Matzon

check the Game in org.lwjgl.examples

The default OpenGL state was changed in 2.0, and you need set it to the proper values.

gonzo


Evil-Devil

I don't see any opengl init stuff like setting the viewport. You need at least a viewport to see something.

Matthias

You don't need to set the viewport - the default setting is the complete GL window.

broumbroum

Try pulling the Quad at another z-depth, 0f is the camera pos I think !

elam

I love all these guesses.

Hey Gonzo,

I got it to work by creating a projection matrix after the Display.create() method in the init() method:

GL11.glOrtho(0.0, Display.getDisplayMode().getWidth(), 0.0, Display.getDisplayMode().getHeight(), -1.0, 1.0);

Ya'll need to update the tutorials.

Evil-Devil

Quote from: elam on January 02, 2009, 18:39:21
GL11.glOrtho(0.0, Display.getDisplayMode().getWidth(), 0.0, Display.getDisplayMode().getHeight(), -1.0, 1.0);
What i said before. Some init stuff is needed. AFAIK with LWJGL 2.x the API user have to provide all essential basic info to opengl to see anything. May be confusing for beginners but its very neat for everyone else.