Thread: gluLookAt Problems.. (OpenGL)

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    102

    gluLookAt Problems.. (OpenGL)

    Hey all I'm having some serious trouble with this small game I'm writing. I seem to get no effect on using the gluLookAt.
    My goal is to have the camera following the arrow drawn by the Player Class. However I can move the arrow fine, but the camera stays stationary to it and the floor below. (I just threw it in there for debugging) I'll include the necessary code.

    WinMain:
    Code:
    #define WIN32_LEAN_AND_MEAN
    #define WIN32_EXTRA_LEAN
    
    #include <windows.h>
    
    
    #include "Graphics Class.h"
    #include "Game Engine.h"
    
    bool exiting = false;
    long windowWidth = 800;
    long windowHeight = 600;
    long windowBits = 32;
    bool fullscreen = false;
    
    HDC hDC;
    
    GameEngine MainEngine;
    
    Graphics GraphicsEng(windowWidth, windowHeight);
    
    void SetupPixelFormat(HDC hDC)
    {
        int pixelFormat;
    
        PIXELFORMATDESCRIPTOR pfd =
        {   
            sizeof(PIXELFORMATDESCRIPTOR),  // size
                1,                          // version
                PFD_SUPPORT_OPENGL |        // OpenGL window
                PFD_DRAW_TO_WINDOW |        // render to window
                PFD_DOUBLEBUFFER,           // support double-buffering
                PFD_TYPE_RGBA,              // color type
                32,                         // prefered color depth
                0, 0, 0, 0, 0, 0,           // color bits (ignored)
                0,                          // no alpha buffer
                0,                          // alpha bits (ignored)
                0,                          // no accumulation buffer
                0, 0, 0, 0,                 // accum bits (ignored)
                16,                         // depth buffer
                0,                          // no stencil buffer
                0,                          // no auxiliary buffers
                PFD_MAIN_PLANE,             // main layer
                0,                          // reserved
                0, 0, 0,                    // no layer, visible, damage masks
        };
    
        pixelFormat = ChoosePixelFormat(hDC, &pfd);
        SetPixelFormat(hDC, pixelFormat, &pfd);
    }
    
    LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        static HDC hDC;
        static HGLRC hRC;
        int height, width;
    
        // dispatch messages
        switch (uMsg)
        {   
        case WM_CREATE:         // window creation
            hDC = GetDC(hWnd);
            SetupPixelFormat(hDC);
            //SetupPalette();
            hRC = wglCreateContext(hDC);
            wglMakeCurrent(hDC, hRC);
            
            MainEngine.hDC = hDC;
            break;
    
        case WM_DESTROY:            // window destroy
        case WM_QUIT:
        case WM_CLOSE:                  // windows is closing
    
            // deselect rendering context and delete it
            wglMakeCurrent(hDC, NULL);
            wglDeleteContext(hRC);
    
            // send WM_QUIT to message queue
            PostQuitMessage(0);
            
            break;
    
        case WM_SIZE:
            height = HIWORD(lParam);        // retrieve width and height
            width = LOWORD(lParam);
    		
    		// Resize Window Here
            GraphicsEng.SetProjection(width, height);
            break;
    
        case WM_ACTIVATEAPP:        // activate app
            break;
    
        case WM_PAINT:              // paint
            PAINTSTRUCT ps;
            BeginPaint(hWnd, &ps);
            EndPaint(hWnd, &ps);
            break;
    
        case WM_LBUTTONDOWN:        // left mouse button
            break;
    
        case WM_RBUTTONDOWN:        // right mouse button
            break;
    
        case WM_MOUSEMOVE:          // mouse movement
            break;
    
        case WM_LBUTTONUP:          // left button release
            break;
    
        case WM_RBUTTONUP:          // right button release
            break;
    
        case WM_KEYUP:
        MainEngine.Keys[wParam] = FALSE;
            
            break;
    
        case WM_KEYDOWN:
            
            MainEngine.Keys[wParam] = TRUE;
            break;
    
        default:
            break;
        }
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
        WNDCLASSEX windowClass;     // window class
        HWND       hwnd;            // window handle
        MSG        msg;             // message
        DWORD      dwExStyle;       // Window Extended Style
        DWORD      dwStyle;         // Window Style
        RECT       windowRect;
    
        windowRect.left=(long)0;                        // Set Left Value To 0
        windowRect.right=(long)windowWidth; // Set Right Value To Requested Width
        windowRect.top=(long)0;                         // Set Top Value To 0
        windowRect.bottom=(long)windowHeight;   // Set Bottom Value To Requested Height
    
        // fill out the window class structure
        windowClass.cbSize          = sizeof(WNDCLASSEX);
        windowClass.style           = CS_HREDRAW | CS_VREDRAW;
        windowClass.lpfnWndProc     = MainWindowProc;
        windowClass.cbClsExtra      = 0;
        windowClass.cbWndExtra      = 0;
        windowClass.hInstance       = hInstance;
        windowClass.hIcon           = LoadIcon(NULL, IDI_APPLICATION);  // default icon
        windowClass.hCursor         = LoadCursor(NULL, IDC_ARROW);      // default arrow
        windowClass.hbrBackground   = NULL;                             // don't need background
        windowClass.lpszMenuName    = NULL;                             // no menu
        windowClass.lpszClassName   = "GLClass";
        windowClass.hIconSm         = LoadIcon(NULL, IDI_WINLOGO);      // windows logo small icon
    
        // register the windows class
        if (!RegisterClassEx(&windowClass))
            return 0;
    
        if (fullscreen)                             // fullscreen?
        {
            DEVMODE dmScreenSettings;                   // device mode
            memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
            dmScreenSettings.dmSize = sizeof(dmScreenSettings); 
            dmScreenSettings.dmPelsWidth = windowWidth;         // screen width
            dmScreenSettings.dmPelsHeight = windowHeight;           // screen height
            dmScreenSettings.dmBitsPerPel = windowBits;             // bits per pixel
            dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
    
            // 
            if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
            {
                // setting display mode failed, switch to windowed
                MessageBox(NULL, "Display mode failed", NULL, MB_OK);
                fullscreen = FALSE; 
            }
        }
    
        if (fullscreen)                             // Are We Still In Fullscreen Mode?
        {
            dwExStyle=WS_EX_APPWINDOW;                  // Window Extended Style
            dwStyle=WS_POPUP;                       // Windows Style
            ShowCursor(FALSE);                      // Hide Mouse Pointer
        }
        else
        {
            dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;   // Window Extended Style
            dwStyle=WS_OVERLAPPEDWINDOW;                    // Windows Style
        }
    
        AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);     // Adjust Window To True Requested Size
    
        // class registered, so now create our window
        hwnd = CreateWindowEx(NULL,                                 // extended style
            "GLClass",                          // class name
            "BOGLGP - Chapter 2 - OpenGL Application",      // app name
            dwStyle | WS_CLIPCHILDREN |
            WS_CLIPSIBLINGS,
            0, 0,                               // x,y coordinate
            windowRect.right - windowRect.left,
            windowRect.bottom - windowRect.top, // width, height
            NULL,                               // handle to parent
            NULL,                               // handle to menu
            hInstance,                          // application instance
            NULL);                              // no extra params
    
        hDC = GetDC(hwnd);
    
        // check if window creation failed (hwnd would equal NULL)
        if (!hwnd)
            return 0;
    
        ShowWindow(hwnd, SW_SHOW);          // display the window
        UpdateWindow(hwnd);                 // update the window
    
        // Pre-Gl Processes here
        GraphicsEng.Init();
        MainEngine.Init();
    
        while (!exiting)
        {
    	 	// Gl Rendering Goes Here
    		MainEngine.PlayGame();
    
            while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
            {
                if (!GetMessage (&msg, NULL, 0, 0))
                {
                    exiting = true;
                    break;
                }
    
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
    
        if (fullscreen)
        {
            ChangeDisplaySettings(NULL,0);          // If So Switch Back To The Desktop
            ShowCursor(TRUE);                       // Show Mouse Pointer
        }
    
        return (int)msg.wParam;
    }
    Game Engine Class(not much of an engine yet, but working on it)
    Code:
    /** Game Engine .cpp  */
    
    #include "Game Engine.h"
    
    #include <gl/gl.h>
    #include <cmath>
    #include <windows.h>
    
    #define piover180 .0174
    
    void GameEngine::HandleKeys()
    {
        if (Keys[VK_UP])
        {
            Players[0]->Move( -(float)sin(Players[0]->XAngle * piover180) * .15f, 0.0f, -(float)cos(Players[0]->XAngle * piover180) * .15f);
            
        }
        
        if (Keys[VK_DOWN])
        {
            Players[0]->Move( (float)sin(Players[0]->XAngle * piover180) * .15f, 0.0f, (float)cos(Players[0]->XAngle * piover180) * .15f);
            
        }
        
        if (Keys[VK_LEFT])
        {
            Players[0]->SetFaceAngle(Players[0]->XAngle + 2.0f, 0.0f);
            
            
        }
        
        if (Keys[VK_RIGHT])
        {
            Players[0]->SetFaceAngle(Players[0]->XAngle - 2.0f, 0.0f);
            
            
        }
        
        if (Keys[VK_ESCAPE])
        { 
            PostQuitMessage(0);
            
            
        }
        
        if (Keys[VK_SPACE])
        {
    
        }
        
    }
    
    void GameEngine::Init()
    {
        MainCamera = new Camera();
        MainCamera->pPlayer = Players[0];
    }
    
    void GameEngine::PlayGame()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        MainCamera->pPlayer = Players[0];
    	
    	HandleKeys();
    	
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    	
    	glColor3f(.5f, .7f, .23f);
    	
    	glBegin(GL_QUADS);
    	
    	   glVertex3f(-20.0f, -3.0f, 20.0f);
    	   glVertex3f(-20.0f, -3.0f, -20.0f);
    	   glVertex3f(20.0f, -3.0f, -20.0f);
    	   glVertex3f(20.0f, -3.0f, 20.0f);
    	   
    	glEnd();
    	
    	Players[0]->DrawArrow();
    		
        SwapBuffers(hDC);
    
    }
    The Player and Camera Classes:
    Code:
    /** CPP Implementation for Player.h  */
    
    #include "Player.h"
    
    #include <gl/gl.h>
    
    void Player::PositionAt(float x, float y, float z)
    {
        X = x;
        Y = y;
        Z = z;
    }
    
    void Player::Move(float xinc, float yinc, float zinc)
    {
        X += xinc;
        Y += yinc;
        Z += zinc;
    }
    
    void Player::SetFaceAngle(float xAngle, float yAngle)
    {
        XAngle = xAngle;
        YAngle = yAngle;
    }
    
    void Player::DrawArrow()
    {
        glMatrixMode(GL_MODELVIEW);
        
        glLoadIdentity();
        
        glTranslatef(X, Y, Z);
        glRotatef(XAngle, 0.0f, 1.0f, 0.0f);
        glRotatef(YAngle, 0.0f, 1.0f, 0.0f);
        glPushMatrix();
    
        
        glEnable(GL_BLEND);
        
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        
        glColor4f(.321f, .741f, .176f, .75f);
        glBegin(GL_TRIANGLES);
            
            //Top Part of Arrow
            glVertex3f(0.0f, 0.0f, 0.0f);
            glVertex3f(0.0f -.5f, 0.0f, 0.0f + .5f);
            glVertex3f(0.0f + .5f, 0.0f, 0.0f + .5f);
            //Lower Part of Arrow
            glVertex3f(0.0f, 0.0f - .3f, 0.0f);
            glVertex3f(0.0f -.5f, 0.0f - .3f, 0.0f + .5f);
            glVertex3f(0.0f + .5f, 0.0f - .3f, 0.0f + .5f);
            
        glEnd();
        
        glBegin(GL_QUADS);
        
            //Sides of the Triangle Portion
            glVertex3f(0.0f, 0.0f, 0.0f);
            glVertex3f(0.0f -.5f, 0.0f, 0.0f + .5f);
            glVertex3f(0.0f -.5f, 0.0f - .3f, 0.0f + .5f);
            glVertex3f(0.0f, 0.0f - .3f, 0.0f);
            
            //2nd Side
            glVertex3f(0.0f, 0.0f, 0.0f);
            glVertex3f(0.0f + .5f, 0.0f, 0.0f + .5f);
            glVertex3f(0.0f + .5f, 0.0f - .3f, 0.0f + .5f);
            glVertex3f(0.0f, 0.0f - .3f, 0.0f);
            
            //3rd Side
            glVertex3f(0.0f + .5f, 0.0f, 0.0f + .5f);
            glVertex3f(0.0f -.5f, 0.0f, 0.0f + .5f);
            glVertex3f(0.0f -.5f, 0.0f - .3f, 0.0f + .5f);
            glVertex3f(0.0f + .5f, 0.0f - .3f, 0.0f + .5f);
            
        glEnd();
        
        glBegin(GL_QUADS);
        
            //Quadrilateral end
            glVertex3f(0.0f - .2f, 0.0f, 0.0f + .5f);
            glVertex3f(0.0f - .2f, 0.0f, 0.0f + 1.2f);
            glVertex3f(0.0f + .2f, 0.0f, 0.0f + 1.2f);
            glVertex3f(0.0f + .2f, 0.0f, 0.0f + .5f);
            
            //Lower Quad
            glVertex3f(0.0f - .2f, 0.0f - .3f, 0.0f + .5f);
            glVertex3f(0.0f - .2f, 0.0f - .3f, 0.0f + 1.2f);
            glVertex3f(0.0f + .2f, 0.0f - .3f, 0.0f + 1.2f);
            glVertex3f(0.0f + .2f, 0.0f - .3f, 0.0f + .5f);
            
            //Left Side
            glVertex3f(0.0f - .2f, 0.0f, 0.0f + .5f);
            glVertex3f(0.0f - .2f, 0.0f, 0.0f + 1.2f);
            glVertex3f(0.0f - .2f, 0.0f - .3f, 0.0f + 1.2f);
            glVertex3f(0.0f - .2f, 0.0f - .3f, 0.0f + .5f);
            
            //Right Side
            glVertex3f(0.0f + .2f, 0.0f, 0.0f + 1.2f);
            glVertex3f(0.0f + .2f, 0.0f, 0.0f + .5f);
            glVertex3f(0.0f + .2f, 0.0f - .3f, 0.0f + .5f);
            glVertex3f(0.0f + .2f, 0.0f - .3f, 0.0f + 1.2f);
            
            //Back Side
            glVertex3f(0.0f - .2f, 0.0f - .3f, 0.0f + 1.2f);
            glVertex3f(0.0f + .2f, 0.0f - .3f, 0.0f + 1.2f);
            glVertex3f(0.0f + .2f, 0.0f, 0.0f + 1.2f);
            glVertex3f(0.0f - .2f, 0.0f, 0.0f + 1.2f);
            
        glEnd();
        
        glColor3f(.458f, .925f, .176f);
        
        glLineWidth(7.0f);
        
        glBegin(GL_LINES);
            
            //Lines for Increased coolness
            glVertex3f(0.0f, 0.0f, 0.0f);
            glVertex3f(0.0f -.45f, 0.0f, 0.0f + .45f);
            
            glVertex3f(0.0f -.45f, 0.0f, 0.0f + .45f);
            glVertex3f(0.0f + .45f, 0.0f, 0.0f + .45f);
            
            glVertex3f(0.0f + .45f, 0.0f, 0.0f + .45f);
            glVertex3f(0.0f, 0.0f, 0.0f);
            
            glVertex3f(0.0f - .1f, 0.0f, 0.0f + .6f);
            glVertex3f(0.0f - .1f, 0.0f, 0.0f + 1.1f);
            
            glVertex3f(0.0f + .1f, 0.0f, 0.0f + .6f);
            glVertex3f(0.0f + .1f, 0.0f, 0.0f + 1.1f);
            
        glEnd();
        
        glPopMatrix();
        
    }
    Code:
    /** Camera Class */
    
    #include <gl/glu.h>
    
    #include "Camera.h"
    #include <windows.h>
    
    void Camera::Update()
    {
        //MessageBox(NULL, "CALLED", "DFGDL", MB_OK);
        
        gluLookAt(pPlayer->X, pPlayer->Y + 1.0f, pPlayer->Z + 2.0f, pPlayer->X, pPlayer->Y + 1.0f, pPlayer->Z,
      pPlayer->X, pPlayer->Y + 5.0f, pPlayer->Z + 2.0f);    // Place Camera behind Player's updating position, and Aim the Camera towards the player.
    }
    Please offer any suggestions.
    Last edited by JJFMJR; 07-08-2007 at 05:52 PM.
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  2. #2
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Unless I'm not seeing something in your code, you're not updating the values of gluLookAt for any kind of movement or rotation.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Bah thought I'd edited that, I did that initially to see if there was any difference doing it that way. I'll change it back sorry..
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Well, I fixed it. Thanks for any replies, however it was just a problem in my understanding of gluLookAt(). I thought that it moved the camera's positon based on the world, however it just calls the necessary translate and rotate functions in reverse. I manipulated the code, and discovered this. Thanks again.
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL_ttf and OpenGL
    By sand_man in forum Game Programming
    Replies: 2
    Last Post: 12-01-2004, 06:06 PM
  2. OpenGL lighting
    By BabyG in forum Game Programming
    Replies: 3
    Last Post: 08-29-2004, 09:58 AM
  3. Problems with rotations with OpenGL and GLUT
    By darcome in forum Game Programming
    Replies: 13
    Last Post: 07-05-2002, 12:12 AM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. So.. what's the difference between DirectX and OpenGL?
    By QuestionC in forum Game Programming
    Replies: 6
    Last Post: 01-19-2002, 06:18 PM