Thread: sizing problem

  1. #1
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321

    sizing problem

    Hi
    I have just went through the 3d tuts on NeHe, it works fine, but when i run my program, the triangle is oversized, and when i try to resize my window, the triangle resizes with it, can anyone help me?
    Here is the code:

    Code:
    /* main.cpp*/
    #include <windows.h>
    #include <gl/gl.h>
    
    
    /**************************
     * Function Declarations
     *
     **************************/
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
    WPARAM wParam, LPARAM lParam);
    void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
    void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
    
    
    /**************************
     * WinMain
     *
     **************************/
    
    int WINAPI WinMain (HINSTANCE hInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpCmdLine,
                        int iCmdShow)
    {
        WNDCLASS wc;
        HWND hWnd;
        HDC hDC;
        HGLRC hRC;        
        MSG msg;
        BOOL bQuit = FALSE;
        float theta = 0.0f;
    
        /* register window class */
        wc.style = CS_OWNDC;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor (NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = "GLSample";
        RegisterClass (&wc);
    
        /* create main window */
        hWnd = CreateWindow (
          "GLSample", "OpenGL Sample", 
          WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
          0, 0, 256, 256,
          NULL, NULL, hInstance, NULL);
    
        /* enable OpenGL for the window */
        EnableOpenGL (hWnd, &hDC, &hRC);
    
        /* program main loop */
        while (!bQuit)
        {
            /* check for messages */
            if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
            {
                /* handle or dispatch messages */
                if (msg.message == WM_QUIT)
                {
                    bQuit = TRUE;
                }
                else
                {
                    TranslateMessage (&msg);
                    DispatchMessage (&msg);
                }
            }
            else
            {
                /* OpenGL animation code goes here */
    
                glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
                glClear (GL_COLOR_BUFFER_BIT);
    
                glPushMatrix ();
                glRotatef (theta, 0.0f, 1.0f, 0.0f);
                glBegin (GL_TRIANGLES);
                glColor3f (1.0f, 0.0f, 0.0f);   glVertex3f (0.0f, 1.0f, 0.0f);
                glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f (-1.0f, -1.0f, 1.0f);
                glColor3f (0.0f, 0.0f, 1.0f);   glVertex3f (1.0f, -1.0f, 1.0f);
                
                glColor3f (1.0f, 0.0f, 0.0f);   glVertex3f (0.0f, 1.0f, 0.0f);
                glColor3f (0.0f, 0.0f, 1.0f);   glVertex3f (1.0f, -1.0f, 1.0f);
                glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f (1.0f, -1.0f, -1.0f);
                
                glColor3f (1.0f, 0.0f, 0.0f);   glVertex3f (0.0f, 1.0f, 0.0f);
                glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f (1.0f, -1.0f, -1.0f);
                glColor3f (0.0f, 0.0f, 1.0f);   glVertex3f (-1.0f, -1.0f, -1.0f);
                
                glColor3f (1.0f, 0.0f, 0.0f);   glVertex3f (0.0f, 1.0f, 0.0f);
                glColor3f (0.0f, 0.0f, 1.0f);   glVertex3f (-1.0f, -1.0f, -1.0f);
                glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f (-1.0f, -1.0f, 1.0f);
                glEnd ();
                glPopMatrix ();
    
                SwapBuffers (hDC);
    
                theta += 1.0f;
                Sleep (1);
            }
        }
    
        /* shutdown OpenGL */
        DisableOpenGL (hWnd, hDC, hRC);
    
        /* destroy the window explicitly */
        DestroyWindow (hWnd);
    
        return msg.wParam;
    }
    
    
    /********************
     * Window Procedure
     *
     ********************/
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                              WPARAM wParam, LPARAM lParam)
    {
    
        switch (message)
        {
        case WM_CREATE:
            return 0;
        case WM_CLOSE:
            PostQuitMessage (0);
            return 0;
    
        case WM_DESTROY:
            return 0;
    
        case WM_KEYDOWN:
            switch (wParam)
            {
            case VK_ESCAPE:
                PostQuitMessage(0);
                return 0;
            }
            return 0;
    
        default:
            return DefWindowProc (hWnd, message, wParam, lParam);
        }
    }
    
    
    /*******************
     * Enable OpenGL
     *
     *******************/
    
    void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
    {
        PIXELFORMATDESCRIPTOR pfd;
        int iFormat;
    
        /* get the device context (DC) */
        *hDC = GetDC (hWnd);
    
        /* set the pixel format for the DC */
        ZeroMemory (&pfd, sizeof (pfd));
        pfd.nSize = sizeof (pfd);
        pfd.nVersion = 1;
        pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
          PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = 24;
        pfd.cDepthBits = 16;
        pfd.iLayerType = PFD_MAIN_PLANE;
        iFormat = ChoosePixelFormat (*hDC, &pfd);
        SetPixelFormat (*hDC, iFormat, &pfd);
    
        /* create and enable the render context (RC) */
        *hRC = wglCreateContext( *hDC );
        wglMakeCurrent( *hDC, *hRC );
    
    }
    
    
    /******************
     * Disable OpenGL
     *
     ******************/
    
    void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
    {
        wglMakeCurrent (NULL, NULL);
        wglDeleteContext (hRC);
        ReleaseDC (hWnd, hDC);
    }
    sorry if my post was too long

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    Cool

    A quick fix would be to just make your triangle smaller like so:

    Code:
     glColor3f (1.0f, 0.0f, 0.0f);   glVertex3f (0.0f, 0.5f, 0.0f);
     glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f (-0.5f, -0.5f, 0.5f);
     glColor3f (0.0f, 0.0f, 1.0f);   glVertex3f (0.5f, -0.5f, 0.5f);
    Just by cutting your vertex positions in half you can size your triangle down. If its still to large, go to a smaller number (0.3f, 0.1f);

    That is just a quick fix tho. Other ways would be to move the camera back(also a tutorial on NeHe's site) and physically moving the triangle back in the view. read up on those. NeHe is a great resource for OpenGL. Hope that helped!


    ---Brandon

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I was confused about this topic for awhile, and did not understand that modeling and viewing transformations are so closely related, and it confused me. The OpenGL Redbook explains this very well.

    Code:
    The Viewing Transformation
    
    Recall that the viewing transformation is analogous to positioning and aiming a camera. 
    In this code example, before the viewing transformation can be specified, the current matrix 
    is set to the identity matrix with glLoadIdentity(). This step is necessary since most of the 
    transformation commands multiply the current matrix by the specified matrix and then set
    the result to be the current matrix. If you don't clear the current matrix by loading it with 
    the identity matrix, you continue to combine previous transformation matrices with the new 
    one you supply. In some cases, you do want to perform such combinations, but you also 
    need to clear the matrix sometimes.
    
    Once the matrix is initialized, the viewing transformation is specified with glTranslatef(). 
    The arguments for this command indicate how the camera should be translated (moved) 
    in the x, y, and z directions. The arguments used here move the camera 5 units in the 
    negative z direction. By default, the camera as well as any objects in the scene are originally 
    situated at the origin; also, the camera initially points down the negative z-axis. Thus, the 
    particular viewing transformation used here has the effect of pulling the camera away from 
    where the cube is, but it leaves the camera pointing at the object. If the camera needed 
    to be pointed in another direction, you could have used the glRotatef() command to 
    change its orientation. Viewing transformations are discussed in detail in "Viewing and 
    Modeling Transformations."
    
    The Modeling Transformation
    
    You use the modeling transformation to position and orient the model. For example, you 
    can rotate, translate, or scale the model - or perform some combination of these operations. 
    Rotating and translating are performed using the commands already mentioned - glRotatef() 
    and glTranslatef(). In this example, however, the modeling transformation is invoked with 
    glScalef(). The arguments for this command specify how scaling should occur along the three 
    axes. If all the arguments are 1.0, this command has no effect; in Example 3-1 , the cube is 
    drawn twice as large in the y direction. Thus, if one corner of the cube had originally been at 
    (3.0, 3.0, 3.0), that corner would wind up being drawn at (3.0, 6.0, 3.0). The effect of this 
    modeling transformation is to transform the cube so that it isn't a cube but a rectangular box.
    
    Note that instead of pulling the camera back away from the cube (with a viewing 
    transformation) so that it could be viewed, you could have moved the cube away from the 
    camera (with a modeling transformation). This duality in the nature of viewing and modeling 
    transformations is why you need to think about the effect of both types of transformations 
    simultaneously. It doesn't make sense to try to separate the effects, but sometimes it's 
    easier to think about them one way rather than the other. This is also why modeling and 
    viewing transformations are combined into the modelview matrix before the transformations 
    are applied. "Viewing and Modeling Transformations," explains in more detail how to think 
    about modeling and viewing transformations and how to specify them so that you get the 
    result you want.
    So, you could either physically reduce the size of all the things in your world, or move your viewing point back some as you resize the window I guess. Either way, you could make this problem very easy and very complicated for yourself. You would have fun reading the entire chapter 3 of the redbook http://fly.cc.fer.hr/~unreal/theredbook/chapter03.html You could merely move the viewing point back or you could also change the viewing volume and field of view with some wierd calculations that you decide. That chapter discusses it.
    Last edited by Tonto; 11-13-2006 at 07:47 PM.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The triangle will resize with it if you are using an FOV projection matrix. Hence the aspect ratio being related to height and width of the screen/window. Thus when any of those change, so does the aspect ratio which may affect X, Y, or both.

  5. #5
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    As i remember the tutorial, the triangle should resize itself along with the window. But you can fix that in later tutorials.
    Anyway i highly recommend to use the Red Book. It's better than NeHe ( to me ), although NeHe is pretty good too, but it doesn't explain OpenGL theory, just implementation.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    If you read tutorial 1 again it shows you how to fix it, you left out some of the resizing code in that tutorial. His tutorials are cumulative.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM