Lord and Savior Johan

This commit is contained in:
2016-05-26 12:53:22 +02:00
parent 908786897f
commit 56e174ae2f
8 changed files with 642 additions and 24 deletions
+5 -2
View File
@@ -21,15 +21,18 @@ Entity::~Entity()
void Entity::draw()
{
rotation.y += 1;
if (model)
{
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);
glTranslatef(-model->center.x, 0, -model->center.z);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
@@ -48,8 +51,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;
}
+42 -12
View File
@@ -1,5 +1,6 @@
#include "HeightMap.h"
#include "stb_image.h"
#include "vector.h"
#include <GL/freeglut.h>
#include <iostream>
@@ -11,19 +12,28 @@ HeightMap::HeightMap(const std::string &file)
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) * 100.0f;
};
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);
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])*scale, h*scale, (float)(y + offsets[i][1])*scale,
normal.x, normal.y, normal.z,
(x + offsets[i][0]) / (float)height, (y + offsets[i][1]) / (float)width } );
}
}
}
@@ -48,31 +58,51 @@ 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);
//glDisable(GL_TEXTURE_2D);
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)
void HeightMap::GetHeigth(float x, float y)
{
x /= scale;
y /= scale;
int ix = x;
int iy = y;
int index = (ix + (width - 1) * iy) * 4;
Vertex& a = vertices[index];
Vertex& b = vertices[index+1];
Vertex& c = vertices[index+3];
//http://stackoverflow.com/questions/36090269/finding-height-of-point-on-height-map-triangles
}
+6 -8
View File
@@ -160,6 +160,9 @@ void Model::Optimise(ObjGroup *t)
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)
@@ -190,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)
+9
View File
@@ -80,6 +80,15 @@ bool Vec3f::operator!=(const Vec3f & other)
return x != other.x & y != other.y & z != other.z;
}
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)
+3
View File
@@ -23,6 +23,9 @@ public:
Vec3f operator / (float value);
bool operator ==(const Vec3f &other);
bool operator !=(const Vec3f &other);
Vec3f cross(const Vec3f &other);
};
class Vec2f
+6 -1
View File
@@ -3,6 +3,7 @@
#include "Entity.h"
#include "LevelObject.h"
#include "json.h"
#include "Model.h"
#include <fstream>
#include <iostream>
@@ -82,6 +83,7 @@ World::World(const std::string &fileName)
World::~World()
{
delete heightmap;
}
void World::draw()
@@ -129,7 +131,10 @@ 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;
}
}
+11 -1
View File
@@ -1,7 +1,17 @@
{
"world": {
"heightmap": "worlds/hell.png",
"texture": "worlds/helltexture.png"
"texture": "worlds/helltexture.png",
"object-templates": [
{
"color": 25,
"file": "models/boom/Boom.obj"
},
{
"color": 23,
"file": "models/boom/Boom.obj"
}
]
},
"player": {
"startposition": [ 100, 1.7, 100 ]
+560
View File
File diff suppressed because one or more lines are too long