Added blocks
This commit is contained in:
+111
-4
@@ -1,12 +1,119 @@
|
|||||||
#include "Block.h"
|
#include "Block.h"
|
||||||
|
#include <GL/freeglut.h>
|
||||||
|
|
||||||
|
Block::Block(Vec3f position, int textureTop, int textureSides)
|
||||||
|
|
||||||
Block::Block()
|
|
||||||
{
|
{
|
||||||
}
|
this->position = position;
|
||||||
|
|
||||||
|
int rowNum = textureSides / 16;
|
||||||
|
int columnNum = textureSides % 16;
|
||||||
|
|
||||||
|
float part = (float)1 / 16;
|
||||||
|
|
||||||
|
float row = rowNum * part;
|
||||||
|
float column = columnNum * part;
|
||||||
|
float rowEnd = row + part;
|
||||||
|
float columnEnd = column + part;
|
||||||
|
|
||||||
|
texColsSides = Vec2f(column, columnEnd);
|
||||||
|
texRowsSides = Vec2f(row, rowEnd);
|
||||||
|
|
||||||
|
if (textureTop == textureSides)
|
||||||
|
{
|
||||||
|
texColsTop = texColsSides;
|
||||||
|
texRowsTop = texRowsSides;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (textureTop != -1)
|
||||||
|
{
|
||||||
|
hasTopTexture = true;
|
||||||
|
|
||||||
|
int rowNum = textureTop / 16;
|
||||||
|
int columnNum = textureTop% 16;
|
||||||
|
|
||||||
|
float part = (float)1 / 16;
|
||||||
|
|
||||||
|
row = rowNum * part;
|
||||||
|
column = columnNum * part;
|
||||||
|
rowEnd = row + part;
|
||||||
|
columnEnd = column + part;
|
||||||
|
|
||||||
|
texColsTop = Vec2f(column, columnEnd);
|
||||||
|
texRowsTop = Vec2f(row, rowEnd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Block::~Block()
|
Block::~Block()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Block::draw()
|
||||||
|
{
|
||||||
|
glPushMatrix();
|
||||||
|
|
||||||
|
glTranslatef(position.x, position.y, position.z);
|
||||||
|
|
||||||
|
glBegin(GL_QUADS);
|
||||||
|
|
||||||
|
glColor4f(1.0, 1.0, 1.0, 1.0);
|
||||||
|
|
||||||
|
float column = texColsSides.x;
|
||||||
|
float columnEnd = texColsSides.y;
|
||||||
|
|
||||||
|
float row = texRowsSides.x;
|
||||||
|
float rowEnd = texRowsSides.y;
|
||||||
|
|
||||||
|
//Side size
|
||||||
|
glTexCoord2f(column, rowEnd); glVertex3f(0, 0, 0); //Linksonder
|
||||||
|
glTexCoord2f(columnEnd, rowEnd); glVertex3f(size, 0, 0); //Rechtsonder
|
||||||
|
glTexCoord2f(columnEnd, row); glVertex3f(size, size, 0); //Rechtsboven
|
||||||
|
glTexCoord2f(column, row); glVertex3f(0, size, 0); //Linksboven
|
||||||
|
|
||||||
|
//Side 2
|
||||||
|
glTexCoord2f(column, rowEnd); glVertex3f(0, 0, size);
|
||||||
|
glTexCoord2f(columnEnd, rowEnd); glVertex3f(size, 0, size);
|
||||||
|
glTexCoord2f(columnEnd, row); glVertex3f(size, size, size);
|
||||||
|
glTexCoord2f(column, row); glVertex3f(0, size, size);
|
||||||
|
|
||||||
|
//Side 3
|
||||||
|
glTexCoord2f(column, rowEnd); glVertex3f(0, 0, 0);
|
||||||
|
glTexCoord2f(column, row); glVertex3f(0, size, 0);
|
||||||
|
glTexCoord2f(columnEnd, row); glVertex3f(0, size, size);
|
||||||
|
glTexCoord2f(columnEnd, rowEnd); glVertex3f(0, 0, size);
|
||||||
|
|
||||||
|
//Side 4
|
||||||
|
glTexCoord2f(column, rowEnd); glVertex3f(size, 0, 0);
|
||||||
|
glTexCoord2f(column, row); glVertex3f(size, size, 0);
|
||||||
|
glTexCoord2f(columnEnd, row); glVertex3f(size, size, size);
|
||||||
|
glTexCoord2f(columnEnd, rowEnd); glVertex3f(size, 0, size);
|
||||||
|
|
||||||
|
if (!hasTopTexture)
|
||||||
|
{
|
||||||
|
glEnd();
|
||||||
|
glPopMatrix();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Different textures
|
||||||
|
column = texColsTop.x;
|
||||||
|
columnEnd = texColsTop.y;
|
||||||
|
|
||||||
|
row = texRowsTop.x;
|
||||||
|
rowEnd = texRowsTop.y;
|
||||||
|
|
||||||
|
//Bottom
|
||||||
|
glTexCoord2f(column, row); glVertex3f(0, 0, 0);
|
||||||
|
glTexCoord2f(column, rowEnd); glVertex3f(size, 0, 0);
|
||||||
|
glTexCoord2f(columnEnd, rowEnd); glVertex3f(size, 0, size);
|
||||||
|
glTexCoord2f(columnEnd, row); glVertex3f(0, 0, size);
|
||||||
|
|
||||||
|
//Top
|
||||||
|
glTexCoord2f(column, row); glVertex3f(0, size, 0);
|
||||||
|
glTexCoord2f(column, rowEnd); glVertex3f(size, size, 0);
|
||||||
|
glTexCoord2f(columnEnd, rowEnd); glVertex3f(size, size, size);
|
||||||
|
glTexCoord2f(columnEnd, row); glVertex3f(0, size, size);
|
||||||
|
glEnd();
|
||||||
|
glPopMatrix();
|
||||||
|
}
|
||||||
|
|||||||
+16
-1
@@ -1,8 +1,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Vector.h"
|
||||||
|
|
||||||
class Block
|
class Block
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
float size = 1;
|
||||||
|
bool hasTopTexture = false;
|
||||||
|
|
||||||
|
Vec2f texColsTop;
|
||||||
|
Vec2f texRowsTop;
|
||||||
|
Vec2f texColsSides;
|
||||||
|
Vec2f texRowsSides;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Block();
|
Block(Vec3f position, int top, int sides);
|
||||||
~Block();
|
~Block();
|
||||||
|
|
||||||
|
void draw(void);
|
||||||
|
|
||||||
|
Vec3f position;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
#include "Chunk.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Chunk::Chunk()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Chunk::~Chunk()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
class Chunk
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Chunk();
|
|
||||||
~Chunk();
|
|
||||||
};
|
|
||||||
|
|
||||||
+37
-1
@@ -1,13 +1,44 @@
|
|||||||
|
|
||||||
#include "Meinkraft.h"
|
#include "Meinkraft.h"
|
||||||
#include <GL/freeglut.h>
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
#include "StateHandler.h"
|
#include "StateHandler.h"
|
||||||
|
|
||||||
|
#define _USE_MATH_DEFINES
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
#include "stb_image.h"
|
||||||
|
#include "stb_perlin.h"
|
||||||
|
|
||||||
int Meinkraft::width = 0;
|
int Meinkraft::width = 0;
|
||||||
int Meinkraft::height = 0;
|
int Meinkraft::height = 0;
|
||||||
|
GLuint Meinkraft::texture = NULL;
|
||||||
|
|
||||||
|
void Meinkraft::loadTexture(void)
|
||||||
|
{
|
||||||
|
//Load Textures :: blocks
|
||||||
|
int img_width, img_height, bpp;
|
||||||
|
unsigned char* imgData = stbi_load("/resources/terrain.png", &img_width, &img_height, &bpp, 4);
|
||||||
|
|
||||||
|
glGenTextures(1, &texture);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
|
|
||||||
|
glTexImage2D(GL_TEXTURE_2D,
|
||||||
|
0, //level
|
||||||
|
GL_RGBA, //internal format
|
||||||
|
img_width, //width
|
||||||
|
img_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);
|
||||||
|
}
|
||||||
|
|
||||||
void Meinkraft::init()
|
void Meinkraft::init()
|
||||||
{
|
{
|
||||||
@@ -15,6 +46,8 @@ void Meinkraft::init()
|
|||||||
statehandler = StateHandler::getInstance();
|
statehandler = StateHandler::getInstance();
|
||||||
//cursor = Cursor::getInstance();
|
//cursor = Cursor::getInstance();
|
||||||
|
|
||||||
|
loadTexture();
|
||||||
|
|
||||||
lastFrameTime = 0;
|
lastFrameTime = 0;
|
||||||
|
|
||||||
glClearColor(0.7f, 0.7f, 1.0f, 1.0f);
|
glClearColor(0.7f, 0.7f, 1.0f, 1.0f);
|
||||||
@@ -37,6 +70,9 @@ void Meinkraft::draw()
|
|||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
|
||||||
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
|
|
||||||
statehandler->draw();
|
statehandler->draw();
|
||||||
|
|
||||||
//cursor->draw();
|
//cursor->draw();
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
class Player;
|
class Player;
|
||||||
class StateHandler;
|
class StateHandler;
|
||||||
#include "Vector.h"
|
#include "Vector.h"
|
||||||
|
#include <GL\freeglut.h>
|
||||||
|
|
||||||
class KeyboardState
|
class KeyboardState
|
||||||
{
|
{
|
||||||
@@ -16,6 +17,8 @@ public:
|
|||||||
|
|
||||||
class Meinkraft
|
class Meinkraft
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
void loadTexture(void);
|
||||||
public:
|
public:
|
||||||
void init();
|
void init();
|
||||||
void draw();
|
void draw();
|
||||||
@@ -32,4 +35,6 @@ public:
|
|||||||
Vec2f mousePosition;
|
Vec2f mousePosition;
|
||||||
|
|
||||||
float lastFrameTime;
|
float lastFrameTime;
|
||||||
|
|
||||||
|
static GLuint texture;
|
||||||
};
|
};
|
||||||
@@ -151,7 +151,6 @@
|
|||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Block.cpp" />
|
<ClCompile Include="Block.cpp" />
|
||||||
<ClCompile Include="Chunk.cpp" />
|
|
||||||
<ClCompile Include="Meinkraft.cpp" />
|
<ClCompile Include="Meinkraft.cpp" />
|
||||||
<ClCompile Include="Entity.cpp" />
|
<ClCompile Include="Entity.cpp" />
|
||||||
<ClCompile Include="json.cpp" />
|
<ClCompile Include="json.cpp" />
|
||||||
@@ -168,7 +167,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Block.h" />
|
<ClInclude Include="Block.h" />
|
||||||
<ClInclude Include="Chunk.h" />
|
|
||||||
<ClInclude Include="Meinkraft.h" />
|
<ClInclude Include="Meinkraft.h" />
|
||||||
<ClInclude Include="Entity.h" />
|
<ClInclude Include="Entity.h" />
|
||||||
<ClInclude Include="json.h" />
|
<ClInclude Include="json.h" />
|
||||||
|
|||||||
@@ -39,9 +39,6 @@
|
|||||||
<ClCompile Include="Meinkraft.cpp">
|
<ClCompile Include="Meinkraft.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="Chunk.cpp">
|
|
||||||
<Filter>Source Files\World</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="Block.cpp">
|
<ClCompile Include="Block.cpp">
|
||||||
<Filter>Source Files\World</Filter>
|
<Filter>Source Files\World</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@@ -95,9 +92,6 @@
|
|||||||
<ClInclude Include="World.h">
|
<ClInclude Include="World.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="Chunk.h">
|
|
||||||
<Filter>Header Files</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="Block.h">
|
<ClInclude Include="Block.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ StateHandler* StateHandler::instance = nullptr;
|
|||||||
StateHandler::StateHandler()
|
StateHandler::StateHandler()
|
||||||
{
|
{
|
||||||
available = false;
|
available = false;
|
||||||
CState = MENU;
|
CState = WORLD;
|
||||||
CurrentState = new MenuState();
|
CurrentState = new WorldState();
|
||||||
CurrentState->init();
|
CurrentState->init();
|
||||||
available = true;
|
available = true;
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-1
@@ -1,5 +1,5 @@
|
|||||||
#include "World.h"
|
#include "World.h"
|
||||||
|
#include "Block.h"
|
||||||
|
|
||||||
|
|
||||||
World::World()
|
World::World()
|
||||||
@@ -10,3 +10,13 @@ World::World()
|
|||||||
World::~World()
|
World::~World()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void World::draw(void)
|
||||||
|
{
|
||||||
|
Block b = Block(Vec3f(0, 0, 0), 1, 3);
|
||||||
|
b.draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
void World::update(float deltaTime)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,5 +4,8 @@ class World
|
|||||||
public:
|
public:
|
||||||
World();
|
World();
|
||||||
~World();
|
~World();
|
||||||
|
|
||||||
|
void draw(void);
|
||||||
|
void update(float deltaTime);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -13,16 +13,20 @@ WorldState::~WorldState()
|
|||||||
|
|
||||||
void WorldState::init(void)
|
void WorldState::init(void)
|
||||||
{
|
{
|
||||||
|
world = new World();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldState::exit(void)
|
void WorldState::exit(void)
|
||||||
{
|
{
|
||||||
|
delete world;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldState::draw(void)
|
void WorldState::draw(void)
|
||||||
{
|
{
|
||||||
|
world->draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldState::update(float deltaTime)
|
void WorldState::update(float deltaTime)
|
||||||
{
|
{
|
||||||
|
world->update(deltaTime);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "State.h"
|
#include "State.h"
|
||||||
|
#include "World.h"
|
||||||
|
|
||||||
class WorldState : public State
|
class WorldState : public State
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
World* world;
|
||||||
public:
|
public:
|
||||||
WorldState();
|
WorldState();
|
||||||
~WorldState();
|
~WorldState();
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 90 KiB |
Binary file not shown.
Reference in New Issue
Block a user