Hello Guest

Basic OpenGL Tutorial doesn`t work

  • 7 Replies
  • 13509 Views
Basic OpenGL Tutorial doesn`t work
« 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???

*

Offline Matzon

  • *****
  • 2242
Re: Basic OpenGL Tutorial doesn`t work
« Reply #1 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.

Re: Basic OpenGL Tutorial doesn`t work
« Reply #2 on: November 01, 2008, 10:04:06 »
Still the same problem!

Re: Basic OpenGL Tutorial doesn`t work
« Reply #3 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.

Re: Basic OpenGL Tutorial doesn`t work
« Reply #4 on: December 23, 2008, 19:04:16 »
You don't need to set the viewport - the default setting is the complete GL window.

Re: Basic OpenGL Tutorial doesn`t work
« Reply #5 on: December 31, 2008, 22:09:46 »
Try pulling the Quad at another z-depth, 0f is the camera pos I think !

Re: Basic OpenGL Tutorial doesn`t work
« Reply #6 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.

Re: Basic OpenGL Tutorial doesn`t work
« Reply #7 on: January 05, 2009, 10:59:07 »
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.