Sign in to follow this  
apprentice123

OpenGL OpenGl and Windows

Recommended Posts

[font=arial, sans-serif][size=2][/size][/font]
[font="arial, sans-serif"][size="3"]
[/size][/font]
[font="arial, sans-serif"][size="3"]CGfxOpenGL.h[/size][/font]
[font="arial, sans-serif"][size="3"][code][/size][/font]
[font="arial, sans-serif"]

[size=2]#ifndef __GL_COMPONENT[/size]
[size=2]#define __GL_COMPONENT[/size]
[size=2]
[/size]
[size=2]#define PI 3.14159[/size]
[size=2]#define TWO_PI PI*2.0[/size]
[size=2]#define HALF_PI PI/2.0[/size]
[size=2]
[/size]
[size=2]class CGfxOpenGL[/size]
[size=2]{[/size]
[size=2]private:[/size]
[size=2] int m_windowWidth;[/size]
[size=2] int m_windowHeight;[/size]
[size=2]
[/size]
[size=2] float m_angle;[/size]
[size=2]
[/size]
[size=2]public:[/size]
[size=2] CGfxOpenGL();[/size]
[size=2] virtual ~CGfxOpenGL();[/size]
[size=2]
[/size]
[size=2] bool Init();[/size]
[size=2] bool Shutdown();[/size]
[size=2]
[/size]
[size=2] void SetupProjection(int width, int height);[/size]
[size=2]
[/size]
[size=2] void Prepare(float dt);[/size]
[size=2] void Render();[/size]
[size=2]};[/size]
[size=2]
[/size][size=2]#endif[/code][/size][/font]

[font=arial, sans-serif][size=2]
[/size][/font]
[font=arial, sans-serif][size=2]CGfxOpenGL.cpp[/size][/font]
[font=arial, sans-serif][size=2][code][/size][/font]


