Help with drawing at certain points

Started by kurtis, March 10, 2008, 23:31:56

Previous topic - Next topic

kurtis

I have been attempting to learn how to use OpenGL on my own.

I am running into a problem that I can't seem to get around. In a particular program I created, the point 0,0 is at the bottom left of the screen. For instance, if I translate x by -6 units, the object will run off to the left of the screen. The only way I get around that is by manually drawing each vertex in the quad where I need them. (Similar to plotting on a Cartesian grid)

I have attempted to read through other's code, including Nehe's tutorials but I must be missing something. I also notice that if I change the Z coordinates on these vertexes, the shape will disappear completely. In addition, rotation doesn't make the 2d shape spin, like I've found in other code.

I created a different program while reading some code that I found that works perfectly, but I ran into problems with it also.


Here is the code to 'render' the scene:

---------------------
        // Clear the screen and buffer
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
      
      // Reset the view
      GL11.glLoadIdentity();

      // First, clear the screen
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
      
      // Start the polygon
      GL11.glPushMatrix();
       GL11.glTranslatef(0, 0, 0);
      
      
       GL11.glBegin(GL11.GL_QUADS);
           GL11.glVertex2i(centerX + width, centerY + height);
           GL11.glColor3d(red,green,blue);
           GL11.glVertex2i(centerX - width, centerY + height);
           GL11.glColor3d(red,green,blue);
          GL11.glVertex2i(centerX - width, centerY - height);
          GL11.glColor3d(red,green,blue);
           GL11.glVertex2i(centerX + width,  centerY - height);
       GL11.glEnd();
   
      
       GL11.glPopMatrix();

------------------------------------------------

Here is my initialisation

-----------------------------------------------

      // Set some display properties
      Display.setTitle(GAME_TITLE);         // Set the title
       Display.setFullscreen(fullScreen);      // Set it to full screen if true
       Display.setDisplayMode(mode);         // Set the Resolution
      
      
       // If possible, enable VSync. With OpenGL, not always possible.
       Display.setVSyncEnabled(true);
   
       // Create default display of 640x480
       Display.create();

-------------------------------------

Of course there is code to figure out the DisplayMode and to handle logic and so forth, but I didn't want to fill this post with too much code.

Sorry if this is the improper place to way of asking for this help. I am new to this forum. Thanks for any help in advanced.

Fool Running

Welcome to the forums. ;D

Quote...the point 0,0 is at the bottom left of the screen.
What you are observing is the default OpenGL behavior (The point 0,0 is at the bottom left). You need to call:
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(double left, double right, double bottom, double top, double zNear, double zFar);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

in your init code, if you want different behavior.
QuoteI also notice that if I change the Z coordinates on these vertexes, the shape will disappear completely.
This is also the default OpenGL behavior. The near Z is set to -1.0, and the far Z is set to 1.0. If you go out of (-1.0 to 1.0) then they will disappear. Just change it in your init.
QuoteThe only way I get around that is by manually drawing each vertex in the quad where I need them.
I'm not sure what you mean by this, but drawing them manually is the simplest way to get them on the screen...
QuoteIn addition, rotation doesn't make the 2d shape spin, like I've found in other code.
Rotation always happens around the point (0,0), so you would need to draw your quad around (0, 0), rotate, and then translate to your desired position. I think it would be faster to calculate the points yourself, though, to avoid the matrix pushing and popping.

Hope that helps  ;D
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

kurtis

Thanks a lot. That definitely clears it up for me.