Complete port over to C, replace glm math lib
This commit is contained in:
@@ -1,23 +1,22 @@
|
||||
#include <Dengine/AssetManager.h>
|
||||
|
||||
#define STBI_FAILURE_USERMSG
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <STB/stb_image.h>
|
||||
|
||||
std::map<std::string, Texture> textures;
|
||||
std::map<std::string, Shader> shaders;
|
||||
#include <Dengine/Platform.h>
|
||||
#include <Dengine/AssetManager.h>
|
||||
GLOBAL_VAR AssetManager assetManager;
|
||||
|
||||
Texture *asset_getTexture(const char *const name)
|
||||
Texture *asset_getTexture(const enum TexList type)
|
||||
{
|
||||
// NOTE(doyle): Since we're using a map, the count of an object can
|
||||
// only be 1 or 0
|
||||
if (textures.count(name) == 1)
|
||||
return &textures[name];
|
||||
if (type < texlist_count)
|
||||
return &assetManager.textures[type];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const i32 asset_loadTextureImage(const char *const path, const char *const name)
|
||||
const i32 asset_loadTextureImage(const char *const path, const enum TexList type)
|
||||
{
|
||||
/* Open the texture image */
|
||||
i32 imgWidth, imgHeight, bytesPerPixel;
|
||||
@@ -36,16 +35,16 @@ const i32 asset_loadTextureImage(const char *const path, const char *const name)
|
||||
glCheckError();
|
||||
stbi_image_free(image);
|
||||
|
||||
textures[name] = tex;
|
||||
assetManager.textures[type] = tex;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Shader *asset_getShader(const char *const name)
|
||||
Shader *asset_getShader(const enum ShaderList type)
|
||||
{
|
||||
if (shaders.count(name) == 1)
|
||||
return &shaders[name];
|
||||
if (type < shaderlist_count)
|
||||
return &assetManager.shaders[type];
|
||||
|
||||
return nullptr;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
INTERNAL GLuint createShaderFromPath(const char *const path, GLuint shadertype)
|
||||
@@ -78,7 +77,7 @@ INTERNAL GLuint createShaderFromPath(const char *const path, GLuint shadertype)
|
||||
|
||||
const i32 asset_loadShaderFiles(const char *const vertexPath,
|
||||
const char *const fragmentPath,
|
||||
const char *const name)
|
||||
const enum ShaderList type)
|
||||
{
|
||||
GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER);
|
||||
GLuint fragmentShader =
|
||||
@@ -89,6 +88,6 @@ const i32 asset_loadShaderFiles(const char *const vertexPath,
|
||||
if (result)
|
||||
return result;
|
||||
|
||||
shaders[name] = shader;
|
||||
assetManager.shaders[type] = shader;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,21 +1,15 @@
|
||||
#include <Dengine/OpenGL.h>
|
||||
#include <Dengine/Renderer.h>
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtx/transform.hpp>
|
||||
|
||||
void renderer_entity(Renderer *renderer, Entity *entity, GLfloat rotate,
|
||||
glm::vec3 color)
|
||||
void renderer_entity(Renderer *renderer, Entity *entity, f32 rotate, v3 color)
|
||||
{
|
||||
shader_use(renderer->shader);
|
||||
glm::mat4 transMatrix = glm::translate(glm::vec3(entity->pos, 0.0f));
|
||||
glm::mat4 rotateMatrix = glm::rotate(rotate, glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
glCheckError();
|
||||
|
||||
mat4 transMatrix = mat4_translate(entity->pos.x, entity->pos.y, 0.0f);
|
||||
mat4 rotateMatrix = mat4_rotate(rotate, 0.0f, 0.0f, 1.0f);
|
||||
// NOTE(doyle): We draw everything as a unit square in OGL. Scale it to size
|
||||
glm::mat4 scaleMatrix = glm::scale(glm::vec3(entity->size, 1.0f));
|
||||
mat4 scaleMatrix = mat4_scale(entity->size.x, entity->size.y, 1.0f);
|
||||
|
||||
glm::mat4 model = transMatrix * rotateMatrix * scaleMatrix;
|
||||
mat4 model = mat4_mul(transMatrix, mat4_mul(rotateMatrix, scaleMatrix));
|
||||
shader_uniformSetMat4fv(renderer->shader, "model", model);
|
||||
glCheckError();
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
#include <Dengine/Shader.h>
|
||||
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
const i32 shader_loadProgram(Shader *const shader, const GLuint vertexShader,
|
||||
const GLuint fragmentShader)
|
||||
{
|
||||
@@ -37,10 +32,10 @@ void shader_uniformSet1i(Shader *const shader, const GLchar *name,
|
||||
}
|
||||
|
||||
void shader_uniformSetMat4fv(Shader *const shader, const GLchar *name,
|
||||
const glm::mat4 data)
|
||||
mat4 data)
|
||||
{
|
||||
GLint uniformLoc = glGetUniformLocation(shader->id, name);
|
||||
glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, glm::value_ptr(data));
|
||||
glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, data.e[0]);
|
||||
}
|
||||
|
||||
void shader_use(const Shader *const shader) { glUseProgram(shader->id); }
|
||||
@@ -33,7 +33,7 @@ Texture genTexture(const GLuint width, const GLuint height,
|
||||
{
|
||||
// TODO(doyle): Let us set the parameters gl params as well
|
||||
glCheckError();
|
||||
Texture tex = {};
|
||||
Texture tex = {0};
|
||||
tex.width = width;
|
||||
tex.height = height;
|
||||
tex.internalFormat = GL_RGBA;
|
||||
@@ -1,38 +1,36 @@
|
||||
#include <Dengine/AssetManager.h>
|
||||
#include <Dengine/Math.h>
|
||||
#include <WorldTraveller/WorldTraveller.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
void worldTraveller_gameInit(GameState *state)
|
||||
{
|
||||
/* Initialise assets */
|
||||
|
||||
asset_loadTextureImage("data/textures/WorldTraveller/elisa-spritesheet1.png",
|
||||
"hero");
|
||||
asset_loadTextureImage(
|
||||
"data/textures/WorldTraveller/elisa-spritesheet1.png", texlist_hero);
|
||||
glCheckError();
|
||||
|
||||
state->state = state_active;
|
||||
Renderer *renderer = &state->renderer;
|
||||
asset_loadShaderFiles("data/shaders/sprite.vert.glsl",
|
||||
"data/shaders/sprite.frag.glsl", "sprite");
|
||||
"data/shaders/sprite.frag.glsl", shaderlist_sprite);
|
||||
glCheckError();
|
||||
|
||||
renderer->shader = asset_getShader("sprite");
|
||||
renderer->shader = asset_getShader(shaderlist_sprite);
|
||||
shader_use(renderer->shader);
|
||||
glm::mat4 projection = glm::ortho(0.0f, CAST(GLfloat) state->width, 0.0f,
|
||||
CAST(GLfloat) state->height, 0.0f, 1.0f);
|
||||
|
||||
mat4 projection = mat4_ortho(0.0f, CAST(f32) state->width, 0.0f,
|
||||
CAST(f32) state->height, 0.0f, 1.0f);
|
||||
shader_uniformSetMat4fv(renderer->shader, "projection", projection);
|
||||
glCheckError();
|
||||
|
||||
/* Init renderer */
|
||||
// NOTE(doyle): Draws a series of triangles (three-sided polygons) using
|
||||
// vertices v0, v1, v2, then v2, v1, v3 (note the order)
|
||||
glm::vec4 vertices[] = {
|
||||
// x y s t
|
||||
{0.0f, 1.0f, 0.0f, 1.0f}, // Top left
|
||||
{0.0f, 0.0f, 0.0f, 0.0f}, // Bottom left
|
||||
{1.0f, 1.0f, 1.0f, 1.0f}, // Top right
|
||||
v4 vertices[] = {
|
||||
// x y s t
|
||||
{0.0f, 1.0f, 0.0f, 1.0f}, // Top left
|
||||
{0.0f, 0.0f, 0.0f, 0.0f}, // Bottom left
|
||||
{1.0f, 1.0f, 1.0f, 1.0f}, // Top right
|
||||
{1.0f, 0.0f, 1.0f, 0.0f}, // Bottom right
|
||||
};
|
||||
|
||||
@@ -52,7 +50,7 @@ void worldTraveller_gameInit(GameState *state)
|
||||
|
||||
/* Configure VAO */
|
||||
const GLuint numVertexElements = 4;
|
||||
const GLuint vertexSize = sizeof(glm::vec4);
|
||||
const GLuint vertexSize = sizeof(v4);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, numVertexElements, GL_FLOAT, GL_FALSE, vertexSize,
|
||||
@@ -67,18 +65,15 @@ void worldTraveller_gameInit(GameState *state)
|
||||
/* Init hero */
|
||||
|
||||
Entity *hero = &state->hero;
|
||||
hero->tex = asset_getTexture("hero");
|
||||
hero->size = glm::vec2(100, 100);
|
||||
hero->tex = asset_getTexture(texlist_hero);
|
||||
hero->size = V2(260.0f, 260.0f);
|
||||
|
||||
glm::vec2 screenCentre =
|
||||
glm::vec2(state->width / 2.0f, state->height / 2.0f);
|
||||
glm::vec2 heroOffsetToCentre =
|
||||
glm::vec2(-((i32)hero->size.x / 2), -((i32)hero->size.y / 2));
|
||||
//v2 screenCentre = V2(state->width / 2.0f, state->height / 2.0f);
|
||||
//v2 heroOffsetToCentre =
|
||||
// V2(-(hero->size.x / 2.0f), -(hero->size.y / 2.0f));
|
||||
|
||||
glm::vec2 heroCentered = screenCentre + heroOffsetToCentre;
|
||||
hero->pos = heroCentered;
|
||||
|
||||
srand(CAST(u32)(glfwGetTime()));
|
||||
//v2 heroCentered = v2_add(screenCentre, heroOffsetToCentre);
|
||||
hero->pos = V2(0.0f, 0.0f);
|
||||
glCheckError();
|
||||
}
|
||||
|
||||
@@ -96,7 +91,7 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
|
||||
f (t) = (a/2)*t^2 + v*t + p, where p is a constant, old position
|
||||
*/
|
||||
|
||||
glm::vec2 ddPos = glm::vec2(0, 0);
|
||||
v2 ddPos = V2(0, 0);
|
||||
|
||||
if (state->keys[GLFW_KEY_SPACE])
|
||||
{
|
||||
@@ -125,7 +120,7 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
|
||||
// NOTE(doyle): Cheese it and pre-compute the vector for diagonal using
|
||||
// pythagoras theorem on a unit triangle
|
||||
// 1^2 + 1^2 = c^2
|
||||
ddPos *= 0.70710678118f;
|
||||
ddPos = v2_scale(ddPos, 0.70710678118f);
|
||||
}
|
||||
|
||||
f32 heroSpeed = CAST(f32)(22.0f * METERS_TO_PIXEL); // m/s^2
|
||||
@@ -133,16 +128,24 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
|
||||
{
|
||||
heroSpeed = CAST(f32)(22.0f * 5.0f * METERS_TO_PIXEL);
|
||||
}
|
||||
ddPos *= heroSpeed;
|
||||
ddPos = v2_scale(ddPos, heroSpeed);
|
||||
|
||||
// TODO(doyle): Counteracting force on player's acceleration is arbitrary
|
||||
Entity *hero = &state->hero;
|
||||
ddPos += -(5.5f * hero->dPos);
|
||||
ddPos = v2_sub(ddPos, v2_scale(hero->dPos, 5.5f));
|
||||
|
||||
glm::vec2 newHeroP =
|
||||
(0.5f * ddPos) * squared(dt) + (hero->dPos * dt) + hero->pos;
|
||||
/*
|
||||
NOTE(doyle): Calculate new position from acceleration with old velocity
|
||||
new Position = (a/2) * (t^2) + (v*t) + p,
|
||||
acceleration = (a/2) * (t^2)
|
||||
old velocity = (v*t)
|
||||
*/
|
||||
v2 ddPosNew = v2_scale(v2_scale(ddPos, 0.5f), squared(dt));
|
||||
v2 dPos = v2_scale(hero->dPos, dt);
|
||||
v2 newHeroP = v2_add(v2_add(ddPosNew, dPos), hero->pos);
|
||||
|
||||
hero->dPos = (ddPos * dt) + hero->dPos;
|
||||
// f'(t) = curr velocity = a*t + v, where v is old velocity
|
||||
hero->dPos = v2_add(hero->dPos, v2_scale(ddPos, dt));
|
||||
hero->pos = newHeroP;
|
||||
}
|
||||
|
||||
@@ -153,7 +156,7 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
|
||||
glCheckError();
|
||||
|
||||
/* Render */
|
||||
renderer_entity(&state->renderer, &state->hero);
|
||||
renderer_entity(&state->renderer, &state->hero, 0.0f, V3(0, 0, 0));
|
||||
// TODO(doyle): Clean up lines
|
||||
// Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }
|
||||
}
|
||||
+2
-3
@@ -6,7 +6,6 @@ pushd ..\bin
|
||||
set GLEW=..\extern\glew-1.13.0
|
||||
set GLFW=..\extern\glfw-3.2.bin.WIN32
|
||||
set STB=..\extern\stb-master
|
||||
set GLM=..\extern\glm-0.9.7.5
|
||||
|
||||
REM EHsc enables exception handling
|
||||
REM MD uses dynamic runtime library
|
||||
@@ -14,13 +13,13 @@ REM Zi enables debug data
|
||||
set compileFlags=/EHsc /MD /Zi /W3
|
||||
|
||||
REM Include directories
|
||||
set includeFlags=/I ..\src\include /I %GLEW%\include /I %GLFW%\include /I %STB%\include /I %GLM%\include
|
||||
set includeFlags=/I ..\src\include /I %GLEW%\include /I %GLFW%\include /I %STB%\include
|
||||
|
||||
REM Link libraries
|
||||
set linkLibraries=/link opengl32.lib %GLFW%\lib-vc2015\glfw3.lib %GLEW%\lib\Release\Win32\glew32s.lib gdi32.lib user32.lib shell32.lib
|
||||
set ignoreLibraries=/NODEFAULTLIB:"libc.lib" /NODEFAULTLIB:"libcmt.lib" /NODEFAULTLIB:"libcd.lib" /NODEFAULTLIB:"libcmtd.lib" /NODEFAULTLIB:"msvcrtd.lib"
|
||||
|
||||
cl %compileFlags% ..\src\*.cpp %includeFlags% %linkLibraries% %ignoreLibraries% /OUT:"Dengine.exe"
|
||||
cl %compileFlags% ..\src\*.c %includeFlags% %linkLibraries% %ignoreLibraries% /OUT:"Dengine.exe"
|
||||
REM /SUBSYSTEM:CONSOLE
|
||||
|
||||
popd
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
#if 1
|
||||
#include <Dengine/AssetManager.h>
|
||||
#include <Dengine/Renderer.h>
|
||||
#include <Dengine/Math.h>
|
||||
|
||||
#include <WorldTraveller/WorldTraveller.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mode)
|
||||
{
|
||||
GameState *game = CAST(GameState *)(glfwGetWindowUserPointer(window));
|
||||
@@ -40,9 +35,10 @@ int main()
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
||||
|
||||
glm::ivec2 windowSize = glm::ivec2(1280, 720);
|
||||
v2 windowSize = V2(1280.0f, 720.0f);
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow(windowSize.x, windowSize.y, "Dengine", NULL, NULL);
|
||||
GLFWwindow *window = glfwCreateWindow(
|
||||
CAST(i32) windowSize.x, CAST(i32) windowSize.y, "Dengine", NULL, NULL);
|
||||
|
||||
if (!window)
|
||||
{
|
||||
@@ -64,9 +60,13 @@ int main()
|
||||
// regardless of success. Catch it once by calling glGetError
|
||||
glGetError();
|
||||
|
||||
glm::ivec2 frameBufferSize;
|
||||
glfwGetFramebufferSize(window, &frameBufferSize.x, &frameBufferSize.y);
|
||||
glViewport(0, 0, frameBufferSize.x, frameBufferSize.y);
|
||||
i32 frameBufferSizeX;
|
||||
i32 frameBufferSizeY;
|
||||
glfwGetFramebufferSize(window, &frameBufferSizeX, &frameBufferSizeY);
|
||||
const v2 frameBufferSize =
|
||||
V2(CAST(f32) frameBufferSizeX, CAST(f32) frameBufferSizeY);
|
||||
|
||||
glViewport(0, 0, CAST(i32)frameBufferSize.x, CAST(i32)frameBufferSize.y);
|
||||
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
@@ -79,10 +79,10 @@ int main()
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glCullFace(GL_BACK);
|
||||
|
||||
GameState worldTraveller = {};
|
||||
worldTraveller.state = state_active;
|
||||
worldTraveller.width = frameBufferSize.x;
|
||||
worldTraveller.height = frameBufferSize.y;
|
||||
GameState worldTraveller = {0};
|
||||
worldTraveller.state = state_active;
|
||||
worldTraveller.width = CAST(i32)frameBufferSize.x;
|
||||
worldTraveller.height = CAST(i32)frameBufferSize.y;
|
||||
|
||||
worldTraveller_gameInit(&worldTraveller);
|
||||
|
||||
@@ -118,7 +118,7 @@ int main()
|
||||
/* Swap the buffers */
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
f32 endTime = (f32)glfwGetTime();
|
||||
f32 endTime = CAST(f32)glfwGetTime();
|
||||
secondsElapsed = endTime - startTime;
|
||||
|
||||
#if 0
|
||||
@@ -1,25 +1,39 @@
|
||||
#ifndef DENGINE_ASSET_MANAGER_H
|
||||
#define DENGINE_ASSET_MANAGER_H
|
||||
|
||||
#include <Dengine/Platform.h>
|
||||
#include <Dengine/Shader.h>
|
||||
#include <Dengine/Texture.h>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
enum TexList
|
||||
{
|
||||
texlist_hero,
|
||||
texlist_count,
|
||||
};
|
||||
|
||||
extern std::map<std::string, Texture> textures;
|
||||
extern std::map<std::string, Shader> shaders;
|
||||
enum ShaderList
|
||||
{
|
||||
shaderlist_sprite,
|
||||
shaderlist_count,
|
||||
};
|
||||
|
||||
// TODO(doyle): Switch to hash based lookup
|
||||
typedef struct AssetManager
|
||||
{
|
||||
Texture textures[256];
|
||||
Shader shaders[256];
|
||||
} AssetManager;
|
||||
|
||||
extern AssetManager assetManager;
|
||||
|
||||
/* Texture */
|
||||
Texture *asset_getTexture(const char *const name);
|
||||
Texture *asset_getTexture(const enum TexList type);
|
||||
const i32 asset_loadTextureImage(const char *const path,
|
||||
const char *const name);
|
||||
const enum TexList type);
|
||||
|
||||
/* Shaders */
|
||||
Shader *asset_getShader(const char *const name);
|
||||
Shader *asset_getShader(const enum ShaderList type);
|
||||
const i32 asset_loadShaderFiles(const char *const vertexPath,
|
||||
const char *const fragmentPath,
|
||||
const char *const name);
|
||||
const enum ShaderList type);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,15 +2,14 @@
|
||||
#define DENGINE_ENTITY_H
|
||||
|
||||
#include <Dengine/Texture.h>
|
||||
#include <Dengine/Math.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
struct Entity
|
||||
typedef struct Entity
|
||||
{
|
||||
glm::vec2 pos; // Position
|
||||
glm::vec2 dPos; // Velocity
|
||||
glm::vec2 size;
|
||||
v2 pos; // Position
|
||||
v2 dPos; // Velocity
|
||||
v2 size;
|
||||
Texture *tex;
|
||||
};
|
||||
} Entity;
|
||||
|
||||
#endif
|
||||
|
||||
+181
-42
@@ -2,88 +2,104 @@
|
||||
#define DENGINE_MATH_H
|
||||
|
||||
#include <Dengine/Common.h>
|
||||
#include <math.h>
|
||||
|
||||
#define squared(x) (x * x)
|
||||
#define abs(x) ((x) > 0 ? (x) : -(x))
|
||||
|
||||
union v2
|
||||
{
|
||||
struct
|
||||
{
|
||||
f32 x, y;
|
||||
};
|
||||
struct
|
||||
{
|
||||
f32 w, h;
|
||||
};
|
||||
f32 data[2];
|
||||
};
|
||||
// TODO(doyle): Use this instead of window's math file ...
|
||||
//#define abs(x) ((x) > 0 ? (x) : -(x))
|
||||
|
||||
union v3
|
||||
/* VECTORS */
|
||||
typedef union v2
|
||||
{
|
||||
struct
|
||||
{
|
||||
f32 x, y, z;
|
||||
};
|
||||
struct
|
||||
{
|
||||
f32 r, g, b;
|
||||
};
|
||||
f32 data[3];
|
||||
};
|
||||
struct { f32 x, y; };
|
||||
struct { f32 w, h; };
|
||||
f32 e[2];
|
||||
} v2;
|
||||
|
||||
union v4
|
||||
typedef union v3
|
||||
{
|
||||
struct
|
||||
{
|
||||
f32 x, y, z, w;
|
||||
};
|
||||
struct
|
||||
{
|
||||
f32 r, g, b, a;
|
||||
};
|
||||
f32 data[4];
|
||||
};
|
||||
struct { f32 x, y, z; };
|
||||
struct { f32 r, g, b; };
|
||||
f32 e[3];
|
||||
} v3;
|
||||
|
||||
typedef union v4
|
||||
{
|
||||
struct { f32 x, y, z, w; };
|
||||
struct { f32 r, g, b, a; };
|
||||
f32 e[4];
|
||||
} v4;
|
||||
|
||||
INTERNAL inline v2 V2(const f32 x, const f32 y)
|
||||
{
|
||||
v2 result = v2{x, y};
|
||||
v2 result = {x, y};
|
||||
return result;
|
||||
}
|
||||
INTERNAL inline v3 V3(const f32 x, const f32 y, const f32 z)
|
||||
{
|
||||
v3 result = v3{x, y, z};
|
||||
v3 result = {x, y, z};
|
||||
return result;
|
||||
}
|
||||
INTERNAL inline v4 V4(const f32 x, const f32 y, const f32 z, const f32 w)
|
||||
{
|
||||
v4 result = v4{x, y, z, w};
|
||||
v4 result = {x, y, z, w};
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
DEFINE_VECTOR_MATH
|
||||
Automagically generate basic vector function using the C preprocessor
|
||||
where ##num is replaced by the valued in DEFINE_VECTOR_MATH(num) when it is
|
||||
called, i.e. DEFINE_VECTOR_MATH(2)
|
||||
|
||||
An example of this generates,
|
||||
INTERNAL inline v2 v2_add(const v2 a, const v2 b)
|
||||
{
|
||||
v2 result;
|
||||
for (i32 i = 0; i < 2; i++) { result.data[i] = a.data[i] + b.data[i]; }
|
||||
return result;
|
||||
}
|
||||
|
||||
This is repeated for v3 and v4 for the basic math operations
|
||||
*/
|
||||
|
||||
#define DEFINE_VECTOR_MATH(num) \
|
||||
INTERNAL inline v##num v##num##_add(const v##num a, const v##num b) \
|
||||
{ \
|
||||
v##num result; \
|
||||
for (i32 i = 0; i < ##num; i++) { result.data[i] = a.data[i] + b.data[i]; } \
|
||||
for (i32 i = 0; i < ##num; i++) { result.e[i] = a.e[i] + b.e[i]; } \
|
||||
return result; \
|
||||
} \
|
||||
INTERNAL inline v##num v##num##_sub(const v##num a, const v##num b) \
|
||||
{ \
|
||||
v##num result; \
|
||||
for (i32 i = 0; i < ##num; i++) { result.data[i] = a.data[i] - b.data[i]; } \
|
||||
for (i32 i = 0; i < ##num; i++) { result.e[i] = a.e[i] - b.e[i]; } \
|
||||
return result; \
|
||||
} \
|
||||
INTERNAL inline v##num v##num##_scale(const v##num a, const f32 b) \
|
||||
{ \
|
||||
v##num result; \
|
||||
for (i32 i = 0; i < ##num; i++) { result.data[i] = a.data[i] * b; } \
|
||||
for (i32 i = 0; i < ##num; i++) { result.e[i] = a.e[i] * b; } \
|
||||
return result; \
|
||||
} \
|
||||
INTERNAL inline v##num v##num##_mul(const v##num a, const v##num b) \
|
||||
{ \
|
||||
v##num result; \
|
||||
for (i32 i = 0; i < ##num; i++) { result.data[i] = a.data[i] * b.data[i]; } \
|
||||
for (i32 i = 0; i < ##num; i++) { result.e[i] = a.e[i] * b.e[i]; } \
|
||||
return result; \
|
||||
} \
|
||||
INTERNAL inline f32 v##num##_dot(const v##num a, const v##num b) \
|
||||
{ \
|
||||
/* \
|
||||
DOT PRODUCT \
|
||||
Two vectors with dot product equals |a||b|cos(theta) \
|
||||
|a| |d| \
|
||||
|b| . |e| = (ad + be + cf) \
|
||||
|c| |f| \
|
||||
*/ \
|
||||
f32 result = 0; \
|
||||
for (i32 i = 0; i < ##num; i++) { result += (a.e[i] * b.e[i]); } \
|
||||
return result; \
|
||||
} \
|
||||
|
||||
@@ -91,4 +107,127 @@ DEFINE_VECTOR_MATH(2);
|
||||
DEFINE_VECTOR_MATH(3);
|
||||
DEFINE_VECTOR_MATH(4);
|
||||
|
||||
INTERNAL inline v3 v3_cross(const v3 a, const v3 b)
|
||||
{
|
||||
/*
|
||||
CROSS PRODUCT
|
||||
Generate a perpendicular vector to the 2 vectors
|
||||
|a| |d| |bf - ce|
|
||||
|b| x |e| = |cd - af|
|
||||
|c| |f| |ae - be|
|
||||
*/
|
||||
v3 result;
|
||||
result.e[0] = (a.e[1] * b.e[2]) - (a.e[2] * b.e[1]);
|
||||
result.e[1] = (a.e[2] * b.e[0]) - (a.e[0] * b.e[2]);
|
||||
result.e[2] = (a.e[0] * b.e[1]) - (a.e[1] * b.e[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* MATRICES */
|
||||
typedef union mat4
|
||||
{
|
||||
v4 col[4];
|
||||
f32 e[4][4];
|
||||
} mat4;
|
||||
|
||||
INTERNAL inline mat4 mat4_identity()
|
||||
{
|
||||
mat4 result = {0};
|
||||
result.e[0][0] = 1;
|
||||
result.e[1][1] = 1;
|
||||
result.e[2][2] = 1;
|
||||
result.e[3][3] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
INTERNAL inline mat4 mat4_ortho(const f32 left, const f32 right,
|
||||
const f32 bottom, const f32 top,
|
||||
const f32 zNear, const f32 zFar)
|
||||
{
|
||||
mat4 result = mat4_identity();
|
||||
result.e[0][0] = +2.0f / (right - left);
|
||||
result.e[1][1] = +2.0f / (top - bottom);
|
||||
result.e[2][2] = -2.0f / (zFar - zNear);
|
||||
|
||||
result.e[3][0] = -(right + left) / (right - left);
|
||||
result.e[3][1] = -(top + bottom) / (top - bottom);
|
||||
result.e[3][2] = -(zFar + zNear) / (zFar - zNear);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
INTERNAL inline mat4 mat4_translate(const f32 x, const f32 y, const f32 z)
|
||||
{
|
||||
mat4 result = mat4_identity();
|
||||
result.e[3][0] = x;
|
||||
result.e[3][1] = y;
|
||||
result.e[3][2] = z;
|
||||
return result;
|
||||
}
|
||||
|
||||
INTERNAL inline mat4 mat4_rotate(const f32 radians, const f32 x, const f32 y,
|
||||
const f32 z)
|
||||
{
|
||||
mat4 result = mat4_identity();
|
||||
f32 sinVal = sinf(radians);
|
||||
f32 cosVal = cosf(radians);
|
||||
|
||||
result.e[0][0] = (cosVal + (squared(x) * (1.0f - cosVal)));
|
||||
result.e[0][1] = ((y * z * (1.0f - cosVal)) + (z * sinVal));
|
||||
result.e[0][2] = ((z * x * (1.0f - cosVal)) - (y * sinVal));
|
||||
|
||||
result.e[1][0] = ((x * y * (1.0f - cosVal)) - (z * sinVal));
|
||||
result.e[1][1] = (cosVal + (squared(y) * (1.0f - cosVal)));
|
||||
result.e[1][2] = ((z * y * (1.0f - cosVal)) + (x * sinVal));
|
||||
|
||||
result.e[2][0] = ((x * z * (1.0f - cosVal)) + (y * sinVal));
|
||||
result.e[2][1] = ((y * z * (1.0f - cosVal)) - (x * sinVal));
|
||||
result.e[2][2] = (cosVal + (squared(z) * (1.0f - cosVal)));
|
||||
|
||||
result.e[3][3] = 1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
INTERNAL inline mat4 mat4_scale(const f32 x, const f32 y, const f32 z)
|
||||
{
|
||||
mat4 result = {0};
|
||||
result.e[0][0] = x;
|
||||
result.e[1][1] = y;
|
||||
result.e[2][2] = z;
|
||||
result.e[3][3] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
INTERNAL inline mat4 mat4_mul(const mat4 a, const mat4 b)
|
||||
{
|
||||
mat4 result = {0};
|
||||
for (i32 j = 0; j < 4; j++) {
|
||||
for (i32 i = 0; i < 4; i++) {
|
||||
result.e[j][i] = a.e[0][i] * b.e[j][0]
|
||||
+ a.e[1][i] * b.e[j][1]
|
||||
+ a.e[2][i] * b.e[j][2]
|
||||
+ a.e[3][i] * b.e[j][3];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
INTERNAL inline v4 mat4_mul_v4(const mat4 a, const v4 b)
|
||||
{
|
||||
v4 result = {0};
|
||||
result.x =
|
||||
a.e[0][0] * b.x + a.e[0][1] * b.y + a.e[0][2] * b.z + a.e[0][3] * b.w;
|
||||
result.y =
|
||||
a.e[1][0] * b.x + a.e[1][1] * b.y + a.e[1][2] * b.z + a.e[1][3] * b.w;
|
||||
result.z =
|
||||
a.e[2][0] * b.x + a.e[2][1] * b.y + a.e[2][2] * b.z + a.e[2][3] * b.w;
|
||||
result.w =
|
||||
a.e[3][0] * b.x + a.e[3][1] * b.y + a.e[3][2] * b.z + a.e[3][3] * b.w;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
#include <Dengine/Common.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <Windows.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
#ifndef DENGINE_RENDERER_H
|
||||
#define DENGINE_RENDERER_H
|
||||
|
||||
#include <Dengine/Common.h>
|
||||
#include <Dengine/Math.h>
|
||||
#include <Dengine/Entity.h>
|
||||
#include <Dengine/Shader.h>
|
||||
#include <Dengine/Texture.h>
|
||||
|
||||
#include <GLM/glm.hpp>
|
||||
|
||||
struct Renderer
|
||||
typedef struct Renderer
|
||||
{
|
||||
Shader *shader;
|
||||
GLuint quadVAO;
|
||||
};
|
||||
} Renderer;
|
||||
|
||||
void renderer_entity(Renderer *renderer, Entity *entity, GLfloat rotate = 0.0f,
|
||||
glm::vec3 color = glm::vec3(1.0f));
|
||||
void renderer_entity(Renderer *renderer, Entity *entity, f32 rotate,
|
||||
v3 color);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
#ifndef DENGINE_SHADER_H
|
||||
#define DENGINE_SHADER_H
|
||||
|
||||
#include <Dengine/Common.h>
|
||||
#include <Dengine/OpenGL.h>
|
||||
#include <Dengine/Math.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
struct Shader
|
||||
typedef struct Shader
|
||||
{
|
||||
GLuint id;
|
||||
};
|
||||
} Shader;
|
||||
|
||||
const i32 shader_loadProgram(Shader *const shader,
|
||||
const GLuint vertexShader,
|
||||
@@ -18,7 +16,7 @@ const i32 shader_loadProgram(Shader *const shader,
|
||||
void shader_uniformSet1i(Shader *const shader, const GLchar *name,
|
||||
const GLuint data);
|
||||
void shader_uniformSetMat4fv(Shader *const shader, const GLchar *name,
|
||||
const glm::mat4 data);
|
||||
mat4 data);
|
||||
|
||||
void shader_use(const Shader *const shader);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <Dengine/Common.h>
|
||||
#include <Dengine/OpenGL.h>
|
||||
|
||||
struct Texture
|
||||
typedef struct Texture
|
||||
{
|
||||
// Holds the ID of the texture object, used for all texture operations to
|
||||
// reference to this particlar texture
|
||||
@@ -26,7 +26,7 @@ struct Texture
|
||||
GLuint filterMinification;
|
||||
// Filtering mode if texture pixels > screen pixels
|
||||
GLuint filterMagnification;
|
||||
};
|
||||
} Texture;
|
||||
|
||||
// Generates texture from image data
|
||||
Texture genTexture(const GLuint width, const GLuint height,
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
#ifndef WORLDTRAVELLER_GAME_H
|
||||
#define WORLDTRAVELLER_GAME_H
|
||||
|
||||
#include <Dengine/AssetManager.h>
|
||||
#include <Dengine/Common.h>
|
||||
#include <Dengine/Entity.h>
|
||||
#include <Dengine/Math.h>
|
||||
#include <Dengine/OpenGL.h>
|
||||
#include <Dengine/Renderer.h>
|
||||
#include <Dengine/Shader.h>
|
||||
|
||||
#define NUM_KEYS 1024
|
||||
#define METERS_TO_PIXEL 100
|
||||
@@ -19,15 +15,15 @@ enum State
|
||||
state_win
|
||||
};
|
||||
|
||||
struct GameState
|
||||
typedef struct GameState
|
||||
{
|
||||
State state;
|
||||
GLboolean keys[NUM_KEYS];
|
||||
enum State state;
|
||||
b32 keys[NUM_KEYS];
|
||||
i32 width, height;
|
||||
|
||||
Renderer renderer;
|
||||
Entity hero;
|
||||
};
|
||||
} GameState;
|
||||
|
||||
void worldTraveller_gameInit(GameState *state);
|
||||
void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt);
|
||||
|
||||
Reference in New Issue
Block a user