mirror of
https://github.com/HandmadeMath/HandmadeMath.git
synced 2025-10-17 14:11:47 +00:00
Add a bit of a component tree
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
BUILD_DIR=build
|
BUILD_DIR=build
|
||||||
|
|
||||||
|
CXXFLAGS+=-std=c++11
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(BUILD_DIR)
|
rm -rf $(BUILD_DIR)
|
||||||
|
|
||||||
@@ -13,8 +15,8 @@ example:
|
|||||||
mkdir -p $(BUILD_DIR)/example
|
mkdir -p $(BUILD_DIR)/example
|
||||||
cd $(BUILD_DIR)/example \
|
cd $(BUILD_DIR)/example \
|
||||||
&& $(CXX) $(CPPFLAGS) $(CXXFLAGS) -ohmmexample \
|
&& $(CXX) $(CPPFLAGS) $(CXXFLAGS) -ohmmexample \
|
||||||
-I../../external/glfw/include \
|
-I../../external/glfw/include -I../../.. \
|
||||||
-L../glfw/src \
|
-L../glfw/src \
|
||||||
-lglew -lglfw3 \
|
-lglew -lglfw3 \
|
||||||
-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo \
|
-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo \
|
||||||
../../src/main.cpp
|
../../src/*.cpp
|
||||||
|
115
example/src/Cube.h
Normal file
115
example/src/Cube.h
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
#include <HandmadeMath.h>
|
||||||
|
|
||||||
|
#include "Entity.h"
|
||||||
|
|
||||||
|
#ifndef HMME_CUBE_H
|
||||||
|
#define HMME_CUBE_H
|
||||||
|
|
||||||
|
// Our vertices. Three consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
|
||||||
|
// A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
|
||||||
|
static const GLfloat cubeVertices[] = {
|
||||||
|
-1.0f,-1.0f,-1.0f, // triangle 1 : begin
|
||||||
|
-1.0f,-1.0f, 1.0f,
|
||||||
|
-1.0f, 1.0f, 1.0f, // triangle 1 : end
|
||||||
|
1.0f, 1.0f,-1.0f, // triangle 2 : begin
|
||||||
|
-1.0f,-1.0f,-1.0f,
|
||||||
|
-1.0f, 1.0f,-1.0f, // triangle 2 : end
|
||||||
|
1.0f,-1.0f, 1.0f,
|
||||||
|
-1.0f,-1.0f,-1.0f,
|
||||||
|
1.0f,-1.0f,-1.0f,
|
||||||
|
1.0f, 1.0f,-1.0f,
|
||||||
|
1.0f,-1.0f,-1.0f,
|
||||||
|
-1.0f,-1.0f,-1.0f,
|
||||||
|
-1.0f,-1.0f,-1.0f,
|
||||||
|
-1.0f, 1.0f, 1.0f,
|
||||||
|
-1.0f, 1.0f,-1.0f,
|
||||||
|
1.0f,-1.0f, 1.0f,
|
||||||
|
-1.0f,-1.0f, 1.0f,
|
||||||
|
-1.0f,-1.0f,-1.0f,
|
||||||
|
-1.0f, 1.0f, 1.0f,
|
||||||
|
-1.0f,-1.0f, 1.0f,
|
||||||
|
1.0f,-1.0f, 1.0f,
|
||||||
|
1.0f, 1.0f, 1.0f,
|
||||||
|
1.0f,-1.0f,-1.0f,
|
||||||
|
1.0f, 1.0f,-1.0f,
|
||||||
|
1.0f,-1.0f,-1.0f,
|
||||||
|
1.0f, 1.0f, 1.0f,
|
||||||
|
1.0f,-1.0f, 1.0f,
|
||||||
|
1.0f, 1.0f, 1.0f,
|
||||||
|
1.0f, 1.0f,-1.0f,
|
||||||
|
-1.0f, 1.0f,-1.0f,
|
||||||
|
1.0f, 1.0f, 1.0f,
|
||||||
|
-1.0f, 1.0f,-1.0f,
|
||||||
|
-1.0f, 1.0f, 1.0f,
|
||||||
|
1.0f, 1.0f, 1.0f,
|
||||||
|
-1.0f, 1.0f, 1.0f,
|
||||||
|
1.0f,-1.0f, 1.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
// One color for each vertex. They were generated randomly.
|
||||||
|
static const GLfloat cubeColors[] = {
|
||||||
|
0.583f, 0.771f, 0.014f,
|
||||||
|
0.609f, 0.115f, 0.436f,
|
||||||
|
0.327f, 0.483f, 0.844f,
|
||||||
|
0.822f, 0.569f, 0.201f,
|
||||||
|
0.435f, 0.602f, 0.223f,
|
||||||
|
0.310f, 0.747f, 0.185f,
|
||||||
|
0.597f, 0.770f, 0.761f,
|
||||||
|
0.559f, 0.436f, 0.730f,
|
||||||
|
0.359f, 0.583f, 0.152f,
|
||||||
|
0.483f, 0.596f, 0.789f,
|
||||||
|
0.559f, 0.861f, 0.639f,
|
||||||
|
0.195f, 0.548f, 0.859f,
|
||||||
|
0.014f, 0.184f, 0.576f,
|
||||||
|
0.771f, 0.328f, 0.970f,
|
||||||
|
0.406f, 0.615f, 0.116f,
|
||||||
|
0.676f, 0.977f, 0.133f,
|
||||||
|
0.971f, 0.572f, 0.833f,
|
||||||
|
0.140f, 0.616f, 0.489f,
|
||||||
|
0.997f, 0.513f, 0.064f,
|
||||||
|
0.945f, 0.719f, 0.592f,
|
||||||
|
0.543f, 0.021f, 0.978f,
|
||||||
|
0.279f, 0.317f, 0.505f,
|
||||||
|
0.167f, 0.620f, 0.077f,
|
||||||
|
0.347f, 0.857f, 0.137f,
|
||||||
|
0.055f, 0.953f, 0.042f,
|
||||||
|
0.714f, 0.505f, 0.345f,
|
||||||
|
0.783f, 0.290f, 0.734f,
|
||||||
|
0.722f, 0.645f, 0.174f,
|
||||||
|
0.302f, 0.455f, 0.848f,
|
||||||
|
0.225f, 0.587f, 0.040f,
|
||||||
|
0.517f, 0.713f, 0.338f,
|
||||||
|
0.053f, 0.959f, 0.120f,
|
||||||
|
0.393f, 0.621f, 0.362f,
|
||||||
|
0.673f, 0.211f, 0.457f,
|
||||||
|
0.820f, 0.883f, 0.371f,
|
||||||
|
0.982f, 0.099f, 0.879f
|
||||||
|
};
|
||||||
|
|
||||||
|
class Cube : public Entity {
|
||||||
|
public:
|
||||||
|
float x = 0;
|
||||||
|
|
||||||
|
Cube() {
|
||||||
|
Entity::RenderComponent c;
|
||||||
|
|
||||||
|
glGenVertexArrays(1, &c.vaoID);
|
||||||
|
|
||||||
|
glGenBuffers(1, &c.vertexBufferID);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, c.vertexBufferID);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
glGenBuffers(1, &c.colorBufferID);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, c.colorBufferID);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeColors), cubeColors, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
renderComponent = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Tick(float deltaTime) override {
|
||||||
|
x += 0.1f;
|
||||||
|
position.X = HMM_SINF(x);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
89
example/src/Entity.h
Normal file
89
example/src/Entity.h
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <HandmadeMath.h>
|
||||||
|
|
||||||
|
#ifndef HMME_ENTITY_H
|
||||||
|
#define HMME_ENTITY_H
|
||||||
|
|
||||||
|
class Entity {
|
||||||
|
public:
|
||||||
|
hmm_vec3 position = HMM_Vec3(0.0f, 0.0f, 0.0f);
|
||||||
|
hmm_quaternion rotation = HMM_Quaternion(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
|
hmm_vec3 scale = HMM_Vec3(1.0f, 1.0f, 1.0f);
|
||||||
|
|
||||||
|
std::vector<Entity*> children;
|
||||||
|
|
||||||
|
void AddChild(Entity* e) {
|
||||||
|
children.push_back(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void Tick(float deltaTime) {}
|
||||||
|
|
||||||
|
struct RenderComponent {
|
||||||
|
GLuint vaoID;
|
||||||
|
GLuint vertexBufferID;
|
||||||
|
GLuint colorBufferID;
|
||||||
|
};
|
||||||
|
RenderComponent renderComponent;
|
||||||
|
|
||||||
|
// Context for rendering and stuff
|
||||||
|
hmm_mat4 modelMatrix;
|
||||||
|
};
|
||||||
|
|
||||||
|
class EntityIterator {
|
||||||
|
public:
|
||||||
|
typedef struct State {
|
||||||
|
Entity *entity;
|
||||||
|
int childIndex;
|
||||||
|
|
||||||
|
State(Entity *e) {
|
||||||
|
entity = e;
|
||||||
|
childIndex = -1;
|
||||||
|
}
|
||||||
|
} State;
|
||||||
|
|
||||||
|
EntityIterator(Entity *e) {
|
||||||
|
stack.push_back(State(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HasNext() {
|
||||||
|
return !stack.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
Entity *Next() {
|
||||||
|
Entity *result = 0;
|
||||||
|
|
||||||
|
// Pass 1 - get a result by either grabbing the current entity or making another state
|
||||||
|
while (true) {
|
||||||
|
State *state = &stack.back();
|
||||||
|
|
||||||
|
if (state->childIndex < 0) {
|
||||||
|
result = state->entity;
|
||||||
|
state->childIndex = 0;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
int nextIndex = state->childIndex;
|
||||||
|
state->childIndex++;
|
||||||
|
stack.push_back(State(state->entity->children[nextIndex]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pass 2 - remove exhausted states from the stack
|
||||||
|
while (!stack.empty()) {
|
||||||
|
State state = stack.back();
|
||||||
|
|
||||||
|
if (state.childIndex >= state.entity->children.size()) {
|
||||||
|
stack.pop_back();
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<State> stack;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
2
example/src/HandmadeMath.cpp
Normal file
2
example/src/HandmadeMath.cpp
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#define HANDMADE_MATH_IMPLEMENTATION
|
||||||
|
#include <HandmadeMath.h>
|
@@ -3,10 +3,15 @@
|
|||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include "loadshaders.cpp"
|
#include <HandmadeMath.h>
|
||||||
|
|
||||||
#define HANDMADE_MATH_IMPLEMENTATION
|
#include "shaders.h"
|
||||||
#include "../../HandmadeMath.h"
|
|
||||||
|
#include "Entity.h"
|
||||||
|
#include "Cube.h"
|
||||||
|
|
||||||
|
void TickTree(Entity *e);
|
||||||
|
void ComputeModelMatrices(Entity *ep, hmm_mat4 parentModelMatrix);
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -41,105 +46,6 @@ int main()
|
|||||||
// Ensure we can capture the escape key being pressed below
|
// Ensure we can capture the escape key being pressed below
|
||||||
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
|
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
|
||||||
|
|
||||||
GLuint VertexArrayID;
|
|
||||||
glGenVertexArrays(1, &VertexArrayID);
|
|
||||||
glBindVertexArray(VertexArrayID);
|
|
||||||
|
|
||||||
// Our vertices. Three consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
|
|
||||||
// A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
|
|
||||||
static const GLfloat g_vertex_buffer_data[] = {
|
|
||||||
-1.0f,-1.0f,-1.0f, // triangle 1 : begin
|
|
||||||
-1.0f,-1.0f, 1.0f,
|
|
||||||
-1.0f, 1.0f, 1.0f, // triangle 1 : end
|
|
||||||
1.0f, 1.0f,-1.0f, // triangle 2 : begin
|
|
||||||
-1.0f,-1.0f,-1.0f,
|
|
||||||
-1.0f, 1.0f,-1.0f, // triangle 2 : end
|
|
||||||
1.0f,-1.0f, 1.0f,
|
|
||||||
-1.0f,-1.0f,-1.0f,
|
|
||||||
1.0f,-1.0f,-1.0f,
|
|
||||||
1.0f, 1.0f,-1.0f,
|
|
||||||
1.0f,-1.0f,-1.0f,
|
|
||||||
-1.0f,-1.0f,-1.0f,
|
|
||||||
-1.0f,-1.0f,-1.0f,
|
|
||||||
-1.0f, 1.0f, 1.0f,
|
|
||||||
-1.0f, 1.0f,-1.0f,
|
|
||||||
1.0f,-1.0f, 1.0f,
|
|
||||||
-1.0f,-1.0f, 1.0f,
|
|
||||||
-1.0f,-1.0f,-1.0f,
|
|
||||||
-1.0f, 1.0f, 1.0f,
|
|
||||||
-1.0f,-1.0f, 1.0f,
|
|
||||||
1.0f,-1.0f, 1.0f,
|
|
||||||
1.0f, 1.0f, 1.0f,
|
|
||||||
1.0f,-1.0f,-1.0f,
|
|
||||||
1.0f, 1.0f,-1.0f,
|
|
||||||
1.0f,-1.0f,-1.0f,
|
|
||||||
1.0f, 1.0f, 1.0f,
|
|
||||||
1.0f,-1.0f, 1.0f,
|
|
||||||
1.0f, 1.0f, 1.0f,
|
|
||||||
1.0f, 1.0f,-1.0f,
|
|
||||||
-1.0f, 1.0f,-1.0f,
|
|
||||||
1.0f, 1.0f, 1.0f,
|
|
||||||
-1.0f, 1.0f,-1.0f,
|
|
||||||
-1.0f, 1.0f, 1.0f,
|
|
||||||
1.0f, 1.0f, 1.0f,
|
|
||||||
-1.0f, 1.0f, 1.0f,
|
|
||||||
1.0f,-1.0f, 1.0f
|
|
||||||
};
|
|
||||||
|
|
||||||
// This will identify our vertex buffer
|
|
||||||
GLuint vertexbuffer;
|
|
||||||
// Generate 1 buffer, put the resulting identifier in vertexbuffer
|
|
||||||
glGenBuffers(1, &vertexbuffer);
|
|
||||||
// The following commands will talk about our 'vertexbuffer' buffer
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
|
||||||
// Give our vertices to OpenGL.
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
// One color for each vertex. They were generated randomly.
|
|
||||||
static const GLfloat g_color_buffer_data[] = {
|
|
||||||
0.583f, 0.771f, 0.014f,
|
|
||||||
0.609f, 0.115f, 0.436f,
|
|
||||||
0.327f, 0.483f, 0.844f,
|
|
||||||
0.822f, 0.569f, 0.201f,
|
|
||||||
0.435f, 0.602f, 0.223f,
|
|
||||||
0.310f, 0.747f, 0.185f,
|
|
||||||
0.597f, 0.770f, 0.761f,
|
|
||||||
0.559f, 0.436f, 0.730f,
|
|
||||||
0.359f, 0.583f, 0.152f,
|
|
||||||
0.483f, 0.596f, 0.789f,
|
|
||||||
0.559f, 0.861f, 0.639f,
|
|
||||||
0.195f, 0.548f, 0.859f,
|
|
||||||
0.014f, 0.184f, 0.576f,
|
|
||||||
0.771f, 0.328f, 0.970f,
|
|
||||||
0.406f, 0.615f, 0.116f,
|
|
||||||
0.676f, 0.977f, 0.133f,
|
|
||||||
0.971f, 0.572f, 0.833f,
|
|
||||||
0.140f, 0.616f, 0.489f,
|
|
||||||
0.997f, 0.513f, 0.064f,
|
|
||||||
0.945f, 0.719f, 0.592f,
|
|
||||||
0.543f, 0.021f, 0.978f,
|
|
||||||
0.279f, 0.317f, 0.505f,
|
|
||||||
0.167f, 0.620f, 0.077f,
|
|
||||||
0.347f, 0.857f, 0.137f,
|
|
||||||
0.055f, 0.953f, 0.042f,
|
|
||||||
0.714f, 0.505f, 0.345f,
|
|
||||||
0.783f, 0.290f, 0.734f,
|
|
||||||
0.722f, 0.645f, 0.174f,
|
|
||||||
0.302f, 0.455f, 0.848f,
|
|
||||||
0.225f, 0.587f, 0.040f,
|
|
||||||
0.517f, 0.713f, 0.338f,
|
|
||||||
0.053f, 0.959f, 0.120f,
|
|
||||||
0.393f, 0.621f, 0.362f,
|
|
||||||
0.673f, 0.211f, 0.457f,
|
|
||||||
0.820f, 0.883f, 0.371f,
|
|
||||||
0.982f, 0.099f, 0.879f
|
|
||||||
};
|
|
||||||
|
|
||||||
GLuint colorbuffer;
|
|
||||||
glGenBuffers(1, &colorbuffer);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
// Create and compile our GLSL program from the shaders
|
// Create and compile our GLSL program from the shaders
|
||||||
GLuint programID = LoadShaders("src/vertex.glsl", "src/fragment.glsl");
|
GLuint programID = LoadShaders("src/vertex.glsl", "src/fragment.glsl");
|
||||||
if (!programID) {
|
if (!programID) {
|
||||||
@@ -155,50 +61,74 @@ int main()
|
|||||||
// Accept fragment if it closer to the camera than the former one
|
// Accept fragment if it closer to the camera than the former one
|
||||||
glDepthFunc(GL_LESS);
|
glDepthFunc(GL_LESS);
|
||||||
|
|
||||||
|
Cube root = Cube();
|
||||||
|
|
||||||
|
Cube child = Cube();
|
||||||
|
child.position = HMM_Vec3(2.1f, 0.0f, 0.0f);
|
||||||
|
|
||||||
|
root.AddChild(&child);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
// 1st attribute buffer : vertices
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
|
||||||
glVertexAttribPointer(
|
|
||||||
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
|
|
||||||
3, // size
|
|
||||||
GL_FLOAT, // type
|
|
||||||
GL_FALSE, // normalized?
|
|
||||||
0, // stride
|
|
||||||
(void*)0 // array buffer offset
|
|
||||||
);
|
|
||||||
|
|
||||||
// 2nd attribute buffer : colors
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
|
|
||||||
glVertexAttribPointer(
|
|
||||||
1, // attribute. No particular reason for 1, but must match the layout in the shader.
|
|
||||||
3, // size
|
|
||||||
GL_FLOAT, // type
|
|
||||||
GL_FALSE, // normalized?
|
|
||||||
0, // stride
|
|
||||||
(void*)0 // array buffer offset
|
|
||||||
);
|
|
||||||
|
|
||||||
// Use our shader
|
|
||||||
glUseProgram(programID);
|
|
||||||
|
|
||||||
hmm_mat4 projection = HMM_Perspective(90.0f, 1024.0f / 768.0f, 0.1f, 100.0f);
|
hmm_mat4 projection = HMM_Perspective(90.0f, 1024.0f / 768.0f, 0.1f, 100.0f);
|
||||||
// hmm_mat4 projection = HMM_Orthographic(-2.0f, 2.0f, -2.0f, 2.0f, 0.1f, 100.0f);
|
hmm_mat4 view = HMM_LookAt(HMM_Vec3(3.0f, 3.0f, 4.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
|
||||||
hmm_mat4 view = HMM_LookAt(HMM_Vec3(4.0f, 3.0f, 3.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
|
hmm_mat4 vp = projection * view;
|
||||||
hmm_mat4 model = HMM_Mat4d(1.0f);
|
|
||||||
|
|
||||||
hmm_mat4 mvp = projection * view * model;
|
TickTree(&root);
|
||||||
|
ComputeModelMatrices(&root, HMM_Mat4d(1.0f));
|
||||||
|
|
||||||
// Send our transformation to the currently bound shader, in the "MVP" uniform
|
auto it = EntityIterator(&root);
|
||||||
// This is done in the main loop since each model will have a different MVP matrix (At least for the M part)
|
|
||||||
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &mvp.Elements[0][0]);
|
|
||||||
|
|
||||||
// Draw the triangle !
|
while (it.HasNext()) {
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 36); // Starting from vertex 0; 3 vertices total -> 1 triangle
|
Entity *e = it.Next();
|
||||||
glDisableVertexAttribArray(0);
|
|
||||||
|
if (!e->renderComponent.vaoID) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Entity::RenderComponent rc = e->renderComponent;
|
||||||
|
|
||||||
|
glBindVertexArray(rc.vaoID);
|
||||||
|
|
||||||
|
// 1st attribute buffer : vertices
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, rc.vertexBufferID);
|
||||||
|
glVertexAttribPointer(
|
||||||
|
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
|
||||||
|
3, // size
|
||||||
|
GL_FLOAT, // type
|
||||||
|
GL_FALSE, // normalized?
|
||||||
|
0, // stride
|
||||||
|
(void*)0 // array buffer offset
|
||||||
|
);
|
||||||
|
|
||||||
|
// 2nd attribute buffer : colors
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, rc.colorBufferID);
|
||||||
|
glVertexAttribPointer(
|
||||||
|
1, // attribute. No particular reason for 1, but must match the layout in the shader.
|
||||||
|
3, // size
|
||||||
|
GL_FLOAT, // type
|
||||||
|
GL_FALSE, // normalized?
|
||||||
|
0, // stride
|
||||||
|
(void*)0 // array buffer offset
|
||||||
|
);
|
||||||
|
|
||||||
|
// Use our shader
|
||||||
|
glUseProgram(programID);
|
||||||
|
|
||||||
|
hmm_mat4 mvp = vp * e->modelMatrix;
|
||||||
|
|
||||||
|
// Send our transformation to the currently bound shader, in the "MVP" uniform
|
||||||
|
// This is done in the main loop since each model will have a different MVP matrix (At least for the M part)
|
||||||
|
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &mvp.Elements[0][0]);
|
||||||
|
|
||||||
|
// Draw the triangle!
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 36); // Starting from vertex 0; 3 vertices total -> 1 triangle
|
||||||
|
glDisableVertexAttribArray(0);
|
||||||
|
glDisableVertexAttribArray(1);
|
||||||
|
}
|
||||||
|
|
||||||
// Swap buffers
|
// Swap buffers
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
@@ -209,3 +139,19 @@ int main()
|
|||||||
&& glfwWindowShouldClose(window) == 0
|
&& glfwWindowShouldClose(window) == 0
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TickTree(Entity *e) {
|
||||||
|
e->Tick(0);
|
||||||
|
|
||||||
|
for (auto child : e->children) {
|
||||||
|
TickTree(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ComputeModelMatrices(Entity *e, hmm_mat4 parentModelMatrix) {
|
||||||
|
e->modelMatrix = parentModelMatrix * HMM_Translate(e->position) * HMM_QuaternionToMat4(e->rotation) * HMM_Scale(e->scale);
|
||||||
|
|
||||||
|
for (int i = 0; i < e->children.size(); i++) {
|
||||||
|
ComputeModelMatrices(e->children[i], e->modelMatrix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -5,6 +5,8 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
#include "shaders.h"
|
||||||
|
|
||||||
GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path) {
|
GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path) {
|
||||||
// Create the shaders
|
// Create the shaders
|
||||||
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
9
example/src/shaders.h
Normal file
9
example/src/shaders.h
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include <GL/glew.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
#ifndef HMME_SHADERS_H
|
||||||
|
#define HMME_SHADERS_H
|
||||||
|
|
||||||
|
GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path);
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user