LWJGL Forum

Programming => OpenGL => Topic started by: gonzo on October 31, 2008, 22:15:41

Title: Basic OpenGL Tutorial doesn`t work
Post by: gonzo on October 31, 2008, 22:15:41
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???
Title: Re: Basic OpenGL Tutorial doesn`t work
Post by: Matzon on October 31, 2008, 23:34:22
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.
Title: Re: Basic OpenGL Tutorial doesn`t work
Post by: gonzo on November 01, 2008, 10:04:06
Still the same problem!
Title: Re: Basic OpenGL Tutorial doesn`t work
Post by: Evil-Devil on December 23, 2008, 07:35:09
I don't see any opengl init stuff like setting the viewport. You need at least a viewport to see something.
Title: Re: Basic OpenGL Tutorial doesn`t work
Post by: Matthias on December 23, 2008, 19:04:16
You don't need to set the viewport - the default setting is the complete GL window.
Title: Re: Basic OpenGL Tutorial doesn`t work
Post by: broumbroum on December 31, 2008, 22:09:46
Try pulling the Quad at another z-depth, 0f is the camera pos I think !
Title: Re: Basic OpenGL Tutorial doesn`t work
Post by: elam on January 02, 2009, 18:39:21
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.
Title: Re: Basic OpenGL Tutorial doesn`t work
Post by: Evil-Devil on January 05, 2009, 10:59:07
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.