Translate To Preferred Language

Search Obioku's Thoughts

Please Read Today's Featured Post

Things I Would Like To Happen Soon

For all those who believe in speaking ideas into existence and prayer in the universe will return to a person.  I simply want to write a lis...

Template for Shape Animation in C++ and OpenGL (some bugs may occur)

//
// GLSAMPLE.CPP
//  Source Code was adapted
//  from http://www.nullterminator.net/opengl32.html written by Blaine Hodge
//

// Includes

#include [windows.h]
#include [gl/gl.h]
#include [gl/glu.h]
//#include [gl/glut.h]


// Function Declarations

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  LPSTR lpCmdLine, int iCmdShow)

{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL quit = FALSE;
float theta = 0.0f;
float beta = 0.0f;

// register window class
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 = "COSC 575";
RegisterClass( &wc );

// create main window
hWnd = CreateWindow(
"COSC 575", "Project 1",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
700, 350, 600, 600,
NULL, NULL, hInstance, NULL );

// enable OpenGL for the window
EnableOpenGL( hWnd, &hDC, &hRC );

// program main loop
while ( !quit )
{

// check for messages
if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )  )
{

// handle or dispatch messages
if ( msg.message == WM_QUIT )
{
quit = TRUE;
}
else
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}

}
else
{

GLuint texture;
glGenTextures(1, &texture);

GLuint texture1;
glGenTextures(2, &texture1);

//glBlend(1.0f, 1.0f, 1.0f, 0.5f);
// OpenGL animation code goes here

glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );
glShadeModel(GL_FLAT);
glMatrixMode(GL_MODELVIEW);
glClearDepth(1.0);
//gluLookAt(0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glPushMatrix();
glRotatef( theta, 0.4f, 0.1f, 1.0f );
glMaterialf(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, 125.0f);
glLightf(GL_LIGHT3, GL_SPECULAR, 50.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
glTranslatef(0.4f, 0.1f, 1.0f);
glBegin( GL_TRIANGLES );
glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );
glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.87f, -0.5f );
glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( -0.87f, -0.5f );
glEnd();
glPopMatrix();

glPushMatrix();
glRotatef(beta, 0.1f, 0.3f, 0.5f);
glMaterialf(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, 200.0f);
glLightf(GL_LIGHT1, GL_AMBIENT, 50.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
glTranslatef(0.1f, 0.3f, 0.5f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.87f, 0.5f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(-0.87f, 0.5f);
glEnd();
glPopMatrix();

glLoadIdentity();
glFlush();

SwapBuffers( hDC );

theta += 0.02f;
beta -= 0.02f;

}

}

// shutdown OpenGL
DisableOpenGL( hWnd, hDC, hRC );

// destroy the window explicitly
DestroyWindow( hWnd );

return msg.wParam;

}

// Window Procedure

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:
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 format;

// 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 = 64;
pfd.cDepthBits = 64;
pfd.iLayerType = PFD_MAIN_PLANE;
format = ChoosePixelFormat( *hDC, &pfd );
SetPixelFormat( *hDC, format, &pfd );

// create and enable the render context (RC)
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT3);
glEnable(GL_BLEND);
glEnable(GL_DEPTH_FUNC);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);


}

// Disable OpenGL

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

}

No comments:

Post a Comment

Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!