Preliminary entity system
This commit is contained in:
+2
-1
@@ -1,6 +1,7 @@
|
||||
SRC=src/main.c src/program.c src/resources.c src/camera.c src/chunk/chunk.c \
|
||||
src/chunk/mesh.c src/flags.c src/texture.c src/chunk/manager.c \
|
||||
src/event/manager.c src/event/handlers.c src/state.c src/graphics.c
|
||||
src/event/manager.c src/event/handlers.c src/state.c src/graphics.c \
|
||||
src/entity/meshes.c src/entity/manager.c
|
||||
|
||||
OBJ=$(SRC:.c=.o)
|
||||
CFLAGS=-Werror -Wextra -Wall `pkg-config --cflags sdl3 sdl3-image cglm gl glib-2.0`
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#version 100
|
||||
|
||||
precision mediump float;
|
||||
|
||||
void main ()
|
||||
{
|
||||
gl_FragColor = vec4 (1.0, 1.0, 1.0, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#version 100
|
||||
|
||||
attribute vec4 position;
|
||||
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main ()
|
||||
{
|
||||
gl_Position = projection * view * position;
|
||||
}
|
||||
@@ -2,7 +2,16 @@
|
||||
|
||||
precision mediump float;
|
||||
|
||||
uniform sampler2D texture;
|
||||
|
||||
varying vec2 f_uv;
|
||||
|
||||
void main ()
|
||||
{
|
||||
gl_FragColor = vec4 (0.5, 1.0, 0.5, 1.0);
|
||||
if(texture2D (texture, f_uv).a < 0.1)
|
||||
{
|
||||
discard;
|
||||
}
|
||||
|
||||
gl_FragColor = texture2D (texture, f_uv);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
#version 100
|
||||
|
||||
attribute vec4 position;
|
||||
attribute vec2 uv;
|
||||
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
varying vec2 f_uv;
|
||||
|
||||
void main ()
|
||||
{
|
||||
f_uv = uv;
|
||||
gl_Position = projection * view * position;
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 94 KiB |
@@ -77,7 +77,7 @@ ChunkManager_render (vec3 camera_position, int program)
|
||||
{
|
||||
int cx = 0, cz = 0;
|
||||
cm_world_to_chunk (camera_position[0], camera_position[2], &cx, &cz);
|
||||
|
||||
|
||||
for (int z = cz - RENDER_DISTANCE; z <= cz + RENDER_DISTANCE; ++z)
|
||||
{
|
||||
for (int x = cx - RENDER_DISTANCE; x <= cx + RENDER_DISTANCE; ++x)
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#include "mesh.h"
|
||||
|
||||
/* No need to bind the elements buffer all the time; it's the same for
|
||||
every single ChunkMesh. */
|
||||
|
||||
extern GameFlags game_flags;
|
||||
|
||||
ChunkMesh *
|
||||
@@ -104,7 +107,7 @@ ChunkMesh_render (ChunkMesh *mesh)
|
||||
glBindBuffer (GL_ARRAY_BUFFER, mesh->mbo);
|
||||
glEnableVertexAttribArray (1);
|
||||
glVertexAttribPointer (1, 4, GL_FLOAT, GL_FALSE, sizeof (float) * 4, 0);
|
||||
|
||||
|
||||
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, mesh->ebo);
|
||||
|
||||
if (game_flags.wireframe_enabled)
|
||||
@@ -115,6 +118,4 @@ ChunkMesh_render (ChunkMesh *mesh)
|
||||
{
|
||||
glDrawElements (GL_TRIANGLES, CHUNK_SIZE * 6, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
typedef enum EntityMeshType
|
||||
{
|
||||
ENTITY_MESH_BILLBOARD,
|
||||
ENTITY_MESH_X
|
||||
} EntityMeshType;
|
||||
|
||||
typedef struct Entity
|
||||
{
|
||||
vec3 position;
|
||||
vec3 rotation; /* Unused for now. */
|
||||
vec3 scale; /* Unused for now. */
|
||||
int texture;
|
||||
EntityMeshType type;
|
||||
} Entity;
|
||||
|
||||
Entity *Entity_new (vec3 position, int texture, EntityMeshType type);
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "manager.h"
|
||||
|
||||
GSequence *entity_manager;
|
||||
|
||||
EntityMesh mesh_billboard, mesh_x;
|
||||
|
||||
void
|
||||
EntityManager_init ()
|
||||
{
|
||||
entity_manager = g_sequence_new (NULL);
|
||||
mesh_billboard = EntityMesh_make_billboard ();
|
||||
mesh_x = EntityMesh_make_x ();
|
||||
}
|
||||
|
||||
void
|
||||
EntityManager_tick ()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
EntityManager_render ()
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <glib-2.0/glib.h>
|
||||
#include <cglm/cglm.h>
|
||||
|
||||
#include "entity.h"
|
||||
#include "meshes.h"
|
||||
|
||||
/* TODO: Sort entities by mesh type AS WELL AS distance. Binding
|
||||
buffers is very performance-sensitive, thus to conserve speed one
|
||||
type should be rendered in one go, then the other type, etc. */
|
||||
|
||||
void EntityManager_init ();
|
||||
void EntityManager_tick ();
|
||||
void EntityManager_render ();
|
||||
@@ -0,0 +1,141 @@
|
||||
#include "meshes.h"
|
||||
|
||||
/* TODO: add back faces. */
|
||||
|
||||
extern GameFlags game_flags;
|
||||
|
||||
void
|
||||
EntityMesh_bind (EntityMesh *mesh)
|
||||
{
|
||||
glBindBuffer (GL_ARRAY_BUFFER, mesh->pbo);
|
||||
glEnableVertexAttribArray (0);
|
||||
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, sizeof (float) * 3, 0);
|
||||
|
||||
glBindBuffer (GL_ARRAY_BUFFER, mesh->uvbo);
|
||||
glEnableVertexAttribArray (1);
|
||||
glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, sizeof (float) * 2, 0);
|
||||
|
||||
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, mesh->ebo);
|
||||
}
|
||||
|
||||
void
|
||||
EntityMesh_render (EntityMesh *mesh)
|
||||
{
|
||||
if (game_flags.wireframe_enabled)
|
||||
{
|
||||
glDrawElements (GL_LINES, mesh->indices_len, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
glDrawElements (GL_TRIANGLES, mesh->indices_len, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
}
|
||||
|
||||
EntityMesh
|
||||
EntityMesh_make_billboard ()
|
||||
{
|
||||
/* The positions are trivial. */
|
||||
float positions[12] = {
|
||||
-1.0, -1.0, 0.0,
|
||||
+1.0, -1.0, 0.0,
|
||||
-1.0, 1.0, 0.0,
|
||||
+1.0, 1.0, 0.0
|
||||
};
|
||||
|
||||
unsigned int indices[12] = {
|
||||
0, 2, 3, 3, 1, 0, /* Quad 1 */
|
||||
3, 2, 0, 0, 1, 3, /* Back of quad 1 */
|
||||
};
|
||||
|
||||
float uvs[8] = {
|
||||
0.0, 1.0,
|
||||
1.0, 1.0,
|
||||
0.0, 0.0,
|
||||
1.0, 0.0,
|
||||
};
|
||||
|
||||
EntityMesh mesh;
|
||||
mesh.indices_len = 12;
|
||||
glGenBuffers (1, &mesh.pbo);
|
||||
glGenBuffers (1, &mesh.uvbo);
|
||||
glGenBuffers (1, &mesh.ebo);
|
||||
|
||||
glBindBuffer (GL_ARRAY_BUFFER, mesh.pbo);
|
||||
glBufferData (GL_ARRAY_BUFFER, sizeof (float) * 12, positions, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray (0);
|
||||
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, sizeof (float) * 3, 0);
|
||||
|
||||
glBindBuffer (GL_ARRAY_BUFFER, mesh.uvbo);
|
||||
glBufferData (GL_ARRAY_BUFFER, sizeof (float) * 8, uvs, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray (1);
|
||||
glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, sizeof (float) * 2, 0);
|
||||
|
||||
|
||||
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, mesh.ebo);
|
||||
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (unsigned int) * mesh.indices_len, indices, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer (GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
EntityMesh
|
||||
EntityMesh_make_x ()
|
||||
{
|
||||
/* The positions are trivial. */
|
||||
float positions[24] = {
|
||||
-1.0, -1.0, 0.0,
|
||||
+1.0, -1.0, 0.0,
|
||||
-1.0, 1.0, 0.0,
|
||||
+1.0, 1.0, 0.0,
|
||||
|
||||
0.0, -1.0, -1.0,
|
||||
0.0, -1.0, 1.0,
|
||||
0.0, 1.0, -1.0,
|
||||
0.0, 1.0, 1.0
|
||||
};
|
||||
|
||||
unsigned int indices[24] = {
|
||||
0, 2, 3, 3, 1, 0, /* Quad 1 */
|
||||
3, 2, 0, 0, 1, 3, /* Back of quad 1 */
|
||||
4, 6, 7, 7, 5, 4, /* Quad 2 */
|
||||
7, 6, 4, 4, 5, 7 /* Back of quad 2 */
|
||||
};
|
||||
|
||||
float uvs[16] = {
|
||||
0.0, 1.0,
|
||||
1.0, 1.0,
|
||||
0.0, 0.0,
|
||||
1.0, 0.0,
|
||||
|
||||
0.0, 1.0,
|
||||
1.0, 1.0,
|
||||
0.0, 0.0,
|
||||
1.0, 0.0
|
||||
};
|
||||
|
||||
EntityMesh mesh;
|
||||
mesh.indices_len = 24;
|
||||
glGenBuffers (1, &mesh.pbo);
|
||||
glGenBuffers (1, &mesh.uvbo);
|
||||
glGenBuffers (1, &mesh.ebo);
|
||||
|
||||
glBindBuffer (GL_ARRAY_BUFFER, mesh.pbo);
|
||||
glBufferData (GL_ARRAY_BUFFER, sizeof (float) * 24, positions, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray (0);
|
||||
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, sizeof (float) * 3, 0);
|
||||
|
||||
glBindBuffer (GL_ARRAY_BUFFER, mesh.uvbo);
|
||||
glBufferData (GL_ARRAY_BUFFER, sizeof (float) * 16, uvs, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray (1);
|
||||
glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, sizeof (float) * 2, 0);
|
||||
|
||||
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, mesh.ebo);
|
||||
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (unsigned int) * mesh.indices_len, indices, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer (GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
return mesh;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL_opengles2.h>
|
||||
#include <cglm/cglm.h>
|
||||
|
||||
#include "../flags.h"
|
||||
|
||||
typedef struct EntityMesh
|
||||
{
|
||||
size_t indices_len;
|
||||
unsigned int pbo;
|
||||
unsigned int uvbo;
|
||||
unsigned int ebo;
|
||||
} EntityMesh;
|
||||
|
||||
void EntityMesh_bind (EntityMesh *mesh);
|
||||
void EntityMesh_render (EntityMesh *mesh);
|
||||
EntityMesh EntityMesh_make_billboard ();
|
||||
EntityMesh EntityMesh_make_x ();
|
||||
+12
-3
@@ -6,6 +6,9 @@ extern ChunkManager chunk_manager;
|
||||
extern GameState game_state;
|
||||
extern Resources resources;
|
||||
|
||||
extern EntityMesh mesh_billboard;
|
||||
extern EntityMesh mesh_x;
|
||||
|
||||
void
|
||||
GraphicsContext_init ()
|
||||
{
|
||||
@@ -54,12 +57,11 @@ GraphicsContext_render ()
|
||||
{
|
||||
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
program_use (resources.programs[1]);
|
||||
|
||||
mat4 projection, view;
|
||||
Camera_get_projection (&game_state.camera, 640.0 / 480.0, projection);
|
||||
Camera_get_view (&game_state.camera, view);
|
||||
|
||||
program_use (resources.programs[1]);
|
||||
program_set_mat4 (resources.programs[1], "projection", projection);
|
||||
program_set_mat4 (resources.programs[1], "view", view);
|
||||
|
||||
@@ -71,8 +73,15 @@ GraphicsContext_render ()
|
||||
|
||||
ChunkManager_render (game_state.camera.position, resources.programs[1]);
|
||||
|
||||
glBindTexture (GL_TEXTURE_2D, 0);
|
||||
program_use (resources.programs[0]);
|
||||
program_set_mat4 (resources.programs[0], "projection", projection);
|
||||
program_set_mat4 (resources.programs[0], "view", view);
|
||||
glActiveTexture (GL_TEXTURE0);
|
||||
glBindTexture (GL_TEXTURE_2D, resources.textures[5]);
|
||||
|
||||
EntityMesh_bind (&mesh_billboard);
|
||||
EntityMesh_render (&mesh_billboard);
|
||||
|
||||
SDL_GL_SwapWindow (gfx.window);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "util.h"
|
||||
#include "chunk/manager.h"
|
||||
#include "entity/manager.h"
|
||||
#include "state.h"
|
||||
#include "resources.h"
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ SDL_AppInit (void **appstate, int argc, char **argv)
|
||||
handlers_init ();
|
||||
Resources_init ();
|
||||
ChunkManager_init ();
|
||||
EntityManager_init ();
|
||||
GameState_init (SDL_GetTicks ());
|
||||
/* Set some basic game flags for testing. */
|
||||
game_flags.can_free_look = 1;
|
||||
|
||||
@@ -85,6 +85,7 @@ Resources_init ()
|
||||
Resources_load_texture ("res/textures/grass.png");
|
||||
Resources_load_texture ("res/textures/rock_cement.png");
|
||||
Resources_load_texture ("res/textures/asphalt_gravel.png");
|
||||
Resources_load_texture ("res/textures/tree.png");
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -9,6 +9,7 @@ GameState_init (int ticks)
|
||||
{
|
||||
game_state.last_time = ticks;
|
||||
game_state.camera = Camera_default ();
|
||||
game_state.camera.position[2] = -1.0;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
Reference in New Issue
Block a user