OpenGL screen view coordinates

This is a discussion on OpenGL screen view coordinates within the Game Programming forums, part of the General Programming Boards category; I am currently working on a project in OpenGL and was having a little trouble with rotation of my map. ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    Question OpenGL screen view coordinates

    I am currently working on a project in OpenGL and was having a little trouble with rotation of my map. The way my map is setup is a hexagonal map, so each spot on the map is a hexagon. The base of the map is the lower left corner hexagon. In the entire matrix of hexagons, it is [0,0]. I then set the base hexagon to start out at coordinate (0, -1, 0) on the screen. The reason I am starting the y-coordinate at -1 is because if I started it at 0, the map would be in the center of the screen and would not be visible, because it is a flat map, but that is not the issue here.

    Anyways, just like in several computer games, I use the left and right arrow keys to turn right and left, or in other words, to pivot on the y-axis. However, I am having a problem with the pivoting. The problem is that the base of the map stays at (0, -1, 0) at all times. But that is not what I want to happen. What I need to happen is for the map to conform to the user, in other words, the place in which my personage would be standing on the map if i were a character in the game always needs to be at (0, -1, 0), therefore making the map pivot around me.

    Just in case you need to look at some of my code, I have included the two functions that would be of the most importance in this scenario, the function in which I set up OpenGL, and the function in which I do the drawing. Also notice that I am using OpenGL inside of SDL. I love SDL.

    Code:
    void setup_opengl( int width, int height )
    {
        float ratio = (float) width / (float) height;
    
        /* Our shading model--Gouraud (smooth). */
        glShadeModel( GL_SMOOTH );
        
        /* Set the clear color. */
        glClearColor( 0, 0, 0, 0 );
    
        /* Setup our viewport. */
        glViewport( 0, 0, width, height );
    
        /*
         * Change to the projection matrix and set
         * our viewing volume.
         */
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity( );
    
        gluPerspective( 60.0, ratio, 1.0, 1024.0 );
    
         myMap = new HEXMAP_DTP( 5, 5, 1.0f ); //create a 5x5 hex map with each hex having a 1 unit radius
         myMap->SetBase ( 0.0f, -1.0f, 0.0f ); //base coords of map
    }
    
    static void draw_screen( void )
    {
        /* Our angle of rotation. */
        static float angle = 0.0f;
    
        /* Clear the color and depth buffers. */
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    
        /* We don't want to modify the projection matrix. */
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity( );
    
        /* Move down the z-axis. */
        glTranslatef( 0.0, 0.0, -5.0 );
    	glTranslatef( 0, movy, movz );
    
        /* Rotate. */	
    	glRotatef( movx, 0.0f, 1.0f, 0.0f );    
    
    	//myHex->DrawHex ( );
    	myMap->DrawMap();	
    
    
        /*
         * Swap the buffers. This this tells the driver to
         * render the next frame from the contents of the
         * back-buffer, and to set all rendering operations
         * to occur on what was the front-buffer.
         *
         * Double buffering prevents nasty visual tearing
         * from the application drawing on areas of the
         * screen that are being updated at the same time.
         */
        SDL_GL_SwapBuffers( );
    }

    Thanx for any help you can provide.
    Last edited by DavidP; 03-11-2003 at 09:51 PM.
    My Website

    "Circular logic is good because it is."

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    my 3D's a little rusty, sine i've been focused on 2D for a while now.
    i think, you need to center the map on its origin, your drawing from the origin
    out, use a glTranslatef() or offset when you draw to center it before it draws.

    Code:
     glTranslatef( 0.0, 0.0, -5.0 );
    	glTranslatef( 0, movy, movz );
    
        /* Rotate. */	
    	glRotatef( movx, 0.0f, 1.0f, 0.0f );    
    
    	//myHex->DrawHex ( );
    // save the current matrix state so our changes will have no effect on other objects
        glPushMatrix();
                    glTranslatef(to the objects(ie the maps) origin around the x and y axis);
    	myMap->DrawMap();
        glPopMatrix();
    // load the previous matrix state
    what you'll probably want to do is to translate each of your objects seperatly for its specific translations, or translate it individually for all the translations including the ones you want global.

    ie

    self specific translations.

    Code:
    // global translate
    glTranslatef(0.0,0.0,-5.0);
    
    glPushMatrix();
        glTeanslatef(-1.0,1.0,0.0);
        glRotate(axis,1.0,0.0,0.0);
        DrawObjectCenteredOnItsOrigin();
    glPushMatrx();
    global an selftranslation.

    Code:
    glTranslatef(0.0,0.0,-5.0);
    
    glPushMatrix();
    glLoadIdentity();  // set the current matrix to identity
    
        glTranslatef(0.0,0.0,-5.0); // since we loaded identity we need to retranslate to -5z
        DrawObjectCenteredOnItsOrigin();
    
    glPushMatrx();
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tree View control not appearing
    By Garfield in forum Windows Programming
    Replies: 1
    Last Post: 03-07-2004, 01:47 PM
  2. .TGA files in OpenGL
    By ... in forum Game Programming
    Replies: 2
    Last Post: 10-26-2003, 02:48 PM
  3. How to convert screen coordinates
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 09-13-2003, 12:50 PM
  4. MISC questions about OpenGL
    By Silvercord in forum Game Programming
    Replies: 12
    Last Post: 01-25-2003, 04:20 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 11:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21