[C] Exemplo OpenGL

Sub-fórum especial para usuários que querem discutir/aprender programação, como as linguagens C/C++/C#, Java, HTML, e por aí vai...
Rico
Mensagens: 2354
Registrado em: Dom Set 10, 2006 11:49 am
Answers: 0
17
Localização: Atrás do DU0
Contato:

[C] Exemplo OpenGL

Mensagem por Rico »

Segue um exemplo que eu me baseei em vários lugares para conseguir fazer isto... :)
Não minto, algumas coisas foram CTRL+C CTRL+V mas eu sei o que eu fiz, e o que cada um faz, ou ao menos quase o que cada um faz...
xD

/*******************************
* Exemplo de OpenGL no Windows
* Baseado em vários lugares.
*******************************/

#include windows.h
#include gl/gl.h
// entre < >


/**************************
* Declaração de funções
*
**************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);


/**************************
* WinMain (inicialização da Janela Principal)
*
**************************/

int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;

/* Registra a Classe da janela */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);

/* Cria Janela Principal */
hWnd = CreateWindow (
"GLSample", "Exemplo OpenGL",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 500, 500,
NULL, NULL, hInstance, NULL);

/* Abilita OpenGL na Janela */
EnableOpenGL (hWnd, &hDC, &hRC);

/* Loop do programa */
while (!bQuit) {
/* Checa por mensagens especificas */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
/* Se receber uma mensagem */
if (msg.message == WM_QUIT) {
bQuit = TRUE;
}
else {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else {
/* Animação do OpenGL partir daqui */

glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);

glPushMatrix ();
glRotatef (theta, 0.0f, 0.0f, 1.0f);
glBegin (GL_TRIANGLES);
glColor3f (1.0f, 0.0f, 0.0f); glVertex2f (0.0f, 1.0f);
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);

theta += 1.0f;
Sleep (1);
}
}

/* Desliga o OpenGL */
DisableOpenGL (hWnd, hDC, hRC);

/* Fecha a Janela */
DestroyWindow (hWnd);

return msg.wParam;
}


/********************
* Funções da Janela (Procedures)
*
********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam) {

switch (message) {
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;

case WM_DESTROY:
return 0;

case WM_KEYDOWN: {
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;

default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}


/*******************
* Abilitar OpenGL
*
*******************/

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC) {
PIXELFORMATDESCRIPTOR pfd;
int iFormat;

/* Pega o Contexto do dispositivo (????) (DC) */
*hDC = GetDC (hWnd);

/* Configura o formato do Pixel para o 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);

/* Criar e Abilitar a Renderização do Contexto (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );

}


/******************
* Desabilitar OpenGL
*
******************/

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC) {
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}


Espero que tenha ajudados alguns...

Compilando:
Ex:

gcc <arquivo> -o <arquivo> -lglut -lGL -lGLU -lm
ou
gcc -lglut -lGL -lGLU -lm -o <arquivo> <arquivo>

codeblocks:

Compiler Opitions>Adiciones nas libs as libopengl32.a, libgdi32.a, libkernel32.a e libuser32.a
funfa de qualquer jeito :)






Eu quero saber pq o

Código: Selecionar todos

 não funfa ¬¬

Voltar para “Programação”