Permalink
Browse files

Separated renderer from game logic

  • Loading branch information...
1 parent 9c6aadd commit 6c3cac3021c06c03fa3f803de857715f6b78c4de @Ymihere03 committed with Dec 12, 2011
Showing with 698 additions and 582 deletions.
  1. +3 −5 ActorManager.cpp
  2. +4 −1 ActorManager.h
  3. +4 −1 Bullet.cpp
  4. +1 −1 Camera.cpp
  5. +1 −1 Camera.h
  6. +62 −551 OpenGL.cpp
  7. +15 −15 OpenGL.h
  8. +538 −0 OpenGLRender.cpp
  9. +63 −0 OpenGLRender.h
  10. +7 −4 TerrainGen.cpp
  11. +0 −3 TerrainGen.h
View
8 ActorManager.cpp
@@ -1,7 +1,6 @@
#include "ActorManager.h"
-TerrainGen * world;
int nextID = 0, listSize = 0;
struct ActorList
@@ -13,11 +12,10 @@ struct ActorList
ActorList *root;
-ActorManager::ActorManager(){}
-
-ActorManager::ActorManager(TerrainGen &w)
+ActorManager::ActorManager()
{
- world = &w;
+ //world = w;
+ world = new TerrainGen();
for(int i = 0; i < 5; i++)
{
View
5 ActorManager.h
@@ -4,14 +4,16 @@
#include "TerrainGen.h"
#include "Tank.h"
#include "Bullet.h"
+#include "Camera.h"
class ActorManager
{
public:
+ TerrainGen * world;
ActorManager();
- ActorManager(TerrainGen &w);
+ ActorManager(TerrainGen *w);
void addObject();
void removeObject(int index);
void updateObjects(int timeElapsed);
@@ -26,6 +28,7 @@ class ActorManager
~ActorManager(void);
private:
+
int getNextID();
//Linked List functions
View
5 Bullet.cpp
@@ -43,23 +43,26 @@ void Bullet::update(int timeElapsed, double height)
void Bullet::draw()
{
glColor3f(1,1,1);
- glPointSize(5);
+ glPointSize(4);
glBegin(GL_POINTS);
glVertex3f(location.x, location.y, location.z);
glEnd();
glLoadIdentity();
}
+//Kills the bullet on next update
void Bullet::kill()
{
dead = true;
}
+//Checks to see if the bullet is dead
bool Bullet::isDead()
{
return dead;
}
+//Updates the lifespan counter
void Bullet::setLifeTime(double l)
{
lifeTime = l;
View
2 Camera.cpp
@@ -12,7 +12,7 @@ double theta, phi, radius;
double ** camTrack;
-Camera::Camera(double ** cTrack, int scale)
+Camera::Camera(double ** cTrack)
{
camTrack = cTrack;
theta = -PI*3/4;
View
2 Camera.h
@@ -6,7 +6,7 @@ class Camera
{
public:
- Camera(double ** cTrack, int scale);
+ Camera(double ** cTrack);
void strafe(double, double);
void zoom(double zoomSpeed);
void rotate(double rotate);
View
613 OpenGL.cpp
@@ -13,8 +13,8 @@
HINSTANCE* hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
-GL_Window* g_window;
-Keys* g_keys;
+//GL_Window* g_window;
+//Keys* g_keys;
#define WM_TOGGLEFULLSCREEN (WM_USER+1) // Application Define Message For Toggling
// Between Fullscreen / Windowed Mode
@@ -25,35 +25,35 @@ int mouse_x, mouse_y; // The Current Position Of The Mouse
int newMouseX = -1;
int mouseMode = 0; // 0 = Select mode 1 = Movement mode
int selectedID = -1;
-Vector3 clickRay1, clickRay2, target;
+
// OpenGL variables
+OpenGLRender *glRender;
HGLRC hRC=NULL; // Permanent Rendering Context
HDC hDC=NULL; // Private GDI Device Context
HWND hWnd=NULL; // Holds the Window Handle
-bool keys[256]; // Keyboard routines
+int screenH, screenW;
+Vector3 clickRay1, clickRay2;
+/*bool keys[256]; // Keyboard routines
bool active=TRUE; // Flag sets window to be active by default
bool fullscreen=TRUE; // Flag sets window to be fullscreen by default
bool light = true, lp, fp;
-Camera cam = Camera(world.getCamTrack(), worldScale); //Camera object that holds orientation values for camera
+Camera *camera; //Camera object that holds orientation values for camera
int screenH, screenW; //User screen width and height
GLuint testTex[6]; //Array for holding OpenGL textures
int cursorTex;
GLuint terrainList; //Terrain Display List
//Actor testCube = Actor("data/models/Will this work.obj");
-//Actor *testBox[2];
-ActorManager actorManager = ActorManager(world);
+//Actor *testBox[2];*/
-//Texture Variable
-GLuint fontSet; //Holds the OpenGL font data
//Debug display variables
-bool debug = FALSE, debugP;
-double drawX=0, drawY=0; //Location to draw text on the screen
-int overlayLineCount = 0;
+//bool debug = FALSE, debugP;
+//double drawX=0, drawY=0; //Location to draw text on the screen
+//int overlayLineCount = 0;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
@@ -85,20 +85,24 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
Application application; // Application Structure
GL_Window window; // Window Structure
- Keys keys; // Key Structure
BOOL isMessagePumpActive; // Message Pump Active?
MSG msg; // Window Message Structure
DWORD tickCount; // Used For The Tick Counter
hInst = &hInstance;
// Fill Out Application Data
-
application.className = "OpenGL"; // Application Class Name
application.hInstance = hInst; // Application Instance
+ //Initialize world objects
+
+ actorManager = new ActorManager();
+ camera = new Camera(actorManager->world->getCamTrack());
+ glRender = new OpenGLRender(*camera, *actorManager, fontSet);
+
+
// Fill Out Window
ZeroMemory (&window, sizeof (GL_Window)); // Make Sure Memory Is Zeroed
- window.keys = &keys; // Window Key Structure
window.init.application = &application; // Window Application
window.init.title = "NeHe's Picking Tutorial"; // Window Title
window.init.width = 1680; // Window Width
@@ -108,8 +112,6 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
window.init.bitsPerPixel = 16; // Bits Per Pixel
window.init.isFullScreen = TRUE; // Fullscreen? (Set To TRUE)
- ZeroMemory (&keys, sizeof (Keys)); // Zero keys Structure
-
if (MessageBox (HWND_DESKTOP, L"Would You Like To Run In Fullscreen Mode?", L"Start FullScreen?", MB_YESNO | MB_ICONQUESTION) == IDNO)
{
window.init.isFullScreen = FALSE; // If Not, Run In Windowed Mode
@@ -132,13 +134,16 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
if (CreateWindowGL (&window) == TRUE) // Was Window Creation Successful?
{
ReshapeGL(window.init.width, window.init.height);
- if (Initialize (&window, &keys) == FALSE) // Call User Intialization
+ Vector2 size;
+ setCoord(size, window.init.width, window.init.height);
+ if (glRender->initialize (&size) == FALSE) // Call User Intialization
{
// Failure
TerminateApplication(&window); // Close Window, This Will Handle The Shutdown
}
else
{
+ buildFont(&window);
isMessagePumpActive = TRUE;
while (isMessagePumpActive == TRUE) // While The Message Pump Is Active
{
@@ -165,18 +170,13 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
{
// Process Application Loop
tickCount = GetTickCount (); // Get The Tick Count
- if(!Update (tickCount - window.lastTickCount)) // Update The Counter
+ if(!glRender->update (tickCount - window.lastTickCount, mouse_x, mouse_y)) // Update The Counter
TerminateApplication(&window);
int time = tickCount - window.lastTickCount;
window.lastTickCount = tickCount; // Set Last Count To Current Count
-
- actorManager.updateObjects(time);
- //testBox[0]->move(world.getSpecificTerrainHeight(testBox[0]->getLocation().x, testBox[0]->getLocation().z));
- //testBox[1]->move(world.getSpecificTerrainHeight(testBox[1]->getLocation().x, testBox[1]->getLocation().z));
- if(cam.hasFocus())
- cam.followFocus();
- DrawGLScene(); // Draw Our Scene
+ actorManager->updateObjects(time);
+ glRender->drawGLScene(); // Draw Our Scene
SwapBuffers (window.hDC); // Swap Buffers (Double Buffering)
}
@@ -196,431 +196,6 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
}
}
-//
-// FUNCTION: DrawGLScene()
-//
-// PURPOSE: Draw everything in the OpenGL environment
-//
-// COMMENTS:
-//
-
-int DrawGLScene(GLvoid)
-{
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- //glScalef(worldScale, 1.0, worldScale);
- glCallList(terrainList); //Draw Terrain List
- //glScalef(-worldScale, -1.0, -worldScale);
-
- glBindTexture( GL_TEXTURE_2D, 0);
-
- /*if(selectedID != -1)
- {
- int n = 1;
- Vector2 *next = actorManager.getActorByID(selectedID)->getNodePath(0);
- while(next)
- {
- glColor3f(1,1,0);
- glBegin(GL_LINES);
- glVertex3f(next->x*32, *world.getTerrainHeight((next->x)*32, (next->y)*32), next->y*32);
- glVertex3f(next->x*32+1, *world.getTerrainHeight((next->x)*32, (next->y)*32)+3, next->y*32-1);
- glEnd();
-
- next = actorManager.getActorByID(selectedID)->getNodePath(n);
- n++;
- }
- }
-
- for(int a = 0; a < MAX_WORLD_SIZE/32; a++)
- for(int b = 0; b < MAX_WORLD_SIZE/32; b++)
- {
- glColor3f(1-(float)world.nodes[a][b].incentive/15.0,0,0);
- glBegin(GL_LINES);
-
- if(world.nodes[a][b].incentive != -2)
- {
- glVertex3f(world.nodes[a][b].nodeData.x, *world.getTerrainHeight(a*32, b*32), world.nodes[a][b].nodeData.y);
- glVertex3f(world.nodes[a][b].nodeData.x, *world.getTerrainHeight(a*32, b*32)+5, world.nodes[a][b].nodeData.y);
- }
- glEnd();
-
- if(world.nodes[a][b].incentive > -1)
- {
- glColor3f(0,1-(float)world.nodes[a][b].incentive/15.0,0);
- glTranslated(world.nodes[a][b].nodeData.x, *world.getTerrainHeight(a*32, b*32)+5, world.nodes[a][b].nodeData.y);
- glRasterPos2f(0,0);
- string str = itos(world.nodes[a][b].incentive);
- const char *c = str.c_str();//new char(str.size()+1);
- //strcpy (c, str.c_str());
- glPrint(c);
- glLoadIdentity();
- }
- }*/
-
- glLoadIdentity();
- if(target.x != -1)
- {
- checkLighting(false);
- glBegin(GL_LINES);
- glVertex3f(target.x, target.y, target.z);
- glVertex3f(target.x, target.y+5, target.z);
-
- glVertex3f(target.x+1, target.y, target.z-1);
- glVertex3f(target.x-1, target.y, target.z+1);
-
- glVertex3f(target.x+1, target.y, target.z+1);
- glVertex3f(target.x-1, target.y, target.z-1);
- glEnd();
-
-
- //LightPosition[0] = target.x;
- //LightPosition[1] = target.y-100;
- //LightPosition[2] = target.z;
- //glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
- }
-
- //testCube.draw();
- //glmDraw(testCube, GLM_SMOOTH| GLM_TEXTURE);
- //---DEBUG DISPLAY
-
- checkLighting(true);
- actorManager.drawObjects();
- overlayDisplay();
-
- //------
- return TRUE;
-}
-
-//
-// FUNCTION: overlayDisplay()
-//
-// PURPOSE: Display data to the screen using OpenGL fonts
-//
-// COMMENTS: Currently being used to display variables at runtime for debugging purposes
-//
-
-void overlayDisplay()
-{
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- glOrtho(0, screenW, 0, screenH, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- glLoadIdentity();
-
- //Lighting should not affect objects in the overlay
- checkLighting(false);
-
- //Only display debug text if user turned it on
- if(!debug)
- {
- //glTranslatef(cam.getPosX(),cam.getPosY()-1,cam.getPosZ()+1);
- //glTranslatef(0,0,0);
- glColor3f(1.0f,1.0f,1.0f);
-
- overlayLineCount = 0;
- rasterStringToOverlay("("+itos(cam.getLookAt().x)+","+ dtos(cam.getLookAt().y)+","+ itos(cam.getLookAt().z)+")");
- rasterStringToOverlay(""+ itos(mouse_x) + "," + itos(mouse_y));
- if(selectedID != -1)
- {
- rasterStringToOverlay("Box Data:");
- rasterStringToOverlay(" Box Height: "+itos(actorManager.getActorByID(selectedID)->getLocation().y));
- rasterStringToOverlay(" Box ID: "+itos(selectedID));
- }
- }
-
- //Draw the cursor
- int xTex = 25, yTex = 59;
- glBindTexture(GL_TEXTURE_2D, testTex[cursorTex]);
- glBegin(GL_QUADS);
- glTexCoord2f(1,1); glVertex3f(mouse_x+xTex, screenH-mouse_y,0);
- glTexCoord2f(0,1); glVertex3f(mouse_x, screenH-mouse_y,0);
- glTexCoord2f(0,0); glVertex3f(mouse_x, screenH-mouse_y-yTex,0);
- glTexCoord2f(1,0); glVertex3f(mouse_x+xTex, screenH-mouse_y-yTex,0);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, 0);
-
- checkLighting(true);
- glPopMatrix();
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glPopMatrix();
-
-
-
- perspective();
-}
-
-//Utility function to make displaying lines of text on the overlay less code-intensive
-void rasterStringToOverlay(string str)
-{
- glRasterPos2f(1,screenH-16-(overlayLineCount*16));
- const char *c = str.c_str();//new char(str.size()+1);
- //strcpy (c, str.c_str());
- glPrint(c);
- overlayLineCount++;
-}
-
-void drawTerrainAsList()
-{
- terrainList = glGenLists(1);
- glNewList(terrainList, GL_COMPILE);
-
- //Find all the tiles that use the first texture and then loop to the next texture
- for(int i = 0; i < 5; i++)
- {
- glBindTexture( GL_TEXTURE_2D, testTex[i]);
-
- //Loop over all terrain tiles
- for(int z = 0; z < MAX_WORLD_SIZE-1; z++)
- for(int x = 0; x < MAX_WORLD_SIZE-1; x++)
- {
- //Is the current tile using the loaded texture?
- if(world.getTerrainType(x,z) != i)
- continue;
-
- double * h1 = world.getTerrainHeight(x,z);
- double * h2 = world.getTerrainHeight(x+1,z);
- double * h3 = world.getTerrainHeight(x+1,z+1);
- double * h4 = world.getTerrainHeight(x,z+1);
-
- Vector3 n1, n2, normal1, normal2;
-
- //Get normal vector for the first triangle
- setVector(n1, 0, *h3-*h2, 1);
- setVector(n2, -1, *h1-*h2, 0);
- normal1 = crossProduct(n2, n1);
- normalize(normal1);
-
- //Get normal vector for the second triangle
- setVector(n1, 1, *h3-*h4, 0);
- setVector(n2, 0, *h1-*h4, -1);
- normal2 = crossProduct(n1, n2);
- normalize(normal2);
-
- float xTex = .1, zTex = .1, texStep = .8;
-
- texStep = 1/8.0;
-
- xTex = (float)(x % 8)/8.1;
- zTex = (float)(z % 8)/8.1;
-
- /*Vector3 color;
- switch(world.getTerrainObject(x, z))
- {
- case 0: //Draw Rock
- setVector(color, .2, .2, .2);
- break;
-
- case 1: //Draw bush
- setVector(color, .1, .4, .1);
- break;
-
- case 2: //Draw Tree
- setVector(color, .5, .5, .1);
- break;
-
- case 3: //Draw cactus
- setVector(color, .1, .4, .1);
- break;
-
- default:
- setVector(color, 1,1,1);
- break;
- }*/
-
-
- /*glBegin(GL_QUADS);
- glNormal3d(normal1.x, normal1.y, normal1.z);
-
- //world.getColor(x, z);
- glTexCoord2f(0.1f, 0.1f); glVertex3f(x, *h1,z);
-
- //world.getColor(x+1, z);
- glTexCoord2f(.9f, 0.1f); glVertex3f(x+1, *h2,z);
-
- //world.getColor(x+1, z+1);
- glTexCoord2f(.9f, .9f); glVertex3f(x+1, *h3,z+1);
-
- //world.getColor(x, z+1);
- glTexCoord2f(0.1f, .9f); glVertex3f(x, *h4,z+1);
- glEnd();*/
-
- /*glBegin(GL_LINES);
- glVertex3f(x, *h1,z);
- glVertex3f(x+1, *h2,z);
- glVertex3f(x, *h1,z);
- glVertex3f(x, *h4,z+1);
- glEnd();*/
-
- //if(world.getTerrainGradient(x,z) > .5)
- //glColor3f(0.0, 1, 0.0);
- glBegin(GL_TRIANGLES);
-
- glNormal3d(normal1.x, normal1.y, normal1.z);
- glTexCoord2f(xTex, zTex); glVertex3f(x, *h1,z);
- glTexCoord2f(xTex + texStep, zTex + texStep); glVertex3f(x+1, *h3,z+1);
- glTexCoord2f(xTex + texStep, zTex); glVertex3f(x+1, *h2,z);
-
- glNormal3d(normal2.x, normal2.y, normal2.z);
-
- glTexCoord2f(xTex, zTex); glVertex3f(x, *h1,z);
- glTexCoord2f(xTex, zTex + texStep); glVertex3f(x, *h4,z+1);
- glTexCoord2f(xTex + texStep, zTex + texStep); glVertex3f(x+1, *h3,z+1);
-
- glEnd();
- }
- }
- glEndList();
-
- glBindTexture( GL_TEXTURE_2D, 0);
-}
-
-
-BOOL Initialize(GL_Window* window, Keys* keys)
-{
- g_window = window;
- g_keys = keys;
-
- glShadeModel(GL_SMOOTH);
- glClearColor(0.3f,0.3f,0.7f,0.0f);
- glClearDepth(1.0f);
- glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_BLEND);
- glEnable(GL_TEXTURE_2D);
- glDepthFunc(GL_LEQUAL);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glFrontFace(GL_CCW);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
-
-
- GLfloat LightAmbient[]= { .6,.5, .5, 1.0f };
- GLfloat LightDiffuse[]= { 1.0f, 1, 1, 1.0f };
- GLfloat LightSpecular[]= { 1.0f, 1, 1, 1.0f };
- GLfloat LightPosition[]= { -1.0f,1.0f, 0.0f, 0.0f };
-
- glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
- glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
- glLightfv(GL_LIGHT1, GL_SPECULAR, LightSpecular);
- glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
- //glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, .05);
- glEnable(GL_LIGHT1);
- checkLighting(true);
- perspective();
-
- //testBox[0] = new Actor(10, *world.getTerrainHeight(10,10),10, 1,1,1, 0);
- //log("Actor "+itos(testBox[0]->getID())+" was created.\n");
-
- buildFont(window);
- loadTextures();
- drawTerrainAsList();
-
-
- //testBox[1] = new Actor(20, *world.getTerrainHeight(20,20),20, 1,2,1, 1);
- //log("Actor "+itos(testBox[1]->getID())+" was created.\n");
-
-
- return TRUE;
-}
-
-BOOL Update(DWORD milliseconds)
-{
- if(g_keys->keyDown[VK_ESCAPE])
- return FALSE;
-
- if(g_keys->keyDown['L'] && !lp)
- {
- lp = TRUE;
- light = !light;
- }
- if(!g_keys->keyDown['L'])
- lp = FALSE;
-
- //Toggle the debug printing to the screen
- //Toggles at every key press
- if(g_keys->keyDown['P'] && !debugP)
- {
- debugP = TRUE;
- debug = !debug;
- }
- if(!g_keys->keyDown['P'])
- debugP = FALSE;
-
- //Moves the camera around
- if(g_keys->keyDown['W'])
- cam.strafe(3,0.0);
- if(g_keys->keyDown['S'])
- cam.strafe(-3,0.0);
- if(g_keys->keyDown['A'])
- cam.strafe(0.0,3);
- if(g_keys->keyDown['D'])
- cam.strafe(0.0,-3);
-
- //Zooms the camera in or out
- if(g_keys->keyDown['Q'])
- cam.rotate(-.05);
- if(g_keys->keyDown['E'])
- cam.rotate(.05);
-
- if(g_keys->keyDown[VK_SPACE])
- actorManager.shoot(selectedID);
-
- //if (g_keys->keyDown[VK_F1]) // Is F1 Being Pressed?
- //ToggleFullscreen (g_window); // Toggle Fullscreen Mode
-
- //Adjust GL camera with the new frame
- perspective();
- return TRUE;
-}
-
-void checkLighting(bool lightSwitch)
-{
- if(light)
- {
- if(lightSwitch)
- glEnable(GL_LIGHTING);
- else
- glDisable(GL_LIGHTING);
- }
- else
- glDisable(GL_LIGHTING);
-
-}
-
-//
-// FUNCTION: perspective()
-//
-// PURPOSE: Changes the viewing angle for the camera when the
-// camera variables change
-//
-// COMMENTS:
-//
-
-void perspective()
-{
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
-
- gluPerspective(45.0f, (GLfloat)screenW/(GLfloat)screenH,2.0f,1000.0f);
-
- double adjust = 0;
- if(cam.getPosition().y < (*world.getTerrainHeight(cam.getPosition().x, cam.getPosition().z))+5)
- cam.adjustHeight(*world.getTerrainHeight(cam.getPosition().x, cam.getPosition().z)+5);
-
- gluLookAt(cam.getPosition().x,cam.getPosition().y,cam.getPosition().z,
- cam.getLookAt().x,cam.getLookAt().y,cam.getLookAt().z, 0,1,0);
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-}
-
-
-
-
@@ -647,7 +222,7 @@ void ReshapeGL (int width, int height) // Reshape The Window When It's M
{
glViewport (0, 0, (GLsizei)(width), (GLsizei)(height)); // Reset The Current Viewport
- perspective();
+ glRender->perspective();
}
BOOL ChangeScreenResolution (int width, int height, int bitsPerPixel) // Change The Screen Resolution
@@ -822,8 +397,6 @@ BOOL CreateWindowGL (GL_Window* window) // This Code Creates Our OpenGL Window
ReshapeGL (window->init.width, window->init.height); // Reshape Our GL Window
- ZeroMemory (window->keys, sizeof (Keys)); // Clear All Keys
-
window->lastTickCount = GetTickCount (); // Get Tick Count
return TRUE; // Window Creating Was A Success
@@ -910,12 +483,12 @@ LRESULT CALLBACK WndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
break; // Break
case WM_KEYDOWN: // Update Keyboard Buffers For Keys Pressed
- window->keys->keyDown [wParam] = TRUE; // Set The Selected Key (wParam) To True
+ glRender->keys[wParam] = TRUE; // Set The Selected Key (wParam) To True
break; // Break
case WM_KEYUP: // Update Keyboard Buffers For Keys Released
- window->keys->keyDown [wParam] = FALSE; // Set The Selected Key (wParam) To False
+ glRender->keys[wParam] = FALSE; // Set The Selected Key (wParam) To False
break; // Break
@@ -927,9 +500,9 @@ LRESULT CALLBACK WndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
case WM_MOUSEWHEEL:
newMouseX = wParam;
if(newMouseX>0)
- cam.zoom(-5);
+ camera->zoom(-5);
else
- cam.zoom(5);
+ camera->zoom(5);
break;
case WM_RBUTTONDOWN:
@@ -942,7 +515,7 @@ LRESULT CALLBACK WndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
case WM_MBUTTONDOWN:
if(selectedID != -1)
- cam.setFocus(*actorManager.getActorByID(selectedID));
+ camera->setFocus(*actorManager->getActorByID(selectedID));
break;
@@ -988,10 +561,10 @@ void lMouseSelection()
clearCurrentSelection();
- if(actorManager.checkHitBoxes(selectedID, clickRay1, clickRay2))
+ if(actorManager->checkHitBoxes(selectedID, clickRay1, clickRay2))
{
- setVector(target, NULL,NULL,NULL);
- actorManager.getActorByID(selectedID)->setSelected(true);
+ setVector(glRender->target, NULL,NULL,NULL);
+ actorManager->getActorByID(selectedID)->setSelected(true);
//return; //No need to continue dealing with selection handling
}
/*else
@@ -1108,30 +681,30 @@ void rMouseSelection()
continue;
if(!last)
{
- setVector(tp1, y+step, *world.getTerrainHeight(y+step, x), x);
- setVector(tp2, y+step+1, *world.getTerrainHeight(y+step+1, x), x);
- setVector(tp3, y+step+1, *world.getTerrainHeight(y+step+1, x+1), x+1);
- setVector(tp4, y+step, *world.getTerrainHeight(y+step, x+1), x+1);
+ setVector(tp1, y+step, *actorManager->world->getTerrainHeight(y+step, x), x);
+ setVector(tp2, y+step+1, *actorManager->world->getTerrainHeight(y+step+1, x), x);
+ setVector(tp3, y+step+1, *actorManager->world->getTerrainHeight(y+step+1, x+1), x+1);
+ setVector(tp4, y+step, *actorManager->world->getTerrainHeight(y+step, x+1), x+1);
}
else
{
- setVector(tp1, y, *world.getTerrainHeight(y+step, x), x+step);
- setVector(tp2, y+1, *world.getTerrainHeight(y+1, x+step), x+step);
- setVector(tp3, y+1, *world.getTerrainHeight(y+1, x+step+1), x+step+1);
- setVector(tp4, y, *world.getTerrainHeight(y, x+step+1), x+step+1);
+ setVector(tp1, y, *actorManager->world->getTerrainHeight(y+step, x), x+step);
+ setVector(tp2, y+1, *actorManager->world->getTerrainHeight(y+1, x+step), x+step);
+ setVector(tp3, y+1, *actorManager->world->getTerrainHeight(y+1, x+step+1), x+step+1);
+ setVector(tp4, y, *actorManager->world->getTerrainHeight(y, x+step+1), x+step+1);
}
//Did the click ray pass through either triangle in the chunk plane?
- if(checkLineIntersect(tp2, tp1, tp3, clickRay1, clickRay2, target) || checkLineIntersect(tp4, tp3, tp1, clickRay1, clickRay2, target))
+ if(checkLineIntersect(tp2, tp1, tp3, clickRay1, clickRay2, glRender->target) || checkLineIntersect(tp4, tp3, tp1, clickRay1, clickRay2, glRender->target))
{
if(last)
continue;
else
{
if(selectedID != -1)
- actorManager.getActorByID(selectedID)->setMoveTarget(target);
- //actorManager.setMovePath(selectedID, target);
+ //actorManager.getActorByID(selectedID)->setMoveTarget(target);
+ actorManager->setMovePath(selectedID, glRender->target);
//testBox[selectedID]->setMoveTarget(target);
return; //Target found
}
@@ -1141,8 +714,8 @@ void rMouseSelection()
if(last)
{
if(selectedID != -1)
- actorManager.getActorByID(selectedID)->setMoveTarget(target);
- //actorManager.setMovePath(selectedID, target);
+ //actorManager.getActorByID(selectedID)->setMoveTarget(target);
+ actorManager->setMovePath(selectedID, glRender->target);
//testBox[selectedID]->setMoveTarget(target);
}
@@ -1155,28 +728,28 @@ void rMouseSelection()
continue;
if(!last)
{
- setVector(tp1, x+step, *world.getTerrainHeight(x+step, y), y);
- setVector(tp2, x+step + 1, *world.getTerrainHeight(x+step+1, y), y);
- setVector(tp3, x+step + 1, *world.getTerrainHeight(x+step+1, y+1), y+1);
- setVector(tp4, x+step, *world.getTerrainHeight(x+step, y+1), y+1);
+ setVector(tp1, x+step, *actorManager->world->getTerrainHeight(x+step, y), y);
+ setVector(tp2, x+step + 1, *actorManager->world->getTerrainHeight(x+step+1, y), y);
+ setVector(tp3, x+step + 1, *actorManager->world->getTerrainHeight(x+step+1, y+1), y+1);
+ setVector(tp4, x+step, *actorManager->world->getTerrainHeight(x+step, y+1), y+1);
}
else
{
- setVector(tp1, x, *world.getTerrainHeight(x, y+step), y+step);
- setVector(tp2, x + 1, *world.getTerrainHeight(x+1, y+step), y+step);
- setVector(tp3, x + 1, *world.getTerrainHeight(x+1, y+step+1), y+step+1);
- setVector(tp4, x, *world.getTerrainHeight(x, y+step+1), y+step+1);
+ setVector(tp1, x, *actorManager->world->getTerrainHeight(x, y+step), y+step);
+ setVector(tp2, x + 1, *actorManager->world->getTerrainHeight(x+1, y+step), y+step);
+ setVector(tp3, x + 1, *actorManager->world->getTerrainHeight(x+1, y+step+1), y+step+1);
+ setVector(tp4, x, *actorManager->world->getTerrainHeight(x, y+step+1), y+step+1);
}
- if(checkLineIntersect(tp2, tp1, tp3, clickRay1, clickRay2, target) || checkLineIntersect(tp4, tp3, tp1, clickRay1, clickRay2, target))
+ if(checkLineIntersect(tp2, tp1, tp3, clickRay1, clickRay2, glRender->target) || checkLineIntersect(tp4, tp3, tp1, clickRay1, clickRay2, glRender->target))
{
if(last)
continue;
else
{
if(selectedID != -1)
- actorManager.getActorByID(selectedID)->setMoveTarget(target);
- //actorManager.setMovePath(selectedID, target);
+ //actorManager.getActorByID(selectedID)->setMoveTarget(target);
+ actorManager->setMovePath(selectedID, glRender->target);
//testBox[selectedID]->setMoveTarget(target);
return; //Target found
}
@@ -1186,8 +759,8 @@ void rMouseSelection()
if(last)
{
if(selectedID != -1)
- actorManager.getActorByID(selectedID)->setMoveTarget(target);
- //actorManager.setMovePath(selectedID, target);
+ //actorManager.getActorByID(selectedID)->setMoveTarget(target);
+ actorManager->setMovePath(selectedID, glRender->target);
//testBox[selectedID]->setMoveTarget(target);
}
}
@@ -1199,56 +772,18 @@ void rMouseSelection()
err -= 1;
}
}
-
- if(target.x == 0 && target.y == 0 && target.x == 0)
- log("Target error detected\n");
}
void clearCurrentSelection()
{
if(selectedID != -1)
{
- actorManager.getActorByID(selectedID)->unselect();
+ actorManager->getActorByID(selectedID)->unselect();
selectedID = -1;
}
}
-void loadTextures()
-{
- loadTexture("data/images/grass.jpg", 0);
- loadTexture("data/images/sand_texture.jpg", 2);
- loadTexture("data/images/forest.jpg", 1);
- loadTexture("data/images/snow_texture.jpg", 3);
- loadTexture("data/images/water.jpg", 4);
- loadTexture("data/images/cursor2.png", 5);
- cursorTex = 5;
-}
-
-//
-// FUNCTION: loadTexture()
-//
-// PURPOSE: Loads a texture from the given file into the given array index
-//
-// COMMENTS:
-//
-void loadTexture(string path, int index)
-{
- testTex[index] = SOIL_load_OGL_texture
- (
- path.c_str(),
- SOIL_LOAD_AUTO,
- SOIL_CREATE_NEW_ID,
- SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
- );
-
- if(testTex[index])
- {
- log("Texture "+path+" loaded successfully\n");
- // bind an OpenGL texture ID
- //glBindTexture( GL_TEXTURE_2D, testTex[index]);
- }
-}
//
// FUNCTION: buildFont()
@@ -1285,29 +820,5 @@ void buildFont(GL_Window* window)
wglUseFontBitmaps(window->hDC, 32, 96, fontSet);
}
-//
-// FUNCTION: glPrint()
-//
-// PURPOSE: Prints text to the screen using OpenGL fonts
-//
-// COMMENTS:
-//
-void glPrint(const char *fmt)
-{
- char text[256];
- va_list ap;
-
- if(fmt == NULL)
- return;
-
- va_start(ap, fmt);
- vsprintf_s(text, fmt, ap);
- va_end(ap);
-
- glListBase(fontSet - 32);
- glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
- glPopAttrib();
-}
-
View
30 OpenGL.h
@@ -3,6 +3,7 @@
#pragma comment( lib, "glu32.lib" )
#pragma comment( lib, "vfw32.lib" )
+#include "OpenGLRender.h"
#include "resource.h"
#include "TerrainGen.h"
#include "Camera.h"
@@ -11,29 +12,26 @@
#include "stdafx.h"
#include <stdarg.h>
-TerrainGen world;
-const int worldScale = 1;
+//TerrainGen *world;
+ActorManager *actorManager;
+Camera *camera; //Camera object that holds orientation values for camera
GLvoid ResizeGLScene(GLsizei, GLsizei);
GLvoid KillGLWindow(GLvoid);
-int DrawGLScene(GLvoid);
-int InitGL(GLvoid);
+//int DrawGLScene(GLvoid);
void glPrint(const char *);
-void checkLighting(bool lightSwitch);
-void overlayDisplay();
+//void checkLighting(bool lightSwitch);
+//void overlayDisplay();
void loadTexture(string path, int index);
-void drawTerrainAsList();
-void rasterStringToOverlay(string str);
+//void drawTerrainAsList();
+//void rasterStringToOverlay(string str);
void loadTextures();
-typedef struct { // Structure For Keyboard Stuff
- BOOL keyDown [256]; // Holds TRUE / FALSE For Each Key
-} Keys; // Keys
typedef struct { // Contains Information Vital To Applications
HINSTANCE* hInstance; // Application Instance
@@ -50,7 +48,6 @@ typedef struct { // Window Creation Info
} GL_WindowInit; // GL_WindowInit
typedef struct { // Contains Information Vital To A Window
- Keys* keys; // Key Structure
HWND hWnd; // Window Handle
HDC hDC; // Device Context
HGLRC hRC; // Rendering Context
@@ -59,9 +56,12 @@ typedef struct { // Contains Information Vital To A Window
DWORD lastTickCount; // Tick Counter
} GL_Window; // GL_Window
+GL_Window *g_window;
+GLuint fontSet;
+//Keys* g_keys;
void buildFont(GL_Window* window);
-void perspective();
+//void perspective();
void TerminateApplication (GL_Window* window); // Terminate The Application
void ToggleFullscreen (GL_Window* window); // Toggle Fullscreen / Windowed Mode
@@ -72,11 +72,11 @@ BOOL CreateWindowGL(GL_Window* window);
BOOL DestroyWindowGL(GL_Window* window);
// These Are The Function You Must Provide
-BOOL Initialize (GL_Window* window, Keys* keys); // Performs All Your Initialization
+
void Deinitialize (void); // Performs All Your DeInitialization
-BOOL Update (DWORD milliseconds); // Perform Motion Updates
+
void Draw (void); // Perform All Your Scene Drawing
View
538 OpenGLRender.cpp
@@ -0,0 +1,538 @@
+#include "OpenGLRender.h"
+
+
+OpenGLRender::OpenGLRender(Camera &c, ActorManager &m, GLuint &fSet)
+{
+ fontSet = &fSet;
+ manager = &m;
+ active=TRUE; // Flag sets window to be active by default
+ fullscreen=TRUE; // Flag sets window to be fullscreen by default
+ light = true;
+
+ cam = &c;
+
+ //Debug display variables
+ debug = FALSE;
+ drawX=0;
+ drawY=0; //Location to draw text on the screen
+ overlayLineCount=0;
+ selectedID = -1;
+
+ for(int i = 0; i < 256; i++)
+ keys[i] = false;
+ //ZeroMemory (&keys, sizeof (bool)); // Zero keys Structure
+}
+
+
+BOOL OpenGLRender::initialize(Vector2* windowSize)
+{
+ screenH = windowSize->y;
+ screenW = windowSize->x;
+
+ glShadeModel(GL_SMOOTH);
+ glClearColor(0.3f,0.3f,0.7f,0.0f);
+ glClearDepth(1.0f);
+ glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
+ glEnable(GL_COLOR_MATERIAL);
+ glEnable(GL_DEPTH_TEST);
+ glEnable(GL_BLEND);
+ glEnable(GL_TEXTURE_2D);
+ glDepthFunc(GL_LEQUAL);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glFrontFace(GL_CCW);
+ glEnable(GL_CULL_FACE);
+ glCullFace(GL_BACK);
+
+
+ GLfloat LightAmbient[]= { .6,.5, .5, 1.0f };
+ GLfloat LightDiffuse[]= { 1.0f, 1, 1, 1.0f };
+ GLfloat LightSpecular[]= { 1.0f, 1, 1, 1.0f };
+ GLfloat LightPosition[]= { -1.0f,1.0f, 0.0f, 0.0f };
+
+ glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
+ glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
+ glLightfv(GL_LIGHT1, GL_SPECULAR, LightSpecular);
+ glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
+ //glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, .05);
+ glEnable(GL_LIGHT1);
+ checkLighting(true);
+ perspective();
+
+ loadTextures();
+ drawTerrainAsList();
+
+
+ return TRUE;
+}
+
+
+//
+// FUNCTION: DrawGLScene()
+//
+// PURPOSE: Draw everything in the OpenGL environment
+//
+// COMMENTS:
+//
+
+int OpenGLRender::drawGLScene(GLvoid)
+{
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glLoadIdentity();
+ //glScalef(worldScale, 1.0, worldScale);
+ glCallList(terrainList); //Draw Terrain List
+ //glScalef(-worldScale, -1.0, -worldScale);
+
+ glBindTexture( GL_TEXTURE_2D, 0);
+
+ /*if(selectedID != -1)
+ {
+ int n = 1;
+ Vector2 *next = actorManager.getActorByID(selectedID)->getNodePath(0);
+ while(next)
+ {
+ glColor3f(1,1,0);
+ glBegin(GL_LINES);
+ glVertex3f(next->x*32, *world.getTerrainHeight((next->x)*32, (next->y)*32), next->y*32);
+ glVertex3f(next->x*32+1, *world.getTerrainHeight((next->x)*32, (next->y)*32)+3, next->y*32-1);
+ glEnd();
+
+ next = actorManager.getActorByID(selectedID)->getNodePath(n);
+ n++;
+ }
+ }*/
+
+ for(int a = 0; a < MAX_WORLD_SIZE/32; a++)
+ for(int b = 0; b < MAX_WORLD_SIZE/32; b++)
+ {
+ glColor3f(1-(float)manager->world->nodes[a][b].incentive/15.0,0,0);
+ glBegin(GL_LINES);
+
+ if(manager->world->nodes[a][b].incentive != -2)
+ {
+ Vector3 v, w;
+ setVector(v, manager->world->nodes[a][b].nodeData.x, *manager->world->getTerrainHeight(a*32, b*32), manager->world->nodes[a][b].nodeData.y);
+ setVector(w, manager->world->nodes[a][b].nodeData.x, *manager->world->getTerrainHeight(a*32, b*32)+5, manager->world->nodes[a][b].nodeData.y);
+
+ glVertex3f(v.x, v.y, v.z);
+ glVertex3f(w.x, w.y, w.z);
+ }
+ glEnd();
+
+ if(manager->world->nodes[a][b].incentive > -1)
+ {
+ glColor3f(0,1-(float)manager->world->nodes[a][b].incentive/15.0,0);
+ glTranslated(manager->world->nodes[a][b].nodeData.x, *manager->world->getTerrainHeight(a*32, b*32)+5, manager->world->nodes[a][b].nodeData.y);
+ glRasterPos2f(0,0);
+ string str = itos(manager->world->nodes[a][b].incentive);
+ const char *c = str.c_str();
+ glPrint(c);
+ glLoadIdentity();
+ }
+ }
+
+ glLoadIdentity();
+ if(target.x != -1)
+ {
+ checkLighting(false);
+ glBegin(GL_LINES);
+ glVertex3f(target.x, target.y, target.z);
+ glVertex3f(target.x, target.y+5, target.z);
+
+ glVertex3f(target.x+1, target.y, target.z-1);
+ glVertex3f(target.x-1, target.y, target.z+1);
+
+ glVertex3f(target.x+1, target.y, target.z+1);
+ glVertex3f(target.x-1, target.y, target.z-1);
+ glEnd();
+
+
+ //LightPosition[0] = target.x;
+ //LightPosition[1] = target.y-100;
+ //LightPosition[2] = target.z;
+ //glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
+ }
+
+ //testCube.draw();
+ //glmDraw(testCube, GLM_SMOOTH| GLM_TEXTURE);
+ //---DEBUG DISPLAY
+
+ checkLighting(true);
+ manager->drawObjects();
+ overlayDisplay();
+
+ //------
+ return TRUE;
+}
+
+
+//
+// FUNCTION: overlayDisplay()
+//
+// PURPOSE: Display data to the screen using OpenGL fonts
+//
+// COMMENTS: Currently being used to display variables at runtime for debugging purposes
+//
+
+void OpenGLRender::overlayDisplay()
+{
+ glMatrixMode(GL_PROJECTION);
+ glPushMatrix();
+ glLoadIdentity();
+ glOrtho(0, screenW, 0, screenH, -1, 1);
+ glMatrixMode(GL_MODELVIEW);
+ glPushMatrix();
+ glLoadIdentity();
+
+ //Lighting should not affect objects in the overlay
+ checkLighting(false);
+
+ //Only display debug text if user turned it on
+ if(!debug)
+ {
+ //glTranslatef(cam.getPosX(),cam.getPosY()-1,cam.getPosZ()+1);
+ //glTranslatef(0,0,0);
+ glColor3f(1.0f,1.0f,1.0f);
+
+ overlayLineCount = 0;
+ rasterStringToOverlay("("+itos(cam->getLookAt().x)+","+ dtos(cam->getLookAt().y)+","+ itos(cam->getLookAt().z)+")");
+ rasterStringToOverlay(itos(mouseX) + "," + itos(mouseY));
+ if(selectedID != -1)
+ {
+ rasterStringToOverlay("Box Data:");
+ rasterStringToOverlay(" Box Height: "+itos(manager->getActorByID(selectedID)->getLocation().y));
+ rasterStringToOverlay(" Box ID: "+itos(selectedID));
+ }
+ }
+
+ //Draw the cursor
+ int xTex = 25, yTex = 59;
+ glBindTexture(GL_TEXTURE_2D, testTex[cursorTex]);
+ glBegin(GL_QUADS);
+ glTexCoord2f(1,1); glVertex3f(mouseX+xTex, screenH-mouseY,0);
+ glTexCoord2f(0,1); glVertex3f(mouseX, screenH-mouseY,0);
+ glTexCoord2f(0,0); glVertex3f(mouseX, screenH-mouseY-yTex,0);
+ glTexCoord2f(1,0); glVertex3f(mouseX+xTex, screenH-mouseY-yTex,0);
+ glEnd();
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ checkLighting(true);
+ glPopMatrix();
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glPopMatrix();
+
+
+
+ perspective();
+}
+
+
+//Utility function to make displaying lines of text on the overlay less messy
+void OpenGLRender::rasterStringToOverlay(string str)
+{
+ glRasterPos2f(1,screenH-16-(overlayLineCount*16));
+ const char *c = str.c_str();//new char(str.size()+1);
+ //strcpy (c, str.c_str());
+ glPrint(c);
+ overlayLineCount++;
+}
+
+
+
+
+void OpenGLRender::drawTerrainAsList()
+{
+ terrainList = glGenLists(1);
+ glNewList(terrainList, GL_COMPILE);
+
+ //Find all the tiles that use the first texture and then loop to the next texture
+ for(int i = 0; i < 5; i++)
+ {
+ glBindTexture( GL_TEXTURE_2D, testTex[i]);
+
+ //Loop over all terrain tiles
+ for(int z = 0; z < MAX_WORLD_SIZE-1; z++)
+ for(int x = 0; x < MAX_WORLD_SIZE-1; x++)
+ {
+ //Is the current tile using the loaded texture?
+ if(manager->world->getTerrainType(x,z) != i)
+ continue;
+
+ double * h1 = manager->world->getTerrainHeight(x,z);
+ double * h2 = manager->world->getTerrainHeight(x+1,z);
+ double * h3 = manager->world->getTerrainHeight(x+1,z+1);
+ double * h4 = manager->world->getTerrainHeight(x,z+1);
+
+ Vector3 n1, n2, normal1, normal2;
+
+ //Get normal vector for the first triangle
+ setVector(n1, 0, *h3-*h2, 1);
+ setVector(n2, -1, *h1-*h2, 0);
+ normal1 = crossProduct(n2, n1);
+ normalize(normal1);
+
+ //Get normal vector for the second triangle
+ setVector(n1, 1, *h3-*h4, 0);
+ setVector(n2, 0, *h1-*h4, -1);
+ normal2 = crossProduct(n1, n2);
+ normalize(normal2);
+
+ float xTex = .1, zTex = .1, texStep = .8;
+
+ texStep = 1/8.0;
+
+ xTex = (float)(x % 8)/8.1;
+ zTex = (float)(z % 8)/8.1;
+
+ /*Vector3 color;
+ switch(world.getTerrainObject(x, z))
+ {
+ case 0: //Draw Rock
+ setVector(color, .2, .2, .2);
+ break;
+
+ case 1: //Draw bush
+ setVector(color, .1, .4, .1);
+ break;
+
+ case 2: //Draw Tree
+ setVector(color, .5, .5, .1);
+ break;
+
+ case 3: //Draw cactus
+ setVector(color, .1, .4, .1);
+ break;
+
+ default:
+ setVector(color, 1,1,1);
+ break;
+ }*/
+
+
+ /*glBegin(GL_QUADS);
+ glNormal3d(normal1.x, normal1.y, normal1.z);
+
+ //world.getColor(x, z);
+ glTexCoord2f(0.1f, 0.1f); glVertex3f(x, *h1,z);
+
+ //world.getColor(x+1, z);
+ glTexCoord2f(.9f, 0.1f); glVertex3f(x+1, *h2,z);
+
+ //world.getColor(x+1, z+1);
+ glTexCoord2f(.9f, .9f); glVertex3f(x+1, *h3,z+1);
+
+ //world.getColor(x, z+1);
+ glTexCoord2f(0.1f, .9f); glVertex3f(x, *h4,z+1);
+ glEnd();*/
+
+ /*glBegin(GL_LINES);
+ glVertex3f(x, *h1,z);
+ glVertex3f(x+1, *h2,z);
+ glVertex3f(x, *h1,z);
+ glVertex3f(x, *h4,z+1);
+ glEnd();*/
+
+ //if(world.getTerrainGradient(x,z) > .5)
+ //glColor3f(0.0, 1, 0.0);
+ glBegin(GL_TRIANGLES);
+
+ glNormal3d(normal1.x, normal1.y, normal1.z);
+ glTexCoord2f(xTex, zTex); glVertex3f(x, *h1,z);
+ glTexCoord2f(xTex + texStep, zTex + texStep); glVertex3f(x+1, *h3,z+1);
+ glTexCoord2f(xTex + texStep, zTex); glVertex3f(x+1, *h2,z);
+
+ glNormal3d(normal2.x, normal2.y, normal2.z);
+
+ glTexCoord2f(xTex, zTex); glVertex3f(x, *h1,z);
+ glTexCoord2f(xTex, zTex + texStep); glVertex3f(x, *h4,z+1);
+ glTexCoord2f(xTex + texStep, zTex + texStep); glVertex3f(x+1, *h3,z+1);
+
+ glEnd();
+ }
+ }
+ glEndList();
+
+ glBindTexture( GL_TEXTURE_2D, 0);
+}
+
+
+
+
+
+
+BOOL OpenGLRender::update(DWORD milliseconds, int mX, int mY)
+{
+ mouseX = mX;
+ mouseY = mY;
+
+ if(cam->hasFocus())
+ cam->followFocus();
+
+ if(keys[VK_ESCAPE])
+ return FALSE;
+
+ //Toggle lighting if key is pressed
+ toggleLighting(keys['L']);
+
+ //Toggle debug display if key is pressed
+ toggleDebug(keys['P']);
+
+ //Moves the camera around
+ if(keys['W'])
+ cam->strafe(3,0.0);
+ if(keys['S'])
+ cam->strafe(-3,0.0);
+ if(keys['A'])
+ cam->strafe(0.0,3);
+ if(keys['D'])
+ cam->strafe(0.0,-3);
+
+ //Zooms the camera in or out
+ if(keys['Q'])
+ cam->rotate(-.05);
+ if(keys['E'])
+ cam->rotate(.05);
+
+ if(keys[VK_SPACE])
+ manager->shoot(selectedID);
+
+ //if (g_keys->keyDown[VK_F1]) // Is F1 Being Pressed?
+ //ToggleFullscreen (g_window); // Toggle Fullscreen Mode
+
+ //Adjust GL camera with the new frame
+ perspective();
+ return TRUE;
+}
+
+
+
+void OpenGLRender::checkLighting(bool lightSwitch)
+{
+ if(light)
+ {
+ if(lightSwitch)
+ glEnable(GL_LIGHTING);
+ else
+ glDisable(GL_LIGHTING);
+ }
+ else
+ glDisable(GL_LIGHTING);
+
+}
+
+//
+// FUNCTION: perspective()
+//
+// PURPOSE: Changes the viewing angle for the camera when the
+// camera variables change
+//
+// COMMENTS:
+//
+
+void OpenGLRender::perspective()
+{
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+
+ gluPerspective(45.0f, (GLfloat)screenW/(GLfloat)screenH,2.0f,1000.0f);
+
+ double adjust = 0;
+ if(cam->getPosition().y < *manager->world->getTerrainHeight(cam->getPosition().x, cam->getPosition().z)+5)
+ cam->adjustHeight(*manager->world->getTerrainHeight(cam->getPosition().x, cam->getPosition().z)+5);
+
+ gluLookAt(cam->getPosition().x,cam->getPosition().y,cam->getPosition().z,
+ cam->getLookAt().x,cam->getLookAt().y,cam->getLookAt().z, 0,1,0);
+
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+}
+
+void OpenGLRender::toggleLighting(bool lighting)
+{
+ if(lighting && !lp)
+ {
+ lp = TRUE;
+ light = !light;
+ }
+ if(!lighting)
+ lp = FALSE;
+}
+
+void OpenGLRender::toggleDebug(bool debug)
+{
+ //Toggle the debug printing to the screen
+ //Toggles at every key press
+ if(debug && !debugP)
+ {
+ debugP = TRUE;
+ debug = !debug;
+ }
+ if(!debug)
+ debugP = FALSE;
+}
+
+
+//
+// FUNCTION: loadTexture()
+//
+// PURPOSE: Loads a texture from the given file into the given array index
+//
+// COMMENTS:
+//
+
+void OpenGLRender::loadTexture(string path, int index)
+{
+ testTex[index] = SOIL_load_OGL_texture
+ (
+ path.c_str(),
+ SOIL_LOAD_AUTO,
+ SOIL_CREATE_NEW_ID,
+ SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
+ );
+
+ if(testTex[index])
+ {
+ log("Texture "+path+" loaded successfully\n");
+ // bind an OpenGL texture ID
+ //glBindTexture( GL_TEXTURE_2D, testTex[index]);
+ }
+}
+
+void OpenGLRender::loadTextures()
+{
+ loadTexture("data/images/grass.jpg", 0);
+ loadTexture("data/images/sand_texture.jpg", 2);
+ loadTexture("data/images/forest.jpg", 1);
+ loadTexture("data/images/snow_texture.jpg", 3);
+ loadTexture("data/images/water.jpg", 4);
+ loadTexture("data/images/cursor2.png", 5);
+ cursorTex = 5;
+}
+
+//
+// FUNCTION: glPrint()
+//
+// PURPOSE: Prints text to the screen using OpenGL fonts
+//
+// COMMENTS:
+//
+void OpenGLRender::glPrint(const char *fmt)
+{
+ char text[256];
+ va_list ap;
+
+ if(fmt == NULL)
+ return;
+
+ va_start(ap, fmt);
+ vsprintf_s(text, fmt, ap);
+ va_end(ap);
+
+ glListBase(*fontSet - 32);
+ glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
+ glPopAttrib();
+}
+
+OpenGLRender::~OpenGLRender(void)
+{
+}
View
63 OpenGLRender.h
@@ -0,0 +1,63 @@
+#pragma once
+#pragma comment( lib, "opengl32.lib" )
+#pragma comment( lib, "glu32.lib" )
+#pragma comment( lib, "vfw32.lib" )
+
+#include "stdafx.h"
+#include "Camera.h"
+#include "ActorManager.h"
+
+class OpenGLRender
+{
+public:
+
+ //typedef struct { // Structure For Keyboard Stuff
+ // BOOL keyDown [256]; // Holds TRUE / FALSE For Each Key
+ //} Keys; // Keys
+ //Keys* keys; // Key Structure
+
+ bool keys[256]; // Keyboard routines
+ bool active; // Flag sets window to be active by default
+ bool fullscreen; // Flag sets window to be fullscreen by default
+ bool light, lp, fp;
+
+ //Texture Variables
+ GLuint testTex[6]; //Array for holding OpenGL textures
+ GLuint *fontSet; //Holds the OpenGL font data
+ int cursorTex;
+ GLuint terrainList; //Terrain Display List
+
+ //Object variables
+ Camera *cam;
+ ActorManager *manager;
+ int screenH, screenW; //User screen width and height
+ int mouseX, mouseY; //Mouse location
+ int selectedID;
+
+ Vector3 target;
+
+ //Debug display variables
+ bool debug, debugP;
+ double drawX, drawY;
+ int overlayLineCount;
+
+ BOOL initialize (Vector2* windowSize); // Performs All Your Initialization
+ int drawGLScene(GLvoid);
+ void overlayDisplay();
+ void rasterStringToOverlay(string str);
+
+ void drawTerrainAsList();
+ void checkLighting(bool lightSwitch);
+ void perspective();
+ BOOL update (DWORD milliseconds, int mouseX, int mouseY); // Perform Motion Updates
+ void toggleLighting(bool lighting);
+ void toggleDebug(bool debug);
+
+ void loadTexture(string path, int index);
+ void loadTextures();
+ void glPrint(const char *fmt);
+
+ OpenGLRender(Camera &c, ActorManager &m, GLuint &fSet);
+ ~OpenGLRender(void);
+};
+
View
11 TerrainGen.cpp
@@ -105,16 +105,16 @@ TerrainGen::TerrainGen(void)
}
}
- int choice = 0;//getRandomAsI(4);
- log("Terrain type "+itos(choice)+" was picked.\n");
- switch(choice)
+
+ switch(0)//getRandomAsI(4))
{
//Fields with sparse trees
case 0: pField = 75, pForest = 25, pDesert = 0; //Probabilities of each terrain type being generated (Must sum to 100)
persistence = .38 + getRandomAsD(7)/100;
waterHeight = 10;
snowHeight = 0;
featurePoints = 100;
+ log("Terrain type 0 was picked.\n");
break;
//Mountainous with lots of trees
@@ -123,6 +123,7 @@ TerrainGen::TerrainGen(void)
waterHeight = 20;
snowHeight = 30;
featurePoints = 40;
+ log("Terrain type 1 was picked.\n");
break;
//Flat deserts with some brush
@@ -131,6 +132,7 @@ TerrainGen::TerrainGen(void)
waterHeight = 0;
snowHeight = 0;
featurePoints = 100;
+ log("Terrain type 2 was picked.\n");
break;
//Average mix of fields and trees with extra water
@@ -139,10 +141,11 @@ TerrainGen::TerrainGen(void)
waterHeight = 35; //Height for water to be generated
snowHeight = 0; //Height for snow to be generated
featurePoints = 40;
+ log("Terrain type 3 was picked.\n");
break;
}
sandHeight = waterHeight + 3;
- log("choice :"+itos(choice)+" persistence: "+dtos(persistence)+"\n");
+ log("persistence: "+dtos(persistence)+"\n");
//Initialize the terrain with a few random points and initial height values
View
3 TerrainGen.h
@@ -7,9 +7,6 @@ class TerrainGen
{
public:
-
-
-
nodePath ** nodes;
TerrainGen(void);

0 comments on commit 6c3cac3

Please sign in to comment.