Commit b5173bd6 authored by bbguimaraes's avatar bbguimaraes
Browse files

open.gl: depthstencils

parent c882b904
CPPFLAGS = -std=c++11 -I..
all: common.o texture_common.o
OBJECTS = common.o transformations_common.o
all: $(OBJECTS)
common.o: common.h common.cpp
texture_common.o: texture_common.h texture_common.cpp
.PHONY: clean
clean:
rm -f common.o texture_common.o
rm -f $(OBJECTS)
......@@ -5,6 +5,81 @@
#include <GL/glu.h>
#include <GLFW/glfw3.h>
#include <SOIL/SOIL.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
const unsigned int SQUARE_VERTICES_COUNT = 16;
const float SQUARE_VERTICES[] = {
// pos texcoords
-0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 1.0f,
};
const unsigned int SQUARE_ELEMENTS_COUNT = 6;
const GLuint SQUARE_ELEMENTS[] = {
0, 1, 2,
2, 3, 0,
};
const unsigned int CUBE_VERTICES_COUNT = 336;
const float CUBE_VERTICES[] = {
// pos color texcoords
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
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, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
1.0f, -1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
};
GLFWwindow * init() {
glfwInit();
......@@ -51,7 +126,7 @@ GLuint create_vao() {
return vao;
}
GLuint create_vbo(size_t size, float vertices[]) {
GLuint create_vbo(size_t size, const float vertices[]) {
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
......@@ -59,7 +134,7 @@ GLuint create_vbo(size_t size, float vertices[]) {
return vbo;
}
GLuint create_ebo(size_t size, unsigned int elements[]) {
GLuint create_ebo(size_t size, const unsigned int elements[]) {
GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
......@@ -107,3 +182,65 @@ GLuint create_program(GLuint vertex_shader, GLuint frag_shader) {
glUseProgram(program);
return program;
}
GLint create_texture_from_image(const char * filename, GLenum object) {
GLuint tex;
glActiveTexture(object);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height;
unsigned char * image = SOIL_load_image(
filename, &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
glGenerateMipmap(GL_TEXTURE_2D);
return tex;
}
void setup_projection(GLuint program) {
glm::mat4 view = glm::lookAt(
glm::vec3(1.2f, 1.2f, 1.2f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 1.0f));
GLint view_uni = glGetUniformLocation(program, "view");
glUniformMatrix4fv(view_uni, 1, GL_FALSE, glm::value_ptr(view));
glm::mat4 proj = glm::perspective(45.0f, 800.0f / 600.0f, 1.0f, 10.0f);
GLint proj_uni = glGetUniformLocation(program, "proj");
glUniformMatrix4fv(proj_uni, 1, GL_FALSE, glm::value_ptr(proj));
}
float generate_angle(clock_t c, clock_t cs) {
return static_cast<float>(c) / cs * 25.0f * glm::radians(180.0f);
}
void draw_scene(float angle, GLint model_uni, GLint ov_col_uni) {
glm::mat4 model;
model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
glUniformMatrix4fv(model_uni, 1, GL_FALSE, glm::value_ptr(model));
glEnable(GL_DEPTH_TEST);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 36);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glStencilMask(0xFF);
glDepthMask(GL_FALSE);
glClear(GL_STENCIL_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 36, 6);
glStencilFunc(GL_EQUAL, 1, 0xFF);
glStencilMask(0x00);
glDepthMask(GL_TRUE);
model = glm::scale(
glm::translate(model, glm::vec3(0.0f, 0.0f, -1.0f)),
glm::vec3(1.0f, 1.0f, -1.0f));
glUniformMatrix4fv(model_uni, 1, GL_FALSE, glm::value_ptr(model));
glUniform3f(ov_col_uni, 0.3f, 0.3f, 0.3f);
glDrawArrays(GL_TRIANGLES, 0, 36);
glUniform3f(ov_col_uni, 1.0f, 1.0f, 1.0f);
glDisable(GL_STENCIL_TEST);
}
#ifndef COMMON_H
#define COMMON_H
#include <ctime> // clock_t
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
class GLFWwindow;
extern const unsigned int SQUARE_VERTICES_COUNT;
extern const float SQUARE_VERTICES[];
extern const unsigned int SQUARE_ELEMENTS_COUNT;
extern const GLuint SQUARE_ELEMENTS[];
extern const unsigned int CUBE_VERTICES_COUNT;
extern const float CUBE_VERTICES[];
GLFWwindow * init();
GLFWwindow * create_window(int width, int height, const char * title);
void init_glew_or_exit();
void exit_on_gl_error();
GLuint create_vao();
GLuint create_vbo(size_t size, float vertices[]);
GLuint create_ebo(size_t size, unsigned int elements[]);
GLuint create_vbo(size_t size, const float vertices[]);
GLuint create_ebo(size_t size, const unsigned int elements[]);
GLuint create_shader(const GLchar * src, GLenum type);
GLuint create_shader_or_exit(const GLchar * src, GLenum type);
void check_shader_or_exit(GLuint id);
bool is_valid_shader(GLuint id);
GLuint create_program(GLuint vertex_shader, GLuint frag_shader);
GLint create_texture_from_image(const char * filename, GLenum object);
void setup_projection(GLuint program);
float generate_angle(clock_t c, clock_t cs);
void draw_scene(float angle, GLint model_uni, GLint ov_col_uni);
#endif
CPPFLAGS = -std=c++11 -I..
context: context.cpp ../common.h ../common.o -lglfw -lGLEW -lGLU -lGL
context: context.cpp ../common.h ../common.o -lSOIL -lglfw -lGLEW -lGLU -lGL
.PHONY: clean
clean:
rm -f context
CPPFLAGS = -std=c++11 -I..
COMMON = ../common.h ../common.o
LIBRARIES = -lSOIL -lglfw -lGLEW -lGLU -lGL
all: depth depthstencils planar
depth: depth.cpp ${COMMON} ${LIBRARIES}
depthstencils: depthstencils.cpp ${COMMON} ${LIBRARIES}
planar: planar.cpp \
../transformations_common.h ../transformations_common.o \
${COMMON} ${LIBRARIES}
.PHONY: clean
clean:
rm -f depth depthstencils planar
#include "common.h"
#include "transformations_common.h"
#include <ctime> // clock, CLOCKS_PER_SEC
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <SOIL/SOIL.h>
float vertices[] = {
// pos color texcoords
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f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 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
};
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec3 position;\n"
"in vec3 color;\n"
"in vec2 texcoord;\n"
"\n"
"out vec3 Color;\n"
"out vec2 Texcoord;\n"
"\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 proj;\n"
"\n"
"void main() {\n"
" Color = color;\n"
" Texcoord = texcoord;\n"
" gl_Position = proj * view * model * vec4(position, 1.0);\n"
"}\n";
const GLchar * frag_shader_src =
"#version 150 core\n"
"\n"
"in vec3 Color;\n"
"in vec2 Texcoord;\n"
"\n"
"out vec4 outColor;\n"
"\n"
"uniform sampler2D texKitten;\n"
"uniform sampler2D texPuppy;\n"
"\n"
"void main() {\n"
" vec4 texColor = mix(\n"
" texture(texKitten, Texcoord),\n"
" texture(texPuppy, Texcoord),\n"
" 0.5);\n"
" outColor = vec4(Color, 1.0) * texColor;\n"
"}\n";
int main() {
GLFWwindow * window = init();
create_vao();
create_vbo(sizeof(vertices), vertices);
GLuint vertex_shader = create_shader_or_exit(
vertex_shader_src, GL_VERTEX_SHADER);
GLuint frag_shader = create_shader_or_exit(
frag_shader_src, GL_FRAGMENT_SHADER);
GLuint program = create_program(vertex_shader, frag_shader);
GLint pos_attr = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(pos_attr);
glVertexAttribPointer(
pos_attr, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0);
GLint color_attr = glGetAttribLocation(program, "color");
glEnableVertexAttribArray(color_attr);
glVertexAttribPointer(
color_attr, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
reinterpret_cast<void *>(3 * sizeof(float)));
GLint tex_attr = glGetAttribLocation(program, "texcoord");
glEnableVertexAttribArray(tex_attr);
glVertexAttribPointer(
tex_attr, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
reinterpret_cast<void *>(6 * sizeof(float)));
GLint tex = create_texture_from_image("../sample.png", GL_TEXTURE0);
glUniform1i(glGetUniformLocation(program, "texKitten"), 0);
tex = create_texture_from_image("../sample2.png", GL_TEXTURE1);
glUniform1i(glGetUniformLocation(program, "texPuppy"), 1);
glm::mat4 view = glm::lookAt(
glm::vec3(1.2f, 1.2f, 1.2f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 1.0f));
GLint view_uni = glGetUniformLocation(program, "view");
glUniformMatrix4fv(view_uni, 1, GL_FALSE, glm::value_ptr(view));
glm::mat4 proj = glm::perspective(45.0f, 800.0f / 600.0f, 1.0f, 10.0f);
GLint proj_uni = glGetUniformLocation(program, "proj");
glUniformMatrix4fv(proj_uni, 1, GL_FALSE, glm::value_ptr(proj));
GLint model_uni = glGetUniformLocation(program, "model");
glEnable(GL_DEPTH_TEST);
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
float angle = static_cast<float>(std::clock())
/ static_cast<float>(CLOCKS_PER_SEC)
* 25.0f * 3.1415f;
glm::mat4 model;
model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
glUniformMatrix4fv(model_uni, 1, GL_FALSE, glm::value_ptr(model));
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 36);
exit_on_gl_error();
}
glfwTerminate();
return 0;
}
#include "common.h"
#include <ctime> // clock, CLOCKS_PER_SEC
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <SOIL/SOIL.h>
float vertices[] = {
// pos color texcoords
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0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;\n"
"uniform mat4 view;\n"
"uniform mat4 proj;\n"
"\n"
"void main() {\n"
" Color = color;\n"
" Texcoord = texcoord;\n"
" gl_Position = proj * view * model * vec4(position, 1.0);\n"
"}\n";
const GLchar * frag_shader_src =
"#version 150 core\n"
"\n"
"in vec3 Color;\n"
"in vec2 Texcoord;\n"
"\n"
"out vec4 outColor;\n"
"\n"
"uniform sampler2D texKitten;\n"
"uniform sampler2D texPuppy;\n"
"\n"
"void main() {\n"
" vec4 texColor = mix(\n"
" texture(texKitten, Texcoord),\n"
" texture(texPuppy, Texcoord),\n"
" 0.5);\n"
" outColor = vec4(Color, 1.0) * texColor;\n"
"}\n";
int main() {
GLFWwindow * window = init();
create_vao();
create_vbo(sizeof(vertices), vertices);
GLuint vertex_shader = create_shader_or_exit(
vertex_shader_src, GL_VERTEX_SHADER);
GLuint frag_shader = create_shader_or_exit(
frag_shader_src, GL_FRAGMENT_SHADER);
GLuint program = create_program(vertex_shader, frag_shader);
GLint pos_attr = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(pos_attr);
glVertexAttribPointer(
pos_attr, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0);
GLint color_attr = glGetAttribLocation(program, "color");
glEnableVertexAttribArray(color_attr);
glVertexAttribPointer(
color_attr, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
reinterpret_cast<void *>(3 * sizeof(float)));
GLint tex_attr = glGetAttribLocation(program, "texcoord");
glEnableVertexAttribArray(tex_attr);
glVertexAttribPointer(
tex_attr, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
reinterpret_cast<void *>(6 * sizeof(float)));
GLint tex = create_texture_from_image("../sample.png", GL_TEXTURE0);
glUniform1i(glGetUniformLocation(program, "texKitten"), 0);
tex = create_texture_from_image("../sample2.png", GL_TEXTURE1);
glUniform1i(glGetUniformLocation(program, "texPuppy"), 1);
glm::mat4 view = glm::lookAt(
glm::vec3(1.2f, 1.2f, 1.2f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 1.0f));
GLint view_uni = glGetUniformLocation(program, "view");
glUniformMatrix4fv(view_uni, 1, GL_FALSE, glm::value_ptr(view));
glm::mat4 proj = glm::perspective(45.0f, 800.0f / 600.0f, 1.0f, 10.0f);
GLint proj_uni = glGetUniformLocation(program, "proj");
glUniformMatrix4fv(proj_uni, 1, GL_FALSE, glm::value_ptr(proj));
GLint model_uni = glGetUniformLocation(program, "model");
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
float angle = static_cast<float>(std::clock())
/ static_cast<float>(CLOCKS_PER_SEC)
* 25.0f * 3.1415f;
glm::mat4 model;
model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
glUniformMatrix4fv(model_uni, 1, GL_FALSE, glm::value_ptr(model));
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 36);
exit_on_gl_error();
}
glfwTerminate();
return 0;
}
#include "common.h"
#include "transformations_common.h"
#include <ctime> // clock, CLOCKS_PER_SEC
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <SOIL/SOIL.h>
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec3 position;\n"
"in vec3 color;\n"
"in vec2 texcoord;\n"
"\n"
"out vec3 Color;\n"