polyadvent

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

commit 3ec43ba51f6ca9ca04799e0851ddf9c5e7648e68
parent a5926cc763fd6c5c61347eddc0839f3c13f802ff
Author: William Casarin <bill@casarin.me>
Date:   Sun,  5 Apr 2015 11:21:40 -0700

progress

Diffstat:
A.gitignore | 2++
MMakefile | 36++++++++++++++++++++----------------
Aetc/shaders/test.f.glsl | 6++++++
Aetc/shaders/test.v.glsl | 11+++++++++++
Asrc/game.h | 9+++++++++
Msrc/gl.h | 2+-
Msrc/main.cc | 1+
Msrc/render.cc | 102++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Msrc/shader.cc | 70+++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Msrc/shader.h | 3+++
Asrc/update.cc | 3+++
Asrc/update.h | 9+++++++++
12 files changed, 197 insertions(+), 57 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +*.o+ \ No newline at end of file diff --git a/Makefile b/Makefile @@ -1,33 +1,37 @@ - -BIN ?= polyadvent +NAME ?= polyadvent +BIN ?= $(NAME) PREFIX ?= /usr/local LDFLAGS = -lglfw3 -lX11 -lGL -lXi -lXrandr -lXxf86vm -lepoxy DEFS= -DGLFW_INCLUDE_NONE SRC=src -OBJS = ${SRC}/main.o -OBJS += ${SRC}/window.o -OBJS += ${SRC}/buffer.o -OBJS += ${SRC}/shader.o -OBJS += ${SRC}/file.o -OBJS += ${SRC}/debug.o -OBJS += ${SRC}/render.o +SHLIB=$(SRC)/lib$(NAME).so + +OBJS = $(SRC)/window.o +OBJS += $(SRC)/buffer.o +OBJS += $(SRC)/shader.o +OBJS += $(SRC)/file.o +OBJS += $(SRC)/debug.o +OBJS += $(SRC)/render.o all: $(BIN) -%.o: %.cc - $(CXX) $(DEFS) -c $< -o $@ +%.o: %.cc %.h + $(CC) -fPIC $(DEFS) -c $< -o $@ -$(BIN): $(OBJS) - $(CXX) $(DEFS) $^ $(LDFLAGS) -o $@ +$(SHLIB): $(OBJS) + $(CC) -shared $^ -o $@ + +$(BIN): $(SHLIB) $(SRC)/main.o + $(CC) $(DEFS) $^ $(LDFLAGS) -o $@ install: $(BIN) install -d $(PREFIX)/bin install $(BIN) $(PREFIX)/bin nixbuild: - nix-build shell.nix + nix-shell shell.nix --command 'make -j4' clean: - find . -name '*.o' -exec rm -f {} \; - rm -f csv + rm -rf $(OBJS) $(SHLIB) $(BIN) + diff --git a/etc/shaders/test.f.glsl b/etc/shaders/test.f.glsl @@ -0,0 +1,5 @@ +#version 110 + +void main() { + gl_FragColor = vec4(0.4,0.4,0.8,1.0); +}+ \ No newline at end of file diff --git a/etc/shaders/test.v.glsl b/etc/shaders/test.v.glsl @@ -0,0 +1,10 @@ +#version 110 + +attribute vec2 position; + +varying vec2 texcoord; + +void main() +{ + gl_Position = vec4(position, 0.0, 1.0); +}+ \ No newline at end of file diff --git a/src/game.h b/src/game.h @@ -0,0 +1,9 @@ + +#ifndef PA_GAME_H +#define PA_GAME_H + +typedef struct game_state { + int counter; +} game_state; + +#endif /* PA_GAME_H */ diff --git a/src/gl.h b/src/gl.h @@ -1,7 +1,7 @@ #ifndef POLYADVENT_GL_H #define POLYADVENT_GL_H -#include <GLFW/glfw3.h> #include <epoxy/gl.h> +#include <GLFW/glfw3.h> #endif /* POLYADVENT_GL_H */ diff --git a/src/main.cc b/src/main.cc @@ -32,6 +32,7 @@ int main(void) glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective glLoadIdentity(); //Reset the drawing perspective + //update(); render(); /* Swap front and back buffers */ diff --git a/src/render.cc b/src/render.cc @@ -1,47 +1,115 @@ +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + #include "gl.h" #include "buffer.h" +#include "shader.h" +#include "debug.h" -// TESTING -static GLuint g_vertex_buffer; -static GLuint g_element_buffer; - +/* + * Global data used by our render callback: + */ static const GLfloat g_vertex_buffer_data[] = { - -1.0f, -1.0f, - 1.0f, -1.0f, - -1.0f, 1.0f, - 1.0f, 1.0f + -0.5f, -0.5f, + 0.5f, -0.5f, + -0.5f, 0.6f, + 0.5f, 0.5f }; static const GLushort g_element_buffer_data[] = { 0, 1, 2, 3 }; +/* + * Global data used by our render callback: + */ +static struct { + GLuint vertex_buffer, element_buffer; + GLuint textures[2]; + GLuint vertex_shader, fragment_shader, program; + + struct { + GLint fade_factor; + GLint textures[2]; + } uniforms; + + struct { + GLint position; + } attributes; + + GLfloat fade_factor; +} g_resources; + + + void init_gl() { //glEnable(GL_DEPTH_TEST); - g_vertex_buffer = make_buffer( + // VBOs + g_resources.vertex_buffer = make_buffer( GL_ARRAY_BUFFER, g_vertex_buffer_data, sizeof(g_vertex_buffer_data) ); - - g_element_buffer = make_buffer( + g_resources.element_buffer = make_buffer( GL_ELEMENT_ARRAY_BUFFER, g_element_buffer_data, sizeof(g_element_buffer_data) ); + + // Shaders + g_resources.vertex_shader = make_shader( + GL_VERTEX_SHADER, + SHADER("test.v.glsl") + ); + assert(g_resources.vertex_shader != 0); + g_resources.fragment_shader = make_shader( + GL_FRAGMENT_SHADER, + SHADER("test.f.glsl") + ); + assert(g_resources.fragment_shader != 0); + + + // Shader program + g_resources.program = make_program(g_resources.vertex_shader, + g_resources.fragment_shader); + assert(g_resources.program != 0); + + + // Program variables + g_resources.uniforms.fade_factor + = glGetUniformLocation(g_resources.program, "fade_factor"); + + g_resources.attributes.position + = glGetAttribLocation(g_resources.program, "position"); + + assert(g_resources.program != 0); } void render () { - glBindBuffer(GL_ARRAY_BUFFER, g_vertex_buffer); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_element_buffer); + glUseProgram(g_resources.program); + glBindBuffer(GL_ARRAY_BUFFER, g_resources.vertex_buffer); + glVertexAttribPointer( + g_resources.attributes.position, /* attribute */ + 2, /* size */ + GL_FLOAT, /* type */ + GL_FALSE, /* normalized? */ + sizeof(GLfloat)*2, /* stride */ + (void*)0 /* array buffer offset */ + ); + glEnableVertexAttribArray(g_resources.attributes.position); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_resources.element_buffer); glDrawElements( - GL_TRIANGLE_STRIP, /* mode */ - 4, /* count */ - GL_UNSIGNED_SHORT, /* type */ - (void*)0 /* element array buffer offset */ + GL_TRIANGLE_STRIP, /* mode */ + 4, /* count */ + GL_UNSIGNED_SHORT, /* type */ + (void*)0 /* element array buffer offset */ ); + + glDisableVertexAttribArray(g_resources.attributes.position); } diff --git a/src/shader.cc b/src/shader.cc @@ -6,27 +6,51 @@ #include "debug.h" GLuint -make_shader(GLenum type, const char *filename) -{ - GLint length; - GLchar *source = (GLchar *)file_contents(filename, &length); - GLuint shader; - GLint shader_ok; - - if (!source) - return 0; - - shader = glCreateShader(type); - glShaderSource(shader, 1, (const GLchar**)&source, &length); - free(source); - glCompileShader(shader); - - glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok); - if (!shader_ok) { - fprintf(stderr, "Failed to compile %s:\n", filename); - show_info_log(shader, glGetShaderiv, glGetShaderInfoLog); - glDeleteShader(shader); - return 0; - } - return shader; +make_shader(GLenum type, const char *filename) { + GLint length; + GLchar *source = (GLchar *)file_contents(filename, &length); + GLuint shader; + GLint shader_ok; + + if (!source) + return 0; + + shader = glCreateShader(type); + glShaderSource(shader, 1, (const GLchar**)&source, &length); + free(source); + glCompileShader(shader); + + glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok); + + if (!shader_ok) { + fprintf(stderr, "Failed to compile %s:\n", filename); + show_info_log(shader, glGetShaderiv, glGetShaderInfoLog); + glDeleteShader(shader); + return 0; + } + + return shader; +} + + + +GLuint +make_program(GLuint vertex_shader, GLuint fragment_shader) { + GLint program_ok; + GLuint program = glCreateProgram(); + + if (vertex_shader) + glAttachShader(program, vertex_shader); + + glAttachShader(program, fragment_shader); + glLinkProgram(program); + + glGetProgramiv(program, GL_LINK_STATUS, &program_ok); + if (!program_ok) { + fprintf(stderr, "Failed to link shader program:\n"); + show_info_log(program, glGetProgramiv, glGetProgramInfoLog); + glDeleteProgram(program); + return 0; + } + return program; } diff --git a/src/shader.h b/src/shader.h @@ -1,6 +1,9 @@ #ifndef POLYADVENT_SHADER_H #define POLYADVENT_SHADER_H +#define SHADER(f) "etc/shaders/" f + GLuint make_shader(GLenum type, const char *filename); +GLuint make_program(GLuint vertex_shader, GLuint fragment_shader); #endif /* POLYADVENT_SHADER_H */ diff --git a/src/update.cc b/src/update.cc @@ -0,0 +1,3 @@ + +void update () { +} diff --git a/src/update.h b/src/update.h @@ -0,0 +1,9 @@ + +#ifndef PA_UPDATE_H +#define PA_UPDATE_H + +#include "game.h" + +void update(game_state * game); + +#endif /* PA_UPDATE_H */