Commit c81a3775 authored by bbguimaraes's avatar bbguimaraes
Browse files

open.gl: drawing/exercises

parent 44b0d68e
CPPFLAGS = -std=c++11 -I..
all: colors drawing element uniforms
colors: colors.cpp ../common.h ../common.o -lglfw -lGLEW -lGLU -lGL
drawing: drawing.cpp ../common.h ../common.o -lglfw -lGLEW -lGLU -lGL
element: element.cpp ../common.h ../common.o -lglfw -lGLEW -lGLU -lGL
uniforms: uniforms.cpp ../common.h ../common.o -lglfw -lGLEW -lGLU -lGL
COMMON = ../common.h ../common.o
LIBRARIES = -lglfw -lGLEW -lGLU -lGL
all: colors drawing element ex1 ex2 ex3 uniforms
colors: colors.cpp ${COMMON} ${LIBRARIES}
drawing: drawing.cpp ${COMMON} ${LIBRARIES}
element: element.cpp ${COMMON} ${LIBRARIES}
ex1: ex1.cpp ${COMMON} ${LIBRARIES}
ex2: ex2.cpp ${COMMON} ${LIBRARIES}
ex3: ex3.cpp ${COMMON} ${LIBRARIES}
uniforms: uniforms.cpp ${COMMON} ${LIBRARIES}
.PHONY: clean
clean:
rm -f colors drawing element uniforms
rm -f colors drawing element ex1 ex2 ex3 uniforms
#include "common.h"
float vertices[] = {
0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
};
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec2 position;\n"
"in vec3 color;\n"
"\n"
"out vec3 Color;\n"
"\n"
"void main() {\n"
" Color = color;\n"
" gl_Position = vec4(position.x, -position.y, 0.0, 1.0);\n"
"}\n";
const GLchar * frag_shader_src =
"#version 150 core\n"
"\n"
"in vec3 Color;\n"
"out vec4 outColor;\n"
"\n"
"void main() {\n"
" outColor = vec4(Color, 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, 5 * sizeof(float), 0);
GLint col_attr = glGetAttribLocation(program, "color");
glEnableVertexAttribArray(col_attr);
glVertexAttribPointer(
col_attr, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
reinterpret_cast<void *>(2 * sizeof(float)));
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);
glDrawArrays(GL_TRIANGLES, 0, 3);
exit_on_gl_error();
}
glfwTerminate();
return 0;
}
#include "common.h"
float vertices[] = {
0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
};
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec2 position;\n"
"in vec3 color;\n"
"\n"
"out vec3 Color;\n"
"\n"
"void main() {\n"
" Color = color;\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"
"out vec4 outColor;\n"
"\n"
"void main() {\n"
" outColor = vec4(1.0 - Color, 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, 5 * sizeof(float), 0);
GLint col_attr = glGetAttribLocation(program, "color");
glEnableVertexAttribArray(col_attr);
glVertexAttribPointer(
col_attr, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
reinterpret_cast<void *>(2 * sizeof(float)));
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);
glDrawArrays(GL_TRIANGLES, 0, 3);
exit_on_gl_error();
}
glfwTerminate();
return 0;
}
#include "common.h"
float vertices[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 1.0f,
};
const GLchar * vertex_shader_src =
"#version 150 core\n"
"\n"
"in vec2 position;\n"
"in float color;\n"
"\n"
"out float Color;\n"
"\n"
"void main() {\n"
" Color = color;\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n";
const GLchar * frag_shader_src =
"#version 150 core\n"
"\n"
"in float Color;\n"
"out vec4 outColor;\n"
"\n"
"void main() {\n"
" outColor = vec4(Color, Color, Color, 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, 3 * sizeof(float), 0);
GLint col_attr = glGetAttribLocation(program, "color");
glEnableVertexAttribArray(col_attr);
glVertexAttribPointer(
col_attr, 1, GL_FLOAT, GL_FALSE, 3 * sizeof(float),
reinterpret_cast<void *>(2 * sizeof(float)));
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);
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