Hello Guest

Conceptualizing OpenGL

  • 1 Replies
  • 6036 Views
Conceptualizing OpenGL
« on: October 30, 2005, 20:37:11 »
I am working on a developing a game for one of my classes in college, and we are trying to use OpenGL to do the 2D rendering this time because we had issues with java 2d last time. After reading tutorials, and a short OpenGL primer here is how I have conceptualized drawing in the game...

(Without Code)
All objects in the game have game units (we'll call them feet for now), each object is then placed in the OpenGL coordinate system. To place them I simply draw rectangles at the appropriate position relative to the origin. Then, I use a camera class that 'takes a picture' of the game world around the player.

(With Code)
First I did generic OpenGL setup operations. I created a Display, set the clear color to black, enabled the use of 2D textures, disabled buffer testing, and finally set the matrix mode to projection and cleared to the identity matrix.

To draw I first clear the color and depth buffers.

Then I draw a sprite in projection mode by the following code:

Code: [Select]

/* Bind the texture, this tells open gl that we will use this texture
* when we draw a rectangle.
*/
oglTexture.bind();

/* Draw a rectangle on the screen using the bound texture.
* We need to flip the texture because of the way Open GL does things.
* (I forget exactly why)
*/
GL11.glBegin(GL11.GL_QUADS);
{
// Set the bottom left coordinate.
GL11.glTexCoord2f (0, 1);
GL11.glVertex2f (position.x, position.y);
//GL11.glVertex3f (position.x, position.y, level);
   // Set the top left coordinate.
GL11.glTexCoord2f (0, 0);
GL11.glVertex2f (position.x, position.y + height);
//GL11.glVertex3f (position.x, position.y + height, level);
// Set the top right coordinate.
GL11.glTexCoord2f (1, 0);
GL11.glVertex2f (position.x + width, position.y + height);
//GL11.glVertex3f (position.x + width, position.y + height, level);
// Set the bottom right coordinate.
GL11.glTexCoord2f (1, 1);
GL11.glVertex2f (position.x + width, position.y);
//GL11.glVertex3f (position.x + width, position.y, level);
}
GL11.glEnd();


Then, I set the current position of the camera and make a gluOrtho2D call like so:

Code: [Select]

// Calculate the boundaries of the clipping area.
final float left = (float) (center.getX() - (0.5 * clippingArea.getWidth()));
final float right = (float) (center.getX() + (0.5 * clippingArea.getWidth()));
final float bottom = (float) (center.getY() - (0.5 * clippingArea.getHeight()));
final float top = (float) (center.getY() + (0.5 * clippingArea.getHeight()));

// Set the clipping area.
GLU.gluOrtho2D(left, right, bottom, top);


Finally I call Display.Update()

Ok, so my question is am I doing anything wrong conceptually? Also, is there anything wrong with the code as I have posted it? (I am having trouble getting my sprite to display suddenly and I'm not sure what I changed... but I wanted conceptual help here not trouble shooting for that particular issue.)

The camera is going to follow the player through the game world, so I figured it would be easy to set all coordinates relative to the origin. Plus, it will probably be nice to zoom the camera in or out during things like a boss fight. I can accomplish that by increasing or decreasing the viewing area.
oncetrate on the solution, not the problem.

hmmmmmm...
« Reply #1 on: October 31, 2005, 14:15:45 »
I think you want to do the gluOrtho2D call stuff first.  Also, you should set up the gluOrtho2D with the coords of the screen (like (0, 800, 600, 0) or whatever) and use glTranslatef() for moving the camera after you do the gluOrtho2D (i.e. glTranslatef(-center.getX(), -center.getY(), 0.0f) ). Then you just draw the objects like you have them (except that its not working for you :wink: ).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D