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
jyk    2094
Those are WinAPI functions, so make sure you're linking to the appropriate libraries.

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  

  • Partner Spotlight

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now