c - The triangle is not displayed in OpenGL application -
i'm trying make first polygon.
because wrote code, complied , voila ... triangle not displayed :(
what wrong 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 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 gl device context.","error",mb_ok|mb_iconexclamation); return false; } if (!(pixelformat=choosepixelformat(hdc,&pfd))) { messagebox(null,"can't find suitable pixelformat.","error",mb_ok|mb_iconexclamation); return false; } if(!setpixelformat(hdc,pixelformat,&pfd)) { messagebox(null,"can't set pixelformat.","error",mb_ok|mb_iconexclamation); return false; } if (!(hrc=wglcreatecontext(hdc))) { messagebox(null,"can't create gl rendering context.","error",mb_ok|mb_iconexclamation); return false; } if(!wglmakecurrent(hdc,hrc)) { messagebox(null,"can't activate 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; } using mc visual c++ 2010 express , opengl 1.4.
you need swapbuffers() call in there after render scene:
#include <windows.h> #include <gl/gl.h> #include <gl/glu.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 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 gl device context.","error",mb_ok|mb_iconexclamation); return false; } if (!(pixelformat=choosepixelformat(hdc,&pfd))) { messagebox(null,"can't find suitable pixelformat.","error",mb_ok|mb_iconexclamation); return false; } if(!setpixelformat(hdc,pixelformat,&pfd)) { messagebox(null,"can't set pixelformat.","error",mb_ok|mb_iconexclamation); return false; } if (!(hrc=wglcreatecontext(hdc))) { messagebox(null,"can't create gl rendering context.","error",mb_ok|mb_iconexclamation); return false; } if(!wglmakecurrent(hdc,hrc)) { messagebox(null,"can't activate 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; } while (getmessage (&messages, null, 0, 0)) { translatemessage(&messages); dispatchmessage(&messages); if( !gl_drawit() ) { messagebox(null,"initialization failed.","error",mb_ok|mb_iconexclamation); return false; } swapbuffers(hdc); } 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; } edit: moved gl_drawit() call event loop redraws in response resize events. well, event @ really.
Comments
Post a Comment