Merge pull request #5 from CrystalPointA4/heightmap
Heightmap implemented
This commit is contained in:
+8
-11
@@ -29,7 +29,7 @@ void CrystalJohan::draw()
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
gluPerspective(70, width / (float)height, 0.1f, 100);
|
||||
gluPerspective(70, width / (float)height, 0.1f, 15000);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
|
||||
@@ -52,11 +52,7 @@ void CrystalJohan::draw()
|
||||
|
||||
glEnd();
|
||||
|
||||
|
||||
|
||||
glutSwapBuffers();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -78,14 +74,15 @@ void CrystalJohan::update()
|
||||
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, false);
|
||||
if (keyboardState.keys['d']) world->player.setPosition(180, deltaTime, false);
|
||||
if (keyboardState.keys['w']) world->player.setPosition(90, deltaTime, false);
|
||||
if (keyboardState.keys['s']) world->player.setPosition(270, deltaTime, false);
|
||||
if (keyboardState.keys['q']) world->player.setPosition(1, deltaTime, true);
|
||||
if (keyboardState.keys['e']) world->player.setPosition(-1, deltaTime, true);
|
||||
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;
|
||||
|
||||
|
||||
@@ -156,6 +156,7 @@
|
||||
<ClCompile Include="CrystalJohan.cpp" />
|
||||
<ClCompile Include="Enemy.cpp" />
|
||||
<ClCompile Include="Entity.cpp" />
|
||||
<ClCompile Include="HeightMap.cpp" />
|
||||
<ClCompile Include="json.cpp" />
|
||||
<ClCompile Include="LevelObject.cpp" />
|
||||
<ClCompile Include="Main.cpp" />
|
||||
@@ -168,6 +169,7 @@
|
||||
<ClInclude Include="CrystalJohan.h" />
|
||||
<ClInclude Include="Enemy.h" />
|
||||
<ClInclude Include="Entity.h" />
|
||||
<ClInclude Include="HeightMap.h" />
|
||||
<ClInclude Include="json.h" />
|
||||
<ClInclude Include="LevelObject.h" />
|
||||
<ClInclude Include="Main.h" />
|
||||
|
||||
@@ -48,6 +48,9 @@
|
||||
<ClCompile Include="json.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="HeightMap.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CrystalJohan.h">
|
||||
@@ -86,5 +89,8 @@
|
||||
<ClInclude Include="json.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="HeightMap.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,99 @@
|
||||
#include "HeightMap.h"
|
||||
#include "stb_image.h"
|
||||
|
||||
#include <GL/freeglut.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
|
||||
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++)
|
||||
{
|
||||
for (int w = 0; w < width; w++)
|
||||
{
|
||||
int offsets[4][2] = { { 0, 0 },{ 1, 0 },{ 1, 1 },{ 0, 1 } };
|
||||
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 } );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glGenTextures(1, &imageIndex);
|
||||
glBindTexture(GL_TEXTURE_2D, imageIndex);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D,
|
||||
0, //level
|
||||
GL_RGBA, //internal format
|
||||
width, //width
|
||||
height, //height
|
||||
0, //border
|
||||
GL_RGBA, //data format
|
||||
GL_UNSIGNED_BYTE, //data type
|
||||
imgData); //data
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
stbi_image_free(imgData);
|
||||
}
|
||||
|
||||
HeightMap::~HeightMap()
|
||||
{
|
||||
}
|
||||
|
||||
void HeightMap::Draw()
|
||||
{
|
||||
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);
|
||||
|
||||
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);
|
||||
glDrawArrays(GL_QUADS, 0, vertices.size());
|
||||
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
//glDisableClientState(GL_COLOR_ARRAY);
|
||||
//glDisableClientState(GL_NORMAL_ARRAY);
|
||||
}
|
||||
|
||||
void HeightMap::GetHeigth(float x, float z)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void HeightMap::SetTexture(const std::string &file)
|
||||
{
|
||||
int bpp, width2, height2;
|
||||
unsigned char* imgData = stbi_load(file.c_str(), &width2, &height2, &bpp, 4);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, imageIndex);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D,
|
||||
0, //level
|
||||
GL_RGBA, //internal format
|
||||
width2, //width
|
||||
height2, //height
|
||||
0, //border
|
||||
GL_RGBA, //data format
|
||||
GL_UNSIGNED_BYTE, //data type
|
||||
imgData); //data
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
stbi_image_free(imgData);
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <GL/freeglut.h>
|
||||
|
||||
class HeightMap
|
||||
{
|
||||
private:
|
||||
int height;
|
||||
int width;
|
||||
|
||||
GLuint imageIndex;
|
||||
int scale = 1;
|
||||
public:
|
||||
HeightMap(const std::string &file);
|
||||
~HeightMap();
|
||||
|
||||
void Draw();
|
||||
void GetHeigth(float x, float z);
|
||||
void SetTexture(const std::string &file);
|
||||
|
||||
struct Vertex {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
|
||||
float normalX;
|
||||
float normalY;
|
||||
float normalZ;
|
||||
|
||||
float texX;
|
||||
float texY;
|
||||
};
|
||||
|
||||
std::vector<Vertex> vertices;
|
||||
};
|
||||
|
||||
@@ -11,9 +11,13 @@ World::World() : player(Player::getInstance())
|
||||
std::ifstream file("worlds/world1.json");
|
||||
if(!file.is_open())
|
||||
std::cout<<"Uhoh, can't open file\n";
|
||||
|
||||
json::Value v = json::readJson(file);
|
||||
std::cout<<v;
|
||||
file.close();
|
||||
|
||||
heightmap = new HeightMap(v["world"]["heightmap"].asString());
|
||||
heightmap->SetTexture(v["world"]["texture"].asString());
|
||||
|
||||
player.position.x = v["player"]["startposition"][0];
|
||||
player.position.y = v["player"]["startposition"][1];
|
||||
player.position.z = v["player"]["startposition"][2];
|
||||
@@ -51,14 +55,8 @@ void World::draw()
|
||||
float lightAmbient[4] = { 0.5, 0.5, 0.5, 1 };
|
||||
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
|
||||
|
||||
glColor3f(0.5f, 0.9f, 0.5f);
|
||||
glNormal3f(0, 1, 0);
|
||||
glBegin(GL_QUADS);
|
||||
glVertex3f(-50, 0, -50);
|
||||
glVertex3f(-50, 0, 50);
|
||||
glVertex3f(50, 0, 50);
|
||||
glVertex3f(50, 0, -50);
|
||||
glEnd();
|
||||
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
heightmap->Draw();
|
||||
|
||||
for (auto e : entities)
|
||||
e->draw();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "HeightMap.h"
|
||||
#include "Player.h"
|
||||
|
||||
class Entity;
|
||||
@@ -14,6 +15,7 @@ public:
|
||||
|
||||
Player& player;
|
||||
std::vector<Entity*> entities;
|
||||
HeightMap* heightmap;
|
||||
|
||||
void draw();
|
||||
void update(float elapsedTime);
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 257 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.3 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 130 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 629 KiB |
+6
-3
@@ -1,12 +1,15 @@
|
||||
{
|
||||
"world": {
|
||||
"heightmap": "worlds/hmcs.png",
|
||||
"texture": "worlds/hmcstexture.png"
|
||||
},
|
||||
"player": {
|
||||
"startposition": [ 0, 1.7, 0 ]
|
||||
"startposition": [ -100, 1.7, -100 ]
|
||||
},
|
||||
"objects": [
|
||||
{
|
||||
"file": "models/boom/Boom.obj",
|
||||
"pos": [ 0, 0, -4 ],
|
||||
"rot": [ 0, 20, 0 ]
|
||||
"pos": [ 0, 0, -4 ]
|
||||
},
|
||||
{
|
||||
"file": "models/boom/Boom.obj",
|
||||
|
||||
Reference in New Issue
Block a user