diff options
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | src/Makefile.am | 7 | ||||
-rw-r--r-- | src/glcheck.c | 172 |
3 files changed, 179 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am index 812ebbd..cc10a56 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,3 +1,4 @@ EXTRA_DIST = configure src/game.h src/model.h src/physics.h src/render.h src/texture.h src/types.h src/utils.h src/audio.h data/* SUBDIRS = src data +TESTS = src/glcheck diff --git a/src/Makefile.am b/src/Makefile.am index d3ccec8..50dc1d3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,11 @@ -bin_PROGRAMS = thrust3d +bin_PROGRAMS = thrust3d glcheck + thrust3d_SOURCES = main.c model.c render.c physics.c game.c texture.c utils.c audio.c thrust3d_LDADD = @LIBS@ + +glcheck_SOURCES = glcheck.c +glcheck_LDADD = -lGL -lGLU -lglut + AM_CFLAGS = -Wall -g AM_CPPFLAGS = -DDATADIR=\""$(datadir)"/thrust3d\" diff --git a/src/glcheck.c b/src/glcheck.c new file mode 100644 index 0000000..59ba4b2 --- /dev/null +++ b/src/glcheck.c @@ -0,0 +1,172 @@ +/* + * glcheck.c + * + * Quick check of OpenGL functionality + * + * (c) 2008 Thomas White <taw27@cam.ac.uk> + * + * thrust3d - a silly game + * + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> +#include <gl.h> +#include <glu.h> +#include <glut.h> + +GLuint buffer; +GLfloat angle = 0.0; +GLuint program; + +/* Utility function to load and compile a shader, checking the info log */ +static GLuint render_load_shader(const char *filename, GLenum type) { + + GLuint shader; + char text[4096]; + size_t len; + FILE *fh; + int l; + GLint status; + + fh = fopen(filename, "r"); + if ( fh == NULL ) { + fprintf(stderr, "Couldn't load shader '%s'\n", filename); + return 0; + } + len = fread(text, 1, 4095, fh); + fclose(fh); + text[len] = '\0'; + const GLchar *source = text; + shader = glCreateShader(type); + glShaderSource(shader, 1, &source, NULL); + glCompileShader(shader); + glGetShaderiv(shader, GL_COMPILE_STATUS, &status); + if ( status == GL_FALSE ) { + glGetShaderInfoLog(shader, 4095, &l, text); + if ( l > 0 ) { + printf("%s\n", text); fflush(stdout); + } else { + printf("Shader compilation failed.\n"); + } + } + + return shader; + +} + +static int render_validate_shader(GLuint shader) { + + GLint status; + int l; + char text[4096]; + + glValidateProgram(shader); + glGetProgramiv(shader, GL_VALIDATE_STATUS, &status); + if ( status == GL_FALSE ) { + glGetProgramInfoLog(shader, 4095, &l, text); + if ( l > 0 ) { + printf("%s\n", text); fflush(stdout); + } else { + printf("Shader did not validate successfully.\n"); + } + return 0; + } + + return 1; + +} + +static void glcheck_setup() { + + GLuint vert, frag; + + glGenBuffers(1, &buffer); + glBindBuffer(buffer); + glBindBuffer(0); + + vert = render_load_shader(DATADIR"/shaders/lighting.vert", GL_VERTEX_SHADER); + frag = render_load_shader(DATADIR"/shaders/lighting.frag", GL_FRAGMENT_SHADER); + program = glCreateProgram(); + glAttachShader(program, vert); + glAttachShader(program, frag); + glLinkProgram(program); + render_validate_shader(program); + +} + +static void glcheck_draw() { + + GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 }; + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(50.0, 1.0, 0.1, 100.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt(0.0, 0.0, -2.0, + 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0); + + glEnable(GL_LIGHTING); + GLfloat pos[] = { 0.0, 0.0, -2.0, 0.0 }; + GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; + GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 }; + glLightfv(GL_LIGHT0, GL_POSITION, pos); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); + glLightfv(GL_LIGHT0, GL_SPECULAR, specular); + glEnable(GL_LIGHT0); + + glRotatef(angle, 0.0, 1.0, 0.0); + + glFrontFace(GL_CW); + glUseProgram(program); + glEnable(GL_COLOR_MATERIAL); + glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); + glMaterialfv(GL_FRONT, GL_SPECULAR, white); + glMaterialf(GL_FRONT, GL_SHININESS, 80.0); + glColor3f(0.0, 0.0, 1.0); + glutSolidTeapot(0.5); + glUseProgram(0); + glFrontFace(GL_CCW); + + glutSwapBuffers(); + +} + +void glcheck_update() { + angle += 0.3; + glutPostRedisplay(); + glutTimerFunc(10.0, glcheck_update, 0.0); +} + +int main(int argc, char *argv[]) { + + int window; + + glutInit(&argc,argv); + glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); + glutInitWindowSize(512, 512); + window = glutCreateWindow("Thrust3D GLcheck"); + + glcheck_setup(); + glutTimerFunc(10.0, glcheck_update, 0.0); + glutDisplayFunc(glcheck_draw); + glutMainLoop(); + + glutDestroyWindow(window); + + return 0; + +} + |