polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit fc0b2147c4df50a96dfced9f1e5b1e2625dfaeae
parent f516711560551cdea1ac225e06fabcddd7400c0e
Author: William Casarin <jb55@jb55.com>
Date:   Tue, 24 Apr 2018 15:57:38 -0700

WIP

Diffstat:
M.gitignore | 1+
MMakefile | 8+++++++-
Metc/shaders/test.f.glsl | 8+++++---
Metc/shaders/test.v.glsl | 6++++--
Msrc/geometry.c | 6+++---
Msrc/gl.h | 6++++--
Msrc/main.c | 8++++++--
Msrc/render.c | 12++++++------
Msrc/terrain.c | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Msrc/util.h | 7+++++++
10 files changed, 99 insertions(+), 33 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -2,3 +2,4 @@ *.so polyadvent /TAGS +*.d diff --git a/Makefile b/Makefile @@ -32,8 +32,14 @@ OBJS += $(SRC)/util.o SRCS=$(OBJS:.o=.c) +# include $(OBJS:.o=.d) + all: $(BIN) +# src/%.d: src/%.c +# @rm -f $@; \ +# $(CC) -MM $(CFLAGS) $< > $@ + %.o: %.c %.h $(CC) $(CFLAGS) -fPIC $(DEFS) -c $< -o $@ @@ -51,6 +57,6 @@ TAGS: etags $(SRCS) clean: - rm -f src/main.o $(OBJS) $(SHLIB) $(BIN) + rm -f src/main.o $(OBJS) $(SHLIB) $(BIN) *.d .PHONY: TAGS diff --git a/etc/shaders/test.f.glsl b/etc/shaders/test.f.glsl @@ -1,7 +1,9 @@ + uniform float fade_factor; -varying float v_dot; +varying vec3 v_norm; +out vec4 frag_color; void main() { - vec4 color = vec4(0.4, 0.4, 0.8, 1.0); - gl_FragColor = vec4(color.xyz * v_dot, 1.0); + vec4 color = vec4(0.4, 0.4, 0.8, 0.5); + frag_color = vec4(v_norm, 1); } diff --git a/etc/shaders/test.v.glsl b/etc/shaders/test.v.glsl @@ -1,4 +1,3 @@ -#version 110 attribute vec3 position; attribute vec3 normal; @@ -7,11 +6,14 @@ uniform mat4 mvp; uniform mat4 normal_matrix; varying float v_dot; +varying vec3 v_norm; + uniform vec3 light_dir; void main() { vec4 trans_normal = normal_matrix * vec4(normal, 1); gl_Position = mvp * vec4(position.xyz, 1.0); - v_dot = max(dot(trans_normal.xyz, light_dir), 0.8); + v_dot = dot(normal, light_dir); + v_norm = normal; } diff --git a/src/geometry.c b/src/geometry.c @@ -15,7 +15,7 @@ make_buffer_geometry(struct geometry *geom) { make_vertex_buffer( GL_ARRAY_BUFFER, geom->vertices, - geom->num_verts * 3 * (int)sizeof(float), + geom->num_verts * 3 * (int)sizeof(*geom->vertices), &geom->buffer.vertex_buffer ); @@ -24,7 +24,7 @@ make_buffer_geometry(struct geometry *geom) { make_vertex_buffer( GL_ARRAY_BUFFER, geom->normals, - geom->num_verts * 3 * (int)sizeof(float), + geom->num_verts * 3 * (int)sizeof(*geom->normals), &geom->buffer.normal_buffer ); @@ -33,7 +33,7 @@ make_buffer_geometry(struct geometry *geom) { make_index_buffer( GL_ELEMENT_ARRAY_BUFFER, geom->indices, - geom->num_indices * (int)sizeof(u32), + geom->num_indices * (int)sizeof(*geom->indices), &geom->buffer.index_buffer ); } diff --git a/src/gl.h b/src/gl.h @@ -1,8 +1,10 @@ #ifndef POLYADVENT_GL_H #define POLYADVENT_GL_H -//#include <epoxy/gl.h> +/* #include <epoxy/gl.h> */ #include <SDL2/SDL.h> -#include <SDL2/SDL_opengles2.h> +#include <GL/gl.h> +#include <GL/glx.h> +#include <GL/glext.h> #endif /* POLYADVENT_GL_H */ diff --git a/src/main.c b/src/main.c @@ -12,6 +12,7 @@ #include "render.h" #include "terrain.h" #include <assert.h> +#include <time.h> int main(void) @@ -23,13 +24,16 @@ int main(void) size_t length; + /* SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); */ + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); + SDL_Window *window = SDL_CreateWindow( "SDL2/OpenGL Demo", 0, 0, 640, 480, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); - srand(52); - SDL_GL_CreateContext(window); + srand(42); + SDL_GL_CreateContext(window); init_gl(&game.test_resources); game_init(&game); diff --git a/src/render.c b/src/render.c @@ -57,8 +57,8 @@ void init_gl(struct resources *resources) { float tmp_matrix[16]; glEnable(GL_DEPTH_TEST); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + /* SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); */ + /* SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); */ // VBOs make_vertex_buffer( @@ -172,15 +172,15 @@ render_cube (struct resources * resources) { static void render_geom (struct resources *res, - struct geometry *geom) { + struct geometry *geom, + GLenum type) { struct attributes *attrs = &res->attributes; bind_vbo(&geom->buffer.vertex_buffer, attrs->position); bind_vbo(&geom->buffer.normal_buffer, attrs->normal); bind_ibo(&geom->buffer.index_buffer); - glDrawElements( - GL_LINES, + glDrawElements(type, geom->num_indices, /* count */ GL_UNSIGNED_INT, /* type */ (void*)0 /* element array buffer offset */ @@ -214,5 +214,5 @@ void render (struct resources * resources, struct geometry *geom) { glUniformMatrix4fv(resources->uniforms.mvp, 1, 0, tmp_matrix); /* render_cube(resources); */ - render_geom(resources, geom); + render_geom(resources, geom, GL_TRIANGLES); } diff --git a/src/terrain.c b/src/terrain.c @@ -17,7 +17,7 @@ static const u32 plane_indices[] = { double old_noisy_boi(double x, double y) { - return sin(x) * cos(y) * 5.0; + return sin(x/5.0) * cos(y/5.0) * rand_0to1() * 10; } void @@ -29,7 +29,7 @@ terrain_init(struct terrain *terrain) { void terrain_create(struct terrain *terrain) { u32 i; - const int num_verts = 1000; + const int num_verts = 10000; del_point2d_t *points = calloc(num_verts, sizeof(*points)); float *zs = calloc(num_verts * 3, sizeof(*zs)); float *verts = calloc(num_verts * 3, sizeof(*verts)); @@ -38,40 +38,82 @@ terrain_create(struct terrain *terrain) { // 100 random samples from our noise function for (i = 0; i < num_verts; i++) { + int n = i*3; double x = rand_0to1() * 100.0; double y = rand_0to1() * 100.0; - double z = old_noisy_boi(x, y); points[i].x = x; points[i].y = y; zs[i] = z; - verts[(i*3)] = (float)x; - verts[(i*3)+1] = (float)y; - verts[(i*3)+2] = (float)z; + verts[n] = (float)x; + verts[n+1] = (float)y; + verts[n+2] = (float)z; + + normals[n] = 0; + normals[n+1] = 0; + normals[n+2] = 1; } delaunay2d_t *del = delaunay2d_from(points, num_verts); tri_delaunay2d_t *tri = tri_delaunay2d_from(del); + printf("faces %d tris %d points %d\n", + del->num_faces, tri->num_triangles, tri->num_points); + // TODO: calc normals for (i = 0; i < tri->num_points; i++) { - /* float y1 = */ - /* float nx = (y2-y1)*(z3-z1)-(y3-y1)*(z2*-z1); */ - /* float ny = (y2-y1)*(z3-z1)-(y3-y1)*(z2*-z1); */ int n = i*3; verts[n] = (float)tri->points[i].x; verts[n+1] = (float)tri->points[i].y; - verts[n+2] = 0; - normals[n] = 0; - normals[n+1] = 0; - normals[n+2] = 1; + normals[n] = rand_0to1(); + normals[n+1] = rand_0to1(); + normals[n+2] = rand_0to1(); } - terrain->geom.num_verts = tri->num_triangles * 3; + /* for (i = 0; i < tri->num_triangles; ++i) { */ + + /* int p0 = tri->tris[i * 3 + 0]; */ + /* int p1 = tri->tris[i * 3 + 1]; */ + /* int p2 = tri->tris[i * 3 + 2]; */ + /* vec3 *v0 = (vec3*)&verts[p0]; */ + /* vec3 *v1 = (vec3*)&verts[p1]; */ + /* vec3 *v2 = (vec3*)&verts[p2]; */ + + /* float x0 = v0->x; */ + /* float x1 = v1->x; */ + /* float x2 = v2->x; */ + + /* float y0 = v0->y; */ + /* float y1 = v1->y; */ + /* float y2 = v2->y; */ + + /* float z0 = v0->z; */ + /* float z1 = v1->z; */ + /* float z2 = v2->z; */ + + /* float nx = (y1-y0)*(z2-z0)-(y2-y0)*(z1-z0); */ + /* float ny = (z1-z0)*(x2-x0)-(x1-x0)*(z2-z0); */ + /* float nz = (x1-x0)*(y2-x0)-(x2-x1)*(y1-y0); */ + + /* normals[p0] = nx; */ + /* normals[p0+1] = ny; */ + /* normals[p0+2] = nz; */ + + /* normals[p1] = nx; */ + /* normals[p1+1] = ny; */ + /* normals[p1+2] = nz; */ + + /* normals[p2] = nx; */ + /* normals[p2+1] = ny; */ + /* normals[p2+2] = nz; */ + /* } */ + + + terrain->geom.num_verts = num_verts; terrain->geom.vertices = (float*)verts; terrain->geom.normals = (float*)normals; terrain->geom.indices = tri->tris; diff --git a/src/util.h b/src/util.h @@ -7,6 +7,13 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +typedef struct vec3 { + union { + float x,y,z; + float xyz[3]; + }; +} vec3; + void check_gl(void); double rand_0to1();