Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: The triangle is not displayed in OpenGL application

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    9

    Question The triangle is not displayed in OpenGL application

    I'm trying to make my first polygon, however seems that the dream is broken.

    Because I wrote a code, complied it and voila ... the triangle is not displayed

    Can somebody say what is wrong with that code?

    Thanks

    Here is the code:

    Code :
    #pragma once
     
    #include <Windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <gl\GL.h>
    #include <gl\glu.h>
    #include <gl\glut.h>
     
    HDC         hDC=NULL;
    HGLRC       hRC=NULL;
     
    GLvoid GL_ReSizeGLScene(GLsizei width, GLsizei height);
    int GL_init( GLvoid );
    int GL_drawit( GLvoid );
     
    typedef struct {
        HWND hWnd;
    } Glab_t;
     
    static Glab_t glab;
     
    char szClassName[ ] = "GLab";
     
    static LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
        switch (message) {
            case WM_DESTROY:
                PostQuitMessage (0);
                break;
            case WM_SIZE:
                GL_ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
                break;
            default:
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
    }
     
     
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
        MSG messages;
        RECT rect;
        WNDCLASSEX wndClass;
        int screenWidth, screenHeight;
        int x, y, w, h;
        GLuint      PixelFormat;
     
        screenWidth = GetSystemMetrics(SM_CXSCREEN);
        screenHeight = GetSystemMetrics(SM_CYSCREEN);
     
        rect.left = (screenWidth - 582) / 2;
        rect.top = (screenHeight - 358) / 2;
        rect.right = rect.left + 582;
        rect.bottom = rect.top + 358;
     
        x = rect.left;
        y = rect.top;
        w = 640;
        h = 480;
     
     
        wndClass.hInstance = hInstance;
        wndClass.lpszClassName = szClassName;
        wndClass.lpfnWndProc = WindowProcedure;
        wndClass.style = CS_DBLCLKS;
        wndClass.cbSize = sizeof (WNDCLASSEX);
        wndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wndClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
        wndClass.lpszMenuName = NULL;
        wndClass.cbClsExtra = 0;
        wndClass.cbWndExtra = 0;
        wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
     
        if (!RegisterClassEx (&wndClass)) {
            return 0;
        }
     
        glab.hWnd = CreateWindowEx (
               0,
               szClassName,
               "GLab - OpenGL",
               WS_OVERLAPPEDWINDOW,
               x,
               y,
               w,
               h,
               HWND_DESKTOP,
               NULL,
               hInstance,
               NULL 
               );
     
        static  PIXELFORMATDESCRIPTOR pfd=  {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
            PFD_TYPE_RGBA,
            16, // Can be 32 tough :)
            0, 0, 0, 0, 0, 0,
            0,
            0,
            0,
            0, 0, 0, 0, 
            16, 
            0,
            0,
            PFD_MAIN_PLANE, 
            0,
            0, 0, 0 
        };
     
        if (!(hDC=GetDC(glab.hWnd))) {
            MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
            return FALSE;
        }
     
        if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) {
            MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
            return FALSE;
        }
     
        if(!SetPixelFormat(hDC,PixelFormat,&pfd)) {
            MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
            return FALSE;
        }
     
        if (!(hRC=wglCreateContext(hDC))) {
            MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
            return FALSE;
        }
     
        if(!wglMakeCurrent(hDC,hRC)) {
            MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
            return FALSE;
        }
     
        ShowWindow (glab.hWnd, nCmdShow);
        SetForegroundWindow(glab.hWnd);
        SetFocus(glab.hWnd);
        GL_ReSizeGLScene( w, h );
     
        if (!GL_init() ) {
            MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
            return false;
        }
     
        if( !GL_drawit() ) {
            MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
            return false;
        }
     
        while (GetMessage (&messages, NULL, 0, 0)) {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
     
        return messages.wParam;
    }
     
    GLvoid GL_ReSizeGLScene(GLsizei width, GLsizei height) {
        if (height==0) {
            height=1;
        }
     
        glViewport(0,0,width,height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
     
    int GL_init( GLvoid ) {
        glShadeModel(GL_SMOOTH);
        glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
        glClearDepth(1.0f);
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
        return true;
    }
     
    int GL_drawit( GLvoid ) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        glTranslatef(-1.5f,0.0f,-6.0f);
        glBegin(GL_TRIANGLES);
            glVertex3f( 0.0f, 1.0f, 0.0f);
            glVertex3f(-1.0f,-1.0f, 0.0f);
            glVertex3f( 1.0f,-1.0f, 0.0f);
        glEnd();            
        return true;
    }

    By the way, I'm using OpenGL 1.4 ( Don't be rude D: , I'm only 15, don't have that much money for new PC )

  2. #2
    Intern Contributor
    Join Date
    Jul 2009
    Posts
    53
    After a VERY quick look, I'm not seeing a SwapBuffers in there...

    Code :
     
    int GL_drawit( GLvoid )
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glLoadIdentity();     glTranslatef(-1.5f,0.0f,-6.0f);
         glBegin(GL_TRIANGLES);
             glVertex3f( 0.0f, 1.0f, 0.0f);
             glVertex3f(-1.0f,-1.0f, 0.0f);
             glVertex3f( 1.0f,-1.0f, 0.0f);
         glEnd();
     
        SwapBuffers( hDC ); <--- This line
     
        return true; }

    EDIT: You're also forgetting to call GL_drawit() in the main message loop
    Last edited by mark ds; 06-05-2012 at 04:26 PM.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    9
    Quote Originally Posted by mark ds View Post
    After a VERY quick look, I'm not seeing a SwapBuffers in there...
    EDIT: You're also forgetting to call GL_drawit() in the main message loop
    Thank You!

    At first it didn't work, so I re-wrote the code and it works!

    After I have learned OpenGL, should I study OpenGL 3.3 or OpenGL 4.2 then?

    Who is interested: ( glab.h includes libs )
    Code :
    #include "glab.h"
     
    #define WINDOW_NAME     "Glab - OpenGL - Polygon"
    #define WINDOW_CLASS    "glab"
    #define WINDOW_STYLE    (WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
     
    typedef struct {
    	HWND             hWnd;
    	HINSTANCE        hInstance;
    } glab_t;
     
    static glab_t glab;
     
    HDC         hDC = 0;
    HGLRC       hRC = 0;
     
    //---------------------------------------------------------------------------------------------------------------------------
    static void Glab_DestoryWindow( void ) {
    	if ( glab.hWnd ){
    		ShowWindow( glab.hWnd, SW_HIDE );
    		DestroyWindow( glab.hWnd );
    		glab.hWnd = NULL;
     
    		UnregisterClass( WINDOW_CLASS, glab.hInstance );
    	}
    }
    //---------------------------------------------------------------------------------------------------------------------------
    static void Glab_Quit( void ) {
    	Glab_DestoryWindow();
    	ExitProcess( 0 );
    }
    //---------------------------------------------------------------------------------------------------------------------------
    static void Glab_Error( const char *text ) {
    	MessageBox( NULL,text,"ERROR",MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL );
    	Glab_Quit();
    }
     
    //---------------------------------------------------------------------------------------------------------------------------
    static LRESULT CALLBACK Glab_WindowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
        switch ( msg ) {
            case WM_DESTROY:
                PostQuitMessage (0);
                break;     
        }
     
        return DefWindowProc (hwnd, msg, wParam, lParam);
    }
    //---------------------------------------------------------------------------------------------------------------------------
    static void Glab_CreateWindow( void ) {
    	RECT        rect;
    	WNDCLASSEX  wndClass;
    	GLuint      PixelFormat;
    	int         screenWidth, screenHeight;
    	int         x,y,w,h;
     
    	screenWidth = GetSystemMetrics( 0 );
    	screenHeight = GetSystemMetrics( 1 );
     
    	rect.left = ( screenWidth - 582 ) / 2;
        rect.top = ( screenHeight - 358 ) / 2;
        rect.right = rect.left + 582;
        rect.bottom = rect.top + 358;
     
    	AdjustWindowRectEx(&rect, WINDOW_STYLE, FALSE, 0);
     
    	x = rect.left;
        y = rect.top;
    	w = 640;
    	h = 480;
     
    	memset( &wndClass, 0, sizeof(WNDCLASSEX) );	
     
    	wndClass.cbSize = sizeof(WNDCLASSEX);
    	wndClass.style = 0;
    	wndClass.lpfnWndProc = Glab_WindowProc;
    	wndClass.cbClsExtra = 0;
        wndClass.cbWndExtra = 0;
    	wndClass.hInstance = glab.hInstance;
    	wndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    	wndClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    	wndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
    	wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    	wndClass.lpszMenuName = NULL;
    	wndClass.lpszClassName = WINDOW_CLASS;
     
    	if ( !RegisterClassEx(&wndClass) ) {
    		Glab_Error( "Could not register window class" );
    	}
     
    	glab.hWnd = CreateWindowEx(0, WINDOW_CLASS, WINDOW_NAME, WINDOW_STYLE, x, y, w, h, NULL, NULL, glab.hInstance, NULL);
     
    	if( !glab.hWnd ) {
    		 UnregisterClass(WINDOW_CLASS, glab.hInstance);
    		 Glab_Error( "Could not create window" );
    	}
     
    	static PIXELFORMATDESCRIPTOR pfd= { sizeof( PIXELFORMATDESCRIPTOR ), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 };
     
    	if ( !(hDC=GetDC(glab.hWnd)) ) {
            Glab_Error("Can't Create A GL Device Context.");
        }
     
    	if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) {
           Glab_Error("Can't Find A Suitable PixelFormat.");
        }
     
        if(!SetPixelFormat(hDC,PixelFormat,&pfd)) {
            Glab_Error("Can't Set The PixelFormat.");
        }
     
        if (!(hRC=wglCreateContext(hDC))) {
            Glab_Error("Can't Create A GL Rendering Context.");
        }
     
        if(!wglMakeCurrent(hDC,hRC)) {
            Glab_Error("Can't Activate The GL Rendering Context.");
        }
     
    	ShowWindow( glab.hWnd, SW_SHOW );
    	UpdateWindow( glab.hWnd );
        SetForegroundWindow( glab.hWnd );
    	SetFocus( glab.hWnd );
    }
    //---------------------------------------------------------------------------------------------------------------------------
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    	MSG msg;
     
    	glab.hInstance = hInstance;
    	SetErrorMode( SEM_FAILCRITICALERRORS );
     
    	Glab_CreateWindow();
     
    	while( 1 ) {
    		if ( !GetMessage(&msg, NULL, 0, 0) ) {
    			Glab_Quit();
    		}
     
    		if ( IsDialogMessage( glab.hWnd, &msg) ) {
    			continue;
    		}
     
    		if (!GL_init() ) {
                Glab_Error("Initialization Failed.");
                return false;
            }
     
            if( !GL_drawit() ) {
                Glab_Error("Initialization Failed.");
                return false;
            }
     
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
     
    	return true;
    }
    // OpenGL-i osa
    //---------------------------------------------------------------------------------------------------------------------------
    int GL_drawit( GLvoid ) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();     
    	glTranslatef( 0.0f,0.0f,-6.0f); // <-, ->, alla
        glBegin(GL_TRIANGLES);
    	   glColor3f(0.0f,1.0f,0.0f); 
           glVertex3f( 0.0f, 1.0f, 0.0f);
     
    	   glColor3f(0.0f,1.0f,0.0f); 
           glVertex3f(-1.0f,-1.0f, 0.0f);
     
    	   glColor3f(0.0f,1.0f,0.0f); 
           glVertex3f( 1.0f,-1.0f, 0.0f);
        glEnd();
        SwapBuffers( hDC );
     
        return true; 
    }
    //---------------------------------------------------------------------------------------------------------------------------
    int GL_init( GLvoid ) {
        glShadeModel(GL_SMOOTH);
        glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
        glClearDepth(1.0f);
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    	glViewport(0,0,640,480);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective(45.0f,(GLfloat)640/(GLfloat)480,0.1f,100.0f);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
        return true;
    }

  4. #4
    Junior Member Regular Contributor Kopelrativ's Avatar
    Join Date
    Apr 2011
    Posts
    214
    OpenGL 4.2 mostly adds new features to 3.3, while 3.3 changes most of what you have already learned. So going to 3.3 first will not be wasted time.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •