miércoles, 16 de enero de 2013

Ejemplo del Cubo Usando Open Gl /************************** * Includes * **************************/ // LIBRERIAS DE WINDOW //LIBRERIAS DE WINDOW #include #include /************************** * Function Declarations //DECLARACION DE FUNCIONES * **************************/ LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC); //INICIALIZACION DE LA VENTANA GL void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC); //ELEMINA LA VENTANA /************************** * WinMain // WINMAIN.- COMIENZA CON SU EJECUCION COMO MAIN ( ) PERO ES UNA EJECUCION VACIA * **************************/ int WINAPI WinMain (HINSTANCE hInstance, // WINAPI.- AGRUPA LO RELACIONADO CON UN PROGRAMA HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) { WNDCLASS wc; HWND hWnd; // HWND.- SIRVE PARA LLAMAR COMANDOS Y TAMBIEN SE UTILIZA PARA // TENER ACCESO A UNA VENTANA //MANEJADOR ("HANDLE")DE LA NUEVA VENTANA HDC hDC; HGLRC hRC; MSG msg; BOOL bQuit = FALSE; float theta = 0.0f; /* register window class */ //DEFINR DE LAS CLASES DE WINDOW wc.style = CS_OWNDC; //INDICA LA APLICACION CAPTURA DOBLE-CLICKS wc.lpfnWndProc = WndProc; //PUNTERO DE LA FUNCION(2) wc.cbClsExtra = 0; // DATOS ADICIONALES wc.cbWndExtra = 0; //MANEJOR ("HANDLE") A LA INSTANCIA DE LA EJECUCIÓN wc.hIcon = LoadIcon (NULL, IDI_APPLICATION); //ICONO QUE SE USARA EN LA //APLICACIÓN wc.hCursor = LoadCursor (NULL, IDC_ARROW); //CARGA EL CURSOR POR DEFECTO wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH); //COLOR DE FONDO wc.lpszMenuName = NULL; //MENÚ QUE UTILIZARA LA //VENTANA wc.lpszClassName = "GLSample"; //NOMBRE PARA IDENTIFICAR LA CLASE RegisterClass (&wc); //REGISTRAR LA CLASES /* create main window */ //CREAR LA VENTANA DE WINDOW hWnd = CreateWindow ( // "GLSample", "OpenGL Sample", WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 0, 0, 800, 600, NULL, NULL, hInstance, NULL); /* enable OpenGL for the window */ EnableOpenGL (hWnd, &hDC, &hRC); /* program main loop */ while (!bQuit) { /* check for messages */ if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) { /* handle or dispatch messages */ if (msg.message == WM_QUIT) { bQuit = TRUE; } else { TranslateMessage (&msg); DispatchMessage (&msg); } } else { /* OpenGL animation code goes here */ // cambia el color del fondo glClearColor (0.0f, 0.0f, 0.0f, 0.0f); //INICIAR ESTADO GLOBAL (COLOR //DE BORRADO) glClear (GL_COLOR_BUFFER_BIT); //BORRE LA PANTALLA // posicion del trayectori del triangulo o cualquier figura glPushMatrix (); //GLPUSHMATRIX.- COLOCAR LA MATRIZ EN LA PILA. glRotatef (theta, 0.0f, 1.0f, 0.0f); //ANGULO EN QUE SE ROTA EL PUNTO glBegin (GL_TRIANGLES); //PINTE glColor3f (1.0f, 0.0f, 0.0f); glVertex2f (1.0f, 1.0f); //GLVERTEX.- ES UN PROCESO DE ESPECIFICACION DE OBJETO PARA REPREsENTAR UN PROGRAMA //SOMBREADO glColor3f (0.0f, 1.0f, 0.0f); glVertex2f (0.87f, -0.5f); glColor3f (0.0f, 0.0f, 1.0f); glVertex2f (-0.87f, -0.5f); glEnd (); glPopMatrix (); SwapBuffers (hDC); //CAMBIE BUFFERS theta += 1.0f; Sleep (1); } } /* shutdown OpenGL */ DisableOpenGL (hWnd, hDC, hRC); /* destroy the window explicitly */ DestroyWindow (hWnd); return msg.wParam; } /******************** * Window Procedure * ********************/ LRESULT CALLBACK WndProc (HWND hWnd, UINT message, // CALLBACK .- ES UNA FUNCION //QUE SE USA COMO ARGUMENTO DE OTRA, PARA LLAMAR A OTRA FUNCION // WPARAM wParam, LPARAM lParam) { switch (message) //ADICIONAR LOS CASOS PAAR LOS DIFERENTES MENSAJE { case WM_CREATE: return 0; case WM_CLOSE: PostQuitMessage (0); // POSTQUIT MESSAGE.-INDICA QUE HA HECHO UNA SOLICITUD //DE TERMINACION Y SE UTILIZA COMO RESPUESTA return 0; case WM_DESTROY: return 0; case WM_KEYDOWN: switch (wParam) { case VK_ESCAPE: PostQuitMessage(0); return 0; } return 0; default: return DefWindowProc (hWnd, message, wParam, lParam); } } /******************* * Enable OpenGL * *******************/ void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC) { PIXELFORMATDESCRIPTOR pfd; int iFormat; /* get the device context (DC) */ *hDC = GetDC (hWnd); /* set the pixel format for the DC */ ZeroMemory (&pfd, sizeof (pfd)); pfd.nSize = sizeof (pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 16; pfd.iLayerType = PFD_MAIN_PLANE; iFormat = ChoosePixelFormat (*hDC, &pfd); SetPixelFormat (*hDC, iFormat, &pfd); /* create and enable the render context (RC) */ *hRC = wglCreateContext( *hDC ); wglMakeCurrent( *hDC, *hRC ); } /****************** * Disable OpenGL * ******************/ void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC) { wglMakeCurrent (NULL, NULL); wglDeleteContext (hRC); ReleaseDC (hWnd, hDC); }

sábado, 3 de noviembre de 2012