Results 1 to 6 of 6

Thread: How to scale a polygon?

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2011
    Location
    Deutschland
    Posts
    4

    How to scale a polygon?

    I started learning openGL recently. I am using code::blocks (because VS with oGL was a pain in the ***). This is what my initial program looks like:

    Code :
    #include <windows.h>
    #include <gl/gl.h>
     
    LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
    void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
    void DisableOpenGL(HWND, HDC, HGLRC);
     
     
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,
                       int nCmdShow)
    {
        WNDCLASSEX wcex;
        HWND hwnd;
        HDC hDC;
        HGLRC hRC;
        MSG msg;
        BOOL bQuit = FALSE;
        float theta = 0.0f;
     
        /* register window class */
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style = CS_OWNDC;
        wcex.lpfnWndProc = WindowProc;
        wcex.cbClsExtra = 0;
        wcex.cbWndExtra = 0;
        wcex.hInstance = hInstance;
        wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        wcex.lpszMenuName = NULL;
        wcex.lpszClassName = "GLSample";
        wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;
     
     
        if (!RegisterClassEx(&amp;wcex))
            return 0;
     
        /* create main window */
        hwnd = CreateWindowEx(0,
                              "GLSample",
                              "OpenGL Sample",
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              600,
                              500,
                              NULL,
                              NULL,
                              hInstance,
                              NULL);
     
        ShowWindow(hwnd, nCmdShow);
     
        /* enable OpenGL for the window */
        EnableOpenGL(hwnd, &amp;hDC, &amp;hRC);
     
        /* program main loop */
        while (!bQuit)
        {
            /* check for messages */
            if (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
            {
                /* handle or dispatch messages */
                if (msg.message == WM_QUIT)
                {
                    bQuit = TRUE;
                }
                else
                {
                    TranslateMessage(&amp;msg);
                    DispatchMessage(&amp;msg);
                }
            }
            else
            {
     
                DrawGLScene();
                SwapBuffers(hDC);
                Sleep (1);
            }
        }
     
        /* shutdown OpenGL */
        DisableOpenGL(hwnd, hDC, hRC);
     
        /* destroy the window explicitly */
        DestroyWindow(hwnd);
     
        return msg.wParam;
    }
     
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
            case WM_CLOSE:
                PostQuitMessage(0);
            break;
     
            case WM_DESTROY:
                return 0;
     
            case WM_KEYDOWN:
            {
                switch (wParam)
                {
                    case VK_ESCAPE:
                        PostQuitMessage(0);
                    break;
                }
            }
            break;
     
            default:
                return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
     
        return 0;
    }
     
    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(&amp;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, &amp;pfd);
     
        SetPixelFormat(*hDC, iFormat, &amp;pfd);
     
        /* create and enable the render context (RC) */
        *hRC = wglCreateContext(*hDC);
     
        wglMakeCurrent(*hDC, *hRC);
    }
     
    void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
    {
        wglMakeCurrent(NULL, NULL);
        wglDeleteContext(hRC);
        ReleaseDC(hwnd, hDC);
    }
     
    int DrawGLScene(GLvoid) //Here's where we do all the drawing
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen and thedepth buffer
        glLoadIdentity();   //Reset the view
     
     
     
     
        glColor3f(0,1,0);
        glBegin(GL_POLYGON);
     
        glVertex2f(0.0f,0.0f);
        glVertex2f(1.0f,1.0f);
        glVertex2f(1.0f,2.0f);
        glVertex2f(3.0f,2.0f);
        glVertex2f(2.0f,0.0f);
     
     
        glEnd();
     
            return TRUE;
     
    }

    As you can see I am trying to draw a polygon. But somehow it is always out of bounds of the window
    I am drawing to. Though i picked small values (3.0f, is the highest).

    Why is it because of the window size or do I simply have to scale my polygon?

    Thanks in advance!

    BC++

  2. #2
    Senior Member Regular Contributor imported_tksuoran's Avatar
    Join Date
    Mar 2008
    Location
    Cambridge, UK
    Posts
    223

    Re: How to scale a polygon?

    Your projection and modelview matrices are identity matrices. As result your window effectively has range -1 to 1 in both x and y.
    See http://www.opengl.org/resources/faq/...s.htm#tran0011

    You will need to setup a suitable projection matrix to get something else, see http://www.opengl.org/sdk/docs/man/xhtml/glFrustum.xml for example.

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2011
    Location
    Deutschland
    Posts
    4

    Re: How to scale a polygon?

    Are there any tutorials you could recommend that deal with that subject? NeHe perhaps?

  4. #4
    Junior Member Newbie
    Join Date
    Nov 2011
    Location
    Nepal
    Posts
    7

    Re: How to scale a polygon?

    you can use scale3f(GLfloat x,GLfloat y,GLfloat z) function.It takes the scaling factor as a parameter.If you want to scale whole polygon then put all x,y,z parameter same.To increase the size scaling factor is more then 1.0f and to decrease the size scaling factor is less then 1.0f.If you don't want to scale any co-ordinates pass 1.0f value as parameter.
    example
    scale3f(0.5f,1.0f,0.5f) here it scale the x and z co-ordinates into half but y co-ordinates remain same.

  5. #5
    Junior Member Newbie
    Join Date
    Sep 2011
    Posts
    6

    Re: How to scale a polygon?

    you are using 2D primitive
    for that you have to define your 2D projection (in other words 2D window area).

    use gluOrtho2D(Left , Right , Bottom , Top) glLoadIdentity();

    example:

    if you want that your window's size should 640 x 480 and the origin will at left-bottom corner

    then set as:

    gluOrtho2D ( 0 , 640 , 0 , 480);

    hope this will help you

  6. #6
    Junior Member Newbie
    Join Date
    Sep 2011
    Posts
    6

    Re: How to scale a polygon?

    or use the values of vertices in between -1 and 1

    which will work by default..

Similar Threads

  1. 2D polygon to 3D polygon triangles generation
    By Victor Hugo in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-04-2009, 05:42 PM
  2. Exclude a 2D polygon from a 2D polygon
    By ame_nguyenanhtai in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-25-2008, 04:16 AM
  3. motion of a triangle or polygon inside a polygon
    By Dathu in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 04-09-2008, 05:45 AM
  4. rendering polygon elements edge-on & polygon offset
    By rlskinner in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 02-19-2004, 08:43 AM
  5. Polygon size & world scale
    By Jasonb in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 03-22-2000, 09:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean