diff options
| author | Tomasz Kramkowski <tk@the-tk.com> | 2018-10-27 12:41:58 +0100 | 
|---|---|---|
| committer | Tomasz Kramkowski <tk@the-tk.com> | 2018-10-27 12:47:05 +0100 | 
| commit | d3f47e042aebd5fbf86477abf7058704657e8430 (patch) | |
| tree | 4996f0c46f7ebf5037ee7fecceb161bbeb4e549e | |
| parent | adbc72e159d48e6b1ad091e6ac19fd1cd6d5f798 (diff) | |
| download | faqe-d3f47e042aebd5fbf86477abf7058704657e8430.tar.gz faqe-d3f47e042aebd5fbf86477abf7058704657e8430.tar.xz faqe-d3f47e042aebd5fbf86477abf7058704657e8430.zip | |
Cleanup pass
| -rw-r--r-- | camera.c | 5 | ||||
| -rw-r--r-- | faqe.c | 2 | ||||
| -rw-r--r-- | fmd.c | 5 | ||||
| -rw-r--r-- | gl.c | 3 | ||||
| -rw-r--r-- | glfunc.h | 70 | ||||
| -rw-r--r-- | glprog.c | 10 | ||||
| -rw-r--r-- | glprog.h | 2 | ||||
| -rw-r--r-- | ieee754.c | 2 | ||||
| -rw-r--r-- | model.c | 2 | ||||
| -rw-r--r-- | mtl.c | 7 | ||||
| -rw-r--r-- | nelem.h | 1 | ||||
| -rw-r--r-- | test_ieee754.c | 2 | ||||
| -rw-r--r-- | tex.c | 2 | 
13 files changed, 70 insertions, 43 deletions
| @@ -11,6 +11,7 @@  #define PITCH_LIMIT (PI * 0.99) +// camera_clamp: clamp camera pitch and normalize yaw  void camera_clamp(struct camera *cam)  {  	assert(cam != NULL); @@ -21,6 +22,8 @@ void camera_clamp(struct camera *cam)  	else if (cam->pitch < -PITCH_LIMIT / 2)  		cam->pitch = -PITCH_LIMIT / 2;  } + +// camera_dir: get the camera facing vector  void camera_dir(vec3 out, struct camera *cam)  {  	assert(out != NULL); @@ -30,6 +33,8 @@ void camera_dir(vec3 out, struct camera *cam)  	out[1] = sinf(cam->pitch);  	out[2] = cosf(cam->pitch) * sinf(cam->yaw);  } + +// camera_lookat: get the camera view matrix  void camera_lookat(mat4x4 out, struct camera *cam)  {  	vec3 at, dir; @@ -79,7 +79,7 @@ int main(int argc, char **argv)  	if (glc == NULL)  		eprintf("Failed to create OpenGL context: %s", SDL_GetError()); -	gl_load((gl_loadfunc *)SDL_GL_GetProcAddress); +	gl_load(SDL_GL_GetProcAddress);  	glprog_init(); @@ -13,12 +13,14 @@  static const char *MAGIC = "FMD002\r\n"; +// checkmagic: check the fmd magic number  static void checkmagic(FILE *f)  {  	for (size_t i = 0; i < strlen(MAGIC); i++)  		assert(fgetc(f) == MAGIC[i]);  } +// betoul: read big endian 32 bit unsigned integer  static inline unsigned long betoul(FILE *src)  {  	unsigned long ret = 0; @@ -34,6 +36,7 @@ static inline unsigned long betoul(FILE *src)  	return ret;  } +// readfloats: read ieee754 32 bit floating point numbers  void readfloats(float *dest, int count, FILE *src)  {  	assert(dest != NULL); @@ -43,6 +46,7 @@ void readfloats(float *dest, int count, FILE *src)  		dest[i] = ieee754f(betoul(src));  } +// fmd_load: read a FMD file  void fmd_load(struct fmd *fmd, FILE *f)  {  	assert(fmd != NULL); @@ -102,6 +106,7 @@ void fmd_load(struct fmd *fmd, FILE *f)  	}  } +// fmd_free: free a previously loaded FMD file  void fmd_free(struct fmd *fmd)  {  	assert(fmd); @@ -9,6 +9,7 @@  #include "glfunc.h"  #undef GL_FUNC +// load_func: load OpenGL function  static void *load_func(const char *name, gl_loadfunc *load)  {  	void *ret; @@ -20,6 +21,7 @@ static void *load_func(const char *name, gl_loadfunc *load)  	return ret;  } +// gl_load: load required OpenGL functions  void gl_load(gl_loadfunc *load)  {  #define GL_FUNC(glname, rtype, name, ...)		\ @@ -28,6 +30,7 @@ void gl_load(gl_loadfunc *load)  #undef GL_FUNC  } +// gl_strerror: format OpenGL error code into a string  const char *gl_strerror(GLenum error)  {  	switch (error) { @@ -2,54 +2,54 @@   * Copyright (C) 2018 Tomasz Kramkowski <tk@the-tk.com>   * SPDX-License-Identifier: MIT   */ -GL_FUNC(glGetError, GLenum, gl_error, void) -GL_FUNC(glViewport, void, gl_viewport, GLint x, GLint y, GLsizei width, GLsizei height) -GL_FUNC(glClearColor, void, gl_clearcolor, GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) -GL_FUNC(glClear, void, gl_clear, GLbitfield mask) -GL_FUNC(glGenBuffers, void, gl_buf_gen, GLsizei n, GLuint *buffers) +GL_FUNC(glBindAttribLocation, void, gl_attr_bindloc, GLuint program, GLuint index, const char *name)  GL_FUNC(glBindBuffer, void, gl_buf_bind, GLenum target, GLuint buffer)  GL_FUNC(glBufferData, void, gl_buf_data, GLenum target, GLsizeiptr size, const void *data, GLenum usage) -GL_FUNC(glCreateShader, GLuint, gl_shdr_create, GLenum type) -GL_FUNC(glDeleteShader, void, gl_shdr_del, GLuint shader) -GL_FUNC(glShaderSource, void, gl_shdr_source, GLuint shader, GLsizei count, const char * const *string, const GLint *length) -GL_FUNC(glCompileShader, void, gl_shdr_compile, GLuint shader) -GL_FUNC(glGetShaderiv, void, gl_shdr_param, GLuint shader, GLenum pname, GLint *params) -GL_FUNC(glGetShaderInfoLog, void, gl_shdr_infolog, GLuint shader, GLsizei size, GLsizei *len, char *data) +GL_FUNC(glGenBuffers, void, gl_buf_gen, GLsizei n, GLuint *buffers) +GL_FUNC(glClear, void, gl_clear, GLbitfield mask) +GL_FUNC(glClearColor, void, gl_clearcolor, GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) +GL_FUNC(glDisable, void, gl_disable, GLenum cap) +GL_FUNC(glDrawArrays, void, gl_draw_arrays, GLenum mode, GLint first, GLsizei count) +GL_FUNC(glDrawElements, void, gl_draw_elems, GLenum mode, GLsizei count, GLenum type, const void *indices) +GL_FUNC(glEnable, void, gl_enable, GLenum cap) +GL_FUNC(glGetError, GLenum, gl_error, void) +GL_FUNC(glGetIntegerv, void, gl_geti, GLenum pname, GLint *data) +GL_FUNC(glPolygonMode, void, gl_poly_mode, GLenum face, GLenum mode) +GL_FUNC(glAttachShader, void, gl_prog_attachshdr, GLuint program, GLuint shader)  GL_FUNC(glCreateProgram, GLuint, gl_prog_create, void)  GL_FUNC(glDeleteProgram, void, gl_prog_del, GLuint program) -GL_FUNC(glAttachShader, void, gl_prog_attachshdr, GLuint program, GLuint shader)  GL_FUNC(glDetachShader, void, gl_prog_detachshdr, GLuint program, GLuint shader)  GL_FUNC(glGetAttachedShaders, void, gl_prog_getshdrs, GLuint program, GLsizei max, GLsizei *count, GLuint *shaders) +GL_FUNC(glGetProgramInfoLog, void, gl_prog_infolog, GLuint prog, GLsizei size, GLsizei *len, char *data)  GL_FUNC(glLinkProgram, void, gl_prog_link, GLuint program) -GL_FUNC(glUseProgram, void, gl_prog_use, GLuint program)  GL_FUNC(glGetProgramiv, void, gl_prog_param, GLuint prog, GLenum pname, GLint *params) -GL_FUNC(glGetProgramInfoLog, void, gl_prog_infolog, GLuint prog, GLsizei size, GLsizei *len, char *data) -GL_FUNC(glVertexAttribPointer, void, gl_va_define, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *data) -GL_FUNC(glEnableVertexAttribArray, void, gl_va_enable, GLuint index) -GL_FUNC(glDisableVertexAttribArray, void, gl_va_disable, GLuint index) -GL_FUNC(glGenVertexArrays, void, gl_va_gen, GLsizei n, GLuint *arrays) -GL_FUNC(glBindVertexArray, void, gl_va_bind, GLuint va) -GL_FUNC(glDrawArrays, void, gl_draw_arrays, GLenum mode, GLint first, GLsizei count) -GL_FUNC(glDrawElements, void, gl_draw_elems, GLenum mode, GLsizei count, GLenum type, const void *indices) -GL_FUNC(glPolygonMode, void, gl_poly_mode, GLenum face, GLenum mode) -GL_FUNC(glGetUniformLocation, GLint, gl_uni_loc, GLuint program, const char *name) +GL_FUNC(glUseProgram, void, gl_prog_use, GLuint program) +GL_FUNC(glCompileShader, void, gl_shdr_compile, GLuint shader) +GL_FUNC(glCreateShader, GLuint, gl_shdr_create, GLenum type) +GL_FUNC(glDeleteShader, void, gl_shdr_del, GLuint shader) +GL_FUNC(glGetShaderInfoLog, void, gl_shdr_infolog, GLuint shader, GLsizei size, GLsizei *len, char *data) +GL_FUNC(glGetShaderiv, void, gl_shdr_param, GLuint shader, GLenum pname, GLint *params) +GL_FUNC(glShaderSource, void, gl_shdr_source, GLuint shader, GLsizei count, const char * const *string, const GLint *length) +GL_FUNC(glActiveTexture, void, gl_tex_active, GLenum texture) +GL_FUNC(glBindTexture, void, gl_tex_bind, GLenum target, GLuint texture) +GL_FUNC(glGenTextures, void, gl_tex_gen, GLsizei n, GLuint *textures) +GL_FUNC(glGenerateMipmap, void, gl_tex_genmip, GLenum target) +GL_FUNC(glTexImage2D, void, gl_tex_img2d, GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *data) +GL_FUNC(glTexParameteri, void, gl_tex_parami, GLenum target, GLenum pname, GLint param)  GL_FUNC(glGetUniformfv, void, gl_uni_getf, GLuint program, GLint location, GLfloat *params)  GL_FUNC(glGetUniformiv, void, gl_uni_geti, GLuint program, GLint location, GLint *params) -GL_FUNC(glUniform1i, void, gl_uni_set1i, GLint location, GLint v0) +GL_FUNC(glGetUniformLocation, GLint, gl_uni_loc, GLuint program, const char *name)  GL_FUNC(glUniform1f, void, gl_uni_set1f, GLint location, GLfloat v0) +GL_FUNC(glUniform1i, void, gl_uni_set1i, GLint location, GLint v0)  GL_FUNC(glUniform3f, void, gl_uni_set3f, GLint location, GLfloat v0, GLfloat v1, GLfloat v2) -GL_FUNC(glUniform4f, void, gl_uni_set4f, GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3)  GL_FUNC(glUniform3fv, void, gl_uni_set3fv, GLint location, GLsizei count, const GLfloat *value) +GL_FUNC(glUniform4f, void, gl_uni_set4f, GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3)  GL_FUNC(glUniform4fv, void, gl_uni_set4fv, GLint location, GLsizei count, const GLfloat *value)  GL_FUNC(glUniformMatrix3fv, void, gl_uni_setm3fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)  GL_FUNC(glUniformMatrix4fv, void, gl_uni_setm4fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) -GL_FUNC(glGetIntegerv, void, gl_geti, GLenum pname, GLint *data) -GL_FUNC(glEnable, void, gl_enable, GLenum cap) -GL_FUNC(glDisable, void, gl_disable, GLenum cap) -GL_FUNC(glGenTextures, void, gl_tex_gen, GLsizei n, GLuint *textures) -GL_FUNC(glActiveTexture, void, gl_tex_active, GLenum texture) -GL_FUNC(glBindTexture, void, gl_tex_bind, GLenum target, GLuint texture) -GL_FUNC(glTexImage2D, void, gl_tex_img2d, GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *data) -GL_FUNC(glTexParameteri, void, gl_tex_parami, GLenum target, GLenum pname, GLint param) -GL_FUNC(glGenerateMipmap, void, gl_tex_genmip, GLenum target) -GL_FUNC(glBindAttribLocation, void, gl_attr_bindloc, GLuint program, GLuint index, const char *name) +GL_FUNC(glBindVertexArray, void, gl_va_bind, GLuint va) +GL_FUNC(glVertexAttribPointer, void, gl_va_define, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *data) +GL_FUNC(glDisableVertexAttribArray, void, gl_va_disable, GLuint index) +GL_FUNC(glEnableVertexAttribArray, void, gl_va_enable, GLuint index) +GL_FUNC(glGenVertexArrays, void, gl_va_gen, GLsizei n, GLuint *arrays) +GL_FUNC(glViewport, void, gl_viewport, GLint x, GLint y, GLsizei width, GLsizei height) @@ -39,6 +39,7 @@ static const struct {  	},  }; +// readfile: read a whole file into a buffer  static bool readfile(char **data, size_t *size, const char *name)  {  	FILE *f; @@ -63,17 +64,19 @@ static bool readfile(char **data, size_t *size, const char *name)  		*data = erealloc(*data, *size);  	} +	fclose(f); +  	*size = len;  	*data = erealloc(*data, *size);  	return true;  } +// compile: compile shader component  static GLuint compile(const char *src, size_t len, const char *pre, GLuint type)  {  	GLuint id;  	GLint success; -	char log[LOGSIZE];  	static const char *common_pre =  #define SH_VER(v) "#version " v "\n"  #define SH_UNI(type, name) "uniform " #type " " #name ";\n" @@ -88,6 +91,7 @@ static GLuint compile(const char *src, size_t len, const char *pre, GLuint type)  	gl_shdr_param(id, GL_COMPILE_STATUS, &success);  	if (!success) { +		char log[LOGSIZE];  		gl_shdr_infolog(id, sizeof log, NULL, log);  		eprintf("Failed to compile shader:\n%s", log);  	} @@ -108,7 +112,7 @@ static void load_shader(struct shader *s, const char *path)  {  	GLuint prog;  	GLint success, pos = 0; -	char log[LOGSIZE], *full, *src = NULL; +	char *full, *src = NULL;  	size_t srclen = 0, fullsz;  	GLint texture = 0; @@ -139,6 +143,7 @@ static void load_shader(struct shader *s, const char *path)  	gl_prog_link(prog);  	gl_prog_param(prog, GL_LINK_STATUS, &success);  	if (!success) { +		char log[LOGSIZE];  		gl_prog_infolog(prog, sizeof log, NULL, log);  		eprintf("Failed to link program\n%s", log);  	} @@ -153,6 +158,7 @@ static void load_shader(struct shader *s, const char *path)  	s->prog = prog;  } +// glprog_init: compile and link all shader programs  void glprog_init(void)  {  	GLint pos = 0; @@ -19,7 +19,7 @@ struct glprog_progs {  #define SH_PROG(name) struct shader name;  #include "shaders/data.h"  	struct { -#define SH_IN(_0, name) GLint name; +#define SH_IN(_, name) GLint name;  #include "shaders/data.h"  	} attr;  }; @@ -28,7 +28,7 @@ float ieee754f(unsigned long b)  		}  	} else if (exp == 0) {  		if (n == 0) -			return isneg ? -0 : 0; +			return isneg ? -0.0 : 0.0;  		exp = -126;  	} else {  		n += 0x1p23f; @@ -32,7 +32,7 @@ void model_load(struct model *mdl, const struct fmd *fmd)  	v = &fmd->verts[0]; -#define SH_IN(_0, name) \ +#define SH_IN(_, name)							\  	gl_va_define(prog.attr.name, NELEM(v->name), GL_FLOAT, GL_FALSE, sizeof *v, (void *)offsetof(struct vertex, name)); \  	gl_va_enable(prog.attr.name);  #include "shaders/data.h" @@ -22,6 +22,7 @@ static struct mtl *mtls;  static int lmtls;  static size_t nmtls; +// tex_file: Load texture asset from file  GLuint tex_file(const char *name, const char *type)  {  	char loc[100]; @@ -38,6 +39,7 @@ GLuint tex_file(const char *name, const char *type)  	return ret;  } +// mtl_load: Load a set of textures as a material  int mtl_load(char *name)  {  	struct mtl *m; @@ -57,6 +59,7 @@ int mtl_load(char *name)  	return lmtls++;  } +// mtl_use: Set OpenGL state to use a certain material index  void mtl_use(int mtl)  {  	assert(mtl < lmtls); @@ -66,8 +69,8 @@ void mtl_use(int mtl)  	if (mtl < 0)  		return; -#define SH_TEX(type, name) \ -	gl_tex_active(texture++); \ +#define SH_TEX(type, name)				\ +	gl_tex_active(texture++);			\  	gl_tex_bind(GL_TEXTURE_##type, mtls[mtl].name);  #include "shaders/data.h"  } @@ -5,6 +5,7 @@  #ifndef NELEM_H  #define NELEM_H +// NELEM: calculate array element count  #define NELEM(a) (sizeof (a) / sizeof (a)[0])  #endif // NELEM_H diff --git a/test_ieee754.c b/test_ieee754.c index 85a1c95..65d0aba 100644 --- a/test_ieee754.c +++ b/test_ieee754.c @@ -9,6 +9,7 @@  #include "ieee754.h" +// naiive: return the binary representation of a float (non portable)  static unsigned long naiive(float n)  {  	union { @@ -19,6 +20,7 @@ static unsigned long naiive(float n)  	return u.u;  } +// test: test ieee754f against a certain number  void test(float a)  {  	float b = ieee754f(naiive(a)); @@ -9,6 +9,7 @@  #include "eprintf.h"  #include "tex.h" +// png2tex: read a PNG and produce an OpenGL texture out of it  GLuint png2tex(FILE *f)  {  	png_image pi = { .version = PNG_IMAGE_VERSION }; @@ -21,6 +22,7 @@ GLuint png2tex(FILE *f)  	png_image_begin_read_from_stdio(&pi, f);  	pi.format = PNG_FORMAT_RGB;  	stride = sizeof *data * PNG_IMAGE_SAMPLE_CHANNELS(pi.format) * pi.width; +	// OpenGL alignment requirement  	stride = (stride + 3) & ~(size_t)3;  	size = stride * pi.height;  	data = emalloc(size); | 
