Skip to content

Instantly share code, notes, and snippets.

@Eyad-Bereh
Last active December 17, 2019 21:02
Show Gist options
  • Save Eyad-Bereh/0a55e089b517c125842d0c67a9410bd9 to your computer and use it in GitHub Desktop.
Save Eyad-Bereh/0a55e089b517c125842d0c67a9410bd9 to your computer and use it in GitHub Desktop.
Computer Graphics - Moving in 3D space using OpenGL with collision handling
#include "ColorAdapter.h"
RGB ColorAdapter::HexToRGB(int color) {
int Red = ((color & 0xFF0000) >> 16);
int Green = ((color & 0x00FF00) >> 8);
int Blue = color & 0x0000FF;
RGB Result;
Result.Red = Red;
Result.Green = Green;
Result.Blue = Blue;
return Result;
}
RGBSmall ColorAdapter::HexToSmallRGB(int color) {
int Red = ((color & 0xFF0000) >> 16);
int Green = ((color & 0x00FF00) >> 8);
int Blue = color & 0x0000FF;
RGBSmall Result;
Result.Red = (float)Red / 255;
Result.Green = (float)Green / 255;
Result.Blue = (float)Blue / 255;
return Result;
}
int ColorAdapter::RGBToHex(int red, int green, int blue) {
int color = 0x00;
color = color | red;
color = color << 8;
color = color | green;
color = color << 8;
color = color | blue;
return color;
}
int ColorAdapter::RGBToHex(RGB RGBColor) {
int color = 0x00;
color = color | RGBColor.Red;
color = color << 8;
color = color | RGBColor.Green;
color = color << 8;
color = color | RGBColor.Blue;
return color;
}
RGBSmall ColorAdapter::RGBToSmall(int red, int green, int blue) {
RGBSmall Result;
Result.Red = (float)red / 255;
Result.Green = (float)green / 255;
Result.Blue = (float)blue / 255;
return Result;
}
RGBSmall ColorAdapter::RGBToSmall(RGB Color) {
RGBSmall Result;
Result.Red = (float)Color.Red / 255;
Result.Green = (float)Color.Green / 255;
Result.Blue = (float)Color.Blue / 255;
return Result;
}
#ifndef __COLOR_ADAPTER_H__
#define __COLOR_ADAPTER_H__
#include "RGB.h"
#include "RGBSmall.h"
class ColorAdapter {
public:
static const int ALICE_BLUE = 0xF0F8FF;
static const int ANTIQUE_WHITE = 0xFAEBD7;
static const int AQUA = 0x00FFFF;
static const int AQUAMARINE = 0x7FFFD4;
static const int AZURE = 0xF0FFFF;
static const int BEIGE = 0xF5F5DC;
static const int BISQUE = 0xFFE4C4;
static const int BLACK = 0x000000;
static const int BLANCHED_ALMOND = 0xFFEBCD;
static const int BLUE = 0x0000FF;
static const int BLUE_VIOLET = 0x8A2BE2;
static const int BROWN = 0xA52A2A;
static const int BURLY_WOOD = 0xDEB887;
static const int CADET_BLUE = 0x5F9EA0;
static const int CHARTREUSE = 0x7FFF00;
static const int CHOCOLATE = 0xD2691E;
static const int CORAL = 0xFF7F50;
static const int CORNFLOWER_BLUE = 0x6495ED;
static const int CORNSILK = 0xFFF8DC;
static const int CRIMSON = 0xDC143C;
static const int CYAN = 0x00FFFF;
static const int DARK_BLUE = 0x00008B;
static const int DARK_CYAN = 0x008B8B;
static const int DARK_GOLDEN_ROD = 0xB8860B;
static const int DARK_GRAY = 0xA9A9A9;
static const int DARK_GREY = 0xA9A9A9;
static const int DARK_GREEN = 0x006400;
static const int DARK_KHAKI = 0xBDB76B;
static const int DARK_MAGENTA = 0x8B008B;
static const int DARK_OLIVE_GREEN = 0x556B2F;
static const int DARK_ORANGE = 0xFF8C00;
static const int DARK_ORCHID = 0x9932CC;
static const int DARK_RED = 0x8B0000;
static const int DARK_SALMON = 0xE9967A;
static const int DARK_SEA_GREEN = 0x8FBC8F;
static const int DARK_SLATE_BLUE = 0x483D8B;
static const int DARK_SLATE_GRAY = 0x2F4F4F;
static const int DARK_SLATE_GREY = 0x2F4F4F;
static const int DARK_TURQUOISE = 0x00CED1;
static const int DARK_VIOLET = 0x9400D3;
static const int DEEP_PINK = 0xFF1493;
static const int DEEP_SKY_BLUE = 0x00BFFFF;
static const int DIM_GRAY = 0x696969;
static const int DIM_GREY = 0x696969;
static const int DODGER_BLUE = 0x1E90FF;
static const int FIRE_BRICK = 0xB22222;
static const int FLORAL_WHITE = 0xFFFAF0;
static const int FOREST_GREEN = 0x228B22;
static const int FUCHSIA = 0xFF00FF;
static const int GAINSBORO = 0xDCDCDC;
static const int GHOST_WHITE = 0xF8F8FF;
static const int GOLD = 0xFFD700;
static const int GOLDEN_ROD = 0xDAA520;
static const int GRAY = 0x808080;
static const int GREY = 0x808080;
static const int GREEN = 0x008000;
static const int GREEN_YELLOW = 0xADFF2F;
static const int HONEY_DEW = 0xF0FFF0;
static const int HOT_PINK = 0xFF69B4;
static const int INDIAN_RED = 0xCD5C5C;
static const int INDIGO = 0x4B0082;
static const int IVORY = 0xFFFFF0;
static const int KHAKI = 0xF0E68C;
static const int LAVENDER = 0xE6E6FA;
static const int LAVENDER_BLUSH = 0xFFF0F5;
static const int LAWN_GREEN = 0x7CFC00;
static const int LEMON_CHIFFON = 0xFFFACD;
static const int LIGHT_BLUE = 0xADD8E6;
static const int LIGHT_CORAL = 0xF08080;
static const int LIGHT_CYAN = 0xE0FFFF;
static const int LIGHT_GOLDEN_ROD_YELLOW = 0xFAFAD2;
static const int LIGHT_GRAY = 0xD3D3D3;
static const int LIGHT_GREY = 0xD3D3D3;
static const int LIGHT_GREEN = 0x90EE90;
static const int LIGHT_PINK = 0xFFB6C1;
static const int LIGHT_SALMON = 0xFFA07A;
static const int LIGHT_SEA_GREEN = 0x20B2AA;
static const int LIGHT_SKY_BLUE = 0x87CEFA;
static const int LIGHT_SLATE_GRAY = 0x778899;
static const int LIGHT_SLATE_GREY = 0x778899;
static const int LIGHT_STEEL_BLU = 0xB0C4DE;
static const int LIGHT_YELLOW = 0xFFFFE0;
static const int LIME = 0x00FF00;
static const int LIME_GREEN = 0x32CD32;
static const int LINEN = 0xFAF0E6;
static const int MAGENTA = 0xFF00FF;
static const int MAROON = 0x800000;
static const int MEDIUM_AQUA_MARINE = 0x66CDAA;
static const int MEDIUM_BLUE = 0x0000CD;
static const int MEDIUM_ORCHID = 0xBA55D3;
static const int MEDIUM_PURPLE = 0x9370DB;
static const int MEDIUM_SEA_GREEN = 0x3CB371;
static const int MEDIUM_SLATE_BLUE = 0x7B68EE;
static const int MEDIUM_SPRING_GREEN = 0x00FA9A;
static const int MEDIUM_TURQUOISE = 0x48D1CC;
static const int MEDIUM_VIOLET_RED = 0xC71585;
static const int MIDNIGHT_BLUE = 0x191970;
static const int MINT_CREAM = 0xF5FFFA;
static const int MISTY_ROSE = 0xFFE4E1;
static const int MOCCASIN = 0xFFE4B5;
static const int NAVAJO_WHITE = 0xFFDEAD;
static const int NAVY = 0x000080;
static const int OLD_LACE = 0xFDF5E6;
static const int OLIVE = 0x808000;
static const int OLIVE_DRAB = 0x6B8E23;
static const int ORANGE = 0xFFA500;
static const int ORANGE_RED = 0xFF4500;
static const int ORCHID = 0xDA70D6;
static const int PALE_GOLDEN_ROD = 0xEEE8AA;
static const int PALE_GREEN = 0x98FB98;
static const int PALE_TURQUOISE = 0xAFEEEE;
static const int PALE_VIOLET_RED = 0xDB7093;
static const int PAPAYA_WHIP = 0xFFEFD5;
static const int PEACH_PUFF = 0xFFDAB9;
static const int PERU = 0xCD853F;
static const int PINK = 0xFFC0CB;
static const int PLUM = 0xDDA0DD;
static const int POWDER_BLUE = 0xB0E0E6;
static const int PURPLE = 0x800080;
static const int REBECCA_PURPLE = 0x663399;
static const int RED = 0xFF0000;
static const int ROSY_BROWN = 0xBC8F8F;
static const int ROYAL_BLUE = 0x4169E1;
static const int SADDLE_BROWN = 0x8B4513;
static const int SALMON = 0xFA8072;
static const int SANDY_BROWN = 0xF4A460;
static const int SEA_GREEN = 0x2E8B57;
static const int SEA_SHELL = 0xFFF5EE;
static const int SIENNA = 0xA0522D;
static const int SILVER = 0xC0C0C0;
static const int SKY_BLUE = 0x87CEEB;
static const int SLATE_BLUE = 0x6A5ACD;
static const int SLATE_GRAY = 0x708090;
static const int SLATE_GREY = 0x708090;
static const int SNOW = 0xFFFAFA;
static const int SPRING_GREEN = 0x00FF7F;
static const int STEEL_BLUE = 0x4682B4;
static const int TAN = 0xD2B48C;
static const int TEAL = 0x008080;
static const int THISTLE = 0xD8BFD8;
static const int TOMATO = 0xFF6347;
static const int TURQUOISE = 0x40E0D0;
static const int VIOLET = 0xEE82EE;
static const int WHEAT = 0xF5DEB3;
static const int WHITE = 0xFFFFFF;
static const int WHITE_SMOKE = 0xF5F5F5;
static const int YELLOW = 0xFFFF00;
static const int YELLOW_GREEN = 0x9ACD32;
static RGB HexToRGB(int);
static RGBSmall HexToSmallRGB(int);
static int RGBToHex(int, int, int);
static int RGBToHex(RGB);
static RGBSmall RGBToSmall(int, int, int);
static RGBSmall RGBToSmall(RGB);
};
#endif
#ifndef __POINT_H__
#define __POINT_H__
struct Point {
float X;
float Y;
float Z;
};
#endif
#ifndef __RGB_H__
#define __RGB_H__
struct RGB {
short int Red;
short int Green;
short int Blue;
};
#endif
#ifndef __RGB_SMALL_H__
#define __RGB_SMALL_H__
struct RGBSmall {
float Red;
float Green;
float Blue;
};
#endif
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
//#include <gl\glaux.h> // Header File For The GLaux Library
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <string>
#include "ColorAdapter.h"
#include "Point.h"
# define PI acos(-1)
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
HGLRC hRC = NULL; // Permanent Rendering Context
HDC hDC = NULL; // Private GDI Device Context
HWND hWnd = NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
bool keys[256]; // Array Used For The Keyboard Routine
bool active = TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen = FALSE; // Fullscreen Flag Set To Fullscreen Mode By Default
float X = 0, Y = 0.3, Z = 0;
float theta = 0, angle = 0;
Point Points[6] = {
{ 4, 0, 0 },
{-4, 0, 0},
{4, 0, 4},
{4, 0, -4},
{-4, 0, 4},
{-4, 0, -4}
};
const int CUBE_SIZE = 1;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height == 0) // Prevent A Divide By Zero By
{
height = 1; // Making Height Equal One
}
glViewport(0, 0, width, height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
int InitGL() // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enables Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return TRUE; // Initialization Went OK
}
void DrawCube(int top, int bottom, int left, int right, int front, int back, int size = 1) {
glPushMatrix();
RGBSmall Result;
// Top
Result = ColorAdapter::HexToSmallRGB(top);
glBegin(GL_QUADS);
glColor3d(Result.Red, Result.Green, Result.Blue);
glVertex3d(-size, size, size);
glVertex3d(-size, size, -size);
glVertex3d(size, size, -size);
glVertex3d(size, size, size);
glEnd();
// Bottom
Result = ColorAdapter::HexToSmallRGB(bottom);
glBegin(GL_QUADS);
glColor3d(Result.Red, Result.Green, Result.Blue);
glVertex3d(-size, -size, size);
glVertex3d(-size, -size, -size);
glVertex3d(size, -size, -size);
glVertex3d(size, -size, size);
glEnd();
// Left
Result = ColorAdapter::HexToSmallRGB(left);
glBegin(GL_QUADS);
glColor3d(Result.Red, Result.Green, Result.Blue);
glVertex3d(-size, -size, size);
glVertex3d(-size, -size, -size);
glVertex3d(-size, size, -size);
glVertex3d(-size, size, size);
glEnd();
// Right
Result = ColorAdapter::HexToSmallRGB(right);
glBegin(GL_QUADS);
glColor3d(Result.Red, Result.Green, Result.Blue);
glVertex3d(size, -size, size);
glVertex3d(size, -size, -size);
glVertex3d(size, size, -size);
glVertex3d(size, size, size);
glEnd();
// Front
Result = ColorAdapter::HexToSmallRGB(front);
glBegin(GL_QUADS);
glColor3d(Result.Red, Result.Green, Result.Blue);
glVertex3d(-size, -size, size);
glVertex3d(size, -size, size);
glVertex3d(size, size, size);
glVertex3d(-size, size, size);
glEnd();
// Back
Result = ColorAdapter::HexToSmallRGB(back);
glBegin(GL_QUADS);
glColor3d(Result.Red, Result.Green, Result.Blue);
glVertex3d(-size, -size, -size);
glVertex3d(size, -size, -size);
glVertex3d(size, size, -size);
glVertex3d(-size, size, -size);
glEnd();
glPopMatrix();
}
bool IsInsideCube(double x, double y, double z, double x_center, double y_center, double z_center, int size) {
return (x <= x_center + size && x >= x_center - size) &&
(y <= y_center + size && y >= y_center - size) &&
(z <= z_center + size && z >= z_center - size);
}
int DrawGLScene() // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
gluLookAt(X, Y, Z, X + cos(theta), Y + sin(angle), Z + sin(theta), 0, 1, 0);
glPushMatrix();
glTranslated(Points[0].X, Points[0].Y, Points[0].Z);
DrawCube(ColorAdapter::DARK_ORANGE,
ColorAdapter::DARK_ORCHID,
ColorAdapter::DARK_TURQUOISE,
ColorAdapter::RED,
ColorAdapter::SPRING_GREEN,
ColorAdapter::DEEP_PINK,
CUBE_SIZE);
glPopMatrix();
glPushMatrix();
glTranslated(Points[1].X, Points[1].Y, Points[1].Z);
DrawCube( ColorAdapter::DARK_ORANGE,
ColorAdapter::DARK_ORCHID,
ColorAdapter::DARK_TURQUOISE,
ColorAdapter::RED,
ColorAdapter::SPRING_GREEN,
ColorAdapter::DEEP_PINK,
CUBE_SIZE);
glPopMatrix();
glPushMatrix();
glTranslated(Points[2].X, Points[2].Y, Points[2].Z);
DrawCube( ColorAdapter::DARK_ORANGE,
ColorAdapter::DARK_ORCHID,
ColorAdapter::DARK_TURQUOISE,
ColorAdapter::RED,
ColorAdapter::SPRING_GREEN,
ColorAdapter::DEEP_PINK,
CUBE_SIZE);
glPopMatrix();
glPushMatrix();
glTranslated(Points[3].X, Points[3].Y, Points[3].Z);
DrawCube(ColorAdapter::DARK_ORANGE,
ColorAdapter::DARK_ORCHID,
ColorAdapter::DARK_TURQUOISE,
ColorAdapter::RED,
ColorAdapter::SPRING_GREEN,
ColorAdapter::DEEP_PINK,
CUBE_SIZE);
glPopMatrix();
glPushMatrix();
glTranslated(Points[4].X, Points[4].Y, Points[4].Z);
DrawCube(ColorAdapter::DARK_ORANGE,
ColorAdapter::DARK_ORCHID,
ColorAdapter::DARK_TURQUOISE,
ColorAdapter::RED,
ColorAdapter::SPRING_GREEN,
ColorAdapter::DEEP_PINK,
CUBE_SIZE);
glPopMatrix();
glPushMatrix();
glTranslated(Points[5].X, Points[5].Y, Points[5].Z);
DrawCube(ColorAdapter::DARK_ORANGE,
ColorAdapter::DARK_ORCHID,
ColorAdapter::DARK_TURQUOISE,
ColorAdapter::RED,
ColorAdapter::SPRING_GREEN,
ColorAdapter::DEEP_PINK,
CUBE_SIZE);
glPopMatrix();
return TRUE; // Everything Went OK
}
GLvoid KillGLWindow() // Properly Kill The Window
{
if (fullscreen) // Are We In Fullscreen Mode?
{
ChangeDisplaySettings(NULL, 0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
}
if (hRC) // Do We Have A Rendering Context?
{
if (!wglMakeCurrent(NULL, NULL)) // Are We Able To Release The DC And RC Contexts?
{
MessageBox(NULL, "Release Of DC And RC Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
}
if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
{
MessageBox(NULL, "Release Rendering Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
}
hRC = NULL; // Set RC To NULL
}
if (hDC && !ReleaseDC(hWnd, hDC)) // Are We Able To Release The DC
{
MessageBox(NULL, "Release Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
hDC = NULL; // Set DC To NULL
}
if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
{
MessageBox(NULL, "Could Not Release hWnd.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
hWnd = NULL; // Set hWnd To NULL
}
if (!UnregisterClass("OpenGL", hInstance)) // Are We Able To Unregister Class
{
MessageBox(NULL, "Could Not Unregister Class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
hInstance = NULL; // Set hInstance To NULL
}
}
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc; // Windows Class Structure
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left = (long)0; // Set Left Value To 0
WindowRect.right = (long)width; // Set Right Value To Requested Width
WindowRect.top = (long)0; // Set Top Value To 0
WindowRect.bottom = (long)height; // Set Bottom Value To Requested Height
fullscreen = fullscreenflag; // Set The Global Fullscreen Flag
hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Move, And Own DC For Window
wc.lpfnWndProc = (WNDPROC)WndProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = hInstance; // Set The Instance
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
wc.hbrBackground = NULL; // No Background Required For GL
wc.lpszMenuName = NULL; // We Don't Want A Menu
wc.lpszClassName = "OpenGL"; // Set The Class Name
if (!RegisterClass(&wc)) // Attempt To Register The Window Class
{
MessageBox(NULL, "Failed To Register The Window Class.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Exit And Return FALSE
}
if (fullscreen) // Attempt Fullscreen Mode?
{
DEVMODE dmScreenSettings; // Device Mode
memset(&dmScreenSettings, 0, sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
dmScreenSettings.dmSize = sizeof(dmScreenSettings); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
// If The Mode Fails, Offer Two Options. Quit Or Run In A Window.
if (MessageBox(NULL, "The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?", "NeHe GL", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
fullscreen = FALSE; // Select Windowed Mode (Fullscreen=FALSE)
}
else
{
// Pop Up A Message Box Letting User Know The Program Is Closing.
MessageBox(NULL, "Program Will Now Close.", "ERROR", MB_OK | MB_ICONSTOP);
return FALSE; // Exit And Return FALSE
}
}
}
if (fullscreen) // Are We Still In Fullscreen Mode?
{
dwExStyle = WS_EX_APPWINDOW; // Window Extended Style
dwStyle = WS_POPUP; // Windows Style
ShowCursor(FALSE); // Hide Mouse Pointer
}
else
{
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
dwStyle = WS_OVERLAPPEDWINDOW; // Windows Style
}
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
if (!(hWnd = CreateWindowEx(dwExStyle, // Extended Style For The Window
"OpenGL", // Class Name
title, // Window Title
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN | // Required Window Style
dwStyle, // Selected Window Style
0, 0, // Window Position
WindowRect.right - WindowRect.left, // Calculate Adjusted Window Width
WindowRect.bottom - WindowRect.top, // Calculate Adjusted Window Height
NULL, // No Parent Window
NULL, // No Menu
hInstance, // Instance
NULL))) // Don't Pass Anything To WM_CREATE
{
KillGLWindow(); // Reset The Display
MessageBox(NULL, "Window Creation Error.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
bits, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
if (!(hDC = GetDC(hWnd))) // Did We Get A Device Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL, "Can't Create A GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) // Did Windows Find A Matching Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL, "Can't Find A Suitable PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if (!SetPixelFormat(hDC, PixelFormat, &pfd)) // Are We Able To Set The Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL, "Can't Set The PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if (!(hRC = wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL, "Can't Create A GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if (!wglMakeCurrent(hDC, hRC)) // Try To Activate The Rendering Context
{
KillGLWindow(); // Reset The Display
MessageBox(NULL, "Can't Activate The GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
ShowWindow(hWnd, SW_SHOW); // Show The Window
SetForegroundWindow(hWnd); // Slightly Higher Priority
SetFocus(hWnd); // Sets Keyboard Focus To The Window
ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
if (!InitGL()) // Initialize Our Newly Created GL Window
{
KillGLWindow(); // Reset The Display
MessageBox(NULL, "Initialization Failed.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
return TRUE; // Success
}
LRESULT CALLBACK WndProc(HWND hWnd, // Handle For This Window
UINT uMsg, // Message For This Window
WPARAM wParam, // Additional Message Information
LPARAM lParam) // Additional Message Information
{
switch (uMsg) // Check For Windows Messages
{
case WM_ACTIVATE: // Watch For Window Activate Message
{
if (!HIWORD(wParam)) // Check Minimization State
{
active = TRUE; // Program Is Active
}
else
{
active = FALSE; // Program Is No Longer Active
}
return 0; // Return To The Message Loop
}
case WM_SYSCOMMAND: // Intercept System Commands
{
switch (wParam) // Check System Calls
{
case SC_SCREENSAVE: // Screensaver Trying To Start?
case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
return 0; // Prevent From Happening
}
break; // Exit
}
case WM_CLOSE: // Did We Receive A Close Message?
{
PostQuitMessage(0); // Send A Quit Message
return 0; // Jump Back
}
case WM_KEYDOWN: // Is A Key Being Held Down?
{
keys[wParam] = TRUE; // If So, Mark It As TRUE
return 0; // Jump Back
}
case WM_KEYUP: // Has A Key Been Released?
{
keys[wParam] = FALSE; // If So, Mark It As FALSE
return 0; // Jump Back
}
case WM_SIZE: // Resize The OpenGL Window
{
ReSizeGLScene(LOWORD(lParam), HIWORD(lParam)); // LoWord=Width, HiWord=Height
return 0; // Jump Back
}
}
// Pass All Unhandled Messages To DefWindowProc
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, // Instance
HINSTANCE hPrevInstance, // Previous Instance
LPSTR lpCmdLine, // Command Line Parameters
int nCmdShow) // Window Show State
{
MSG msg; // Windows Message Structure
BOOL done = FALSE; // Bool Variable To Exit Loop
/*
// Ask The User Which Screen Mode They Prefer
if (MessageBox(NULL, "Would You Like To Run In Fullscreen Mode?", "Start FullScreen?", MB_YESNO | MB_ICONQUESTION) == IDNO)
{
fullscreen = FALSE; // Windowed Mode
}
*/
// Create Our OpenGL Window
if (!CreateGLWindow("NeHe's OpenGL Framework", 640, 480, 16, fullscreen))
{
return 0; // Quit If Window Was Not Created
}
while (!done) // Loop That Runs Until done=TRUE
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message == WM_QUIT) // Have We Received A Quit Message?
{
done = TRUE; // If So done=TRUE
}
else // If Not, Deal With Window Messages
{
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
else // If There Are No Messages
{
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if (active) // Program Active?
{
if (keys[VK_ESCAPE]) // Was ESC Pressed?
{
done = TRUE; // ESC Signalled A Quit
}
else // Not Time To Quit, Update Screen
{
DrawGLScene(); // Draw The Scene
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
}
}
if (keys[VK_LEFT]) {
keys[VK_LEFT] = false;
theta -= 0.1;
}
if (keys[VK_RIGHT]) {
keys[VK_RIGHT] = false;
theta += 0.1;
}
if (keys['W']) {
keys['W'] = false;
bool flag = true;
for (int i = 0; i < 6; i++) {
if (IsInsideCube( X + 0.4 * cos(theta),
Y + 0.4 * sin(angle),
Z + 0.4 * sin(theta),
Points[i].X,
Points[i].Y,
Points[i].Z,
CUBE_SIZE)) {
flag = false;
}
}
if (flag) {
X += 0.4 * cos(theta);
Y += 0.4 * sin(angle);
Z += 0.4 * sin(theta);
}
}
if (keys['S']) {
keys['S'] = false;
bool flag = true;
for (int i = 0; i < 6; i++) {
if (IsInsideCube( X - 0.4 * cos(theta),
Y - 0.4 * sin(angle),
Z - 0.4 * sin(theta),
Points[i].X,
Points[i].Y,
Points[i].Z,
CUBE_SIZE)) {
flag = false;
}
}
if (flag) {
X -= 0.4 * cos(theta);
Y -= 0.4 * sin(angle);
Z -= 0.4 * sin(theta);
}
}
if (keys['A']) {
keys['A'] = false;
bool flag = true;
for (int i = 0; i < 6; i++) {
if (IsInsideCube( X + 0.2 * sin(theta),
Y,
Z - 0.2 * cos(theta),
Points[i].X,
Points[i].Y,
Points[i].Z,
CUBE_SIZE)) {
flag = false;
}
}
if (flag) {
X += 0.2 * sin(theta);
Z -= 0.2 * cos(theta);
}
}
if (keys['D']) {
keys['D'] = false;
bool flag = true;
for (int i = 0; i < 6; i++) {
if (IsInsideCube( X - 0.2 * sin(theta),
Y,
Z + 0.2 * cos(theta),
Points[i].X,
Points[i].Y,
Points[i].Z,
CUBE_SIZE)) {
flag = false;
}
}
if (flag) {
X -= 0.2 * sin(theta);
Z += 0.2 * cos(theta);
}
}
if (keys[VK_UP]) {
keys[VK_UP] = false;
angle += 0.1;
if (angle > PI / 2) {
angle = PI / 2;
}
}
if (keys[VK_DOWN]) {
keys[VK_DOWN] = false;
angle -= 0.1;
if (angle < -PI / 2) {
angle = -PI / 2;
}
}
if (keys[VK_F2]) {
keys[VK_F2] = false;
char* message = "Coordinates:\nX: %f\nY: %f\nZ: %f\nAngle between X-Axis and Z-Axis: %f\nAngle between X-Axis and Y-Axis: %f";
char buffer[256];
sprintf(buffer, message, X, Y, Z, theta * 180 / PI, angle * 180 / PI);
MessageBox(NULL, buffer, "Coordinates", 0x00000000L);
}
if (keys[VK_F1]) // Is F1 Being Pressed?
{
keys[VK_F1] = FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window
fullscreen = !fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if (!CreateGLWindow("NeHe's OpenGL Framework", 640, 480, 16, fullscreen))
{
return 0; // Quit If Window Was Not Created
}
}
}
}
// Shutdown
KillGLWindow(); // Kill The Window
return (msg.wParam); // Exit The Program
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment