Skip to content

Instantly share code, notes, and snippets.

@Overv
Created December 24, 2010 06:07
Show Gist options
  • Save Overv/753955 to your computer and use it in GitHub Desktop.
Save Overv/753955 to your computer and use it in GitHub Desktop.
// Headers
#include <windows.h>
#include "GLee.h"
// Globals
bool g_WindowOpen = true;
// Window event handler
LRESULT CALLBACK windowEvent( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
if ( msg == WM_DESTROY ) g_WindowOpen = false;
return DefWindowProc( hWnd, msg, wParam, lParam );
}
// Application entry point
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
// Create the window
WNDCLASSEX windowClass;
ZeroMemory( &windowClass, sizeof( windowClass ) );
windowClass.cbSize = sizeof( WNDCLASSEX );
windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
windowClass.lpfnWndProc = windowEvent;
windowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
windowClass.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
windowClass.hCursor = LoadCursor( NULL, IDC_ARROW );
windowClass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
windowClass.lpszClassName = L"OpenGLWindow";
RegisterClassEx( &windowClass );
RECT windowSize;
windowSize.left = 0;
windowSize.top = 0;
windowSize.right = 800;
windowSize.bottom = 600;
AdjustWindowRect( &windowSize, WS_POPUPWINDOW | WS_CAPTION, false );
HWND window = CreateWindowEx( WS_EX_OVERLAPPEDWINDOW, L"OpenGLWindow", L"OpenGL", WS_POPUPWINDOW | WS_CAPTION, 250, 50, windowSize.right - windowSize.left, windowSize.bottom - windowSize.top, NULL, NULL, NULL, NULL );
HDC dc = GetDC( window );
ShowWindow( window, SW_SHOWNORMAL );
UpdateWindow( window );
// Choose appropriate pixel format
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
int pixelFormat = ChoosePixelFormat( dc, &pfd );
SetPixelFormat( dc, pixelFormat, &pfd );
// Create OpenGL context
HGLRC context = wglCreateContext( dc );
wglMakeCurrent( dc, context );
// Set viewport and clear color
glViewport( 0, 0, 800, 600 );
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
// Load shaders and specify input sources
const char* vertexShaderSource = "in vec2 in_Position;\
in vec3 in_Color;\
varying vec3 ex_Color;\
\
void main()\
{\
gl_Position = vec4( ( in_Position.x - 400.0 ) / 400.0, ( 300.0 - in_Position.y ) / 300.0, 0.0, 1.0 );\
ex_Color = in_Color;\
}";
const char* fragmentShaderSource = "varying vec3 ex_Color;\
\
void main()\
{\
gl_FragColor = vec4( ex_Color, 1.0 );\
}";
int vertexShader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( vertexShader, 1, &vertexShaderSource, 0 );
glCompileShader( vertexShader );
int fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( fragmentShader, 1, &fragmentShaderSource, 0 );
glCompileShader( fragmentShader );
int shaderProgram = glCreateProgram();
glAttachShader( shaderProgram, vertexShader );
glAttachShader( shaderProgram, fragmentShader );
glBindAttribLocation( shaderProgram, 0, "in_Position" );
glBindAttribLocation( shaderProgram, 1, "in_Color" );
glLinkProgram( shaderProgram );
glUseProgram( shaderProgram );
// Put vertex data in a VBO and set the position and color pointers
float vertices[] = {
50.0f, 50.0f, 1.0f, 0.0f, 0.0f,
750.0f, 50.0f, 0.0f, 1.0f, 0.0f,
750.0f, 550.0f, 0.0f, 0.0f, 1.0f,
750.0f, 550.0f, 0.0f, 0.0f, 1.0f,
50.0f, 550.0f, 1.0f, 0.0f, 1.0f,
50.0f, 50.0f, 1.0f, 0.0f, 0.0f,
};
unsigned int vbo;
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof( float ) * 5, 0 );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, sizeof( float ) * 5, (void*)( 2 * sizeof( float ) ) );
glEnableVertexAttribArray( 1 );
// Main loop
MSG msg;
while ( g_WindowOpen )
{
while ( PeekMessage( &msg, window, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
// Clear and draw
glClear( GL_COLOR_BUFFER_BIT );
glDrawArrays( GL_TRIANGLES, 0, 6 );
// Present
SwapBuffers( dc );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment