Commit 07300ebc authored by bbguimaraes's avatar bbguimaraes
Browse files

open.gl: textures

parent c81a3775
CPPFLAGS = -std=c++11 -I..
common.o: common.cpp
all: common.o texture_common.o
common.o: common.h common.cpp
texture_common.o: texture_common.h texture_common.cpp
.PHONY: clean
clean:
rm -f common.o
rm -f common.o texture_common.o
......@@ -59,6 +59,14 @@ GLuint create_vbo(size_t size, float vertices[]) {
return vbo;
}
GLuint create_ebo(size_t size, unsigned int elements[]) {
GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, elements, GL_STATIC_DRAW);
return ebo;
}
GLuint create_shader(const GLchar * src, GLenum type) {
GLuint id = glCreateShader(type);
glShaderSource(id, 1, &src, NULL);
......
......@@ -13,6 +13,7 @@ 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_shader(const GLchar * src, GLenum type);
GLuint create_shader_or_exit(const GLchar * src, GLenum type);
void check_shader_or_exit(GLuint id);
......
#include "common.h"
GLuint create_ebo(size_t size, unsigned int elements[]);
float vertices[] = {
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
......@@ -69,11 +68,3 @@ int main() {
glfwTerminate();
return 0;
}
GLuint create_ebo(size_t size, unsigned int elements[]) {
GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, elements, GL_STATIC_DRAW);
return ebo;
}
#include "texture_common.h"
#include <SOIL/SOIL.h>
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;
}
#ifndef TEXTURE_COMMON_H
#define TEXTURE_COMMON_H
#include <GL/glew.h>
#include <GL/gl.h>
GLint create_texture_from_image(const char * filename, GLenum object);
#endif
CPPFLAGS = -std=c++11 -I..
COMMON = ../common.h ../common.o ../texture_common.h ../texture_common.o
LIBRARIES = -lSOIL -lglfw -lGLEW -lGLU -lGL
all: ex1 ex2 ex3 textures units
ex1: ex1.cpp ${COMMON} ${LIBRARIES}
ex2: ex2.cpp ${COMMON} ${LIBRARIES}
ex3: ex3.cpp ${COMMON} ${LIBRARIES}
textures: textures.cpp ${COMMON} ${LIBRARIES}
units: units.cpp ${COMMON} ${LIBRARIES}
.PHONY: clean
clean:
rm -f ex1 ex2 ex3 textures units
#include "common.h"
#include "texture_common.h"
#include <ctime> // clock, CLOCKS_PER_SEC
#include <SOIL/SOIL.h>
float 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,
};
GLuint elements[] = {
0, 1, 2,
2, 3, 0,
};
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec2 position;\n"
"in vec2 texcoord;\n"
"\n"
"out vec2 Texcoord;\n"
"\n"
"void main() {\n"
" Texcoord = texcoord;\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n";
const GLchar * frag_shader_src =
"#version 150 core\n"
"\n"
"in vec2 Texcoord;\n"
"\n"
"out vec4 outColor;\n"
"\n"
"uniform sampler2D texKitten;\n"
"uniform sampler2D texPuppy;\n"
"uniform float time;\n"
"\n"
"void main() {\n"
" vec4 colKitten = texture(texKitten, Texcoord);\n"
" vec4 colPuppy = texture(texPuppy, Texcoord);\n"
" outColor = mix(colKitten, colPuppy, (sin(time * 50.0) + 1.0) / 2.0)\n"
" * vec4(1.0, 1.0, 1.0, 1.0);\n"
"}\n";
int main() {
GLFWwindow * window = init();
create_vao();
create_vbo(sizeof(vertices), vertices);
create_ebo(sizeof(elements), elements);
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, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
GLint tex_attr = glGetAttribLocation(program, "texcoord");
glEnableVertexAttribArray(tex_attr);
glVertexAttribPointer(
tex_attr, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float),
reinterpret_cast<void *>(2 * 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);
GLint time_uni = glGetUniformLocation(program, "time");
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
float time = static_cast<float>(std::clock())
/ static_cast<float>(CLOCKS_PER_SEC);
glUniform1f(time_uni, time);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
exit_on_gl_error();
}
glfwTerminate();
return 0;
}
#include "common.h"
#include "texture_common.h"
#include <SOIL/SOIL.h>
float 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,
};
GLuint elements[] = {
0, 1, 2,
2, 3, 0,
};
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec2 position;\n"
"in vec2 texcoord;\n"
"\n"
"out vec2 Texcoord;\n"
"\n"
"void main() {\n"
" Texcoord = texcoord;\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n";
const GLchar * frag_shader_src =
"#version 150 core\n"
"\n"
"in vec2 Texcoord;\n"
"\n"
"out vec4 outColor;\n"
"\n"
"uniform sampler2D texKitten;\n"
"uniform sampler2D texPuppy;\n"
"\n"
"void main() {\n"
" if(Texcoord.y >= 0.5)\n"
" outColor = texture(\n"
" texKitten, vec2(Texcoord.x, 1.0 - Texcoord.y));\n"
" else {\n"
" vec4 colKitten = texture(texKitten, Texcoord);\n"
" vec4 colPuppy = texture(texPuppy, Texcoord);\n"
" outColor = mix(colKitten, colPuppy, 0.5)\n"
" * vec4(1.0, 1.0, 1.0, 1.0);\n"
" }\n"
"}\n";
int main() {
GLFWwindow * window = init();
create_vao();
create_vbo(sizeof(vertices), vertices);
create_ebo(sizeof(elements), elements);
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, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
GLint tex_attr = glGetAttribLocation(program, "texcoord");
glEnableVertexAttribArray(tex_attr);
glVertexAttribPointer(
tex_attr, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float),
reinterpret_cast<void *>(2 * 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);
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
exit_on_gl_error();
}
glfwTerminate();
return 0;
}
#include "common.h"
#include "texture_common.h"
#include <ctime> // clock, CLOCKS_PER_SEC
#include <SOIL/SOIL.h>
float 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,
};
GLuint elements[] = {
0, 1, 2,
2, 3, 0,
};
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec2 position;\n"
"in vec2 texcoord;\n"
"\n"
"out vec2 Texcoord;\n"
"\n"
"void main() {\n"
" Texcoord = texcoord;\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n";
const GLchar * frag_shader_src =
"#version 150 core\n"
"\n"
"in vec2 Texcoord;\n"
"\n"
"out vec4 outColor;\n"
"\n"
"uniform sampler2D tex;\n"
"uniform float time;\n"
"\n"
"void main() {\n"
" if(Texcoord.y < 0.5)\n"
" outColor = texture(tex, Texcoord);\n"
" else\n"
" outColor = texture(\n"
" tex,\n"
" vec2(\n"
" Texcoord.x + (sin(time * 60.0 + Texcoord.y * 60.0) / 30.0),\n"
" 1.0 - Texcoord.y))\n"
" * vec4(0.7, 0.7, 1.0, 1.0);\n"
"}\n";
int main() {
GLFWwindow * window = init();
create_vao();
create_vbo(sizeof(vertices), vertices);
create_ebo(sizeof(elements), elements);
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, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
GLint tex_attr = glGetAttribLocation(program, "texcoord");
glEnableVertexAttribArray(tex_attr);
glVertexAttribPointer(
tex_attr, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float),
reinterpret_cast<void *>(2 * sizeof(float)));
GLint tex = create_texture_from_image("../sample.png", GL_TEXTURE0);
GLint time_uni = glGetUniformLocation(program, "time");
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
float time = static_cast<float>(std::clock())
/ static_cast<float>(CLOCKS_PER_SEC);
glUniform1f(time_uni, time);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
exit_on_gl_error();
}
glfwTerminate();
return 0;
}
#include "common.h"
#include "texture_common.h"
float vertices[] = {
// pos color texcoords
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
};
GLuint elements[] = {
0, 1, 2,
2, 3, 0,
};
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec2 position;\n"
"in vec3 color;\n"
"in vec2 texcoord;\n"
"\n"
"out vec3 Color;\n"
"out vec2 Texcoord;\n"
"\n"
"void main() {\n"
" Color = color;\n"
" Texcoord = texcoord;\n"
" gl_Position = vec4(position, 0.0, 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 tex;\n"
"\n"
"void main() {\n"
" outColor = texture(tex, Texcoord) * vec4(Color, 1.0);\n"
"}\n";
int main() {
GLFWwindow * window = init();
create_vao();
create_vbo(sizeof(vertices), vertices);
create_ebo(sizeof(elements), elements);
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, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(float), 0);
GLint col_attr = glGetAttribLocation(program, "color");
glEnableVertexAttribArray(col_attr);
glVertexAttribPointer(
col_attr, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float),
reinterpret_cast<void *>(2 * sizeof(float)));
GLint tex_attr = glGetAttribLocation(program, "texcoord");
glEnableVertexAttribArray(tex_attr);
glVertexAttribPointer(
tex_attr, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(float),
reinterpret_cast<void *>(5 * sizeof(float)));
GLint tex = create_texture_from_image("../sample.png", GL_TEXTURE0);
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
exit_on_gl_error();
}
glfwTerminate();
return 0;
}
#include "common.h"
#include "texture_common.h"
#include <SOIL/SOIL.h>
float 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,
};
GLuint elements[] = {
0, 1, 2,
2, 3, 0,
};
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec2 position;\n"
"in vec2 texcoord;\n"
"\n"
"out vec2 Texcoord;\n"
"\n"
"void main() {\n"
" Texcoord = texcoord;\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n";
const GLchar * frag_shader_src =
"#version 150 core\n"
"\n"
"in vec2 Texcoord;\n"
"\n"
"out vec4 outColor;\n"
"\n"
"uniform sampler2D texKitten;\n"
"uniform sampler2D texPuppy;\n"
"\n"
"void main() {\n"
" vec4 colKitten = texture(texKitten, Texcoord);\n"
" vec4 colPuppy = texture(texPuppy, Texcoord);\n"
" outColor = mix(colKitten, colPuppy, 0.5)\n"
" * vec4(1.0, 1.0, 1.0, 1.0);\n"
"}\n";
int main() {
GLFWwindow * window = init();
create_vao();
create_vbo(sizeof(vertices), vertices);
create_ebo(sizeof(elements), elements);
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, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
GLint tex_attr = glGetAttribLocation(program, "texcoord");
glEnableVertexAttribArray(tex_attr);
glVertexAttribPointer(
tex_attr, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float),
reinterpret_cast<void *>(2 * 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);
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
exit_on_gl_error();
}
glfwTerminate();
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment