Created
March 8, 2015 20:30
-
-
Save nlguillemot/fcedd3fccdea163186a7 to your computer and use it in GitHub Desktop.
super basic GL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <GL/gl.h> | |
#include <cmath> | |
int lineWidth = 1; | |
LRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) | |
{ | |
if (message == WM_CLOSE) | |
{ | |
ExitProcess(0); | |
} | |
else if (message == WM_KEYDOWN) | |
{ | |
if (wParam == VK_UP) | |
{ | |
lineWidth++; | |
} | |
else if (wParam == VK_DOWN) | |
{ | |
lineWidth--; | |
if (lineWidth <= 0) lineWidth = 1; | |
} | |
} | |
return DefWindowProc(hWnd, message, wParam, lParam); | |
} | |
int main() | |
{ | |
WNDCLASSEX wndClass; | |
ZeroMemory(&wndClass, sizeof wndClass); | |
wndClass.cbSize = sizeof wndClass; | |
wndClass.style = CS_OWNDC; | |
wndClass.hInstance = GetModuleHandle(NULL); | |
wndClass.hbrBackground = (HBRUSH) COLOR_BACKGROUND; | |
wndClass.lpszClassName = TEXT("SuperBasicGLWindowClass"); | |
wndClass.lpfnWndProc = MyWndProc; | |
RegisterClassEx(&wndClass); | |
HWND hWnd = CreateWindowEx( | |
0, TEXT("SuperBasicGLWindowClass"), | |
TEXT("SuperBasicGL"), | |
WS_OVERLAPPEDWINDOW, | |
0, 0, 640, 480, | |
0, 0, GetModuleHandle(NULL), | |
0); | |
ShowWindow(hWnd, SW_SHOWNORMAL); | |
PIXELFORMATDESCRIPTOR pfd = { | |
sizeof pfd, | |
1, | |
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, | |
PFD_TYPE_RGBA, | |
32, | |
0, 0, 0, 0, 0, 0, | |
0, | |
0, | |
0, | |
0, 0, 0, 0, | |
24, | |
8, | |
0, | |
PFD_MAIN_PLANE, | |
0, | |
0, 0, 0 | |
}; | |
HDC hDC = GetDC(hWnd); | |
int chosenPixelFormat = ChoosePixelFormat(hDC, &pfd); | |
SetPixelFormat(hDC, chosenPixelFormat, &pfd); | |
HGLRC hGLRC = wglCreateContext(hDC); | |
wglMakeCurrent(hDC, hGLRC); | |
while (1) | |
{ | |
MSG msg; | |
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) | |
{ | |
TranslateMessage(&msg); | |
DispatchMessage(&msg); | |
} | |
glClear(GL_COLOR_BUFFER_BIT); | |
static float x = 0; | |
x++; | |
float translationX = std::sin(x / 30.0f) * 0.5f; | |
glLoadIdentity(); | |
glTranslatef(translationX, 0, 0); | |
glBegin(GL_TRIANGLES); | |
glColor3ub(255, 0, 0); | |
glVertex2f(0.0f, 0.0f); | |
glVertex2f(0.0f,-1.0f); | |
glVertex2f(-1.0f, -1.0f); | |
glEnd(); | |
static float rot = 0; | |
rot++; | |
glRotatef(rot, 0, 0, 1); | |
glBegin(GL_TRIANGLES); | |
glColor3ub(255, 255, 255); | |
glVertex2f(0.0f, 0.0f); | |
glVertex2f(0.5f, 0.0f); | |
glVertex2f(0.5f, 0.5f); | |
glEnd(); | |
glTranslatef(0.5f, 0.5f, 0.0f); | |
glRotatef(rot *2, 0, 0, 1); | |
glBegin(GL_TRIANGLES); | |
glColor3ub(0, 255, 0); | |
glVertex2f(0.0f, 0.0f); | |
glVertex2f(1.0f, 0.0f); | |
glVertex2f(1.0f, 1.0f); | |
glEnd(); | |
glLoadIdentity(); | |
glPointSize(lineWidth); | |
glBegin(GL_POINTS); | |
int numPoints = 1000; | |
for (int i = 0; i < numPoints; i++) | |
{ | |
float p = (float) i / numPoints; | |
static float lineOffset = 0.0f; | |
lineOffset++; | |
glColor3f(p, 1 - p, std::sin(p * lineOffset * 5 / 10000.0f) * 2); | |
glVertex2f(-1.0f + p * 2.0f, | |
std::sin(p * M_PI * 20) * 0.5f + std::sin(lineOffset / numPoints * M_PI * 2.01f) * 0.5f); | |
} | |
glEnd(); | |
glEnable(GL_BLEND); | |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
glBegin(GL_TRIANGLES); | |
glColor4f(1.0f, 0.0f, 0.0f, 0.5f); | |
glVertex2f(0.0f, -0.5f); | |
glColor4f(0.0f, 1.0f, 0.0f, 0.5f); | |
glVertex2f(0.5f, 0.5f); | |
glColor4f(0.0f, 0.0f, 1.0f, 0.5f); | |
glVertex2f(-0.5f, 0.5f); | |
glEnd(); | |
glDisable(GL_BLEND); | |
SwapBuffers(hDC); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment