diff --git a/Makefile b/Makefile index 753412f..e62ff56 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,9 @@ TARGET := app OBJS := app.o GL3/gl3w.o +OBJS += glcxx/src/glcxx/Array.o +OBJS += glcxx/src/glcxx/Buffer.o +OBJS += glcxx/src/glcxx/Program.o +OBJS += glcxx/src/glcxx/Shader.o REPOS := glcxx glm CCFLAGS := $(shell sdl2-config --cflags) @@ -28,6 +32,9 @@ $(TARGET): $(OBJS) %.o: %.cc $(CXX) -c -o $@ $(CCFLAGS) $(CXXFLAGS) $< +%.o: %.cpp + $(CXX) -c -o $@ $(CCFLAGS) $(CXXFLAGS) $< + .PHONY: submodule_init submodule_init: @if [ ! -e glcxx ]; then \ @@ -35,4 +42,4 @@ submodule_init: fi clean: - -rm -f $(TARGET) *~ *.o + -rm -f $(TARGET) *~ $(OBJS) diff --git a/app.cc b/app.cc index 376c25a..108c07a 100644 --- a/app.cc +++ b/app.cc @@ -7,15 +7,46 @@ using namespace std; std::shared_ptr program; +std::shared_ptr cube_buffer; +std::shared_ptr cube_array; #define WIDTH 800 #define HEIGHT 800 -void init(void) +bool init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glViewport(0, 0, WIDTH, HEIGHT); + try + { + program = glcxx::Program::create( + glcxx::Shader::create_from_file(GL_VERTEX_SHADER, "cube.v.glsl"), + glcxx::Shader::create_from_file(GL_FRAGMENT_SHADER, "cube.f.glsl"), + "position", 0, + "normal", 1, + "tex_coord", 2); + + cube_buffer = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, + {-1, -1, -1, 0, 0, -1, 0, 0, + 1, -1, -1, 0, 0, -1, 1, 0, + 1, 1, -1, 0, 0, -1, 1, 1, + -1, 1, -1, 0, 0, -1, 0, 1}); + cube_array = make_shared(); + cube_array->bind(); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), NULL); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat))); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(6 * sizeof(GLfloat))); + } + catch (glcxx::Error & e) + { + cerr << "glcxx error: " << e.what() << endl; + return false; + } + return true; } void display(SDL_Window * window) @@ -60,7 +91,11 @@ int main(int argc, char *argv[]) return 2; } - init(); + if (!init()) + { + return 2; + } + display(window); SDL_Event event; while (SDL_WaitEvent(&event)) diff --git a/cube.f.glsl b/cube.f.glsl new file mode 100644 index 0000000..461aa9d --- /dev/null +++ b/cube.f.glsl @@ -0,0 +1,35 @@ + +uniform vec4 ambient, specular; +uniform float shininess; +uniform sampler2D tex; + +varying vec3 pos_i; +varying vec3 normal_i; +varying vec2 tex_coord_i; + +void main(void) +{ + vec3 n, lightDir; + vec4 color; + float NdotL, RdotEye; + + lightDir = normalize(vec3(-0.1, 0, -0.9)); + color = vec4(0.2, 0.2, 0.2, 1.0) * ambient; /* ambient light */ + n = normalize(normal_i); + + NdotL = dot(n, -lightDir); + + if (NdotL > 0.0) + { + /* diffuse component */ + color += texture2D(tex, tex_coord_i) * NdotL; + /* specular component */ + RdotEye = dot(normalize(-pos_i), normalize(reflect(-lightDir, n))); + if (RdotEye > 0.0) + { + color += clamp(specular * pow(RdotEye, shininess), 0.0, 1.0); + } + } + + gl_FragColor = color; +} diff --git a/cube.v.glsl b/cube.v.glsl new file mode 100644 index 0000000..9780d82 --- /dev/null +++ b/cube.v.glsl @@ -0,0 +1,19 @@ + +uniform mat4 projection; +uniform mat4 modelview; + +attribute vec3 pos; +attribute vec3 normal; +attribute vec2 tex_coord; + +varying vec3 pos_i; +varying vec3 normal_i; +varying vec2 tex_coord_i; + +void main(void) +{ + gl_Position = projection * modelview * vec4(pos, 1); + pos_i = gl_Position.xyz; + tex_coord_i = tex_coord; + normal_i = (modelview * vec4(normal, 0)).xyz; +}