Seperated world generation & rendering from main()
parent
0825c03752
commit
346eead47a
|
@ -17,6 +17,7 @@ set(src
|
|||
${src_root}/q3renderer.cc
|
||||
${src_root}/q3window.cc
|
||||
${src_root}/q3texture.cc
|
||||
${src_root}/q3world.cc
|
||||
|
||||
${libs_root}/lodepng.cc
|
||||
)
|
||||
|
@ -30,6 +31,7 @@ set(headers
|
|||
${headers_root}/q3window.hpp
|
||||
${headers_root}/q3shared_constants.hpp
|
||||
${headers_root}/q3texture.hpp
|
||||
${headers_root}/q3world.hpp
|
||||
|
||||
${libs_root}/lodepng/lodepng.h
|
||||
)
|
||||
|
|
|
@ -8,7 +8,7 @@ class Q3Renderer
|
|||
Q3Renderer();
|
||||
|
||||
void clear();
|
||||
void drawCube(float x, float y, float z, float sx, float sy, float sz, glm::vec3 topCol);
|
||||
void drawCube(float x, float y, float z, float sx, float sy, float sz);
|
||||
|
||||
void handleCamera(Q3Camera *camera);
|
||||
|
||||
|
|
|
@ -16,4 +16,6 @@
|
|||
#define Q3_FOV 45.f
|
||||
|
||||
#define Q3_PIF 3.14159265359f
|
||||
#define Q3_PID 3.14159265359
|
||||
#define Q3_PID 3.14159265359
|
||||
|
||||
#define Q3_MAPSIZE 50
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include <qub3d/q3shared_constants.hpp>
|
||||
#include <qub3d/q3renderer.hpp>
|
||||
|
||||
struct Q3Block {
|
||||
glm::vec3 position;
|
||||
};
|
||||
|
||||
class Q3World {
|
||||
public:
|
||||
void generate();
|
||||
|
||||
Q3Block *getBlocks() { return m_blocks; }
|
||||
private:
|
||||
Q3Block m_blocks[Q3_MAPSIZE * Q3_MAPSIZE];
|
||||
};
|
||||
|
||||
class Q3WorldRenderer {
|
||||
public:
|
||||
void render(Q3World *world, Q3Renderer *renderer);
|
||||
};
|
|
@ -3,16 +3,11 @@
|
|||
#include <qub3d/q3renderer.hpp>
|
||||
#include <qub3d/q3camera.hpp>
|
||||
#include <qub3d/q3perlin.hpp>
|
||||
#include <qub3d/q3world.hpp>
|
||||
#undef main
|
||||
|
||||
#include <time.h>
|
||||
#include <iostream>
|
||||
#include <qub3d/q3opengl.hpp>
|
||||
struct Q3Cube
|
||||
{
|
||||
glm::vec3 position;
|
||||
glm::vec3 color;
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
@ -24,23 +19,10 @@ int main(int argc, char **argv)
|
|||
|
||||
Q3Camera camera(window.getWindow());
|
||||
Q3Renderer renderer;
|
||||
Q3PerlinNoiseGenerator perlin_noise_generator;
|
||||
|
||||
const int mapsize = 50;
|
||||
Q3Cube terrain[mapsize * mapsize];
|
||||
for (int z = 0; z < mapsize; ++z)
|
||||
{
|
||||
for (int x = 0; x < mapsize; ++x)
|
||||
{
|
||||
Q3Cube &r = terrain[x + z * mapsize];
|
||||
r.position.x = std::round(+(x * 2.f));
|
||||
float pNorm = perlin_noise_generator.perlin(glm::vec3((x * 2) / 10.f, (z * 2) / 10.0f, 0.f));
|
||||
pNorm *= 15.f;
|
||||
r.position.y = (int)(pNorm / 2) * 2;
|
||||
r.position.z = std::round(+(z * 2.f));
|
||||
r.color = glm::vec3(127.f / 255.f, 214.f / 255.f, 145.f / 255.f);
|
||||
}
|
||||
}
|
||||
|
||||
Q3World world;
|
||||
world.generate();
|
||||
Q3WorldRenderer world_renderer;
|
||||
|
||||
Uint32 last_time = SDL_GetTicks();
|
||||
while (window.isRunning())
|
||||
|
@ -59,11 +41,8 @@ int main(int argc, char **argv)
|
|||
|
||||
renderer.start3d();
|
||||
renderer.handleCamera(&camera);
|
||||
for (int i = 0; i < mapsize * mapsize; ++i)
|
||||
{
|
||||
Q3Cube p = terrain[i];
|
||||
renderer.drawCube(p.position.x, p.position.y, p.position.z, 1, 1, 1, p.color);
|
||||
}
|
||||
world_renderer.render(&world, &renderer);
|
||||
|
||||
renderer.end3d();
|
||||
|
||||
window.swapBuffers();
|
||||
|
|
|
@ -87,7 +87,7 @@ void Q3Renderer::drawRect(float x, float y, float w, float h, glm::vec3 col)
|
|||
glEnd();
|
||||
}
|
||||
|
||||
void Q3Renderer::drawCube(float x, float y, float z, float sx, float sy, float sz, glm::vec3 topCol)
|
||||
void Q3Renderer::drawCube(float x, float y, float z, float sx, float sy, float sz)
|
||||
{
|
||||
glPushMatrix();
|
||||
glTranslatef(x, y, z);
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
#include "..\include\qub3d\q3world.hpp"
|
||||
|
||||
#include <qub3d/q3perlin.hpp>
|
||||
|
||||
void Q3World::generate()
|
||||
{
|
||||
memset(m_blocks, 0, Q3_MAPSIZE * Q3_MAPSIZE);
|
||||
Q3PerlinNoiseGenerator perlin;
|
||||
for (int z = 0; z < Q3_MAPSIZE; ++z) {
|
||||
for (int x = 0; x < Q3_MAPSIZE; ++x) {
|
||||
Q3Block *block = &(m_blocks[x + z * Q3_MAPSIZE]);
|
||||
block->position.x = std::round(+(x * 2.f));
|
||||
block->position.z = std::round(+(z * 2.f));
|
||||
|
||||
float normalized_perlin = perlin.perlin(glm::vec3((x * 2.f) / 10.f, (z * 2.f) / 10.f, 0.f));
|
||||
float scaled_perlin = normalized_perlin * 15.f;
|
||||
float rounded_scaled_perlin = static_cast<float>((int)(scaled_perlin / 2) * 2);
|
||||
|
||||
block->position.y = rounded_scaled_perlin;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Q3WorldRenderer::render(Q3World * world, Q3Renderer *renderer)
|
||||
{
|
||||
Q3Block *blocks = world->getBlocks();
|
||||
for (int i = 0; i < Q3_MAPSIZE * Q3_MAPSIZE; ++i)
|
||||
{
|
||||
Q3Block *block = &(blocks[i]);
|
||||
const glm::vec3& block_pos = block->position;
|
||||
|
||||
renderer->drawCube(block_pos.x, block_pos.y, block_pos.z, 1, 1, 1);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue