1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
#include "Font.h"
#include "ogro.h"
#include "Box.h"
#include "md2model.h"
#include "sstream"
#include "Camera.h"
#include "Time.h"
#include <vector>
using namespace std;
#include "vgl.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/norm.hpp>
using namespace glm;
#define SPACEBAR_KEY 32
#define ESCAPE_KEY 033
Camera* camera;
Ogro* ogro;
//Box* boxes;
Font* font;
string fontText;
vector<GameObject*> gameObjects;
vector<Box * > boxes;
//vector<Box * > boxes;
int treesTapped = 0, points = 0;
float lastTime;
bool keyDown[255];
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(25, timer, 0);
}
void closeApp()
{
delete camera;
delete font;
for (auto it = gameObjects.begin(); it != gameObjects.end(); ++it)
{
delete (*it);
}
}
void CheckKeyboard()
{
int o = 0;
if (keyDown['a'])
camera->RotateLeft();
if (keyDown['d'])
camera->RotateRight();
if (keyDown['w'])
camera->MoveForward();
if (keyDown['s'])
camera->MoveBackWard();
/*if (keyDown['e'])
camera->StrafeRight();
if (keyDown['q'])
camera->StrafeLeft();*/
vec3 p = camera->getPosition();
camera->Update();
bool collided = false;
for (vector<Box*>::iterator i = boxes.begin(); i != boxes.end(); ++i)
{
if ((*i)->getType() == ObjectType::BOX)
{
if (camera->getPosition().x >= ((*i)->Position.x - (*i)->getXBuffer()) && camera->getPosition().x <= ((*i)->Position.x + (*i)->getXBuffer()))
{
if (camera->getPosition().z >= ((*i)->Position.z - (*i)->getZBuffer()) && camera->getPosition().z <= ((*i)->Position.z + (*i)->getZBuffer())) //change to Z axis and get camera position instead of radius
{
o++;
cout << o << endl;
collided = true;
}
}
}
}
if (collided)
{
camera->position = p;
camera->Update();
}
if (camera->position.x >= 90.0f)
{
camera->position.x = 90.0f;
}
if (camera->position.x <= -90.0f)
{
camera->position.x = -90.0f;
}
if (camera->position.z >= 90.0f)
{
camera->position.z = 90.0f;
}
if (camera->position.z <= -90.0f)
{
camera->position.z = -90.0f;
}
srand(time(NULL));
}
float getElapsedSeconds()
{
float currentTime = float(GetTickCount()) / 1000.0f;
float seconds = float(currentTime - lastTime);
lastTime = currentTime;
return seconds;
}
void keyboardUp(unsigned char key, int x, int y)
{
keyDown[key] = false;
}
void mouseMovement(int x, int y)
{
static bool warp = true;
if (warp)
{
if (x>glutGet(GLUT_WINDOW_WIDTH) / 2)
camera->RotateRight();
else if (x<glutGet(GLUT_WINDOW_WIDTH) / 2)
camera->RotateLeft();
if (y>glutGet(GLUT_WINDOW_HEIGHT) / 2)
camera->RotateUp();
else if (y<glutGet(GLUT_WINDOW_HEIGHT) / 2)
camera->RotateDown();
glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT) / 2);
warp = false;
}
else
warp = true;
}
void mouseWheel(int button, int direction, int x, int y)
{
if (button == 16)
camera->ResetFOV();
else if (direction > 0)
camera->ZoomIn();
else
camera->ZoomOut();
}
void keyboardSpecialKeys(int key, int x, int y)
{
float yaw;
switch (key)
{
case GLUT_KEY_LEFT:
yaw = ((Ogro*)gameObjects[1])->getYaw();
yaw -= 5.0;
((Ogro*)gameObjects[1])->setYaw(yaw);
break;
case GLUT_KEY_RIGHT:
yaw = ((Ogro*)gameObjects[1])->getYaw();
yaw += 5.0;
((Ogro*)gameObjects[1])->setYaw(yaw);
break;
}
}
void keyboard(unsigned char key, int x, int y)
{
keyDown[key] = true;
switch (key)
{
case ESCAPE_KEY:
exit(EXIT_SUCCESS);
break;
case 'e':
((Box*)gameObjects[1])->ChangeTexture();
treesTapped++;
break;
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
CheckKeyboard();
if (fontText.length() != 0)
font->printText(fontText, 350, 550, 25);
float dt = getElapsedSeconds();
for (int i = 0; i <= gameObjects.size() - 1; i++)
{
switch (gameObjects[i]->getType())
{
case ObjectType::BOX:
((Box*)gameObjects[i])->setMVPMatrix(camera->ViewMatrix, camera->ProjectionMatrix);
gameObjects[i]->Draw(dt);
break;
/*case ObjectType::OGRO:
((Ogro*)gameObjects[i])->changeInTime = dt;
((Ogro*)gameObjects[i])->Draw(camera->ViewMatrix, camera->ProjectionMatrix);
break;*/
}
}
std::stringstream ss1;
ss1 << "Trees tapped: " << treesTapped;
font->printText(ss1.str().c_str(), 20, 550, 15);
std::stringstream ss2;
ss2 << "Points: " << points;
font->printText(ss2.str().c_str(), 20, 520, 15);
glutSwapBuffers();
}
void init()
{
glClearColor(0.0f, 0.0f, 0.7f, 0.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
font = new Font();
camera = new Camera();
camera->setPosition(vec3(20.0f, 1.5f, 0.0f));
glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT) / 2);
std::fill(keyDown, keyDown + 255, false);
gameObjects.push_back(new Box(vec3(0.0f, -2.0f, 0.0f), "data/images/grass-1.png", "data/shaders/Texture.vertexshader", "data/shaders/Texture.fragmentshader", "Data\\Models\\Floor.xml", true, 0.1f, vec3(100.0f, 0.1f, 100.0f), true));
gameObjects.push_back(new Box(vec3(0.0f, -2.0f, 0.0f), "data/images/wood.bmp", "data/shaders/Texture.vertexshader", "data/shaders/Texture.fragmentshader", "Data\\Models\\Floor.xml", true, 0.1f, vec3(1.0f, 10.0f, 1.0f), true));
gameObjects.push_back(new Box(vec3(0.0f, 6.0f, 0.0f), "data/images/leaves.jpg", "data/shaders/Texture.vertexshader", "data/shaders/Texture.fragmentshader", "Data\\Models\\Floor.xml", true, 0.1f, vec3(2.5f, 2.5f, 2.5f), true));
/*gameObjects.push_back(new Ogro());
((Ogro*)gameObjects[1])->Initialize();
((Ogro*)gameObjects[1])->setPosition(vec3(0, 0, 0));
((Ogro*)gameObjects[1])->setDirection(vec3(1, 0, 0));
((Ogro*)gameObjects[1])->setYaw(90);
gameObjects.push_back(new Ogro());
((Ogro*)gameObjects[2])->Initialize();
((Ogro*)gameObjects[2])->setYaw(-90);
((Ogro*)gameObjects[2])->setPosition(vec3(0, 0, -3));
((Ogro*)gameObjects[2])->setDirection(vec3(1, 0, 0));*/
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(1024, 768);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("Model Example1");
if (glewInit())
{
cerr << "Unable to init glew" << endl;
exit(EXIT_FAILURE);
}
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardUp);
glutSpecialFunc(keyboardSpecialKeys);
//glutPassiveMotionFunc(mouseMovement);
glutMouseWheelFunc(mouseWheel);
glutTimerFunc(25, timer, 0);
glutMainLoop();
return 0;
}
| |