Restructure code to make it follow standards.

master
Vyom Fadia 2018-07-07 17:31:48 +01:00
parent fdf6980970
commit ebdd5417be
10 changed files with 192 additions and 134 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
.vs/
.vs*/
bin/
*.filters
build/

View File

@ -2,21 +2,22 @@
#include <glm/glm.hpp>
#include <SDL.h>
class Q3Camera {
public:
class Q3Camera
{
public:
void update(float dt);
void glLook();
Q3Camera(SDL_Window *window);
private:
private:
glm::vec3 m_position;
glm::vec3 m_up;
glm::vec3 m_dir;
float m_hAngle;
float m_vAngle;
float m_speed = 0.01f;
float m_mouseSpeed = 0.000075f;

View File

@ -2,18 +2,21 @@
#include <glm/glm.hpp>
class Q3PerlinNoiseGenerator {
private:
class Q3PerlinNoiseGenerator
{
private:
float fade(float t);
int inc(int num);
float grad(int hash, float x, float y, float z);
float lerp(float a, float b, float x);
public:
public:
Q3PerlinNoiseGenerator();
float perlin(glm::vec3 pos);
float octavePerlin(glm::vec3 pos, int octaves, float persitance);
private:
private:
int m_p[512];
int m_repeat;
};

View File

@ -2,13 +2,15 @@
#include <qub3d/q3camera.hpp>
class Q3Renderer {
public:
class Q3Renderer
{
public:
Q3Renderer();
void clear();
void drawCube(float x, float y, float z, float sx, float sy, float sz, glm::vec3 topCol);
void handleCamera(Q3Camera *camera);
private:
private:
};

View File

@ -2,8 +2,9 @@
#include <SDL.h>
class Q3Window {
public:
class Q3Window
{
public:
Q3Window(const char *title, int w, int h);
void pollEvents();
@ -13,8 +14,8 @@ public:
SDL_Window *getWindow() { return m_window; }
private:
SDL_Window * m_window;
private:
SDL_Window *m_window;
SDL_GLContext m_context;
bool m_running;

View File

@ -7,37 +7,42 @@
#include <time.h>
struct Q3Cube {
struct Q3Cube
{
glm::vec3 position;
glm::vec3 color;
};
int main(int argc, char **argv) {
int main(int argc, char **argv)
{
srand(static_cast<unsigned int>(time(NULL)));
SDL_Init(SDL_INIT_EVERYTHING);
Q3Window window(Q3_WINDOWTITLE, Q3_WINDOWWIDTH, Q3_WINDOWHEIGHT);
window.pollEvents();
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 = x*2.f;
float pNorm = perlin_noise_generator.perlin(glm::vec3((x*2) / 10.f, (z*2) / 10.0f, 0.f));
r.position.y = pNorm*20.f;
r.position.z = z*2.f;
for (int z = 0; z < mapsize; ++z)
{
for (int x = 0; x < mapsize; ++x)
{
Q3Cube &r = terrain[x + z * mapsize];
r.position.x = x * 2.f;
float pNorm = perlin_noise_generator.perlin(glm::vec3((x * 2) / 10.f, (z * 2) / 10.0f, 0.f));
r.position.y = pNorm * 20.f;
r.position.z = z * 2.f;
r.color = glm::vec3(126.f / 255.f, 214.f / 255.f, 145.f / 255.f);
}
}
Uint32 last_time = SDL_GetTicks();
while (window.isRunning()) {
while (window.isRunning())
{
Uint32 current_time = SDL_GetTicks();
float dt = static_cast<float>(current_time - last_time);
@ -45,13 +50,14 @@ int main(int argc, char **argv) {
camera.update(dt);
renderer.clear();
renderer.handleCamera(&camera);
for (int i = 0; i < mapsize * mapsize; ++i) {
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);
}
window.swapBuffers();
last_time = current_time;

View File

@ -6,66 +6,79 @@
#include <glm/gtc/matrix_transform.hpp>
Q3Camera::Q3Camera(SDL_Window *window) : m_hAngle(0.f), m_vAngle(0.f), m_position(0.f, 0.f, -10.f), m_window(window) {
Q3Camera::Q3Camera(SDL_Window *window) : m_hAngle(0.f), m_vAngle(0.f), m_position(0.f, 0.f, -10.f), m_window(window)
{
SDL_ShowCursor(0);
SDL_WarpMouseInWindow(window, Q3_WINDOWWIDTH / 2, Q3_WINDOWHEIGHT / 2);
}
bool s_isPaused = false, s_wasPaused = false;
void Q3Camera::update(float dt) {
void Q3Camera::update(float dt)
{
const Uint8 *state = SDL_GetKeyboardState(NULL);
if (!s_isPaused) {
if (!s_isPaused)
{
int x, y;
SDL_GetMouseState(&x, &y);
SDL_WarpMouseInWindow(NULL, Q3_WINDOWWIDTH / 2, Q3_WINDOWHEIGHT / 2);
m_hAngle += m_mouseSpeed * dt * float(Q3_WINDOWWIDTH / 2 - x);
m_vAngle += m_mouseSpeed * dt * float(Q3_WINDOWHEIGHT / 2 - y);
m_dir = glm::vec3(
SDL_cosf(m_vAngle) * SDL_sinf(m_hAngle),
SDL_sinf(m_vAngle),
SDL_cosf(m_vAngle) * SDL_cosf(m_hAngle)
);
SDL_cosf(m_vAngle) * SDL_cosf(m_hAngle));
glm::vec3 right(
SDL_sinf(m_hAngle - Q3_PIF / 2.f),
0.f,
SDL_cosf(m_hAngle - Q3_PIF / 2.f)
);
SDL_cosf(m_hAngle - Q3_PIF / 2.f));
m_up = glm::cross(right, m_dir);
if (state[SDL_SCANCODE_W]) m_position += m_dir * dt * m_speed;
if (state[SDL_SCANCODE_S]) m_position -= m_dir * dt * m_speed;
if (state[SDL_SCANCODE_D]) m_position += right * dt * m_speed;
if (state[SDL_SCANCODE_A]) m_position -= right * dt * m_speed;
if (state[SDL_SCANCODE_SPACE]) m_position.y += dt * m_speed;
if (state[SDL_SCANCODE_LCTRL]) m_position.y -= dt * m_speed;
if (state[SDL_SCANCODE_W])
m_position += m_dir * dt * m_speed;
if (state[SDL_SCANCODE_S])
m_position -= m_dir * dt * m_speed;
if (state[SDL_SCANCODE_D])
m_position += right * dt * m_speed;
if (state[SDL_SCANCODE_A])
m_position -= right * dt * m_speed;
if (state[SDL_SCANCODE_SPACE])
m_position.y += dt * m_speed;
if (state[SDL_SCANCODE_LCTRL])
m_position.y -= dt * m_speed;
}
if (state[SDL_SCANCODE_ESCAPE]) {
if (!s_wasPaused) {
if (state[SDL_SCANCODE_ESCAPE])
{
if (!s_wasPaused)
{
s_isPaused = !s_isPaused;
SDL_ShowCursor(s_isPaused);
if (!s_isPaused) {
if (!s_isPaused)
{
SDL_WarpMouseInWindow(m_window, Q3_WINDOWWIDTH / 2, Q3_WINDOWHEIGHT / 2);
SDL_SetWindowTitle(m_window, Q3_WINDOWTITLE);
}
else {
else
{
SDL_SetWindowTitle(m_window, Q3_WINDOWTITLE_PAUSED);
}
}
s_wasPaused = true;
}
else {
else
{
s_wasPaused = false;
}
}
void Q3Camera::glLook() {
void Q3Camera::glLook()
{
glm::vec3 center = m_position + m_dir;
glm::mat4 view = glm::lookAt(m_position, center, m_up);
glLoadMatrixf(&view[0][0]);

View File

@ -4,81 +4,103 @@ http://flafla2.github.io/2014/08/09/perlinnoise.html
*/
static int s_permutation[] = {
151,160,137,91,90,15, 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,
8,99,37,240,21,10,23, 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,
117,35,11,32,57,177,33, 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,
165,71,134,139,48,27,166, 77,146,158,231,83,111,229,122,60,211,133,230,220,
105,92,41,55,46,245,40,244, 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,
187,208, 89,18,169,200,196, 135,130,116,188,159,86,164,100,109,198,173,186,
3,64,52,217,226,250,124,123, 5,202,38,147,118,126,255,82,85,212,207,206,59,227,
47,16,58,17,182,189,28,42, 223,183,170,213,119,248,152, 2,44,154,163, 70,221,
153,101,155,167, 43,172,9, 129,22,39,253, 19,98,108,110,79,113,224,232,178,185,
112,104,218,246,97,228, 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,
145,235,249,14,239,107, 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,
50,45,127, 4,150,254, 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,
61,156,180
};
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142,
8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203,
117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74,
165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220,
105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132,
187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186,
3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227,
47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221,
153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185,
112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51,
145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121,
50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215,
61, 156, 180};
Q3PerlinNoiseGenerator::Q3PerlinNoiseGenerator(): m_repeat(-1) {
for (int i = 0; i < 512; ++i) m_p[i] = s_permutation[i % 256];
Q3PerlinNoiseGenerator::Q3PerlinNoiseGenerator() : m_repeat(-1)
{
for (int i = 0; i < 512; ++i)
m_p[i] = s_permutation[i % 256];
}
float Q3PerlinNoiseGenerator::fade(float t) {
float Q3PerlinNoiseGenerator::fade(float t)
{
return t * t * t * (t * (t * 6 - 15) + 10);
}
int Q3PerlinNoiseGenerator::inc(int num) {
int Q3PerlinNoiseGenerator::inc(int num)
{
num++;
if (m_repeat > 0)
if (m_repeat > 0)
num %= m_repeat;
return num;
}
float Q3PerlinNoiseGenerator::grad(int hash, float x, float y, float z) {
float Q3PerlinNoiseGenerator::grad(int hash, float x, float y, float z)
{
switch (hash & 0xF)
{
case 0x0: return x + y;
case 0x1: return -x + y;
case 0x2: return x - y;
case 0x3: return -x - y;
case 0x4: return x + z;
case 0x5: return -x + z;
case 0x6: return x - z;
case 0x7: return -x - z;
case 0x8: return y + z;
case 0x9: return -y + z;
case 0xA: return y - z;
case 0xB: return -y - z;
case 0xC: return y + x;
case 0xD: return -y + z;
case 0xE: return y - x;
case 0xF: return -y - z;
default: return 0;
case 0x0:
return x + y;
case 0x1:
return -x + y;
case 0x2:
return x - y;
case 0x3:
return -x - y;
case 0x4:
return x + z;
case 0x5:
return -x + z;
case 0x6:
return x - z;
case 0x7:
return -x - z;
case 0x8:
return y + z;
case 0x9:
return -y + z;
case 0xA:
return y - z;
case 0xB:
return -y - z;
case 0xC:
return y + x;
case 0xD:
return -y + z;
case 0xE:
return y - x;
case 0xF:
return -y - z;
default:
return 0;
}
}
float Q3PerlinNoiseGenerator::lerp(float a, float b, float x) {
float Q3PerlinNoiseGenerator::lerp(float a, float b, float x)
{
return a + x * (b - a);
}
float Q3PerlinNoiseGenerator::perlin(glm::vec3 pos) {
if (m_repeat > 0) {
float Q3PerlinNoiseGenerator::perlin(glm::vec3 pos)
{
if (m_repeat > 0)
{
pos.x = static_cast<float>((int)pos.x % m_repeat);
pos.y = static_cast<float>((int)pos.y % m_repeat);
pos.z = static_cast<float>((int)pos.z % m_repeat);
}
glm::ivec3 posi(
(int)pos.x & 255,
(int)pos.y & 255,
(int)pos.z & 255
);
(int)pos.z & 255);
glm::vec3 posf(
pos.x - (int)pos.x,
pos.y - (int)pos.y,
pos.z - (int)pos.z
);
pos.z - (int)pos.z);
float u = fade(posf.x);
float v = fade(posf.y);
@ -104,32 +126,34 @@ float Q3PerlinNoiseGenerator::perlin(glm::vec3 pos) {
bbb = m_p[m_p[m_p[inc(xi)] + inc(yi)] + inc(zi)];
float x1, x2, y1, y2;
x1 = lerp(grad(aaa, xf, yf, zf),
grad(baa, xf - 1, yf, zf),
u);
x2 = lerp(grad(aba, xf, yf - 1, zf),
grad(bba, xf - 1, yf - 1, zf),
u);
x1 = lerp(grad(aaa, xf, yf, zf),
grad(baa, xf - 1, yf, zf),
u);
x2 = lerp(grad(aba, xf, yf - 1, zf),
grad(bba, xf - 1, yf - 1, zf),
u);
y1 = lerp(x1, x2, v);
x1 = lerp(grad(aab, xf, yf, zf - 1),
grad(bab, xf - 1, yf, zf - 1),
u);
grad(bab, xf - 1, yf, zf - 1),
u);
x2 = lerp(grad(abb, xf, yf - 1, zf - 1),
grad(bbb, xf - 1, yf - 1, zf - 1),
u);
grad(bbb, xf - 1, yf - 1, zf - 1),
u);
y2 = lerp(x1, x2, v);
return (lerp(y1, y2, w) + 1) / 2;
}
float Q3PerlinNoiseGenerator::octavePerlin(glm::vec3 pos, int octaves, float persitance) {
float Q3PerlinNoiseGenerator::octavePerlin(glm::vec3 pos, int octaves, float persitance)
{
float tot = 0;
float f = 1;
float a = 1;
float maxValue = 0;
for (int i = 0; i < octaves; ++i) {
for (int i = 0; i < octaves; ++i)
{
tot += perlin(glm::vec3(pos.x * f, pos.y * f, pos.z * f)) * a;
maxValue += a;
a *= persitance;

View File

@ -5,7 +5,8 @@
#include <glm/gtc/matrix_transform.hpp>
Q3Renderer::Q3Renderer() {
Q3Renderer::Q3Renderer()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
@ -19,63 +20,66 @@ Q3Renderer::Q3Renderer() {
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]);
}
void Q3Renderer::clear() {
void Q3Renderer::clear()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Q3Renderer::handleCamera(Q3Camera *camera) {
void Q3Renderer::handleCamera(Q3Camera *camera)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
camera->glLook();
}
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, glm::vec3 topCol)
{
glPushMatrix();
glTranslatef(x, y, z);
glBegin(GL_QUADS);
{
// top
glColor3f(topCol.x, topCol.y,topCol.z);
glColor3f(topCol.x, topCol.y, topCol.z);
glVertex3f(sx, sy, -sz);
glVertex3f(-sx, sy, -sz);
glVertex3f(-sx, sy, sz);
glVertex3f(sx, sy, sz);
// bottom
glColor3f(89.f/255.f,87.f/ 255.f,87.f/255.f);
glColor3f(89.f / 255.f, 87.f / 255.f, 87.f / 255.f);
glVertex3f(sx, -sy, sz);
glVertex3f(-sx, -sy, sz);
glVertex3f(-sx, -sy, -sz);
glVertex3f(sx, -sy, -sz);
// front
//glColor3f(1.0f, 0.0f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(sx, sy, sz);
glVertex3f(-sx, sy, sz);
glVertex3f(-sx, -sy, sz);
glVertex3f(sx, -sy, sz);
// back
//glColor3f(1.0f, 1.0f, 0.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(sx, -sy, -sz);
glVertex3f(-sx, -sy, -sz);
glVertex3f(-sx, sy, -sz);
glVertex3f(sx, sy, -sz);
// left
//glColor3f(0.0f, 0.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-sx, sy, sz);
glVertex3f(-sx, sy, -sz);
glVertex3f(-sx, -sy, -sz);
glVertex3f(-sx, -sy, sz);
// right
//glColor3f(1.0f, 0.0f, 1.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(sx, sy, -sz);
glVertex3f(sx, sy, sz);
glVertex3f(sx, -sy, sz);

View File

@ -1,13 +1,13 @@
#include <qub3d/q3window.hpp>
#include <qub3d/q3shared_constants.hpp>
Q3Window::Q3Window(const char *title, int w, int h): m_running(true) {
Q3Window::Q3Window(const char *title, int w, int h) : m_running(true)
{
m_window = SDL_CreateWindow(
"Qub3d",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
w, h,
SDL_WINDOW_OPENGL
);
"Qub3d",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
w, h,
SDL_WINDOW_OPENGL);
m_context = SDL_GL_CreateContext(m_window);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
@ -21,18 +21,22 @@ Q3Window::Q3Window(const char *title, int w, int h): m_running(true) {
bool Q3Window::isRunning() { return m_running; }
void Q3Window::sleep(float t) {
void Q3Window::sleep(float t)
{
SDL_Delay(static_cast<Uint32>(t));
}
void Q3Window::pollEvents() {
void Q3Window::pollEvents()
{
SDL_Event e;
while (SDL_PollEvent(&e) > 0) {
if (e.type == SDL_QUIT)
while (SDL_PollEvent(&e) > 0)
{
if (e.type == SDL_QUIT)
m_running = false;
}
}
void Q3Window::swapBuffers() {
void Q3Window::swapBuffers()
{
SDL_GL_SwapWindow(m_window);
}