Jump to content
  • Advertisement
Sign in to follow this  
GL_aLpHa

OpenGL OpenGL Scene Won't Work

This topic is 4424 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there! I am finally coming back to OpenGL after a break from programming all together. I never really got deep into programming in OpenGL except for copy NeHe's basecode and editing it for the fun of it. Now, I've just wrote basic code to display a triangle, to get the form of a basic OpenGL Window down again. My only problem is that the triangle is not being displayed... the window's fine.
[SOURCE]
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
using namespace std;

//Globals
static HGLRC  g_hRC;                  // rendering context
static HDC    g_hDC;                  // the device context

// Rezize GL Scene
void ResizeGLScene(int width, int height)
{                        
     if(height = 0)            // dont want to divide by zero
     {
               height = 1;
     }
                            
     //reset viewport to new dimensions
     glViewport(0, 0, width, height);
     glMatrixMode(GL_PROJECTION);    // select projection matrix
     glLoadIdentity();               // reset projection matrix
                            
     // calculate aspect ratio of window
     gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f);
                            
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
}   
  
// RenderGLScene
void RenderGLScene()
{
     float angle = 0.0f;   
     
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glLoadIdentity();
     
     angle += 0.1f;
     if(angle >= 360.0f)
     {
              angle = 0.0f;
     }
     
     glTranslatef(0.0f, 0.0f, -5.0f);
     glRotatef(angle, 0.0f, 0.0f, 0.0f);                // rotate along z-axis
     
     glColor3f(1.0f, 1.0f, 1.0f);
     glBegin(GL_TRIANGLES);
         glVertex3f(-1.0f,-1.0f, 0.0f);
         glVertex3f( 1.0f,-1.0f, 0.0f);
         glVertex3f( 0.0f, 1.0f, 0.0f);
     glEnd();
     
     SwapBuffers(g_hDC);
}

// Sets up the pixel format descriptor
void SetupPixelFormat(HDC hDC)
{
     int nPixelFormat;
     
     static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR),
                                          1,
                                          PFD_DRAW_TO_WINDOW |
                                          PFD_SUPPORT_OPENGL |
                                          PFD_DOUBLEBUFFER,
                                          PFD_TYPE_RGBA,
                                          32,
                                          0, 0, 0, 0, 0, 0,
                                          0,
                                          0,
                                          0,
                                          0, 0, 0, 0,
                                          16,
                                          0,
                                          0,
                                          PFD_MAIN_PLANE,
                                          0,
                                          0, 0, 0 };

     // Choose best matching pixel format, return index
     nPixelFormat = ChoosePixelFormat(hDC, &pfd);
     
     // Set Pixel Format to Device Context
     SetPixelFormat(hDC, nPixelFormat, &pfd);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        static HGLRC hRC;                         // rendering context
        static HDC   hDC;                         // device context
                
        switch(message)
        {
                       case WM_CREATE:
                            
                            hDC = GetDC(hWnd);            // get current window's device context
                            g_hDC = hDC;                  // set global device context to current
                            SetupPixelFormat(hDC);        // call pixel format setup function
                            
                            hRC = wglCreateContext(hDC);  // create rendering context
                            g_hRC = hRC;
                            wglMakeCurrent(hDC, hRC);     // make it current
                            
                            return 0;
                            break;
                            
                       case WM_CLOSE:
                            
                            // deselect rendering context and delete it
                            wglMakeCurrent(hDC, NULL);
                            wglDeleteContext(hRC);
                            
                            PostQuitMessage(0);
                            
                            return 0;
                            break;
                            
                       case WM_SIZE:
                            
                            ResizeGLScene(LOWORD(lParam),HIWORD(lParam));
                            
                            return 0;
                            break;
                            
                       default:
                               break;
        }
        
        return (DefWindowProc(hWnd, message, wParam, lParam));    //All unhandled messages
        
}


// Main App Entry
int WINAPI WinMain(     HINSTANCE     hInstance,
                        HINSTANCE     hPrevInstance,
                        LPSTR         lpCmdLine,
                        int           nCmdShow)
{
                   WNDCLASSEX windowClass;                        // Window Class
                   char       lpszClassName[] = "MyClass";        // window class name
                   HWND       hWnd;                               // Window handle
                   MSG        msg;                                // message
                   bool       done = FALSE;                         // flag saying when the app is complete
                   
                   windowClass.cbSize             = sizeof(WNDCLASSEX);
                   windowClass.style              = CS_HREDRAW | CS_VREDRAW;
                   windowClass.lpfnWndProc        = WndProc;
                   windowClass.cbClsExtra         = 0;
                   windowClass.cbWndExtra         = 0;
                   windowClass.hInstance          = hInstance;
                   windowClass.hIcon              = LoadIcon(NULL, IDI_APPLICATION);
                   windowClass.hCursor            = LoadCursor(NULL, IDC_ARROW);
                   windowClass.hbrBackground      = NULL;
                   windowClass.lpszMenuName       = NULL;
                   windowClass.lpszClassName      = lpszClassName;
                   windowClass.hIconSm            = LoadIcon(NULL, IDI_WINLOGO);
                   
                   
                   // Register Window Class
                   if(!RegisterClassEx(&windowClass))
                   {
                             MessageBox(NULL,"ERROR: Could not register window class!", "ERROR",MB_OK|MB_ICONEXCLAMATION);
                             return 0;
                   }
                   
                   // Create Window
                   if(!(hWnd = CreateWindowEx(NULL,                    // Extended style
                                         lpszClassName,           // Class Name
                                         "Default OpenGL Window App",     // App name
                                         WS_OVERLAPPEDWINDOW |    // Window style Flags
                                         WS_VISIBLE |
                                         WS_SYSMENU |
                                         WS_CLIPCHILDREN |
                                         WS_CLIPSIBLINGS,
                                         100, 100,                // X,Y Coordinates
                                         400, 400,                // Window Width, Height
                                         NULL,                    // handle to parent
                                         NULL,                    // handle to menu
                                         hInstance,               // application instance
                                         NULL)))                   // no extra params
                   {
                            MessageBox(NULL,"ERROR: Window could not be created!","ERROR",MB_OK|MB_ICONEXCLAMATION);
                            return 0;
                   }
                   
                   ShowWindow(hWnd, SW_SHOW);
                   UpdateWindow(hWnd);                 
                   
                   // Main message loop
                   done = FALSE;
                   
                   while(!done)
                   {
                               PeekMessage(&msg, hWnd, NULL, NULL, PM_REMOVE);
                               
                               if(msg.message == WM_QUIT)    // Receive a quit meesage?
                               {
                                              done = TRUE;
                               }
                               else
                               {    
                                   // do rendering here //
                                   
                                   RenderGLScene();
                               
                                   TranslateMessage(&msg);   // translatte and dispatch to event queue
                                   DispatchMessage(&msg);
                               }
                   }
                   
     return msg.wParam;
}
[/SOURCE]

Share this post


Link to post
Share on other sites
Advertisement
sorry for posting again... i might as well go to the top of the list while saying what i think the problem is. I think its either the swapbuffers or rendering function... lol pretty obvious... but i get no errors


EDIT:: Nevermind the problem! I fixed it... it was the if(height == 0)... i had if(height = 0)... woops

[Edited by - GL_aLpHa on June 9, 2006 11:15:33 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!