//PROGRAMA QUE CREA UNA PANTALLA Y LA CONECTA CON UN CONTEXTO DE RENDER.. PARA MOSTRAR UN POLIGONO
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <windows.h>
#include <iostream>
#include <cstdio>
#include <ctime>
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) //Esta función siempre se crea siempre con estos argumentos
{ //hwnd será el manejador de la ventana que ha generado el mensaje. Diferencia las distintas ventanas
switch(msg) //.. de una misma clase y, del mismo wndproc.
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
//case WM_CREATE:
//break;
case WM_CREATE:
puts("creada");
break;
default:
puts("mensaje default");
return DefWindowProc(hwnd, msg, wParam, lParam);
break;
}
return 0;
}
int WINAPI WinMain(HINSTANCE m_hinstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int cmdShow){
//.....................variables.....................
HWND m_hwnd; //esto es la ventana
HDC m_hdc; //esto es el contexto de dispositivo
HGLRC m_hglrc;
DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
DWORD dwStyle = WS_OVERLAPPEDWINDOW; //
const int AnchoPantalla = 1000;
const int AltoPantalla = 1000;
//AHORA CREO LA CLASE DE VENTANA: ................... //ANTES DE CREAR LA VENTANA SE HA DE CREAR LA CLASE DE VENTANA
WNDCLASSEX m_windowClass; // Crea la estructura tipo clase ventana
m_windowClass.cbSize = sizeof(WNDCLASSEX); //tamaño de la estructura del tipo wndclassex
m_windowClass.style = CS_HREDRAW | CS_VREDRAW;
m_windowClass.lpfnWndProc = WndProc; //Esta es la funcion que procesa los eventos en la ventana
m_windowClass.cbClsExtra = 0;
m_windowClass.cbWndExtra = 0;
m_windowClass.hInstance = m_hinstance; //Esta es la instancia a la cual pertenece la ventana, normalmente la instancia que se le pasa a winmain()
m_windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // default icon que se muestra al pulsar alt+tab
m_windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); // default arrow Handle al cursor que será mostrado sobre la ventana.
m_windowClass.hbrBackground = NULL; // Pincel para fijar el color de fondo de nuestra ventana.
m_windowClass.lpszMenuName = NULL; // no menu
m_windowClass.lpszClassName = "ClaseVentana"; // nombre para identificar la clase
m_windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); // Handle al ícono pequeño (16x16), usado en la barra de tareas y en la esquina superior izquierda de la ventana.
if (!RegisterClassEx(&m_windowClass)){
MessageBox(NULL, "Falla Registro Ventana", NULL, MB_OK);
exit(1);
}
//Fin clase ventana...................................
//Estructura de tipo RECT ESTO ES UNA ESTRUCTURA QUE CONTIENE 4 DATOS: left, right, top y bottom
RECT m_windowRect;
m_windowRect.left = (long)0;
m_windowRect.right = (long)AnchoPantalla;
m_windowRect.top = (long)0;
m_windowRect.bottom = (long)AltoPantalla;
//.......................
const int windowBPP = 16;
const int windowFullscreen = false;
AdjustWindowRectEx(&m_windowRect, dwStyle, false, dwExStyle);
m_hwnd = CreateWindowEx(NULL, "ClaseVentana", "Capitulo 2 OpenGL", dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
0, 0, m_windowRect.right - m_windowRect.left, m_windowRect.bottom - m_windowRect.top, NULL, NULL, m_hinstance, NULL);
if (m_hwnd == NULL){
MessageBox(NULL, "No se puede crear la ventana de opengl", "Ha ocurrido un error", MB_ICONERROR | MB_OK);
exit(1);
}
m_hdc = GetDC(m_hwnd);
ShowWindow(m_hwnd, SW_SHOW);
UpdateWindow(m_hwnd);
//..............................................
//ESTA PARTE CREA LA VARIABLE DE TIPO PIXELFORMAT.. CON IDENTIFICADOR PFD.. Y LUEGO LA RELLENO
PIXELFORMATDESCRIPTOR pfd;
pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size
1, // version
PFD_SUPPORT_OPENGL | // OpenGL window
PFD_DRAW_TO_WINDOW | // render to window
PFD_DOUBLEBUFFER, // support double-buffering
PFD_TYPE_RGBA, // color type
32, // prefered color depth
0, 0, 0, 0, 0, 0, // color bits (ignored)
0, // no alpha buffer
0, // alpha bits (ignored)
0, // no accumulation buffer
0, 0, 0, 0, // accum bits (ignored)
16, // depth buffer
0, // no stencil buffer
0, // no auxiliary buffers
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0, // no layer, visible, damage masks
};
//...............................
int pixel_format;
pixel_format = ChoosePixelFormat(m_hdc, &pfd); //!!!SUPER IMPORTANTE!!! CHOOSEPIXELFORMAT REQUIERE LIBRERÍA: GDI32 || EN EL LINKER
SetPixelFormat(m_hdc, pixel_format, &pfd);
//....Esto crea el contexto de render a partir de lo anterior....................................................
m_hglrc = wglCreateContext(m_hdc);
wglMakeCurrent(m_hdc, m_hglrc);
if (!m_hglrc){
puts("NO se ha podido crear el contexto del render");
getchar();
exit(1);
}
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_TRIANGLES);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, -0.5f, -4.0f);
glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, -0.5f, -4.0f);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.5f, -4.0f);
glEnd();
getchar();
}