Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // opengl_context.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "opengl_context.h"
- #include "GLExtensions.h"
- #include <windows.h> /* must include this before GL/gl.h */
- #include <GL/gl.h> /* OpenGL header file */
- #include <GL/glu.h> /* OpenGL utilities header file */
- #include <stdio.h>
- #include <assert.h>
- #define POLL 1
- #define ENABLE_VBO 1
- #define RENDER_ITERATIONS 0xf
- static const GLfloat vertices[] = {
- // front
- -1.0f, -1.0f, +1.0f,
- +1.0f, -1.0f, +1.0f,
- -1.0f, +1.0f, +1.0f,
- +1.0f, +1.0f, +1.0f,
- // back
- +1.0f, -1.0f, -1.0f,
- -1.0f, -1.0f, -1.0f,
- +1.0f, +1.0f, -1.0f,
- -1.0f, +1.0f, -1.0f,
- // right
- +1.0f, -1.0f, +1.0f,
- +1.0f, -1.0f, -1.0f,
- +1.0f, +1.0f, +1.0f,
- +1.0f, +1.0f, -1.0f,
- // left
- -1.0f, -1.0f, -1.0f,
- -1.0f, -1.0f, +1.0f,
- -1.0f, +1.0f, -1.0f,
- -1.0f, +1.0f, +1.0f,
- // top
- -1.0f, +1.0f, +1.0f,
- +1.0f, +1.0f, +1.0f,
- -1.0f, +1.0f, -1.0f,
- +1.0f, +1.0f, -1.0f,
- // bottom
- -1.0f, -1.0f, -1.0f,
- +1.0f, -1.0f, -1.0f,
- -1.0f, -1.0f, +1.0f,
- +1.0f, -1.0f, +1.0f,
- };
- static const GLfloat colors[] = {
- // front
- 0.0f, 0.0f, 1.0f, // blue
- 1.0f, 0.0f, 1.0f, // magenta
- 0.0f, 1.0f, 1.0f, // cyan
- 1.0f, 1.0f, 1.0f, // white
- // back
- 1.0f, 0.0f, 0.0f, // red
- 0.0f, 0.0f, 0.0f, // black
- 1.0f, 1.0f, 0.0f, // yellow
- 0.0f, 1.0f, 0.0f, // green
- // right
- 1.0f, 0.0f, 1.0f, // magenta
- 1.0f, 0.0f, 0.0f, // red
- 1.0f, 1.0f, 1.0f, // white
- 1.0f, 1.0f, 0.0f, // yellow
- // left
- 0.0f, 0.0f, 0.0f, // black
- 0.0f, 0.0f, 1.0f, // blue
- 0.0f, 1.0f, 0.0f, // green
- 0.0f, 1.0f, 1.0f, // cyan
- // top
- 0.0f, 1.0f, 1.0f, // cyan
- 1.0f, 1.0f, 1.0f, // white
- 0.0f, 1.0f, 0.0f, // green
- 1.0f, 1.0f, 0.0f, // yellow
- // bottom
- 0.0f, 0.0f, 0.0f, // black
- 1.0f, 0.0f, 0.0f, // red
- 0.0f, 0.0f, 1.0f, // blue
- 1.0f, 0.0f, 1.0f // magenta
- };
- static const GLfloat normals[] = {
- // front
- +0.0f, +0.0f, +1.0f, // forward
- +0.0f, +0.0f, +1.0f, // forward
- +0.0f, +0.0f, +1.0f, // forward
- +0.0f, +0.0f, +1.0f, // forward
- // back
- +0.0f, +0.0f, -1.0f, // backward
- +0.0f, +0.0f, -1.0f, // backward
- +0.0f, +0.0f, -1.0f, // backward
- +0.0f, +0.0f, -1.0f, // backward
- // right
- +1.0f, +0.0f, +0.0f, // right
- +1.0f, +0.0f, +0.0f, // right
- +1.0f, +0.0f, +0.0f, // right
- +1.0f, +0.0f, +0.0f, // right
- // left
- -1.0f, +0.0f, +0.0f, // left
- -1.0f, +0.0f, +0.0f, // left
- -1.0f, +0.0f, +0.0f, // left
- -1.0f, +0.0f, +0.0f, // left
- // top
- +0.0f, +1.0f, +0.0f, // up
- +0.0f, +1.0f, +0.0f, // up
- +0.0f, +1.0f, +0.0f, // up
- +0.0f, +1.0f, +0.0f, // up
- // bottom
- +0.0f, -1.0f, +0.0f, // down
- +0.0f, -1.0f, +0.0f, // down
- +0.0f, -1.0f, +0.0f, // down
- +0.0f, -1.0f, +0.0f // down
- };
- void
- display()
- {
- /* rotate a triangle around */
- glClearColor(0.0, 0.5, 0.5, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- #if ENABLE_VBO
- glDrawArrays(GL_TRIANGLES, 0, 3);
- /*
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
- glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
- glDrawArrays(GL_TRIANGLE_STRIP, 12, 4);
- glDrawArrays(GL_TRIANGLE_STRIP, 16, 4);
- glDrawArrays(GL_TRIANGLE_STRIP, 20, 4);
- */
- #endif
- glFlush();
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- static int pass = 0;
- static PAINTSTRUCT ps;
- UNREFERENCED_PARAMETER(pass);
- switch (uMsg) {
- case WM_PAINT:
- display();
- BeginPaint(hWnd, &ps);
- EndPaint(hWnd, &ps);
- return 0;
- case WM_SIZE:
- glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
- PostMessage(hWnd, WM_PAINT, 0, 0);
- return 0;
- case WM_CHAR:
- switch (wParam) {
- case 27: /* ESC key */
- PostQuitMessage(0);
- break;
- }
- return 0;
- case WM_CLOSE:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- HWND
- CreateOpenGLWindow(char* title, int x, int y, int width, int height,
- BYTE type, DWORD flags)
- {
- int pf;
- HDC hDC;
- HWND hWnd;
- WNDCLASS wc;
- PIXELFORMATDESCRIPTOR pfd;
- static HINSTANCE hInstance = 0;
- const LPCWSTR err1 = L"RegisterClass() failed : Cannot register window class.";
- const LPCWSTR err2 = L"CreateWindow() failed : Cannot create a window.";
- /* only register the window class once - use hInstance as a flag. */
- if (!hInstance) {
- hInstance = GetModuleHandle(NULL);
- wc.style = CS_OWNDC;
- wc.lpfnWndProc = (WNDPROC)WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = NULL;
- wc.lpszMenuName = NULL;
- wc.lpszClassName = L"OpenGL";
- if (!RegisterClass(&wc)) {
- MessageBox(NULL, err1, L"Error", MB_OK);
- return NULL;
- }
- }
- UNREFERENCED_PARAMETER(title);
- hWnd = CreateWindow(L"OpenGL", L"Window Title", WS_OVERLAPPEDWINDOW |
- WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
- x, y, width, height, NULL, NULL, hInstance, NULL);
- if (hWnd == NULL) {
- MessageBox(NULL, err2, L"Error", MB_OK);
- return NULL;
- }
- hDC = GetDC(hWnd);
- /* there is no guarantee that the contents of the stack that become
- the pfd are zeroed, therefore _make sure_ to clear these bits. */
- memset(&pfd, 0, sizeof(pfd));
- pfd.nSize = sizeof(pfd);
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | flags;
- pfd.iPixelType = type;
- pfd.cColorBits = 32;
- pf = ChoosePixelFormat(hDC, &pfd);
- if (pf == 0) {
- MessageBox(NULL, L"ChoosePixelFormat() failed: "
- "Cannot find a suitable pixel format.", L"Error", MB_OK);
- return 0;
- }
- if (SetPixelFormat(hDC, pf, &pfd) == FALSE) {
- MessageBox(NULL, L"SetPixelFormat() failed: "
- "Cannot set format specified.", L"Error", MB_OK);
- return 0;
- }
- DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
- ReleaseDC(hWnd, hDC);
- return hWnd;
- }
- int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPWSTR lpCmdLine,
- _In_ int nCmdShow)
- {
- HDC hDC; /* device context */
- HGLRC hRC; /* opengl context */
- HWND hWnd; /* window */
- MSG msg; /* message */
- hWnd = CreateOpenGLWindow("minimal", 0, 0, 256, 256, PFD_TYPE_RGBA, 0);
- if (hWnd == NULL)
- exit(1);
- hDC = GetDC(hWnd);
- assert(hDC && "hDC context is null");
- hRC = wglCreateContext(hDC);
- wglMakeCurrent(hDC, hRC);
- glEnable(GL_CULL_FACE);
- static const GLfloat g_vertex_buffer_data[] = {
- -1.0f, -1.0f, 0.0f,
- 1.0f, -1.0f, 0.0f,
- 0.0f, 1.0f, 0.0f,
- };
- #if ENABLE_VBO
- GLuint vertexbuffer;
- // Generate 1 buffer, put the resulting identifier in vertexbuffer
- glGenBuffers(1, &vertexbuffer);
- // The following commands will talk about our 'vertexbuffer' buffer
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- // Give our vertices to OpenGL.
- glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- glVertexAttribPointer(
- 0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
- 3, // size
- GL_FLOAT, // type
- GL_FALSE, // normalized?
- 0, // stride
- (void*)0 // array buffer offset
- );
- #endif
- ShowWindow(hWnd, nCmdShow);
- #if POLL
- UNREFERENCED_PARAMETER(msg);
- for (UINT32 i = 0; i < RENDER_ITERATIONS; i++)
- display();
- #else
- while (GetMessage(&msg, hWnd, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- #endif
- wglMakeCurrent(NULL, NULL);
- ReleaseDC(hWnd, hDC);
- wglDeleteContext(hRC);
- DestroyWindow(hWnd);
- return static_cast<int>(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement