Commit c50e8811 authored by bbguimaraes's avatar bbguimaraes
Browse files

open.gl: drawing/uniforms

parent b7229c57
......@@ -3,8 +3,6 @@
#include <cstdlib>
#include <iostream>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GLFW/glfw3.h>
......@@ -45,3 +43,59 @@ void exit_on_gl_error() {
std::exit(1);
}
}
GLuint create_vao() {
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
return vao;
}
GLuint create_vbo(size_t size, float vertices[]) {
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
return vbo;
}
GLuint create_shader(const GLchar * src, GLenum type) {
GLuint id = glCreateShader(type);
glShaderSource(id, 1, &src, NULL);
glCompileShader(id);
return id;
}
void check_shader_or_exit(GLuint id) {
if(!is_valid_shader(id)) {
std::cerr
<< "glCompileShader(vertex_shader): failed, compile log follows"
<< std::endl;
char buffer[512];
glGetShaderInfoLog(id, 512, NULL, buffer);
std::cerr << buffer;
exit(1);
}
}
GLuint create_shader_or_exit(const GLchar * src, GLenum type) {
GLuint id = create_shader(src, type);
check_shader_or_exit(id);
return id;
}
bool is_valid_shader(GLuint id) {
GLint status;
glGetShaderiv(id, GL_COMPILE_STATUS, &status);
return status == GL_TRUE;
}
GLuint create_program(GLuint vertex_shader, GLuint frag_shader) {
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, frag_shader);
glBindFragDataLocation(program, 0, "outColor");
glLinkProgram(program);
glUseProgram(program);
return program;
}
......@@ -11,5 +11,12 @@ 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_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);
#endif
CPPFLAGS = -std=c++11 -I..
all: drawing uniforms
drawing: drawing.cpp ../common.h ../common.o -lglfw -lGLEW -lGLU -lGL
uniforms: uniforms.cpp ../common.h ../common.o -lglfw -lGLEW -lGLU -lGL
.PHONY: clean
clean:
rm -f drawing
rm -f drawing uniforms
#include "common.h"
#include <cstdlib> // exit
#include <iostream> // cerr, endl
GLuint create_vao();
GLuint create_vbo(size_t size, float vertices[]);
GLuint create_shader(const GLchar * src, GLenum type);
void check_shader(GLuint id);
bool is_valid_shader(GLuint id);
GLuint create_program(GLuint vertex_shader, GLuint frag_shader);
void loop(GLFWwindow * window);
float vertices[] = {
0.0f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f,
};
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec2 position;\n"
"\n"
"void main() {\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n";
const GLchar * frag_shader_src =
"#version 150 core\n"
"\n"
"out vec4 outColor;\n"
"\n"
"void main() {\n"
" outColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
"}\n";
int main() {
GLFWwindow * window = init();
float vertices[] = {
0.0f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f,
};
create_vao();
create_vbo(sizeof(vertices), vertices);
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec2 position;\n"
"\n"
"void main() {\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n";
GLuint vertex_shader = create_shader(vertex_shader_src, GL_VERTEX_SHADER);
check_shader(vertex_shader);
const GLchar * frag_shader_src =
"#version 150 core\n"
"\n"
"out vec4 outColor;\n"
"\n"
"void main() {\n"
" outColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
"}\n";
GLuint frag_shader = create_shader(frag_shader_src, GL_FRAGMENT_SHADER);
check_shader(frag_shader);
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);
loop(window);
glfwTerminate();
return 0;
}
GLuint create_vao() {
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
return vao;
}
GLuint create_vbo(size_t size, float vertices[]) {
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
return vbo;
}
GLuint create_shader(const GLchar * src, GLenum type) {
GLuint id = glCreateShader(type);
glShaderSource(id, 1, &src, NULL);
glCompileShader(id);
return id;
}
void check_shader(GLuint id) {
if(!is_valid_shader(id)) {
std::cerr
<< "glCompileShader(vertex_shader): failed, compile log follows"
<< std::endl;
char buffer[512];
glGetShaderInfoLog(id, 512, NULL, buffer);
std::cerr << buffer;
exit(1);
}
}
bool is_valid_shader(GLuint id) {
GLint status;
glGetShaderiv(id, GL_COMPILE_STATUS, &status);
return status == GL_TRUE;
}
GLuint create_program(GLuint vertex_shader, GLuint frag_shader) {
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, frag_shader);
glBindFragDataLocation(program, 0, "outColor");
glLinkProgram(program);
glUseProgram(program);
GLint pos_attr = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(pos_attr);
glVertexAttribPointer(pos_attr, 2, GL_FLOAT, GL_FALSE, 0, 0);
return program;
}
void loop(GLFWwindow * window) {
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
......@@ -110,4 +46,6 @@ void loop(GLFWwindow * window) {
glDrawArrays(GL_TRIANGLES, 0, 3);
exit_on_gl_error();
}
glfwTerminate();
return 0;
}
#include "common.h"
#include <cmath> // sin
#include <ctime> // clock, CLOCKS_PER_SEC
float vertices[] = {
0.0f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f,
};
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec2 position;\n"
"\n"
"void main() {\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n";
const GLchar * frag_shader_src =
"#version 150 core\n"
"\n"
"uniform vec3 triangleColor;\n"
"out vec4 outColor;\n"
"\n"
"void main() {\n"
" outColor = vec4(triangleColor, 1.0);\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, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLint uni_color = glGetUniformLocation(program, "triangleColor");
glUniform3f(uni_color, 1.0f, 0.0f, 0.0f);
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) * 50.0f;
glUniform3f(
uni_color, (std::sin(time * 4.0f) + 1.0f) / 2.0f, 0.0f, 0.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
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