Merge pull request #1 from DontBelieveMe/master

Pulling Changes
master
Gerold55 2018-07-08 19:07:49 -04:00 committed by GitHub
commit 0d7638337d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 122 additions and 50 deletions

View File

@ -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
)
@ -46,6 +48,13 @@ set(libdeps ${CMAKE_CURRENT_LIST_DIR}/../qub3d-libdeps)
include_directories(${libdeps}/glm)
include_directories(${libdeps}/SDL2/include)
add_custom_command(TARGET ${project_name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy_directory
"${CMAKE_CURRENT_SOURCE_DIR}/assets/"
"${CMAKE_CURRENT_BINARY_DIR}/assets/"
)
if(UNIX)
target_link_libraries(${project_name}
${libdeps}/SDL2/build/libSDL2-2.0.so

BIN
assets/crosshair.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 B

View File

@ -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);
@ -19,6 +19,8 @@ class Q3Renderer
void end2d();
void drawRect(float x, float y, float w, float h, glm::vec3 col);
void drawCrosshair();
private:
};

View File

@ -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

View File

@ -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);
};

View File

@ -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,48 +19,34 @@ 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())
{
Uint32 current_time = SDL_GetTicks();
float dt = static_cast<float>(current_time - last_time);
window.pollEvents();
camera.update(dt);
renderer.clear();
renderer.start2d();
renderer.drawRect(-0.01f, -0.01f, 0.02, 0.03, glm::vec3(1.0f, 1.0f, 1.0f));
renderer.end2d();
window.pollEvents();
camera.update(dt);
renderer.clear();
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);
renderer.handleCamera(&camera);
world_renderer.render(&world, &renderer);
}
renderer.end3d();
renderer.start2d();
{
renderer.drawCrosshair();
}
renderer.end2d();
window.swapBuffers();
last_time = current_time;

View File

@ -9,30 +9,25 @@
Q3Texture *grassTexture;
Q3Texture *sideTexture;
Q3Texture *crosshairTexture;
Q3Renderer::Q3Renderer()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_BACK);
glViewport(0, 0, Q3_WINDOWWIDTH, Q3_WINDOWHEIGHT);
/*
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glm::mat4 perspective = glm::perspective(Q3_FOV, (float)Q3_WINDOWWIDTH / Q3_WINDOWHEIGHT, 0.1f, 100.0f);
glLoadMatrixf(&perspective[0][0]);*/
glEnable(GL_TEXTURE_2D);
grassTexture = new Q3Texture("assets/grass.png");
sideTexture = new Q3Texture("assets/sideGrass.png");
crosshairTexture = new Q3Texture("assets/crosshair.png");
}
void Q3Renderer::clear()
@ -87,7 +82,32 @@ 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::drawCrosshair()
{
glPushMatrix();
glTranslatef(-0.1f, -0.1f, 0.1f);
crosshairTexture->bind();
glBegin(GL_QUADS);
{
glTexCoord2f(0.f, 1.f);
glVertex2f(0.f, 0.f);
glTexCoord2f(0.f, 0.f);
glVertex2f(0.f, 0.2f);
glTexCoord2f(1.f, 0.f);
glVertex2f(0.2f, 0.2f);
glTexCoord2f(1.f, 1.f);
glVertex2f(0.2f, 0.f);
}
glEnd();
crosshairTexture->unbind();
glPopMatrix();
}
void Q3Renderer::drawCube(float x, float y, float z, float sx, float sy, float sz)
{
glPushMatrix();
glTranslatef(x, y, z);

View File

@ -0,0 +1,34 @@
#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);
}
}