LWJGL Views

LWJGL - Looking for some clarification and what nots. If I want to do a 2d game, with no 3d at all, then I could use the game loop in another thread here and use the default ortho 1:1 view, draw on screen with 0,0 in the lower left corner? and leave all z’s at 0?

If I want any 3d in it, then I need to go ortho box deeper than 1 (like AlienFlux below)

// Go into orthographic projection mode
            GL11.glMatrixMode(GL11.GL_PROJECTION);
            GL11.glLoadIdentity();
            GLU.gluOrtho2D(0, WIDTH, 0, HEIGHT);
            GL11.glMatrixMode(GL11.GL_MODELVIEW);
            GL11.glLoadIdentity();
            GL11.glViewport(0, 0, width, height);

or gluPerspective (fovy, aspect, near, far) or something similar?

M

[quote]LWJGL - Looking for some clarification and what nots. If I want to do a 2d game, with no 3d at all, then I could use the game loop in another thread here and use the default ortho 1:1 view, draw on screen with 0,0 in the lower left corner? and leave all z’s at 0?
[/quote]
Yup.

[quote]If I want any 3d in it, then I need to go ortho box deeper than 1 (like AlienFlux below)
[/quote]
gluOrtho2D will give you a depth range of -1 to 1 (although you could also use glOrtho and specify near and far yourself). However it’ll still give you a parallel projection - you won’t get any foreshortening of objects in the distance. If you want the perspective shortening you use glPerspective as you mentioned.

great…thanks! lovin it!

So if I have a starship with a planet in the distance, use glPerspective?

Well, there’s 2 ways of doing it: either actually use Z coodinates to make things appear smaller, using perspective, or just draw the planet first and much smaller.

Cas :slight_smile:

Great, thanks!! Off to some 2d and 3d game dev with LWJGL…after spending some time with the redbook. ;D

an assumption…

to do an overhead scroller with LWJGL, I need to keep my “veiwpoint/camera” in in the same position as my centerd object just higher up and no rotating (think Subspace) correct?

More or less. Just translate the x and y coordinates to your ship’s coordinates and you’ll find that it appears in the middle of the screen. Try it and see.

Cas :slight_smile:

Thanks, I’ll give a try! My problem is learning how do do and move the veiwpoint. I have your basic loop with the default 1:1 orhte2d set up from Window.create(string), and a glu.gluPerspective(…) for a 3d view.

Need to figure out how to move the viewpoint… :-/

Love the performance I am getting with what I see so far!

Unlike DirectX, OpenGL doesn’t have separate camera and object matrices, the modelview matrix takes the place of both of them. So typically you do something like:

glMatrixMode( PROJECTION );
glOrtho( … ); // Setup projection

glMatrixMode( MODELVIEW );
glTranslatef( inverse of camera position ); // ‘moves’ the view so it looks at the camera position

// For each object…
glPushMatrix();
glTranslatef / glRotatef // Set position/rotation of current object
// draw object
// …

glPopMatrix(); // Pop off current transforms and return to just camera transform.

Although for a 2d game you probably don’t want the overhead of push, translate, pop, for every sprite. So just calculate this manually.

ok, I did this, using the base game loop…

    int x = 5;
    int y = Window.getHeight() - height - 50;
    int z = 20;
    
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glTranslatef(0.4f, 0.0f, 0.0f);  // positive y goes up, positive x goes right, any change to z takes it out or 1:1 2d view
    GL11.glPushMatrix();
    
        GL11.glBegin(GL11.GL_LINE_STRIP);
        {
          GL11.glColor3f(0.0f, 0.9f, 0.0f);
          GL11.glVertex2i(x - 1, y);
          GL11.glVertex2i(x + width, y);
          GL11.glVertex2i(x + width, y + height + 1);
          GL11.glVertex2i(x - 1, y + height + 1);
          GL11.glVertex2i(x - 1, y);
          System.out.println("x: " + x);

        }
        GL11.glEnd();
        GL11.glPopMatrix();

When I run this, my green line array that makes a rectangle moves to the right…which means my viewpoint is moving to the left and x is always 5 because I am not moving the box, just my viewpoint. Now to take Cas’ suggestion and send my viewpoint x and y to my centered avatar, it will sit in the “center” where my camera is looking. Its x and y will be my “camera” x and y…