update
This commit is contained in:
10
assets/shaders/2d-solid.fragment.glsl
Normal file
10
assets/shaders/2d-solid.fragment.glsl
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 frag_color;
|
||||||
|
|
||||||
|
uniform vec3 color;
|
||||||
|
|
||||||
|
in vec2 uv;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
frag_color = vec4(color, 0.0);
|
||||||
|
};
|
||||||
10
assets/shaders/2d-tex.fragment.glsl
Normal file
10
assets/shaders/2d-tex.fragment.glsl
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 frag_color;
|
||||||
|
|
||||||
|
uniform sampler2D texture;
|
||||||
|
|
||||||
|
in vec2 uv;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
frag_color = texture(texture, uv);
|
||||||
|
};
|
||||||
12
assets/shaders/2d.vertex.glsl
Normal file
12
assets/shaders/2d.vertex.glsl
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec2 xy;
|
||||||
|
layout (location = 1) in vec2 uv;
|
||||||
|
|
||||||
|
uniform mat4 projection;
|
||||||
|
|
||||||
|
out vec2 tex_coord;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = projection * vec4(xy, 0.0, 1.0);
|
||||||
|
tex_coord = uv;
|
||||||
|
}
|
||||||
@@ -1,42 +1,64 @@
|
|||||||
#include <vector>
|
|
||||||
#include "Mesh.h"
|
#include "Mesh.h"
|
||||||
#include "../lib/loaders/tinyobj.h"
|
#include "../lib/loaders/tinyobj.h"
|
||||||
|
#include "../lib/djstdlib/os.h"
|
||||||
|
|
||||||
|
static void tinyobj_get_filedata(void* ctx, const char* filename, const int is_mtl, const char* obj_filename, char** data, size_t* len) {
|
||||||
|
string file = os_readEntireFile((Arena *)ctx, "./assets/models/cube.obj"_s);
|
||||||
|
*data = file.str;
|
||||||
|
*len = file.length;
|
||||||
|
}
|
||||||
|
|
||||||
Mesh createMesh(const char* obj_file) {
|
Mesh createMesh(const char* obj_file) {
|
||||||
Scratch temp = scratchStart(0, 0);
|
Scratch temp = scratchStart(0, 0);
|
||||||
|
|
||||||
Mesh result = {0};
|
Mesh result = {0};
|
||||||
|
|
||||||
tinyobj::ObjReader reader = tinyobj::ObjReader();
|
tinyobj_attrib_t attrib;
|
||||||
bool success = reader.ParseFromFile(obj_file);
|
tinyobj_shape_t* shapes = NULL;
|
||||||
print("%s\n", reader.Error().c_str());
|
size_t num_shapes;
|
||||||
|
tinyobj_material_t* materials = NULL;
|
||||||
|
size_t num_materials;
|
||||||
|
|
||||||
const tinyobj::attrib_t attrib = reader.GetAttrib();
|
int success = tinyobj_parse_obj(
|
||||||
|
&attrib,
|
||||||
|
&shapes,
|
||||||
|
&num_shapes,
|
||||||
|
&materials,
|
||||||
|
&num_materials,
|
||||||
|
obj_file,
|
||||||
|
tinyobj_get_filedata,
|
||||||
|
(void *)temp.arena,
|
||||||
|
TINYOBJ_FLAG_TRIANGULATE
|
||||||
|
);
|
||||||
|
|
||||||
std::vector<tinyobj::index_t> indices_t = reader.GetShapes().at(0).mesh.indices;
|
if (success != TINYOBJ_SUCCESS || num_shapes <= 0) {
|
||||||
list<uint32> indices = PushFullList(temp.arena, uint32, indices_t.size());
|
print("Failed to load obj from '%s'! Success %i\n", obj_file, success);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
list<real32> vertices = PushFullList(temp.arena, real32, 3*indices_t.size());
|
list<uint32> indices = PushFullList(temp.arena, uint32, attrib.num_faces);
|
||||||
list<real32> normals = PushFullList(temp.arena, real32, 3*indices_t.size());
|
list<real32> vertices = PushFullList(temp.arena, real32, 3*attrib.num_faces);
|
||||||
list<real32> texcoords = PushFullList(temp.arena, real32, 2*indices_t.size());
|
list<real32> normals = PushFullList(temp.arena, real32, 3*attrib.num_faces);
|
||||||
|
list<real32> texcoords = PushFullList(temp.arena, real32, 2*attrib.num_faces);
|
||||||
|
|
||||||
for (int i = 0; i < indices_t.size(); i++) {
|
for (int i = 0; i < attrib.num_faces; i++) {
|
||||||
tinyobj::index_t vertex_data = indices_t[i];
|
tinyobj_vertex_index_t vertex_data = attrib.faces[i];
|
||||||
vertices.data[3*i] = attrib.vertices[3*vertex_data.vertex_index];
|
|
||||||
vertices.data[3*i+1] = attrib.vertices[3*vertex_data.vertex_index + 1];
|
|
||||||
vertices.data[3*i+2] = attrib.vertices[3*vertex_data.vertex_index + 2];
|
|
||||||
|
|
||||||
normals.data[3*i] = attrib.normals[3*vertex_data.normal_index];
|
vertices.data[3*i] = attrib.vertices[3*vertex_data.v_idx];
|
||||||
normals.data[3*i+1] = attrib.normals[3*vertex_data.normal_index + 1];
|
vertices.data[3*i+1] = attrib.vertices[3*vertex_data.v_idx + 1];
|
||||||
normals.data[3*i+2] = attrib.normals[3*vertex_data.normal_index + 2];
|
vertices.data[3*i+2] = attrib.vertices[3*vertex_data.v_idx + 2];
|
||||||
|
|
||||||
texcoords.data[2*i] = attrib.texcoords[2*vertex_data.texcoord_index];
|
normals.data[3*i] = attrib.normals[3*vertex_data.vn_idx];
|
||||||
texcoords.data[2*i+1] = attrib.texcoords[2*vertex_data.texcoord_index + 1];
|
normals.data[3*i+1] = attrib.normals[3*vertex_data.vn_idx + 1];
|
||||||
|
normals.data[3*i+2] = attrib.normals[3*vertex_data.vn_idx + 2];
|
||||||
|
|
||||||
|
texcoords.data[2*i] = attrib.texcoords[2*vertex_data.vt_idx];
|
||||||
|
texcoords.data[2*i+1] = attrib.texcoords[2*vertex_data.vt_idx + 1];
|
||||||
|
|
||||||
indices.data[i] = i;
|
indices.data[i] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.num_indices = indices_t.size();
|
result.num_indices = attrib.num_faces*3;
|
||||||
glGenVertexArrays(1, &result.vao);
|
glGenVertexArrays(1, &result.vao);
|
||||||
glGenBuffers(1, &result.vbo_xyz);
|
glGenBuffers(1, &result.vbo_xyz);
|
||||||
glGenBuffers(1, &result.vbo_uv);
|
glGenBuffers(1, &result.vbo_uv);
|
||||||
|
|||||||
@@ -86,6 +86,14 @@ void setUniform3fv(int uniformLocation, glm::vec3 *vector) {
|
|||||||
glUniform3fv(uniformLocation, 1, glm::value_ptr(*vector));
|
glUniform3fv(uniformLocation, 1, glm::value_ptr(*vector));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setUniform2fv(Shader *s, const char *uniformName, glm::vec2 *vector) {
|
||||||
|
glUniform2fv(glGetUniformLocation(s->prog_id, uniformName), 1, glm::value_ptr(*vector));
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUniform2fv(int uniformLocation, glm::vec2 *vector) {
|
||||||
|
glUniform2fv(uniformLocation, 1, glm::value_ptr(*vector));
|
||||||
|
}
|
||||||
|
|
||||||
int getUniformLocation(Shader *s, const char *uniformName) {
|
int getUniformLocation(Shader *s, const char *uniformName) {
|
||||||
return glGetUniformLocation(s->prog_id, uniformName);
|
return glGetUniformLocation(s->prog_id, uniformName);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#ifndef LEDDA_SHADER_H
|
#ifndef LEDDA_SHADER_H
|
||||||
#define LEDDA_SHADER_H
|
#define LEDDA_SHADER_H
|
||||||
|
|
||||||
#include "../lib/djstdlib/core.h"
|
|
||||||
#include "glm/glm.hpp"
|
#include "glm/glm.hpp"
|
||||||
|
#include "../lib/djstdlib/core.h"
|
||||||
|
|
||||||
struct Shader {
|
struct Shader {
|
||||||
uint32 prog_id;
|
uint32 prog_id;
|
||||||
@@ -19,6 +19,9 @@ void setUniform4fv(int uniformLocation, glm::vec4 *vector);
|
|||||||
void setUniform3fv(Shader *s, const char *uniformName, glm::vec3 *vector);
|
void setUniform3fv(Shader *s, const char *uniformName, glm::vec3 *vector);
|
||||||
void setUniform3fv(int uniformLocation, glm::vec3 *vector);
|
void setUniform3fv(int uniformLocation, glm::vec3 *vector);
|
||||||
|
|
||||||
|
void setUniform2fv(Shader *s, const char *uniformName, glm::vec2 *vector);
|
||||||
|
void setUniform2fv(int uniformLocation, glm::vec2 *vector);
|
||||||
|
|
||||||
int getUniformLocation(Shader *s, const char *uniformName);
|
int getUniformLocation(Shader *s, const char *uniformName);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -64,10 +64,10 @@ uint32 cube_indices[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
real32 square_xyz[] = {
|
real32 square_xyz[] = {
|
||||||
0.5f, 0.5f, 0.0f,
|
200.0f, 200.0f, 0.0f,
|
||||||
0.5f, -0.5f, 0.0f,
|
200.0f, -200.0f, 0.0f,
|
||||||
-0.5f, -0.5f, 0.0f,
|
-200.0f, -200.0f, 0.0f,
|
||||||
-0.5f, 0.5f, 0.0f,
|
-200.0f, 200.0f, 0.0f,
|
||||||
};
|
};
|
||||||
|
|
||||||
real32 square_uv[] = {
|
real32 square_uv[] = {
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
42
src/main.cpp
42
src/main.cpp
@@ -22,7 +22,7 @@
|
|||||||
// Library initialisation
|
// Library initialisation
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include "lib/loaders/stb_image.h"
|
#include "lib/loaders/stb_image.h"
|
||||||
#define TINYOBJLOADER_IMPLEMENTATION
|
#define TINYOBJ_LOADER_C_IMPLEMENTATION
|
||||||
#include "lib/loaders/tinyobj.h"
|
#include "lib/loaders/tinyobj.h"
|
||||||
|
|
||||||
#define PI (real32)3.14159265358979323846264338327950288
|
#define PI (real32)3.14159265358979323846264338327950288
|
||||||
@@ -223,6 +223,7 @@ void update_viewport_from_frame(WindowDims* window_dims, Frame* frame) {
|
|||||||
glViewport(frame->x, window_dims->height - frame->y - frame->height, frame->width, frame->height);
|
glViewport(frame->x, window_dims->height - frame->y - frame->height, frame->width, frame->height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Mesh sprite_mesh = {0};
|
||||||
Mesh cube_mesh = {0};
|
Mesh cube_mesh = {0};
|
||||||
Texture wall_tex = {0};
|
Texture wall_tex = {0};
|
||||||
std::vector<Entity> entities = std::vector<Entity>();
|
std::vector<Entity> entities = std::vector<Entity>();
|
||||||
@@ -352,8 +353,8 @@ Polycube create_polycube_from_repr(Space *repr) {
|
|||||||
for (int z = 0; z < repr->dim_z; z++) {
|
for (int z = 0; z < repr->dim_z; z++) {
|
||||||
if (filledAt(repr, x, y, z)) {
|
if (filledAt(repr, x, y, z)) {
|
||||||
Entity *polycube_segment = get_entity(new_entity());
|
Entity *polycube_segment = get_entity(new_entity());
|
||||||
polycube_segment->mesh=&cube_mesh,
|
polycube_segment->mesh = &cube_mesh;
|
||||||
polycube_segment->tex=&wall_tex;
|
polycube_segment->tex = &wall_tex;
|
||||||
SceneGraphNode *graph_node = get_scene_graph_node(polycube_segment->scene_graph_node);
|
SceneGraphNode *graph_node = get_scene_graph_node(polycube_segment->scene_graph_node);
|
||||||
init_sg_node(graph_node);
|
init_sg_node(graph_node);
|
||||||
graph_node->translation = glm::vec3(
|
graph_node->translation = glm::vec3(
|
||||||
@@ -385,6 +386,19 @@ void recalculate_scene_graph(SceneGraphNode *top) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void draw_rectangle(int x, int y, int width, int height, glm::vec3 color) {
|
||||||
|
setUniform3fv(app_state.active_shader, "color", &color);
|
||||||
|
glm::mat4 ortho = glm::ortho(0.0, 800.0, 600.0, 0.0, -1.0, 1.0);
|
||||||
|
setUniformMat4fv(app_state.active_shader, "projection", &ortho);
|
||||||
|
glBindVertexArray(sprite_mesh.vao);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)sprite_mesh.num_indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_ui() {
|
||||||
|
glUseProgram(app_state.active_shader->prog_id);
|
||||||
|
draw_rectangle(20, 20, 100, 100, glm::vec3( 1, 0, 0 ));
|
||||||
|
}
|
||||||
|
|
||||||
int main_cmd() {
|
int main_cmd() {
|
||||||
interactive_cmd_line_solve_soma();
|
interactive_cmd_line_solve_soma();
|
||||||
return 0;
|
return 0;
|
||||||
@@ -411,12 +425,24 @@ int main_gfx() {
|
|||||||
SceneGraphNode *light_graph_node = get_scene_graph_node(get_entity(app_state.light)->scene_graph_node);
|
SceneGraphNode *light_graph_node = get_scene_graph_node(get_entity(app_state.light)->scene_graph_node);
|
||||||
light_graph_node->translation = glm::vec3(4.0f, 6.0f, 24.0f);
|
light_graph_node->translation = glm::vec3(4.0f, 6.0f, 24.0f);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Shader solid_texture_shader = createShader(
|
||||||
|
"./assets/shaders/2d.vertex.glsl"_s,
|
||||||
|
"./assets/shaders/2d-tex.fragment.glsl"_s);
|
||||||
|
*/
|
||||||
|
|
||||||
|
Shader solid_color_shader = createShader(
|
||||||
|
"./assets/shaders/2d.vertex.glsl"_s,
|
||||||
|
"./assets/shaders/2d-solid.fragment.glsl"_s);
|
||||||
|
|
||||||
Shader phong_shader = createShader(
|
Shader phong_shader = createShader(
|
||||||
"./assets/shaders/phong-solid.vertex.glsl"_s,
|
"./assets/shaders/phong-solid.vertex.glsl"_s,
|
||||||
"./assets/shaders/phong-solid.fragment.glsl"_s);
|
"./assets/shaders/phong-solid.fragment.glsl"_s);
|
||||||
|
|
||||||
app_state.active_shader = &phong_shader;
|
app_state.active_shader = &phong_shader;
|
||||||
|
|
||||||
cube_mesh = createMesh("./assets/models/c000000.obj");
|
sprite_mesh = createMesh(&SQUARE);
|
||||||
|
cube_mesh = createMesh("./assets/models/cube.obj");
|
||||||
wall_tex = createTexture("./assets/textures/brick-wall.jpg");
|
wall_tex = createTexture("./assets/textures/brick-wall.jpg");
|
||||||
|
|
||||||
SceneGraphNode root_node = {};
|
SceneGraphNode root_node = {};
|
||||||
@@ -463,6 +489,7 @@ int main_gfx() {
|
|||||||
Input input = get_current_input(window);
|
Input input = get_current_input(window);
|
||||||
process_input(window, &input, &prevInput);
|
process_input(window, &input, &prevInput);
|
||||||
|
|
||||||
|
glUseProgram(app_state.active_shader->prog_id);
|
||||||
SceneGraphNode *light_graph_node = get_scene_graph_node(get_entity(app_state.light)->scene_graph_node);
|
SceneGraphNode *light_graph_node = get_scene_graph_node(get_entity(app_state.light)->scene_graph_node);
|
||||||
setUniform3fv(app_state.active_shader, "light_pos", &light_graph_node->translation);
|
setUniform3fv(app_state.active_shader, "light_pos", &light_graph_node->translation);
|
||||||
setUniform3fv(app_state.active_shader, "camera", &main_frame.cam->pos);
|
setUniform3fv(app_state.active_shader, "camera", &main_frame.cam->pos);
|
||||||
@@ -484,17 +511,20 @@ int main_gfx() {
|
|||||||
recalculate_scene_graph(&root_node);
|
recalculate_scene_graph(&root_node);
|
||||||
|
|
||||||
setUniform3fv(app_state.active_shader, "solid_color", ¤t_polycube->color);
|
setUniform3fv(app_state.active_shader, "solid_color", ¤t_polycube->color);
|
||||||
|
|
||||||
int model_uniform = getUniformLocation(app_state.active_shader, "model");
|
int model_uniform = getUniformLocation(app_state.active_shader, "model");
|
||||||
for (Entity &entity : entities) {
|
for (Entity &entity : entities) {
|
||||||
if (entity.visible) {
|
if (entity.visible) {
|
||||||
setUniformMat4fv(model_uniform, &get_scene_graph_node(entity.scene_graph_node)->world);
|
setUniformMat4fv(model_uniform, &get_scene_graph_node(entity.scene_graph_node)->world);
|
||||||
glBindTexture(GL_TEXTURE_2D, entity.tex->tex_id);
|
glBindTexture(GL_TEXTURE_2D, entity.tex->tex_id);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)entity.mesh->num_indices);
|
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)entity.mesh->num_indices);
|
||||||
//glDrawElements(GL_TRIANGLES, entity->mesh->num_indices, GL_UNSIGNED_INT, 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app_state.active_shader = &solid_color_shader;
|
||||||
|
draw_ui();
|
||||||
|
app_state.active_shader = &phong_shader;
|
||||||
|
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
prevInput = input;
|
prevInput = input;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user