Trying to add Synthesis's modifications.
This commit is contained in:
parent
efa7ead846
commit
0c6b7ec1c4
@ -26,6 +26,7 @@ class Chunk {
|
||||
~Chunk();
|
||||
|
||||
void draw();
|
||||
void render();
|
||||
|
||||
void deleteCube(Cube *cube);
|
||||
void addCube(Cube *selectedCube);
|
||||
@ -44,6 +45,16 @@ class Chunk {
|
||||
GLuint m_texture;
|
||||
|
||||
std::vector<Cube*> m_cubes;
|
||||
Cube *getCube(int x, int y, int z);
|
||||
|
||||
Chunk **m_surroundingChunks;
|
||||
|
||||
GLuint m_vboVertices;
|
||||
GLuint m_vboTexCoords;
|
||||
GLuint m_vboColors;
|
||||
int m_vboVertexCount;
|
||||
|
||||
void refreshVBO();
|
||||
};
|
||||
|
||||
#endif // CHUNK_H
|
||||
|
@ -30,8 +30,8 @@
|
||||
|
||||
#define VISION_ANGLE 70.0
|
||||
|
||||
#define CHUNK_WIDTH 16
|
||||
#define CHUNK_DEPTH 16
|
||||
#define CHUNK_HEIGHT 16
|
||||
#define CHUNK_WIDTH 8
|
||||
#define CHUNK_DEPTH 8
|
||||
#define CHUNK_HEIGHT 8
|
||||
|
||||
#endif // CONFIG_H
|
||||
|
@ -24,7 +24,7 @@ typedef std::map<std::string, GLuint> Textures;
|
||||
|
||||
class Cube {
|
||||
public:
|
||||
Cube(int x, int y, int z, GLuint texture);
|
||||
Cube(int x, int y, int z, GLuint texture, unsigned int type);
|
||||
~Cube() {}
|
||||
|
||||
void draw();
|
||||
@ -37,6 +37,8 @@ class Cube {
|
||||
|
||||
GLuint texture() const { return m_texture; }
|
||||
|
||||
unsigned int type() const { return m_type; }
|
||||
|
||||
bool selected() const { return m_selected; }
|
||||
s8 selectedFace() const { return m_selectedFace; }
|
||||
|
||||
@ -47,6 +49,8 @@ class Cube {
|
||||
|
||||
GLuint m_texture;
|
||||
|
||||
unsigned int m_type;
|
||||
|
||||
bool m_selected;
|
||||
s8 m_selectedFace;
|
||||
};
|
||||
|
166
source/chunk.cpp
166
source/chunk.cpp
@ -23,8 +23,7 @@
|
||||
#include <cmath>
|
||||
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "config.h"
|
||||
@ -44,14 +43,28 @@ Chunk::Chunk(int x, int y, int z, Textures textures) {
|
||||
m_z = z;
|
||||
|
||||
m_texture = textures["stone"];
|
||||
|
||||
m_surroundingChunks = new Chunk*[6];
|
||||
for (int i = 0; i < 6; i++) m_surroundingChunks[i] = NULL;
|
||||
|
||||
m_vboVertices = 0; glGenBuffers(1, &m_vboVertices);
|
||||
m_vboTexCoords = 0; glGenBuffers(1, &m_vboTexCoords);
|
||||
m_vboColors = 0; glGenBuffers(1, &m_vboColors);
|
||||
m_vboVertexCount = 0;
|
||||
}
|
||||
|
||||
Chunk::~Chunk() {
|
||||
glDeleteBuffers(1, &m_vboVertices);
|
||||
glDeleteBuffers(1, &m_vboTexCoords);
|
||||
glDeleteBuffers(1, &m_vboColors);
|
||||
|
||||
for(std::vector<Cube*>::iterator it = m_cubes.begin() ; it != m_cubes.end() ; it++) {
|
||||
delete (*it);
|
||||
}
|
||||
|
||||
m_cubes.clear();
|
||||
|
||||
delete[] m_surroundingChunks;
|
||||
}
|
||||
|
||||
void Chunk::draw() {
|
||||
@ -74,38 +87,173 @@ void Chunk::deleteCube(Cube *cube) {
|
||||
}
|
||||
|
||||
void Chunk::addCube(Cube *selectedCube) {
|
||||
unsigned int type = 2;
|
||||
if(selectedCube->selectedFace() == -1) {
|
||||
m_cubes.push_back(new Cube(selectedCube->x(), selectedCube->y(), selectedCube->z(), selectedCube->texture()));
|
||||
m_cubes.push_back(new Cube(selectedCube->x(), selectedCube->y(), selectedCube->z(), selectedCube->texture(), type));
|
||||
}
|
||||
else if(selectedCube->selectedFace() == 0) {
|
||||
if((MAP_POS(selectedCube->x(), selectedCube->y() + 1, selectedCube->z()) >= 0) && (MAP_POS(selectedCube->x(), selectedCube->y() + 1, selectedCube->z()) < Game::map->width() * Game::map->depth() * Game::map->height()))
|
||||
Game::map->map()[MAP_POS(selectedCube->x(), selectedCube->y() + 1, selectedCube->z())] = 1;
|
||||
m_cubes.push_back(new Cube(selectedCube->x(), selectedCube->y() + 1, selectedCube->z(), m_texture));
|
||||
m_cubes.push_back(new Cube(selectedCube->x(), selectedCube->y() + 1, selectedCube->z(), m_texture, type));
|
||||
}
|
||||
else if(selectedCube->selectedFace() == 1) {
|
||||
if((MAP_POS(selectedCube->x() + 1, selectedCube->y(), selectedCube->z()) >= 0) && (MAP_POS(selectedCube->x() + 1, selectedCube->y(), selectedCube->z()) < Game::map->width() * Game::map->depth() * Game::map->height()))
|
||||
Game::map->map()[MAP_POS(selectedCube->x() + 1, selectedCube->y(), selectedCube->z())] = 1;
|
||||
m_cubes.push_back(new Cube(selectedCube->x() + 1, selectedCube->y(), selectedCube->z(), m_texture));
|
||||
m_cubes.push_back(new Cube(selectedCube->x() + 1, selectedCube->y(), selectedCube->z(), m_texture, type));
|
||||
}
|
||||
else if(selectedCube->selectedFace() == 2) {
|
||||
if((MAP_POS(selectedCube->x() - 1, selectedCube->y(), selectedCube->z()) >= 0) && (MAP_POS(selectedCube->x() - 1, selectedCube->y(), selectedCube->z()) < Game::map->width() * Game::map->depth() * Game::map->height()))
|
||||
Game::map->map()[MAP_POS(selectedCube->x() - 1, selectedCube->y(), selectedCube->z())] = 1;
|
||||
m_cubes.push_back(new Cube(selectedCube->x() - 1, selectedCube->y(), selectedCube->z(), m_texture));
|
||||
m_cubes.push_back(new Cube(selectedCube->x() - 1, selectedCube->y(), selectedCube->z(), m_texture, type));
|
||||
}
|
||||
else if(selectedCube->selectedFace() == 3) {
|
||||
if((MAP_POS(selectedCube->x(), selectedCube->y() - 1, selectedCube->z()) >= 0) && (MAP_POS(selectedCube->x(), selectedCube->y() - 1, selectedCube->z()) < Game::map->width() * Game::map->depth() * Game::map->height()))
|
||||
Game::map->map()[MAP_POS(selectedCube->x(), selectedCube->y() - 1, selectedCube->z())] = 1;
|
||||
m_cubes.push_back(new Cube(selectedCube->x(), selectedCube->y() - 1, selectedCube->z(), m_texture));
|
||||
m_cubes.push_back(new Cube(selectedCube->x(), selectedCube->y() - 1, selectedCube->z(), m_texture, type));
|
||||
}
|
||||
else if(selectedCube->selectedFace() == 4) {
|
||||
if((MAP_POS(selectedCube->x(), selectedCube->y(), selectedCube->z() + 1) >= 0) && (MAP_POS(selectedCube->x(), selectedCube->y(), selectedCube->z() + 1) < Game::map->width() * Game::map->depth() * Game::map->height()))
|
||||
Game::map->map()[MAP_POS(selectedCube->x(), selectedCube->y(), selectedCube->z() + 1)] = 1;
|
||||
m_cubes.push_back(new Cube(selectedCube->x(), selectedCube->y(), selectedCube->z() + 1, m_texture));
|
||||
m_cubes.push_back(new Cube(selectedCube->x(), selectedCube->y(), selectedCube->z() + 1, m_texture, type));
|
||||
}
|
||||
else if(selectedCube->selectedFace() == 5) {
|
||||
if((MAP_POS(selectedCube->x(), selectedCube->y(), selectedCube->z() - 1) >= 0) && (MAP_POS(selectedCube->x(), selectedCube->y(), selectedCube->z() - 1) < Game::map->width() * Game::map->depth() * Game::map->height()))
|
||||
Game::map->map()[MAP_POS(selectedCube->x(), selectedCube->y(), selectedCube->z() - 1)] = 1;
|
||||
if(selectedCube->z() - 1 >= 0) m_cubes.push_back(new Cube(selectedCube->x(), selectedCube->y(), selectedCube->z() - 1, m_texture));
|
||||
if(selectedCube->z() - 1 >= 0) m_cubes.push_back(new Cube(selectedCube->x(), selectedCube->y(), selectedCube->z() - 1, m_texture, type));
|
||||
}
|
||||
}
|
||||
|
||||
Cube* Chunk::getCube(int x, int y, int z) {
|
||||
if ((x < 0) || (x >= CHUNK_WIDTH) || (y < 0) || (y >= CHUNK_DEPTH) || (z < 0) || (z >= CHUNK_HEIGHT)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*Coordinates c = Coordinates(x, y, z);
|
||||
t_cube* cube = m_cubes[c];
|
||||
if (cube == NULL) {
|
||||
m_cubes.erase(c);
|
||||
return NULL;
|
||||
}
|
||||
else return cube;*/
|
||||
|
||||
for(std::vector<Cube*>::iterator it = m_cubes.begin() ; it != m_cubes.end() ; it++) {
|
||||
if(((*it)->x() == x) && ((*it)->y() == y) && ((*it)->z() == z)) {
|
||||
return (*it);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
float getTexOffsetU(int type) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
float getTexOffsetV(int type) {
|
||||
return 0.875;
|
||||
}
|
||||
|
||||
void Chunk::refreshVBO() {
|
||||
m_vboVertexCount = 0;
|
||||
if (m_cubes.size() == 0) return;
|
||||
|
||||
float cubeCoords[6 * 12] = {
|
||||
0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1,
|
||||
1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1,
|
||||
0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1,
|
||||
1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1,
|
||||
0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1,
|
||||
1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0
|
||||
};
|
||||
|
||||
unsigned char grey[6] = {159, 191, 127, 223, 255, 95};
|
||||
|
||||
std::vector<float> vertices = std::vector<float>();
|
||||
std::vector<float> texCoords = std::vector<float>();
|
||||
std::vector<unsigned char> colors = std::vector<unsigned char>();
|
||||
|
||||
for (int qc = 0 ; qc < CHUNK_WIDTH * CHUNK_DEPTH * CHUNK_HEIGHT ; qc++) {
|
||||
int x = qc % CHUNK_WIDTH;
|
||||
int y = (qc / CHUNK_WIDTH) % CHUNK_DEPTH;
|
||||
int z = (qc / CHUNK_WIDTH) / CHUNK_DEPTH;
|
||||
Cube* qe = getCube(x, y, z);
|
||||
if (qe == NULL) continue;
|
||||
Cube* cube = NULL;
|
||||
|
||||
int coords[6 * 3] = {
|
||||
x-1, y, z,
|
||||
x+1, y, z,
|
||||
x, y-1, z,
|
||||
x, y+1, z,
|
||||
x, y, z+1,
|
||||
x, y, z-1
|
||||
};
|
||||
|
||||
for (int i = 0 ; i < 6 ; i++) {
|
||||
cube = getCube(coords[i*3], coords[i*3 + 1], coords[i*3 + 2]);
|
||||
|
||||
if ((coords[i*3] < 0) && (m_surroundingChunks[0] != NULL)) {
|
||||
cube = m_surroundingChunks[0]->getCube(coords[i*3] + CHUNK_WIDTH, coords[i*3 + 1], coords[i*3 + 2]);
|
||||
}
|
||||
else if ((coords[i*3] >= CHUNK_WIDTH) && (m_surroundingChunks[1] != NULL)) {
|
||||
cube = m_surroundingChunks[1]->getCube(coords[i*3] - CHUNK_WIDTH, coords[i*3 + 1], coords[i*3 + 2]);
|
||||
}
|
||||
|
||||
if ((coords[i*3 + 1] < 0) && (m_surroundingChunks[2] != NULL)) {
|
||||
cube = m_surroundingChunks[2]->getCube(coords[i*3], coords[i*3 + 1] + CHUNK_DEPTH, coords[i*3 + 2]);
|
||||
}
|
||||
else if ((coords[i*3 + 1] >= CHUNK_DEPTH) && (m_surroundingChunks[3] != NULL)) {
|
||||
cube = m_surroundingChunks[3]->getCube(coords[i*3], coords[i*3 + 1] - CHUNK_DEPTH, coords[i*3 + 2]);
|
||||
}
|
||||
|
||||
if ((coords[i*3 + 2] >= CHUNK_HEIGHT) && (m_surroundingChunks[4] != NULL)) {
|
||||
cube = m_surroundingChunks[4]->getCube(coords[i*3], coords[i*3 + 1], coords[i*3 + 2] - CHUNK_HEIGHT);
|
||||
}
|
||||
else if ((coords[i*3 + 2] < 0) && (m_surroundingChunks[5] != NULL)) {
|
||||
cube = m_surroundingChunks[5]->getCube(coords[i*3], coords[i*3 + 1], coords[i*3 + 2] + CHUNK_HEIGHT);
|
||||
}
|
||||
if ((cube == NULL) || (cube->type() == 0)) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
vertices.push_back(x + cubeCoords[(i * 12) + (j * 3)]);
|
||||
vertices.push_back(y + cubeCoords[(i * 12) + (j * 3) + 1]);
|
||||
vertices.push_back(z + cubeCoords[(i * 12) + (j * 3) + 2]);
|
||||
texCoords.push_back(getTexOffsetU(qe->type()) + (cubeCoords[48 + (j * 3)] * 0.125));
|
||||
texCoords.push_back(getTexOffsetV(qe->type()) + (cubeCoords[48 + (j * 3) + 1] * 0.125));
|
||||
colors.push_back(grey[i]);
|
||||
colors.push_back(grey[i]);
|
||||
colors.push_back(grey[i]);
|
||||
colors.push_back(255);
|
||||
|
||||
m_vboVertexCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboVertices);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_DYNAMIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboTexCoords);
|
||||
glBufferData(GL_ARRAY_BUFFER, texCoords.size() * sizeof(float), texCoords.data(), GL_DYNAMIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboColors);
|
||||
glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(unsigned char), colors.data(), GL_DYNAMIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
vertices.clear();
|
||||
texCoords.clear();
|
||||
colors.clear();
|
||||
}
|
||||
|
||||
void Chunk::render() {
|
||||
refreshVBO();
|
||||
|
||||
if(m_vboVertexCount == 0) return;
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboVertices);
|
||||
glVertexPointer(3, GL_FLOAT, 0, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboTexCoords);
|
||||
glTexCoordPointer(2, GL_FLOAT, 0, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vboColors);
|
||||
glColorPointer(4, GL_UNSIGNED_BYTE, 0, 0);
|
||||
glDrawArrays(GL_QUADS, 0, m_vboVertexCount);
|
||||
}
|
||||
|
||||
|
@ -36,13 +36,15 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
Cube::Cube(int x, int y, int z, GLuint texture) {
|
||||
Cube::Cube(int x, int y, int z, GLuint texture, unsigned int type) {
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
m_z = z;
|
||||
|
||||
m_texture = texture;
|
||||
|
||||
m_type = type;
|
||||
|
||||
m_selected = false;
|
||||
m_selectedFace = -1;
|
||||
}
|
||||
|
@ -109,13 +109,16 @@ void Game::manageEvents() {
|
||||
if(event.key.keysym.sym == SDLK_ESCAPE) {
|
||||
m_cont = false;
|
||||
}
|
||||
if(event.key.keysym.sym == SDLK_BACKSPACE) {
|
||||
unlockMouse();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
if(event.button.button == SDL_BUTTON_LEFT) {
|
||||
// To fix for multiples chunks
|
||||
Map::currentChunk->deleteCube(Map::selectedCube);
|
||||
Map::selectedCube = new Cube(-1, -1, -1, 0);
|
||||
Map::selectedCube = new Cube(-1, -1, -1, 0, 0);
|
||||
}
|
||||
if(event.button.button == SDL_BUTTON_RIGHT) {
|
||||
Map::currentChunk->addCube(Map::selectedCube);
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include <cmath>
|
||||
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/glew.h>
|
||||
#include <GL/glfw.h>
|
||||
|
||||
#include "sdlglutils.h"
|
||||
@ -45,6 +46,12 @@ int main(int argc, char *argv[]) {
|
||||
initSDL();
|
||||
initOpenGL();
|
||||
|
||||
GLenum err = glewInit();
|
||||
if(err != GLEW_OK) {
|
||||
cerr << "Error: " << glewGetErrorString(err) << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Game execution
|
||||
Game game;
|
||||
game.exec();
|
||||
|
@ -79,13 +79,13 @@ Map::Map(u16 width, u16 depth, u16 height, Textures textures) {
|
||||
for(s32 x = (*it)->x() ; x < (*it)->x() + CHUNK_WIDTH ; x++) {
|
||||
switch(m_map[_MAP_POS(x, y, z)]) {
|
||||
case 1:
|
||||
(*it)->addCube(new Cube(x, y, z, m_textures["grass"]));
|
||||
(*it)->addCube(new Cube(x, y, z, m_textures["grass"], 1));
|
||||
break;
|
||||
case 2:
|
||||
(*it)->addCube(new Cube(x, y, z, m_textures["stone"]));
|
||||
(*it)->addCube(new Cube(x, y, z, m_textures["stone"], 2));
|
||||
break;
|
||||
case 3:
|
||||
(*it)->addCube(new Cube(x, y, z, m_textures["bedrock"]));
|
||||
(*it)->addCube(new Cube(x, y, z, m_textures["bedrock"], 3));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -95,7 +95,7 @@ Map::Map(u16 width, u16 depth, u16 height, Textures textures) {
|
||||
|
||||
currentChunk = findNearestChunk(Game::player->x(), Game::player->y(), Game::player->z());
|
||||
|
||||
selectedCube = new Cube(-1, -1, -1, m_textures["dirt"]);
|
||||
selectedCube = new Cube(-1, -1, -1, m_textures["dirt"], 0);
|
||||
}
|
||||
|
||||
Map::~Map() {
|
||||
@ -111,7 +111,8 @@ void Map::draw() {
|
||||
currentChunk = findNearestChunk(Game::player->x(), Game::player->y(), Game::player->z());
|
||||
|
||||
for(vector<Chunk*>::iterator it = m_chunks.begin() ; it != m_chunks.end() ; it++) {
|
||||
(*it)->draw();
|
||||
// (*it)->draw();
|
||||
(*it)->render();
|
||||
}
|
||||
|
||||
testCubes(currentChunk->cubes());
|
||||
|
Loading…
x
Reference in New Issue
Block a user