start using glcxx
This commit is contained in:
parent
8c02c0636e
commit
27d9b1535e
9
Makefile
9
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)
|
||||
|
39
app.cc
39
app.cc
@ -7,15 +7,46 @@
|
||||
using namespace std;
|
||||
|
||||
std::shared_ptr<glcxx::Program> program;
|
||||
std::shared_ptr<glcxx::Buffer> cube_buffer;
|
||||
std::shared_ptr<glcxx::Array> 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<glcxx::Array>();
|
||||
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))
|
||||
|
35
cube.f.glsl
Normal file
35
cube.f.glsl
Normal file
@ -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;
|
||||
}
|
19
cube.v.glsl
Normal file
19
cube.v.glsl
Normal file
@ -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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user