Merge pull request #8 from CrystalPointA4/worldLoading

Added world loading and other various improvements
This commit is contained in:
2016-06-03 11:12:23 +02:00
33 changed files with 1297 additions and 330 deletions
+28
View File
@@ -0,0 +1,28 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CrystalPoint", "CrystalPoint.vcxproj", "{F82158C7-7345-4CB0-9F90-3AB49A071904}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F82158C7-7345-4CB0-9F90-3AB49A071904}.Debug|x64.ActiveCfg = Debug|x64
{F82158C7-7345-4CB0-9F90-3AB49A071904}.Debug|x64.Build.0 = Debug|x64
{F82158C7-7345-4CB0-9F90-3AB49A071904}.Debug|x86.ActiveCfg = Debug|Win32
{F82158C7-7345-4CB0-9F90-3AB49A071904}.Debug|x86.Build.0 = Debug|Win32
{F82158C7-7345-4CB0-9F90-3AB49A071904}.Release|x64.ActiveCfg = Release|x64
{F82158C7-7345-4CB0-9F90-3AB49A071904}.Release|x64.Build.0 = Release|x64
{F82158C7-7345-4CB0-9F90-3AB49A071904}.Release|x86.ActiveCfg = Release|Win32
{F82158C7-7345-4CB0-9F90-3AB49A071904}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
-104
View File
@@ -1,104 +0,0 @@
#include "CrystalJohan.h"
#include <GL/freeglut.h>
#include <cmath>
#include <cstring>
#include "World.h"
void CrystalJohan::init()
{
world = new World();
lastFrameTime = 0;
glClearColor(0.7, 0.7, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
mousePosition = Vec2f(width / 2, height / 2);
}
void CrystalJohan::draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Draw world
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, width / (float)height, 0.1f, 15000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
world->draw();
//Draw Cursor
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,width, height,0,-10,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glColor4f(1, cos(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), sin(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), 1);
glBegin(GL_TRIANGLES);
glVertex2f(mousePosition.x, mousePosition.y);
glVertex2f(mousePosition.x+15, mousePosition.y+15);
glVertex2f(mousePosition.x+5, mousePosition.y+20);
glEnd();*/
glutSwapBuffers();
}
void CrystalJohan::update()
{
float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
float deltaTime = frameTime - lastFrameTime;
lastFrameTime = frameTime;
// if(keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
if (keyboardState.keys[27])
exit(0);
world->player.rotation.y += mouseOffset.x / 10.0f;
world->player.rotation.x += mouseOffset.y / 10.0f;
if (world->player.rotation.x > 90)
world->player.rotation.x = 90;
if (world->player.rotation.x < -90)
world->player.rotation.x = -90;
float speed = 20;
Vec3f oldPosition = world->player.position;
if (keyboardState.keys['a']) world->player.setPosition(0, deltaTime*speed, false);
if (keyboardState.keys['d']) world->player.setPosition(180, deltaTime*speed, false);
if (keyboardState.keys['w']) world->player.setPosition(90, deltaTime*speed, false);
if (keyboardState.keys['s']) world->player.setPosition(270, deltaTime*speed, false);
if (keyboardState.keys['q']) world->player.setPosition(1, deltaTime*speed, true);
if (keyboardState.keys['e']) world->player.setPosition(-1, deltaTime*speed, true);
if (!world->isPlayerPositionValid())
world->player.position = oldPosition;
world->update(deltaTime);
mousePosition = mousePosition + mouseOffset;
mouseOffset = Vec2f(0, 0);
prevKeyboardState = keyboardState;
glutPostRedisplay();
}
KeyboardState::KeyboardState()
{
memset(keys, 0, sizeof(keys));
memset(special, 0, sizeof(special));
}
-28
View File
@@ -1,28 +0,0 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CrystalJohan", "CrystalJohan.vcxproj", "{98776A7C-CD7A-4C62-946A-2263C27E9690}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{98776A7C-CD7A-4C62-946A-2263C27E9690}.Debug|x64.ActiveCfg = Debug|x64
{98776A7C-CD7A-4C62-946A-2263C27E9690}.Debug|x64.Build.0 = Debug|x64
{98776A7C-CD7A-4C62-946A-2263C27E9690}.Debug|x86.ActiveCfg = Debug|Win32
{98776A7C-CD7A-4C62-946A-2263C27E9690}.Debug|x86.Build.0 = Debug|Win32
{98776A7C-CD7A-4C62-946A-2263C27E9690}.Release|x64.ActiveCfg = Release|x64
{98776A7C-CD7A-4C62-946A-2263C27E9690}.Release|x64.Build.0 = Release|x64
{98776A7C-CD7A-4C62-946A-2263C27E9690}.Release|x86.ActiveCfg = Release|Win32
{98776A7C-CD7A-4C62-946A-2263C27E9690}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
+111
View File
@@ -0,0 +1,111 @@
#include "CrystalPoint.h"
#include <GL/freeglut.h>
#include <cmath>
#include <cstring>
#include "WorldHandler.h"
#include "Player.h"
void CrystalPoint::init()
{
player = Player::getInstance();
worldhandler = WorldHandler::getInstance();
lastFrameTime = 0;
glClearColor(0.7, 0.7, 1.0, 1.0);
mousePosition = Vec2f(width / 2, height / 2);
}
void CrystalPoint::draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Draw world
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, width / (float)height, 0.1f, 15000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
worldhandler->draw();
//Draw Cursor
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,width, height,0,-10,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glColor4f(1, cos(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), sin(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), 1);
glBegin(GL_TRIANGLES);
glVertex2f(mousePosition.x, mousePosition.y);
glVertex2f(mousePosition.x+15, mousePosition.y+15);
glVertex2f(mousePosition.x+5, mousePosition.y+20);
glEnd();
glutSwapBuffers();
}
void CrystalPoint::update()
{
float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
float deltaTime = frameTime - lastFrameTime;
lastFrameTime = frameTime;
if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
worldhandler->PreviousWorld();
if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT])
worldhandler->NextWorld();
if (keyboardState.keys[27])
exit(0);
Player* player = Player::getInstance();
player->rotation.y += mouseOffset.x / 10.0f;
player->rotation.x += mouseOffset.y / 10.0f;
if (player->rotation.x > 90)
player->rotation.x = 90;
if (player->rotation.x < -90)
player->rotation.x = -90;
float speed = 1;
Vec3f oldPosition = player->position;
if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false);
if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false);
if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false);
if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false);
//if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
//if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
if (!worldhandler->isPlayerPositionValid())
player->position = oldPosition;
player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 0.7f;
worldhandler->update(deltaTime);
mousePosition = mousePosition + mouseOffset;
mouseOffset = Vec2f(0, 0);
prevKeyboardState = keyboardState;
glutPostRedisplay();
}
KeyboardState::KeyboardState()
{
memset(keys, 0, sizeof(keys));
memset(special, 0, sizeof(special));
}
+5 -3
View File
@@ -1,6 +1,7 @@
#pragma once
class World;
class WorldHandler;
class Player;
#include "Vector.h"
class KeyboardState
@@ -13,14 +14,15 @@ public:
KeyboardState();
};
class CrystalJohan
class CrystalPoint
{
public:
void init();
void draw();
void update();
World* world;
WorldHandler* worldhandler;
Player* player;
int width, height;
KeyboardState keyboardState;
+10 -8
View File
@@ -19,10 +19,11 @@
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{98776A7C-CD7A-4C62-946A-2263C27E9690}</ProjectGuid>
<ProjectGuid>{F82158C7-7345-4CB0-9F90-3AB49A071904}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>CrystalJohan</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<ProjectName>CrystalPoint</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@@ -150,10 +151,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CrystalJohan.cpp" />
<ClCompile Include="CrystalPoint.cpp" />
<ClCompile Include="Enemy.cpp" />
<ClCompile Include="Entity.cpp" />
<ClCompile Include="HeightMap.cpp" />
@@ -165,9 +163,10 @@
<ClCompile Include="Vector.cpp" />
<ClCompile Include="Vertex.cpp" />
<ClCompile Include="World.cpp" />
<ClCompile Include="WorldHandler.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CrystalJohan.h" />
<ClInclude Include="CrystalPoint.h" />
<ClInclude Include="Enemy.h" />
<ClInclude Include="Entity.h" />
<ClInclude Include="HeightMap.h" />
@@ -176,14 +175,17 @@
<ClInclude Include="Main.h" />
<ClInclude Include="Model.h" />
<ClInclude Include="Player.h" />
<ClInclude Include="Singleton.h" />
<ClInclude Include="stb_image.h" />
<ClInclude Include="vector.h" />
<ClInclude Include="Vertex.h" />
<ClInclude Include="World.h" />
<ClInclude Include="WorldHandler.h" />
</ItemGroup>
<ItemGroup>
<None Include="worlds\world1.json" />
<None Include="worlds\fire.json" />
<None Include="worlds\ice.json" />
<None Include="worlds\small.json" />
<None Include="worlds\worlds.json" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
@@ -13,52 +13,58 @@
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Source Files\Object">
<UniqueIdentifier>{1e464995-b141-43fd-9e25-16d6ac47176b}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\World">
<UniqueIdentifier>{0de1a037-e536-40df-a0d0-0d929f2fe752}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\json">
<UniqueIdentifier>{9c655946-3f99-44ea-bc97-2817656954e0}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CrystalJohan.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="World.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Entity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Enemy.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LevelObject.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Model.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Vector.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Player.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="json.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="HeightMap.cpp">
<ClCompile Include="WorldHandler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Entity.cpp">
<Filter>Source Files\Object</Filter>
</ClCompile>
<ClCompile Include="Model.cpp">
<Filter>Source Files\Object</Filter>
</ClCompile>
<ClCompile Include="Vertex.cpp">
<Filter>Source Files\Object</Filter>
</ClCompile>
<ClCompile Include="Vector.cpp">
<Filter>Source Files\Object</Filter>
</ClCompile>
<ClCompile Include="World.cpp">
<Filter>Source Files\World</Filter>
</ClCompile>
<ClCompile Include="HeightMap.cpp">
<Filter>Source Files\World</Filter>
</ClCompile>
<ClCompile Include="LevelObject.cpp">
<Filter>Source Files\World</Filter>
</ClCompile>
<ClCompile Include="Enemy.cpp">
<Filter>Source Files\Object</Filter>
</ClCompile>
<ClCompile Include="CrystalPoint.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CrystalJohan.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="World.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -83,9 +89,6 @@
<ClInclude Include="Player.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Singleton.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Main.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -98,8 +101,25 @@
<ClInclude Include="Vertex.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="WorldHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CrystalPoint.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="worlds\world1.json" />
<None Include="worlds\worlds.json">
<Filter>Source Files\json</Filter>
</None>
<None Include="worlds\fire.json">
<Filter>Source Files\json</Filter>
</None>
<None Include="worlds\ice.json">
<Filter>Source Files\json</Filter>
</None>
<None Include="worlds\small.json">
<Filter>Source Files\json</Filter>
</None>
</ItemGroup>
</Project>
+2 -3
View File
@@ -9,14 +9,13 @@
Enemy::Enemy(const std::string &fileName,
const Vec3f &position,
Vec3f &rotation,
const float &scale,
const bool &hasCollision)
const float &scale)
{
model = Model::load(fileName);
this->position = position;
this->rotation = rotation;
this->scale = scale;
this->canCollide = hasCollision;
this->canCollide = true;
target = position;
speed = 1;
radius = 10;
+1 -1
View File
@@ -7,7 +7,7 @@
class Enemy : public Entity
{
public:
Enemy(const std::string &fileName,const Vec3f &position,Vec3f &rotation,const float &scale,const bool &hasCollision);
Enemy(const std::string &fileName,const Vec3f &position,Vec3f &rotation,const float &scale);
~Enemy();
bool hasTarget;
+6 -2
View File
@@ -14,6 +14,8 @@ Entity::Entity()
Entity::~Entity()
{
if(model)
Model::unload(model);
}
@@ -23,11 +25,13 @@ void Entity::draw()
{
glPushMatrix();
glTranslatef(position.x, position.y, position.z);
glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.z, 0, 0, 1);
glScalef(scale, scale, scale);
//collision gaat hierdoor kapot glTranslatef(-model->center.x, 0, -model->center.z);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
@@ -46,8 +50,8 @@ bool Entity::inObject(const Vec3f & point)
if (!model)
return false;
Vec3f center = position + model->center;
float distance = sqrt((point.x - center.x) * (point.x - center.x) + (point.z - center.z)*(point.z - center.z));
if (distance < model->radius*scale)
float distance = ((point.x - center.x) * (point.x - center.x) + (point.z - center.z)*(point.z - center.z));
if (distance < model->radius*scale*model->radius*scale)
return true;
return false;
}
+68 -13
View File
@@ -1,29 +1,56 @@
#include "HeightMap.h"
#include "stb_image.h"
#include "vector.h"
#include "LevelObject.h"
#include <GL/freeglut.h>
#include <iostream>
#include <string>
#include "World.h"
#define RED 0
#define GREEN 1
#define BLUE 2
#define ALPHA 3
HeightMap::HeightMap(const std::string &file)
HeightMap::HeightMap(const std::string &file, World* world)
{
int bpp;
unsigned char* imgData = stbi_load(file.c_str(), &width, &height, &bpp, 4);
for (int h = 0; h < height; h++)
auto heightAt = [&](int x, int y)
{
for (int w = 0; w < width; w++)
return (imgData[(x + y * width) * 4 ] / 256.0f) * 50.0f;
};
auto valueAt = [&](int x, int y, int offset = 0)
{
return imgData[(x + y * width) * 4 + offset];
};
for (int y = 0; y < height-1; y++)
{
for (int x = 0; x < width-1; x++)
{
int offsets[4][2] = { { 0, 0 },{ 1, 0 },{ 1, 1 },{ 0, 1 } };
Vec3f ca(0, heightAt(x, y + 1) - heightAt(x, y), 1);
Vec3f ba(1, heightAt(x + 1, y) - heightAt(x, y), 0);
if (valueAt(x, y, GREEN) > 0)
{
world->addLevelObject(new LevelObject(world->getObjectFromValue(valueAt(x, y, GREEN)), Vec3f(x, heightAt(x, y), y), Vec3f(0, rand()%360, 0), 1, true));
}
Vec3f normal = ca.cross(ba);
normal.Normalize();
for (int i = 0; i < 4; i++)
{
float y = ((float)imgData[((h + offsets[i][0]) + (w + offsets[i][1]) * width) * 4]);
y = (y / 256.0f) * 100.0f;
vertices.push_back(Vertex{ (float)(h + offsets[i][0])*scale, y*scale, (float)(w + offsets[i][1])*scale,
0, 1, 0,
(h + offsets[i][0]) / (float)height, (w + offsets[i][1]) / (float)width } );
float h = heightAt(x + offsets[i][0], y + offsets[i][1]);
vertices.push_back(Vertex{ (float)(x + offsets[i][0]), h, (float)(y + offsets[i][1]),
normal.x, normal.y, normal.z,
(x + offsets[i][0]) / (float)height, (y + offsets[i][1]) / (float)width } );
}
}
}
@@ -48,32 +75,60 @@ HeightMap::HeightMap(const std::string &file)
HeightMap::~HeightMap()
{
glDeleteTextures(1, &imageIndex);
}
void HeightMap::Draw()
{
glEnable(GL_LIGHTING);
float color[] = { 0.7f, 0.7f, 0.7f, 1 };
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, imageIndex);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//glEnableClientState(GL_COLOR_ARRAY);
//glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 0);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 6);
//glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)cubeVertices.data()) + 3);
glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 3);
glDrawArrays(GL_QUADS, 0, vertices.size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//glDisableClientState(GL_COLOR_ARRAY);
//glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
void HeightMap::GetHeigth(float x, float z)
float HeightMap::GetHeight(float x, float y)
{
int ix = x;
int iy = y;
int index = (ix + (width - 1) * iy) * 4;
if (index + 3 >= vertices.size())
index = vertices.size() - 4;
if (index < 0)
index = 0;
Vertex& a = vertices[index];
Vertex& b = vertices[index+1];
Vertex& c = vertices[index+3];
float lowervalue = ((b.z - c.z)*(a.x - c.x) + (c.x - b.x)*(a.z - c.z));
float labda1 = ((b.z - c.z)*(x - c.x) + (c.x - b.x)*(y - c.z)) / lowervalue;
float labda2 = ((c.y - a.y)*(x - c.x) + (a.x - c.x)*(y - c.y)) / lowervalue;
float labda3 = 1 - labda1 - labda2;
Vertex z = a * labda1 + b * labda2 + c * labda3;
return z.y;
}
void HeightMap::SetTexture(const std::string &file)
+4 -3
View File
@@ -5,6 +5,8 @@
#include <vector>
#include <GL/freeglut.h>
class World;
class HeightMap
{
private:
@@ -12,13 +14,12 @@ private:
int width;
GLuint imageIndex;
int scale = 1;
public:
HeightMap(const std::string &file);
HeightMap(const std::string &file, World* world);
~HeightMap();
void Draw();
void GetHeigth(float x, float z);
float GetHeight(float x, float y);
void SetTexture(const std::string &file);
std::vector<Vertex> vertices;
+8 -6
View File
@@ -1,18 +1,18 @@
#include <GL/freeglut.h>
#include "CrystalJohan.h"
#include "CrystalPoint.h"
#include <stdio.h>
#include "Vector.h"
void configureOpenGL(void);
CrystalJohan* app;
CrystalPoint* app;
bool justMoved = false;
int main(int argc, char* argv[])
{
app = new CrystalJohan();
app = new CrystalPoint();
glutInit(&argc, argv);
configureOpenGL();
@@ -26,6 +26,8 @@ int main(int argc, char* argv[])
//Keyboard
glutKeyboardFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = true; });
glutKeyboardUpFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = false; });
glutSpecialFunc([](int c, int, int) { app->keyboardState.special[c] = true; });
glutSpecialUpFunc([](int c, int, int) { app->keyboardState.special[c] = false; });
//Mouse
glutPassiveMotionFunc([](int x, int y)
@@ -55,7 +57,7 @@ void configureOpenGL()
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Crystal Point");
glutFullScreen();
//glutFullScreen();
//Depth testing
glEnable(GL_DEPTH_TEST);
@@ -81,8 +83,8 @@ void configureOpenGL()
//glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
//glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
}
+14 -12
View File
@@ -158,12 +158,11 @@ void Model::Optimise(ObjGroup *t)
}
}
Model::~Model(void)
{
}
void Model::draw()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
for (auto &g : groups)
{
if (materials[g->materialIndex]->hasTexture)
@@ -194,19 +193,14 @@ void Model::draw()
}
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 0);
glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 3);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 6);
glDrawArrays(GL_TRIANGLES, 0, g->VertexArray.size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
void Model::loadMaterialFile(std::string fileName, std::string dirName)
@@ -381,3 +375,11 @@ void Model::unload(Model* model)
}
}
}
Model::~Model(void)
{
for (auto m : cache)
{
delete m.second.first;
}
}
+24
View File
@@ -3,11 +3,35 @@
#include "Player.h"
#include <GL/freeglut.h>
Player* Player::instance = NULL;
Player::Player()
{
speed = 10;
}
Player* Player::getInstance()
{
if (instance == nullptr)
instance = new Player();
return instance;
}
void Player::init()
{
instance = new Player();
}
Player::~Player()
{
if (leftWeapon)
delete leftWeapon;
if (rightWeapon)
delete rightWeapon;
}
void Player::setCamera()
{
glRotatef(rotation.x, 1, 0, 0);
+7 -4
View File
@@ -1,19 +1,22 @@
#pragma once
#include "Singleton.h"
#include "Vector.h"
class Model;
class Player : public Singleton<Player>
class Player
{
private:
static Player* instance;
public:
Player();
~Player();
void setCamera();
void setPosition(float angle, float fac, bool height);
static Player* getInstance(void);
static void init(void);
Vec3f position;
Vec2f rotation;
+7 -7
View File
@@ -1,27 +1,27 @@
========================================================================
CONSOLE APPLICATION : CrystalJohan Project Overview
CONSOLE APPLICATION : CrystalPoint Project Overview
========================================================================
AppWizard has created this CrystalJohan application for you.
AppWizard has created this CrystalPoint application for you.
This file contains a summary of what you will find in each of the files that
make up your CrystalJohan application.
make up your CrystalPoint application.
CrystalJohan.vcxproj
CrystalPoint.vcxproj
This is the main project file for VC++ projects generated using an Application Wizard.
It contains information about the version of Visual C++ that generated the file, and
information about the platforms, configurations, and project features selected with the
Application Wizard.
CrystalJohan.vcxproj.filters
CrystalPoint.vcxproj.filters
This is the filters file for VC++ projects generated using an Application Wizard.
It contains information about the association between the files in your project
and the filters. This association is used in the IDE to show grouping of files with
similar extensions under a specific node (for e.g. ".cpp" files are associated with the
"Source Files" filter).
CrystalJohan.cpp
CrystalPoint.cpp
This is the main application source file.
/////////////////////////////////////////////////////////////////////////////
@@ -29,7 +29,7 @@ Other standard files:
StdAfx.h, StdAfx.cpp
These files are used to build a precompiled header (PCH) file
named CrystalJohan.pch and a precompiled types file named StdAfx.obj.
named CrystalPoint.pch and a precompiled types file named StdAfx.obj.
/////////////////////////////////////////////////////////////////////////////
Other notes:
-13
View File
@@ -1,13 +0,0 @@
#pragma once
template <class T>
class Singleton
{
public:
static T& getInstance()
{
static T* t = new T();
return *t;
}
};
+14
View File
@@ -80,6 +80,20 @@ bool Vec3f::operator!=(const Vec3f & other)
return x != other.x & y != other.y & z != other.z;
}
Vec3f Vec3f::operator*(const float & other)
{
return Vec3f(x*other, y*other, z*other);
}
Vec3f Vec3f::cross(const Vec3f & other)
{
return Vec3f(
y*other.z - other.y*z,
z*other.x - other.z*x,
x*other.y - other.x*y
);
}
Vec2f::Vec2f(float x, float y)
+4
View File
@@ -23,6 +23,10 @@ public:
Vec3f operator / (float value);
bool operator ==(const Vec3f &other);
bool operator !=(const Vec3f &other);
Vec3f operator *(const float &other);
Vec3f cross(const Vec3f &other);
};
class Vec2f
+20
View File
@@ -17,3 +17,23 @@ Vertex::Vertex(float x, float y, float z, float nx, float ny, float nz, float tx
Vertex::~Vertex()
{
}
Vertex Vertex::operator/(float &other)
{
return Vertex(x / other, y / other, z / other, normalX, normalY, normalZ, texX, texY);
}
Vertex Vertex::operator*(Vertex & other)
{
return Vertex(x*other.x, y*other.y, z*other.z, normalX, normalY, normalZ, texX, texY);
}
Vertex Vertex::operator*(float & other)
{
return Vertex(x*other, y*other, z*other, normalX, normalY, normalZ, texX, texY);
}
Vertex Vertex::operator+(Vertex & other)
{
return Vertex(x+other.x, y+other.y, z+other.z, normalX, normalY, normalZ, texX, texY);
}
+5
View File
@@ -15,5 +15,10 @@ public:
float texX;
float texY;
Vertex operator/(float &other);
Vertex operator*(Vertex &other);
Vertex operator*(float &other);
Vertex operator+(Vertex &other);
};
+81 -54
View File
@@ -1,27 +1,46 @@
#include "World.h"
#include <GL/freeglut.h>
#include "Entity.h"
#include "LevelObject.h"
#include "json.h"
#include "Model.h"
#include <fstream>
#include <iostream>
World::World() : player(Player::getInstance())
World::World(const std::string &fileName)
{
player = Player::getInstance();
std::ifstream file("worlds/world1.json");
std::ifstream file(fileName);
if(!file.is_open())
std::cout<<"Uhoh, can't open file\n";
std::cout<<"Error, can't open world file - " << fileName << "\n";
json::Value v = json::readJson(file);
file.close();
heightmap = new HeightMap(v["world"]["heightmap"].asString());
heightmap->SetTexture(v["world"]["texture"].asString());
//Check file
if(v["world"].isNull() || v["world"]["heightmap"].isNull())
std::cout << "Invalid world file: world - " << fileName << "\n";
if (v["player"].isNull() || v["player"]["startposition"].isNull())
std::cout << "Invalid world file: player - " << fileName << "\n";
if (v["objects"].isNull())
std::cout << "Invalid world file: objects - " << fileName << "\n";
if (v["world"]["object-templates"].isNull())
std::cout << "Invalid world file: object templates - " << fileName << "\n";
player.position.x = v["player"]["startposition"][0];
player.position.y = v["player"]["startposition"][1];
player.position.z = v["player"]["startposition"][2];
//Load object templates
for (auto objt : v["world"]["object-templates"])
{
objecttemplates.push_back(std::pair<int, std::string>(objt["color"], objt["file"]));
}
heightmap = new HeightMap(v["world"]["heightmap"].asString(), this);
if(!v["world"]["texture"].isNull())
heightmap->SetTexture(v["world"]["texture"].asString());
player->position.x = v["player"]["startposition"][0].asFloat();
player->position.y = v["player"]["startposition"][1].asFloat();
player->position.z = v["player"]["startposition"][2].asFloat();
for (auto object : v["objects"])
@@ -32,68 +51,68 @@ World::World() : player(Player::getInstance())
Vec3f rotation(0, 0, 0);
if(!object["rot"].isNull())
rotation = Vec3f(object["rot"][0], object["rot"][1], object["rot"][2]);
rotation = Vec3f(object["rot"][0].asFloat(), object["rot"][1].asFloat(), object["rot"][2].asFloat());
float scale = 1;
if (!object["scale"].isNull())
scale = object["scale"].asFloat();
Vec3f position(object["pos"][0], object["pos"][1], object["pos"][2]);
if (object["pos"].isNull())
std::cout << "Invalid world file: objects pos - " << fileName << "\n";
Vec3f position(object["pos"][0].asFloat(), object["pos"][1].asFloat(), object["pos"][2].asFloat());
entities.push_back(new LevelObject(object["file"], position, rotation, scale, hasCollision));
}
//look up table for the enemies
std::vector<std::pair<int, std::string>>enemy_models;
for (auto enemy_model : v["enemy_models"])
//Enemies
for (auto e : v["enemies"])
{
int id = -1;
if (!enemy_model["id"].isNull())
id = enemy_model["id"].asInt();
std::string fileName = "";
if (!enemy_model["file"].isNull())
fileName = enemy_model["file"].asString();
if (!e["file"].isNull())
fileName = e["file"].asString();
enemy_models.push_back(std::pair<int, std::string>(id,fileName));
}
Vec3f position(0, 0, 0);
if (!e["pos"].isNull())
position = Vec3f(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
for (auto enemy : v["enemy_data"])
{
int id = -1;
if (!enemy["id"].isNull())
id = enemy["id"];
for (auto enemy_model : enemy_models)
{
if (id == enemy_model.first)
{
Vec3f position(0, 0, 0);
if (!enemy["pos"].isNull())
position = Vec3f(enemy["pos"][0], enemy["pos"][1], enemy["pos"][2]);
Vec3f rotation(0, 0, 0);
if (!e["rot"].isNull())
rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
Vec3f rotation(0, 0, 0);
if (!enemy["rot"].isNull())
rotation = Vec3f(enemy["rot"][0], enemy["rot"][1], enemy["rot"][2]);
float scale = 1.0f;
if (!e["scale"].isNull())
scale = e["scale"].asFloat();
float scale = 1.0f;
if (!enemy["scale"].isNull())
scale = enemy["scale"].asFloat();
enemies.push_back(new Enemy(enemy_model.second,position,rotation,scale,true));
}
enemies.push_back(new Enemy(fileName, position, rotation, scale));
}
}
}
World::~World()
{
delete heightmap;
}
std::string World::getObjectFromValue(int val)
{
for (auto i : objecttemplates)
{
if (i.first == val)
return i.second;
}
return objecttemplates[0].second;
}
float World::getHeight(float x, float y)
{
return heightmap->GetHeight(x, y);
}
void World::draw()
{
player.setCamera();
player->setCamera();
float lightPosition[4] = { 0, 2, 1, 0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
@@ -108,9 +127,6 @@ void World::draw()
for (auto &entity : entities)
entity->draw();
}
void World::update(float elapsedTime)
@@ -120,11 +136,13 @@ void World::update(float elapsedTime)
for (auto &enemy : enemies)
{
if (enemy->position.Distance(player.position) <= enemy->radius)
//Al deze code zou in enemy moeten staan
if (enemy->position.Distance(player->position) <= enemy->radius)
{
enemy->hasTarget = true;
enemy->target.x = player.position.x;
enemy->target.z = player.position.z;
enemy->target.x = player->position.x;
enemy->target.z = player->position.z;
}
else
enemy->hasTarget = false;
@@ -137,19 +155,28 @@ void World::update(float elapsedTime)
{
if (e->canCollide && e->inObject(enemy->position))
{
enemy->position = oldpos;
Vec3f difference = e->position - enemy->position; //zou misschien omgedraait moeten worden
difference.Normalize();
difference = difference * (e->model->radius + 0.01f);
enemy->position = e->position + difference;
break;
}
}
}
//tot hier
}
}
void World::addLevelObject(LevelObject* obj)
{
entities.push_back(obj);
}
bool World::isPlayerPositionValid()
{
for (auto e : entities)
{
if (e->canCollide && e->inObject(player.position))
if (e->canCollide && e->inObject(player->position))
return false;
}
return true;
+8 -4
View File
@@ -4,25 +4,29 @@
#include "HeightMap.h"
#include "Player.h"
#include "Enemy.h"
#include "LevelObject.h"
class Entity;
class World
{
private:
std::vector<std::pair<int, std::string>> objecttemplates;
public:
World();
World(const std::string &fileName);
~World();
Player& player;
std::vector<Entity*> entities;
std::vector<Enemy*> enemies;
Player* player;
HeightMap* heightmap;
void draw();
void update(float elapsedTime);
bool isPlayerPositionValid();
float getHeight(float x, float y);
void addLevelObject(LevelObject* obj);
std::string getObjectFromValue(int i);
};
+137
View File
@@ -0,0 +1,137 @@
#include "WorldHandler.h"
#include "World.h"
#include "json.h"
#include <fstream>
#include <iostream>
#include <string>
WorldHandler* WorldHandler::instance = nullptr;
void WorldHandler::ChangeWorld(int i)
{
if (i < 0)
i = worldfiles.size() - 1;
else if (i >= worldfiles.size())
i = 0;
if (i != worldIndex)
{
loadingWorld = true;
if(worldIndex != -1)
delete world;
world = new World(worldfiles[i]);
worldIndex = i;
loadingWorld = false;
}
}
WorldHandler::WorldHandler()
{
loadingWorld = true;
worldIndex = -1;
//Find worlds.json
std::ifstream file("worlds/worlds.json");
if (!file.is_open())
std::cout << "Error, can't open worlds overview file\n";
json::Value v = json::readJson(file);
file.close();
//Load file names into vector
if (v["worlds"].isNull() || !v["worlds"].isArray())
std::cout << "Error, no content in worlds overview file\n";
for (auto line : v["worlds"])
{
std::cout << "Found world: " << line << "\n";
worldfiles.push_back(line);
}
if (worldfiles.size() > 0)
{
ChangeWorld(0);
}
}
WorldHandler::~WorldHandler()
{
worldIndex = -1;
delete world;
}
WorldHandler* WorldHandler::getInstance()
{
if (instance == nullptr)
instance = new WorldHandler();
return instance;
}
void WorldHandler::init()
{
instance = new WorldHandler();
}
void WorldHandler::draw(void)
{
if(!loadingWorld)
world->draw();
else
{
//Draw Loading screen
}
}
void WorldHandler::update(float deltaTime)
{
if(!loadingWorld)
world->update(deltaTime);
}
bool WorldHandler::isPlayerPositionValid(void)
{
if(!loadingWorld)
return world->isPlayerPositionValid();
}
float WorldHandler::getHeight(float x, float y)
{
if (!loadingWorld)
return world->getHeight(x, y);
else
return 0.0f;
}
void WorldHandler::Navigate(const std::string &fileName)
{
if (!loadingWorld)
{
for (int i = 0; i < worldfiles.size(); i++)
{
if (worldfiles[i] == fileName)
ChangeWorld(i);
}
}
}
void WorldHandler::NextWorld()
{
if (!loadingWorld)
{
ChangeWorld(worldIndex + 1);
}
}
void WorldHandler::PreviousWorld()
{
if (!loadingWorld)
{
ChangeWorld(worldIndex - 1);
}
}
+36
View File
@@ -0,0 +1,36 @@
#pragma once
#include <string>
#include <vector>
class World;
class WorldHandler
{
private:
WorldHandler();
static WorldHandler* instance;
bool loadingWorld;
World* world;
int worldIndex;
void ChangeWorld(int i);
public:
~WorldHandler();
static WorldHandler* getInstance(void);
static void init();
void draw(void);
void update(float deltaTime);
bool isPlayerPositionValid(void);
float getHeight(float x, float y);
void Navigate(const std::string &fileName);
void NextWorld();
void PreviousWorld();
std::vector<std::string> worldfiles;
};
+36
View File
@@ -0,0 +1,36 @@
{
"world": {
"heightmap": "worlds/hell.png",
"texture": "worlds/helltexture.png",
"object-templates": [
{
"color": 25,
"file": "models/boom/Boom.obj"
},
{
"color": 23,
"file": "models/boom/Boom.obj"
}
]
},
"player": {
"startposition": [ 200, 40, 200 ]
},
"objects": [
{
"file": "models/boom/Boom.obj",
"pos": [ 4, 0, -4 ]
},
{
"file": "models/boom/Boom.obj",
"pos": [ -4, 0, -4 ]
}
],
"enemies": [
{
"file": "models/squid/Blooper.obj",
"pos": [ 1, 2, -10 ],
"scale": 0.01
}
]
}
+560
View File
File diff suppressed because one or more lines are too long
+26
View File
@@ -0,0 +1,26 @@
{
"world": {
"heightmap": "worlds/hmcs.png",
"texture": "worlds/hmcstexture.png",
},
"player": {
"startposition": [ 100, 20, 100 ]
},
"objects": [
{
"file": "models/boom/Boom.obj",
"pos": [ 4, 0, -4 ]
},
{
"file": "models/Teleporter/Teleporter.obj",
"pos": [ 0, 0, -4 ]
}
],
"enemies": [
{
"file": "models/squid/Blooper.obj",
"pos": [ 1, 2, -10 ],
"scale": 0.01
}
]
}
+16
View File
@@ -0,0 +1,16 @@
{
"world": {
"heightmap": "worlds/small.png",
"object-templates": [
{
"color":100,
"file": "models/boom/Boom.obj"
}
]
},
"player": {
"startposition": [ 20, 5, 20 ]
},
"objects": [],
"enemies": []
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

-33
View File
@@ -1,33 +0,0 @@
{
"world": {
"heightmap": "worlds/hmcs.png",
"texture": "worlds/hmcstexture.png"
},
"player": {
"startposition": [ -100, 1.7, -100 ]
},
"objects": [
{
"file": "models/boom/Boom.obj",
"pos": [ 4, 0, -4 ]
},
{
"file": "models/Teleporter/Teleporter.obj",
"pos": [ 0, 0, -4 ],
"rot": [ 0, 0, 0 ]
}
],
"enemy_models": [
{
"id": 0,
"file": "models/squid/Blooper.obj"
}
],
"enemy_data": [
{
"id": 0,
"pos": [ 1, 2, -10 ],
"scale": 0.01
}
]
}
+7
View File
@@ -0,0 +1,7 @@
{
"worlds": [
"worlds/small.json",
"worlds/ice.json",
"worlds/fire.json"
]
}