commit bab548a362607be12d266adb2a090e6ed4e20316
parent fcf17760bb747380a3dd3f883740f27164865984
Author: William Casarin <bill@casarin.me>
Date: Sat, 28 Mar 2015 01:24:29 -0700
add shader, debug, file helpers
Diffstat:
8 files changed, 147 insertions(+), 10 deletions(-)
diff --git a/Makefile b/Makefile
@@ -4,9 +4,13 @@ PREFIX ?= /usr/local
LDFLAGS = -lglfw3 -lX11 -lGL -lXi -lXrandr -lXxf86vm -lepoxy
DEFS= -DGLFW_INCLUDE_NONE
-OBJS = render.o
+OBJS = main.o
OBJS += window.o
OBJS += buffer.o
+OBJS += shader.o
+OBJS += file.o
+OBJS += debug.o
+OBJS += render.o
all: $(BIN)
@@ -14,7 +18,7 @@ all: $(BIN)
$(CXX) $(DEFS) -c $< -o $@
$(BIN): $(OBJS)
- $(CXX) $(DEFS) $^ main.cc $(LDFLAGS) -o $@
+ $(CXX) $(DEFS) $^ $(LDFLAGS) -o $@
install: $(BIN)
install -d $(PREFIX)/bin
diff --git a/debug.cc b/debug.cc
@@ -0,0 +1,21 @@
+
+#include "debug.h"
+#include <stdlib.h>
+#include <stdio.h>
+
+void
+show_info_log(
+ GLuint object,
+ PFNGLGETSHADERIVPROC glGet__iv,
+ PFNGLGETSHADERINFOLOGPROC glGet__InfoLog
+) {
+ GLint log_length;
+ char *log;
+
+ glGet__iv(object, GL_INFO_LOG_LENGTH, &log_length);
+ // TODO (jb55): remove mallocs?
+ log = (char*)malloc(log_length);
+ glGet__InfoLog(object, log_length, NULL, log);
+ fprintf(stderr, "%s", log);
+ free(log);
+}
diff --git a/debug.h b/debug.h
@@ -0,0 +1,15 @@
+
+#ifndef POLYADVENT_DEBUG_H
+#define POLYADVENT_DEBUG_H
+
+#include "gl.h"
+
+void
+show_info_log(
+ GLuint object,
+ PFNGLGETSHADERIVPROC glGet__iv,
+ PFNGLGETSHADERINFOLOGPROC glGet__InfoLog
+);
+
+
+#endif /* POLYADVENT_DEBUG_H */
diff --git a/file.cc b/file.cc
@@ -0,0 +1,25 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void *
+file_contents(const char *filename, int *length) {
+ FILE *f = fopen(filename, "r");
+ void *buffer;
+
+ if (!f) {
+ fprintf(stderr, "Unable to open %s for reading\n", filename);
+ return NULL;
+ }
+
+ fseek(f, 0, SEEK_END);
+ *length = ftell(f);
+ fseek(f, 0, SEEK_SET);
+
+ buffer = malloc(*length+1);
+ *length = fread(buffer, 1, *length, f);
+ fclose(f);
+ ((char*)buffer)[*length] = '\0';
+
+ return buffer;
+}
diff --git a/file.h b/file.h
@@ -0,0 +1,8 @@
+#ifndef POLYADVEMT_FILE_H
+#define POLYADVEMT_FILE_H
+
+#include "gl.h"
+
+void *file_contents(const char *filename, GLint *length);
+
+#endif /* POLYADVEMT_FILE_H */
diff --git a/render.cc b/render.cc
@@ -1,21 +1,47 @@
#include "gl.h"
+#include "buffer.h"
+
+// TESTING
+static GLuint g_vertex_buffer;
+static GLuint g_element_buffer;
+
+static const GLfloat g_vertex_buffer_data[] = {
+ -1.0f, -1.0f,
+ 1.0f, -1.0f,
+ -1.0f, 1.0f,
+ 1.0f, 1.0f
+};
+static const GLushort g_element_buffer_data[] = { 0, 1, 2, 3 };
void
init_gl() {
- glEnable(GL_DEPTH_TEST);
+ //glEnable(GL_DEPTH_TEST);
+
+ g_vertex_buffer = make_buffer(
+ GL_ARRAY_BUFFER,
+ g_vertex_buffer_data,
+ sizeof(g_vertex_buffer_data)
+ );
+
+ g_element_buffer = make_buffer(
+ GL_ELEMENT_ARRAY_BUFFER,
+ g_element_buffer_data,
+ sizeof(g_element_buffer_data)
+ );
}
void
render () {
- glBegin(GL_TRIANGLES); //Begin triangle coordinates
-
- //Triangle
- glVertex3f(-0.5f, 0.5f, -5.0f);
- glVertex3f(-1.0f, 1.5f, -5.0f);
- glVertex3f(-1.5f, 0.5f, -5.0f);
+ glBindBuffer(GL_ARRAY_BUFFER, g_vertex_buffer);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_element_buffer);
- glEnd(); //End triangle coordinates
+ glDrawElements(
+ GL_TRIANGLE_STRIP, /* mode */
+ 4, /* count */
+ GL_UNSIGNED_SHORT, /* type */
+ (void*)0 /* element array buffer offset */
+ );
}
diff --git a/shader.cc b/shader.cc
@@ -0,0 +1,32 @@
+
+#include <stdlib.h>
+#include <stdio.h>
+#include "file.h"
+#include "gl.h"
+#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;
+}
diff --git a/shader.h b/shader.h
@@ -0,0 +1,6 @@
+#ifndef POLYADVENT_SHADER_H
+#define POLYADVENT_SHADER_H
+
+GLuint make_shader(GLenum type, const char *filename);
+
+#endif /* POLYADVENT_SHADER_H */