Thread: Plz, teach me how to draw a grid!

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    Question Plz, teach me how to draw a grid!

    Hi, everyone
    I'm a student from Thailand named "Boss" (This is really my nickname)
    Now, I studying C++ and I got some problem.

    Code:
    #include <windows.h>
    #include <gl/gl.h>
    #include <gl/glu.h>
    
    // Global variables
    HWND    hWnd;
    HDC     hDC;
    HGLRC   hRC;
    
    // Set up pixel format for graphics initialization
    void SetupPixelFormat()
    {
        PIXELFORMATDESCRIPTOR pfd, *ppfd;
        int pixelformat; 
    
        ppfd = &pfd;
    
        ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
        ppfd->nVersion = 1;
        ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        ppfd->dwLayerMask = PFD_MAIN_PLANE;
        ppfd->iPixelType = PFD_TYPE_COLORINDEX;
        ppfd->cColorBits = 16;
        ppfd->cDepthBits = 16;
        ppfd->cAccumBits = 0;
        ppfd->cStencilBits = 0;
    
        pixelformat = ChoosePixelFormat(hDC, ppfd);
        SetPixelFormat(hDC, pixelformat, ppfd);
    }
    
    // Initialize OpenGL graphics
    void InitGraphics()
    {
        hDC = GetDC(hWnd);
    
        SetupPixelFormat();
    
        hRC = wglCreateContext(hDC);
        wglMakeCurrent(hDC, hRC);
    
        glClearColor(0, 0, 0, 0.5);
        glClearDepth(1.0);
        glEnable(GL_DEPTH_TEST);
    }
    
    // Resize graphics to fit window
    void ResizeGraphics()
    {
        // Get new window size
        RECT rect;
    	int width, height;
    	GLfloat aspect;
    
        GetClientRect(hWnd, &rect);
        width = rect.right;
        height = rect.bottom;
        aspect = (GLfloat)width / height;
    
        // Adjust graphics to window size
        glViewport(0, 0, width, height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0, aspect, 1.0, 100.0);
        glMatrixMode(GL_MODELVIEW);
    }
    
    // Draw frame
    void DrawGraphics()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
    
    	
    
        // Show the new scene
        glFlush();
        SwapBuffers(hDC);
    }
    
    // Handle window events and messages
    LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM  lParam)
    {
        switch (uMsg)
        {
        case WM_SIZE:
            ResizeGraphics();
            break;
    
        case WM_CLOSE: 
            DestroyWindow(hWnd);
            break;
     
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
     
        // Default event handler
        default: 
            return DefWindowProc (hWnd, uMsg, wParam, lParam); 
            break; 
        } 
     
        return 1; 
    }
    
    // Main function
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    
        const LPCWSTR appname = TEXT("OpenGL Sample");
    
        WNDCLASS wndclass;
        MSG      msg;
     
        // Define the window class
        wndclass.style         = 0;
        wndclass.lpfnWndProc   = (WNDPROC)MainWndProc;
        wndclass.cbClsExtra    = 0;
        wndclass.cbWndExtra    = 0;
        wndclass.hInstance     = hInstance;
        wndclass.hIcon         = LoadIcon(hInstance, appname);
        wndclass.hCursor       = LoadCursor(NULL,IDC_ARROW);
        wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wndclass.lpszMenuName  = appname;
        wndclass.lpszClassName = appname;
     
        // Register the window class
        if (!RegisterClass(&wndclass)) return FALSE;
     
        // Create the window
        hWnd = CreateWindow(
                appname,
                appname,
                WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                800,
                600,
                NULL,
                NULL,
                hInstance,
                NULL);
     
        if (!hWnd) return FALSE;
    
        // Initialize OpenGL
        InitGraphics();
    
        // Display the window
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
    
        // Event loop
        while (1)
        {
            if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
            {
                if (!GetMessage(&msg, NULL, 0, 0)) return TRUE;
    
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            DrawGraphics();
        }
    
        wglDeleteContext(hRC);
        ReleaseDC(hWnd, hDC);
    }
    This is the main code that i must use to draw the grid My teacher told me to use for to loop the grid.
    I tried so hard but it's still not work.

    the grid must look like chest board. I'm very confuse now, Plz help me T^T


    Ps. I must write some code in the function "DrawGraphics" and use GL_LINES or something like this

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Edited for correct code tags
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    I tried so hard but it's still not work.
    Code:
    // Draw frame
    void DrawGraphics()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
    
    	
    
        // Show the new scene
        glFlush();
        SwapBuffers(hDC);
    }
    There's nothing there.
    What have you tried?
    Consider this post signed

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    2
    sry i forgot
    i tried it in many way, first i tried it with the line and tried to make it loop

    void DrawGraphics()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glColor3f(1.0f,1.0f,1.0f);

    for(i=2.0f;i<=-5.0f;i--)
    {
    glBegin(GL_LINES);
    {
    glVertex3f(3.0f,i,-7.0f);
    glVertex3f(-3.0f,i,-7.0f);
    }
    }

    // Show the new scene
    glFlush();
    SwapBuffers(hDC);
    }
    it's still not work T^T it's only black screen but when I bring "for" out, the line appear

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    for(i=2.0f;i<=-5.0f;i--)
    {
    glBegin(GL_LINES);
    {
    glVertex3f(3.0f,i,-7.0f);
    glVertex3f(-3.0f,i,-7.0f);
    }
    }
    Why are those brackets there? That is not how glBegin() works (notice, it is a statement of it's own). You should call glBegin() before the loop, do the vertex drawing in the loop, and then call glEnd(). Most GL commands should not appear between Begin() and End(), including glFlush() and (I'm sure) whatever SwapBuffers does. So you do need to call it, most appropriately right after that loop.

    I'd really recommend you just use something as simple as possible until you get the GL stuff to work, otherwise you are just asking for headaches. Then add stuff in a bit at a time, always checking to see that it renders the way you want.
    Last edited by MK27; 06-30-2010 at 11:17 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    0 / 2 posts on using code tags - this isn't a good sign...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which is the better way to draw?
    By g4j31a5 in forum Game Programming
    Replies: 16
    Last Post: 01-22-2007, 11:56 PM
  2. draw function HELP!!!
    By sunoflight77 in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 11:28 PM
  3. More Windows Trouble
    By samGwilliam in forum Windows Programming
    Replies: 9
    Last Post: 04-12-2005, 10:02 AM
  4. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 05:38 PM
  5. Draw Shapes.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-19-2002, 09:22 AM