Commit b7229c57 authored by bbguimaraes's avatar bbguimaraes
Browse files

open.gl: drawing

parent c7acd2b2
CPPFLAGS = -std=c++11 -I..
common.o: common.cpp
.PHONY: clean
clean:
rm -f common.o
#include "common.h"
#include <cstdlib>
#include <iostream>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GLFW/glfw3.h>
GLFWwindow * init() {
glfwInit();
GLFWwindow * window = create_window(800, 600, "OpenGL");
glfwMakeContextCurrent(window);
init_glew_or_exit();
return window;
}
GLFWwindow * create_window(int width, int height, const char * title) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
return glfwCreateWindow(width, height, title, nullptr, nullptr);
}
void init_glew_or_exit() {
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if(err != GLEW_OK) {
std::cerr << "glewInit: failed" << std::endl;
std::exit(1);
}
// "GLEW has a problem with core contexts. It calls
// glGetString(GL_EXTENSIONS), which causes GL_INVALID_ENUM on GL 3.2+ core
// context as soon as glewInit() is called.
// https://www.opengl.org/wiki/OpenGL_Loading_Library
glGetError();
}
void exit_on_gl_error() {
GLenum error;
if((error = glGetError()) != GL_NO_ERROR) {
std::cerr << "glGetError: " << gluErrorString(error) << std::endl;
std::exit(1);
}
}
#ifndef COMMON_H
#define COMMON_H
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
class GLFWwindow;
GLFWwindow * init();
GLFWwindow * create_window(int width, int height, const char * title);
void init_glew_or_exit();
void exit_on_gl_error();
#endif
CPPFLAGS = -std=c++11
context: context.cpp -lglfw -lGLEW -lGLU -lGL
CPPFLAGS = -std=c++11 -I..
context: context.cpp ../common.h ../common.o -lglfw -lGLEW -lGLU -lGL
.PHONY: clean
clean:
rm -f context
......@@ -3,16 +3,12 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow * create_window(int width, int height, const char * title);
void init_glew();
#include "common.h"
void loop(GLFWwindow * window);
void check_gl_error();
int main() {
glfwInit();
GLFWwindow * window = create_window(800, 600, "OpenGL");
glfwMakeContextCurrent(window);
init_glew();
GLFWwindow * window = init();
GLuint vertex_buffer;
glGenBuffers(1, &vertex_buffer);
std::cout << vertex_buffer << std::endl;
......@@ -21,39 +17,12 @@ int main() {
return 0;
}
GLFWwindow * create_window(int width, int height, const char * title) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
return glfwCreateWindow(width, height, title, nullptr, nullptr);
}
void init_glew() {
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if(err != GLEW_OK) {
std::cerr << "glewInit: failed" << std::endl;
exit(1);
}
// https://www.opengl.org/wiki/OpenGL_Loading_Library
glGetError();
}
void loop(GLFWwindow * window) {
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
check_gl_error();
}
}
void check_gl_error() {
GLenum error;
if((error = glGetError()) != GL_NO_ERROR) {
std::cerr << "glGetError: " << gluErrorString(error) << std::endl;
exit(1);
exit_on_gl_error();
}
}
CPPFLAGS = -std=c++11 -I..
drawing: drawing.cpp ../common.h ../common.o -lglfw -lGLEW -lGLU -lGL
.PHONY: clean
clean:
rm -f drawing
#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);
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 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();
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();
}
}
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