Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Come si rende solida un immagine con le Open-gl
Forum - C/C++ - Come si rende solida un immagine con le Open-gl

Avatar
lorenzoscarrone (Normal User)
Pro


Messaggi: 92
Iscritto: 16/11/2011

Segnala al moderatore
Postato alle 22:27
Mercoledì, 29/02/2012
uso Dev-C++ e sono alle prime armi con le open-gl. ho provato a disegnare un tetraedro in /* animation code goes here */, ma il problema sorge durante la rotazione, perchè l'immagine perde la sua compattezza. qualcuno mi saprebbe aiutare, grazie in anticipo? [insieme al codice allego anche il progetto]
Codice sorgente - presumibilmente C++

  1. /**************************
  2. * Includes
  3. *
  4. **************************/
  5.  
  6. #include <windows.h>
  7. #include <gl/gl.h>
  8.  
  9.  
  10. /**************************
  11. * Function Declarations
  12. *
  13. **************************/
  14.  
  15. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  16. WPARAM wParam, LPARAM lParam);
  17. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
  18. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
  19.  
  20.  
  21. /**************************
  22. * WinMain
  23. *
  24. **************************/
  25.  
  26. int WINAPI WinMain (HINSTANCE hInstance,
  27.                     HINSTANCE hPrevInstance,
  28.                     LPSTR lpCmdLine,
  29.                     int iCmdShow)
  30. {
  31.     WNDCLASS wc;
  32.     HWND hWnd;
  33.     HDC hDC;
  34.     HGLRC hRC;        
  35.     MSG msg;
  36.     BOOL bQuit = FALSE;
  37.     float theta = 0.0f;
  38.  
  39.     /* register window class */
  40.     wc.style = CS_OWNDC;
  41.     wc.lpfnWndProc = WndProc;
  42.     wc.cbClsExtra = 0;
  43.     wc.cbWndExtra = 0;
  44.     wc.hInstance = hInstance;
  45.     wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  46.     wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  47.     wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
  48.     wc.lpszMenuName = NULL;
  49.     wc.lpszClassName = "GLSample";
  50.     RegisterClass (&wc);
  51.  
  52.     /* create main window */
  53.     hWnd = CreateWindow (
  54.       "GLSample", "OpenGL Sample",
  55.       WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
  56.       0, 0, 500, 500,
  57.       NULL, NULL, hInstance, NULL);
  58.  
  59.     /* enable OpenGL for the window */
  60.     EnableOpenGL (hWnd, &hDC, &hRC);
  61.  
  62.     /* program main loop */
  63.     while (!bQuit)
  64.     {
  65.         /* check for messages */
  66.         if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  67.         {
  68.             /* handle or dispatch messages */
  69.             if (msg.message == WM_QUIT)
  70.             {
  71.                 bQuit = TRUE;
  72.             }
  73.             else
  74.             {
  75.                 TranslateMessage (&msg);
  76.                 DispatchMessage (&msg);
  77.             }
  78.         }
  79.         else
  80.         {
  81.             /* OpenGL animation code goes here */
  82.  
  83.             glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
  84.             glClear (GL_COLOR_BUFFER_BIT);
  85.  
  86.             glPushMatrix ();
  87.             glRotatef (theta, 0.0f, theta, 0.0f);
  88.             glBegin (GL_TRIANGLES);
  89.              
  90.             glColor3f (1.0f, 0.0f, 0.0f);   glVertex3f ( 0.0f,  0.0f,  1.0f);
  91.             glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f ( 0.8f,  0.0f, -0.5f);
  92.             glColor3f (0.0f, 0.0f, 1.0f);   glVertex3f (-0.8f,  0.0f, -0.5f);
  93.              
  94.             glColor3f (1.0f, 0.0f, 1.0f);   glVertex3f ( 0.0f,  0.0f,  1.0f);
  95.             glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f ( 0.8f,  0.0f, -0.5f);
  96.             glColor3f (1.0f, 0.0f, 1.0f);   glVertex3f ( 0.0f,  0.8f,  0.0f);
  97.              
  98.             glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f ( 0.8f,  0.0f, -0.5f);
  99.             glColor3f (0.0f, 1.0f, 1.0f);   glVertex3f (-0.8f,  0.0f, -0.5f);
  100.             glColor3f (1.0f, 1.0f, 0.0f);   glVertex3f ( 0.0f,  0.8f,  0.0f);
  101.              
  102.             glColor3f (1.0f, 0.0f, 0.0f);   glVertex3f ( 0.0f,  0.8f,  0.0f);
  103.             glColor3f (0.0f, 1.0f, 1.0f);   glVertex3f (-0.8f,  0.0f, -0.5f);
  104.             glColor3f (1.0f, 0.0f, 1.0f);   glVertex3f ( 0.0f,  0.0f,  1.0f);
  105.                          
  106.             glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f ( 0.8f,  0.0f, -0.5f);
  107.             glColor3f (0.0f, 1.0f, 1.0f);   glVertex3f (-0.8f,  0.0f, -0.5f);
  108.             glColor3f (1.0f, 1.0f, 0.0f);   glVertex3f ( 0.0f, -0.8f,  0.0f);
  109.              
  110.             glColor3f (1.0f, 0.0f, 0.0f);   glVertex3f ( 0.0f, -0.8f,  0.0f);
  111.             glColor3f (0.0f, 1.0f, 1.0f);   glVertex3f (-0.8f,  0.0f, -0.5f);
  112.             glColor3f (1.0f, 0.0f, 1.0f);   glVertex3f ( 0.0f,  0.0f,  1.0f);
  113.             glEnd ();
  114.             glPopMatrix ();
  115.  
  116.             SwapBuffers (hDC);
  117.  
  118.             theta = theta+1.0f;
  119.             Sleep (1);
  120.         }
  121.     }
  122.  
  123.     /* shutdown OpenGL */
  124.     DisableOpenGL (hWnd, hDC, hRC);
  125.  
  126.     /* destroy the window explicitly */
  127.     DestroyWindow (hWnd);
  128.  
  129.     return msg.wParam;
  130. }
  131.  
  132.  
  133. /********************
  134. * Window Procedure
  135. *
  136. ********************/
  137.  
  138. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  139.                           WPARAM wParam, LPARAM lParam)
  140. {
  141.  
  142.     switch (message)
  143.     {
  144.     case WM_CREATE:
  145.         return 0;
  146.     case WM_CLOSE:
  147.         PostQuitMessage (0);
  148.         return 0;
  149.  
  150.     case WM_DESTROY:
  151.         return 0;
  152.  
  153.     case WM_KEYDOWN:
  154.         switch (wParam)
  155.         {
  156.         case VK_ESCAPE:
  157.             PostQuitMessage(0);
  158.             return 0;
  159.         }
  160.         return 0;
  161.  
  162.     default:
  163.         return DefWindowProc (hWnd, message, wParam, lParam);
  164.     }
  165. }
  166.  
  167.  
  168. /*******************
  169. * Enable OpenGL
  170. *
  171. *******************/
  172.  
  173. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
  174. {
  175.     PIXELFORMATDESCRIPTOR pfd;
  176.     int iFormat;
  177.  
  178.     /* get the device context (DC) */
  179.     *hDC = GetDC (hWnd);
  180.  
  181.     /* set the pixel format for the DC */
  182.     ZeroMemory (&pfd, sizeof (pfd));
  183.     pfd.nSize = sizeof (pfd);
  184.     pfd.nVersion = 1;
  185.     pfd.dwFlags = PFD_DRAW_TO_WINDOW |
  186.       PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  187.     pfd.iPixelType = PFD_TYPE_RGBA;
  188.     pfd.cColorBits = 24;
  189.     pfd.cDepthBits = 16;
  190.     pfd.iLayerType = PFD_MAIN_PLANE;
  191.     iFormat = ChoosePixelFormat (*hDC, &pfd);
  192.     SetPixelFormat (*hDC, iFormat, &pfd);
  193.  
  194.     /* create and enable the render context (RC) */
  195.     *hRC = wglCreateContext( *hDC );
  196.     wglMakeCurrent( *hDC, *hRC );
  197.  
  198. }
  199.  
  200.  
  201. /******************
  202. * Disable OpenGL
  203. *
  204. ******************/
  205.  
  206. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
  207. {
  208.     wglMakeCurrent (NULL, NULL);
  209.     wglDeleteContext (hRC);
  210.     ReleaseDC (hWnd, hDC);
  211. }



lorenzoscarrone ha allegato un file: OpenGL-prova.zip (12046 bytes)
Clicca qui per scaricare il file
PM