[font="arial, sans-serif"][size=2]#ifdef _WINDOWS[/size][/font]
[font="arial, sans-serif"][size=2]#include <windows.h>[/size][/font]
[font="arial, sans-serif"][size=2]#endif[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]#include <gl/gl.h>[/size][/font]
[font="arial, sans-serif"][size=2]#include <gl/glu.h>[/size][/font]
[font="arial, sans-serif"][size=2]#include <math.h>[/size][/font]
[font="arial, sans-serif"][size=2]#include "CGfxOpenGL.h"[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]// disable implicit float-double casting[/size][/font]
[font="arial, sans-serif"][size=2]#pragma warning(disable:4305)[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]CGfxOpenGL::CGfxOpenGL()[/size][/font]
[font="arial, sans-serif"][size=2]{[/size][/font]
[font="arial, sans-serif"][size=2]}[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]CGfxOpenGL::~CGfxOpenGL()[/size][/font]
[font="arial, sans-serif"][size=2]{[/size][/font]
[font="arial, sans-serif"][size=2]}[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]bool CGfxOpenGL::Init()[/size][/font]
[font="arial, sans-serif"][size=2]{ [/size][/font]
[font="arial, sans-serif"][size=2] // clear to black background[/size][/font]
[font="arial, sans-serif"][size=2] glClearColor(0.0, 0.0, 0.0, 0.0);[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] m_angle = 0.0f;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] return true;[/size][/font]
[font="arial, sans-serif"][size=2]}[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]bool CGfxOpenGL::Shutdown()[/size][/font]
[font="arial, sans-serif"][size=2]{[/size][/font]
[font="arial, sans-serif"][size=2] return true;[/size][/font]
[font="arial, sans-serif"][size=2]}[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]void CGfxOpenGL::SetupProjection(int width, int height)[/size][/font]
[font="arial, sans-serif"][size=2]{[/size][/font]
[font="arial, sans-serif"][size=2] if (height == 0) // don't want a divide by zero[/size][/font]
[font="arial, sans-serif"][size=2] {[/size][/font]
[font="arial, sans-serif"][size=2] height = 1; [/size][/font]
[font="arial, sans-serif"][size=2] }[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] glViewport(0, 0, width, height); // reset the viewport to new dimensions[/size][/font]
[font="arial, sans-serif"][size=2] glMatrixMode(GL_PROJECTION); // set projection matrix current matrix[/size][/font]
[font="arial, sans-serif"][size=2] glLoadIdentity(); // reset projection matrix[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // calculate aspect ratio of window[/size][/font]
[font="arial, sans-serif"][size=2] gluPerspective(52.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] glMatrixMode(GL_MODELVIEW); // set modelview matrix[/size][/font]
[font="arial, sans-serif"][size=2] glLoadIdentity(); // reset modelview matrix[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] m_windowWidth = width;[/size][/font]
[font="arial, sans-serif"][size=2] m_windowHeight = height;[/size][/font]
[font="arial, sans-serif"][size=2]}[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]void CGfxOpenGL::Prepare(float dt)[/size][/font]
[font="arial, sans-serif"][size=2]{[/size][/font]
[font="arial, sans-serif"][size=2] m_angle += 0.1f;[/size][/font]
[font="arial, sans-serif"][size=2]}[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]void CGfxOpenGL::Render()[/size][/font]
[font="arial, sans-serif"][size=2]{[/size][/font]
[font="arial, sans-serif"][size=2] // clear screen and depth buffer[/size][/font]
[font="arial, sans-serif"][size=2] glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); [/size][/font]
[font="arial, sans-serif"][size=2] glLoadIdentity();[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // move back 5 units and rotate about all 3 axes[/size][/font]
[font="arial, sans-serif"][size=2] glTranslatef(0.0, 0.0, -5.0f);[/size][/font]
[font="arial, sans-serif"][size=2] glRotatef(m_angle, 1.0f, 0.0f, 0.0f);[/size][/font]
[font="arial, sans-serif"][size=2] glRotatef(m_angle, 0.0f, 1.0f, 0.0f);[/size][/font]
[font="arial, sans-serif"][size=2] glRotatef(m_angle, 0.0f, 0.0f, 1.0f);[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // lime greenish color[/size][/font]
[font="arial, sans-serif"][size=2] glColor3f(0.7f, 1.0f, 0.3f);[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // draw the triangle such that the rotation point is in the center[/size][/font]
[font="arial, sans-serif"][size=2] glBegin(GL_TRIANGLES);[/size][/font]
[font="arial, sans-serif"][size=2] glVertex3f(1.0f, -1.0f, 0.0f);[/size][/font]
[font="arial, sans-serif"][size=2] glVertex3f(-1.0f, -1.0f, 0.0f);[/size][/font]
[font="arial, sans-serif"][size=2] glVertex3f(0.0f, 1.0f, 0.0f);[/size][/font]
[font="arial, sans-serif"][size=2] glEnd();[/size][/font]
[font="arial, sans-serif"][size=2]}[/size][/font][font="arial, sans-serif"][size="3"][/code][/size][/font]
[font=arial, sans-serif][size=2]
[/size][/font]
[font=arial, sans-serif][size=2]main.cpp[/size][/font]
[font=arial, sans-serif][size=2][code][/size][/font]


[font="arial, sans-serif"][size=2]#define WIN32_LEAN_AND_MEAN[/size][/font]
[font="arial, sans-serif"][size=2]#define WIN32_EXTRA_LEAN[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]#include <windows.h>[/size][/font]
[font="arial, sans-serif"][size=2]#include <gl/gl.h>[/size][/font]
[font="arial, sans-serif"][size=2]#include <gl/glu.h>[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]#include "CGfxOpenGL.h"[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]bool exiting = false;[/size][/font]
[font="arial, sans-serif"][size=2]long windowWidth = 800;[/size][/font]
[font="arial, sans-serif"][size=2]long windowHeight = 600;[/size][/font]
[font="arial, sans-serif"][size=2]long windowBits = 32;[/size][/font]
[font="arial, sans-serif"][size=2]bool fullscreen = false;[/size][/font]
[font="arial, sans-serif"][size=2]HDC hDC;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]CGfxOpenGL *g_glRender = NULL;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]void SetupPixelFormat(HDC hDC)[/size][/font]
[font="arial, sans-serif"][size=2]{[/size][/font]
[font="arial, sans-serif"][size=2] int pixelFormat;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] PIXELFORMATDESCRIPTOR pfd =[/size][/font]
[font="arial, sans-serif"][size=2] { [/size][/font]
[font="arial, sans-serif"][size=2] sizeof(PIXELFORMATDESCRIPTOR), // size[/size][/font]
[font="arial, sans-serif"][size=2] 1, // version[/size][/font]
[font="arial, sans-serif"][size=2] PFD_SUPPORT_OPENGL | // OpenGL window[/size][/font]
[font="arial, sans-serif"][size=2] PFD_DRAW_TO_WINDOW | // render to window[/size][/font]
[font="arial, sans-serif"][size=2] PFD_DOUBLEBUFFER, // support double-buffering[/size][/font]
[font="arial, sans-serif"][size=2] PFD_TYPE_RGBA, // color type[/size][/font]
[font="arial, sans-serif"][size=2] 32, // prefered color depth[/size][/font]
[font="arial, sans-serif"][size=2] 0, 0, 0, 0, 0, 0, // color bits (ignored)[/size][/font]
[font="arial, sans-serif"][size=2] 0, // no alpha buffer[/size][/font]
[font="arial, sans-serif"][size=2] 0, // alpha bits (ignored)[/size][/font]
[font="arial, sans-serif"][size=2] 0, // no accumulation buffer[/size][/font]
[font="arial, sans-serif"][size=2] 0, 0, 0, 0, // accum bits (ignored)[/size][/font]
[font="arial, sans-serif"][size=2] 16, // depth buffer[/size][/font]
[font="arial, sans-serif"][size=2] 0, // no stencil buffer[/size][/font]
[font="arial, sans-serif"][size=2] 0, // no auxiliary buffers[/size][/font]
[font="arial, sans-serif"][size=2] PFD_MAIN_PLANE, // main layer[/size][/font]
[font="arial, sans-serif"][size=2] 0, // reserved[/size][/font]
[font="arial, sans-serif"][size=2] 0, 0, 0, // no layer, visible, damage masks[/size][/font]
[font="arial, sans-serif"][size=2] };[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] pixelFormat = ChoosePixelFormat(hDC, &pfd);[/size][/font]
[font="arial, sans-serif"][size=2] SetPixelFormat(hDC, pixelFormat, &pfd);[/size][/font]
[font="arial, sans-serif"][size=2]}[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)[/size][/font]
[font="arial, sans-serif"][size=2]{[/size][/font]
[font="arial, sans-serif"][size=2] static HDC hDC;[/size][/font]
[font="arial, sans-serif"][size=2] static HGLRC hRC;[/size][/font]
[font="arial, sans-serif"][size=2] int height, width;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // dispatch messages[/size][/font]
[font="arial, sans-serif"][size=2] switch (uMsg)[/size][/font]
[font="arial, sans-serif"][size=2] { [/size][/font]
[font="arial, sans-serif"][size=2] case WM_CREATE: // window creation[/size][/font]
[font="arial, sans-serif"][size=2] hDC = GetDC(hWnd);[/size][/font]
[font="arial, sans-serif"][size=2] SetupPixelFormat(hDC);[/size][/font]
[font="arial, sans-serif"][size=2] //SetupPalette();[/size][/font]
[font="arial, sans-serif"][size=2] hRC = wglCreateContext(hDC);[/size][/font]
[font="arial, sans-serif"][size=2] wglMakeCurrent(hDC, hRC);[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] case WM_DESTROY: // window destroy[/size][/font]
[font="arial, sans-serif"][size=2] case WM_QUIT:[/size][/font]
[font="arial, sans-serif"][size=2] case WM_CLOSE: // windows is closing[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // deselect rendering context and delete it[/size][/font]
[font="arial, sans-serif"][size=2] wglMakeCurrent(hDC, NULL);[/size][/font]
[font="arial, sans-serif"][size=2] wglDeleteContext(hRC);[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // send WM_QUIT to message queue[/size][/font]
[font="arial, sans-serif"][size=2] PostQuitMessage(0);[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] case WM_SIZE:[/size][/font]
[font="arial, sans-serif"][size=2] height = HIWORD(lParam); // retrieve width and height[/size][/font]
[font="arial, sans-serif"][size=2] width = LOWORD(lParam);[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] g_glRender->SetupProjection(width, height);[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] case WM_ACTIVATEAPP: // activate app[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] case WM_PAINT: // paint[/size][/font]
[font="arial, sans-serif"][size=2] PAINTSTRUCT ps;[/size][/font]
[font="arial, sans-serif"][size=2] BeginPaint(hWnd, &ps);[/size][/font]
[font="arial, sans-serif"][size=2] EndPaint(hWnd, &ps);[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] case WM_LBUTTONDOWN: // left mouse button[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] case WM_RBUTTONDOWN: // right mouse button[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] case WM_MOUSEMOVE: // mouse movement[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] case WM_LBUTTONUP: // left button release[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] case WM_RBUTTONUP: // right button release[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] case WM_KEYUP:[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] case WM_KEYDOWN:[/size][/font]
[font="arial, sans-serif"][size=2] int fwKeys;[/size][/font]
[font="arial, sans-serif"][size=2] LPARAM keyData;[/size][/font]
[font="arial, sans-serif"][size=2] fwKeys = (int)wParam; // virtual-key code [/size][/font]
[font="arial, sans-serif"][size=2] keyData = lParam; // key data [/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] switch(fwKeys)[/size][/font]
[font="arial, sans-serif"][size=2] {[/size][/font]
[font="arial, sans-serif"][size=2] case VK_ESCAPE:[/size][/font]
[font="arial, sans-serif"][size=2] PostQuitMessage(0);[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2] default:[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2] }[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] default:[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2] }[/size][/font]
[font="arial, sans-serif"][size=2] return DefWindowProc(hWnd, uMsg, wParam, lParam);[/size][/font]
[font="arial, sans-serif"][size=2]}[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2]int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)[/size][/font]
[font="arial, sans-serif"][size=2]{[/size][/font]
[font="arial, sans-serif"][size=2] WNDCLASSEX windowClass; // window class[/size][/font]
[font="arial, sans-serif"][size=2] HWND hwnd; // window handle[/size][/font]
[font="arial, sans-serif"][size=2] MSG msg; // message[/size][/font]
[font="arial, sans-serif"][size=2] DWORD dwExStyle; // Window Extended Style[/size][/font]
[font="arial, sans-serif"][size=2] DWORD dwStyle; // Window Style[/size][/font]
[font="arial, sans-serif"][size=2] RECT windowRect;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] g_glRender = new CGfxOpenGL;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] windowRect.left=(long)0; // Set Left Value To 0[/size][/font]
[font="arial, sans-serif"][size=2] windowRect.right=(long)windowWidth; // Set Right Value To Requested Width[/size][/font]
[font="arial, sans-serif"][size=2] windowRect.top=(long)0; // Set Top Value To 0[/size][/font]
[font="arial, sans-serif"][size=2] windowRect.bottom=(long)windowHeight; // Set Bottom Value To Requested Height[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // fill out the window class structure[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.cbSize = sizeof(WNDCLASSEX);[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.style = CS_HREDRAW | CS_VREDRAW;[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.lpfnWndProc = MainWindowProc;[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.cbClsExtra = 0;[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.cbWndExtra = 0;[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.hInstance = hInstance;[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // default icon[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); // default arrow[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.hbrBackground = NULL; // don't need background[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.lpszMenuName = NULL; // no menu[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.lpszClassName = "GLClass";[/size][/font]
[font="arial, sans-serif"][size=2] windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); // windows logo small icon[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // register the windows class[/size][/font]
[font="arial, sans-serif"][size=2] if (!RegisterClassEx(&windowClass))[/size][/font]
[font="arial, sans-serif"][size=2] return 0;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] if (fullscreen) // fullscreen?[/size][/font]
[font="arial, sans-serif"][size=2] {[/size][/font]
[font="arial, sans-serif"][size=2] DEVMODE dmScreenSettings; // device mode[/size][/font]
[font="arial, sans-serif"][size=2] memset(&dmScreenSettings,0,sizeof(dmScreenSettings));[/size][/font]
[font="arial, sans-serif"][size=2] dmScreenSettings.dmSize = sizeof(dmScreenSettings); [/size][/font]
[font="arial, sans-serif"][size=2] dmScreenSettings.dmPelsWidth = windowWidth; // screen width[/size][/font]
[font="arial, sans-serif"][size=2] dmScreenSettings.dmPelsHeight = windowHeight; // screen height[/size][/font]
[font="arial, sans-serif"][size=2] dmScreenSettings.dmBitsPerPel = windowBits; // bits per pixel[/size][/font]
[font="arial, sans-serif"][size=2] dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // [/size][/font]
[font="arial, sans-serif"][size=2] if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)[/size][/font]
[font="arial, sans-serif"][size=2] {[/size][/font]
[font="arial, sans-serif"][size=2] // setting display mode failed, switch to windowed[/size][/font]
[font="arial, sans-serif"][size=2] MessageBox(NULL, "Display mode failed", NULL, MB_OK);[/size][/font]
[font="arial, sans-serif"][size=2] fullscreen = FALSE; [/size][/font]
[font="arial, sans-serif"][size=2] }[/size][/font]
[font="arial, sans-serif"][size=2] }[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] if (fullscreen) // Are We Still In Fullscreen Mode?[/size][/font]
[font="arial, sans-serif"][size=2] {[/size][/font]
[font="arial, sans-serif"][size=2] dwExStyle=WS_EX_APPWINDOW; // Window Extended Style[/size][/font]
[font="arial, sans-serif"][size=2] dwStyle=WS_POPUP; // Windows Style[/size][/font]
[font="arial, sans-serif"][size=2] ShowCursor(FALSE); // Hide Mouse Pointer[/size][/font]
[font="arial, sans-serif"][size=2] }[/size][/font]
[font="arial, sans-serif"][size=2] else[/size][/font]
[font="arial, sans-serif"][size=2] {[/size][/font]
[font="arial, sans-serif"][size=2] dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style[/size][/font]
[font="arial, sans-serif"][size=2] dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style[/size][/font]
[font="arial, sans-serif"][size=2] }[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // class registered, so now create our window[/size][/font]
[font="arial, sans-serif"][size=2] hwnd = CreateWindowEx(NULL, // extended style[/size][/font]
[font="arial, sans-serif"][size=2] "GLClass", // class name[/size][/font]
[font="arial, sans-serif"][size=2] "BOGLGP - Chapter 2 - OpenGL Application", // app name[/size][/font]
[font="arial, sans-serif"][size=2] dwStyle | WS_CLIPCHILDREN |[/size][/font]
[font="arial, sans-serif"][size=2] WS_CLIPSIBLINGS,[/size][/font]
[font="arial, sans-serif"][size=2] 0, 0, // x,y coordinate[/size][/font]
[font="arial, sans-serif"][size=2] windowRect.right - windowRect.left,[/size][/font]
[font="arial, sans-serif"][size=2] windowRect.bottom - windowRect.top, // width, height[/size][/font]
[font="arial, sans-serif"][size=2] NULL, // handle to parent[/size][/font]
[font="arial, sans-serif"][size=2] NULL, // handle to menu[/size][/font]
[font="arial, sans-serif"][size=2] hInstance, // application instance[/size][/font]
[font="arial, sans-serif"][size=2] NULL); // no extra params[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] hDC = GetDC(hwnd);[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] // check if window creation failed (hwnd would equal NULL)[/size][/font]
[font="arial, sans-serif"][size=2] if (!hwnd)[/size][/font]
[font="arial, sans-serif"][size=2] return 0;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] ShowWindow(hwnd, SW_SHOW); // display the window[/size][/font]
[font="arial, sans-serif"][size=2] UpdateWindow(hwnd); // update the window[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] g_glRender->Init();[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] while (!exiting)[/size][/font]
[font="arial, sans-serif"][size=2] {[/size][/font]
[font="arial, sans-serif"][size=2] g_glRender->Prepare(0.0f);[/size][/font]
[font="arial, sans-serif"][size=2] g_glRender->Render();[/size][/font]
[font="arial, sans-serif"][size=2] SwapBuffers(hDC);[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))[/size][/font]
[font="arial, sans-serif"][size=2] {[/size][/font]
[font="arial, sans-serif"][size=2] if (!GetMessage (&msg, NULL, 0, 0))[/size][/font]
[font="arial, sans-serif"][size=2] {[/size][/font]
[font="arial, sans-serif"][size=2] exiting = true;[/size][/font]
[font="arial, sans-serif"][size=2] break;[/size][/font]
[font="arial, sans-serif"][size=2] }[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] TranslateMessage (&msg);[/size][/font]
[font="arial, sans-serif"][size=2] DispatchMessage (&msg);[/size][/font]
[font="arial, sans-serif"][size=2] }[/size][/font]
[font="arial, sans-serif"][size=2] }[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] delete g_glRender;[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] if (fullscreen)[/size][/font]
[font="arial, sans-serif"][size=2] {[/size][/font]
[font="arial, sans-serif"][size=2] ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop[/size][/font]
[font="arial, sans-serif"][size=2] ShowCursor(TRUE); // Show Mouse Pointer[/size][/font]
[font="arial, sans-serif"][size=2] }[/size][/font]
[font="arial, sans-serif"][size=2]
[/size][/font]
[font="arial, sans-serif"][size=2] return (int)msg.wParam;[/size][/font][font="arial, sans-serif"][size=2]}[/size][size="3"][/code][/size][/font]
[font="arial, sans-serif"][size="3"]
[/size][/font]
[font="arial, sans-serif"][size="3"]
[/size][/font]
[font="arial, sans-serif"][size="3"][color=#888888][size=2][color=#000000][size=2]I compiled the codeblocks and appeared the following errors:[/size][/color][color=#1111CC][/color][/size][/color][/size][/font]
[font="arial, sans-serif"][size="3"][code][/size][/font]
[font="arial, sans-serif"][size="3"]
obj\Debug\main.o||In function `Z16SetupPixelFormatP5HDC__':|
E:\Projetos OpenGl\Game Programming\Teste\main.cpp|47|undefined reference to `ChoosePixelFormat@8'|
E:\Projetos OpenGl\Game Programming\Teste\main.cpp|48|undefined reference to `SetPixelFormat@12'|
obj\Debug\main.o||In function `WinMain@16':|
E:\Projetos OpenGl\Game Programming\Teste\main.cpp|244|undefined reference to `SwapBuffers@4'|
||=== Build finished: 3 errors, 0 warnings ===|[/size][/font]
[font="arial, sans-serif"][size="3"][/code]
[/size][/font]
[font="arial, sans-serif"][size="3"]
[/size][/font]
[font=arial, sans-serif][size=2]
[/size][/font]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628390
    • Total Posts
      2982411
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
  • Popular Now