Created
February 28, 2014 15:31
-
-
Save johncant/9273069 to your computer and use it in GitHub Desktop.
Try out openGL shaders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "image_view_renderer.h" | |
#include <string> | |
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 | |
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 | |
const std::string vertex_shader_source = "" | |
"#version 130\n" | |
"\n" | |
"in vec2 corners;\n" | |
"//varying vec2 uv;\n" | |
"\n" | |
"\n" | |
"void main() {\n" | |
" gl_Position = vec4(corners[0], corners[0], 2.0, 1.0);\n" | |
" gl_PointSize = 5;\n" | |
"// gl_ClipDistance[0] = 10.0;\n" | |
"// gl_ClipDistance[1] = 10.0;\n" | |
"}\n" | |
"\n"; | |
static const std::string fragment_shader_source = "" | |
"#version 130\n" | |
"\n" | |
"void main() {\n" | |
" gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0);\n" | |
" //FragColor = vec4(0.0, 1.0, 1.0, 1.0);\n" | |
"}\n"; | |
GLuint GtkGui::ImageViewRenderer::compile(const std::string& source, GLenum type) { | |
const GLchar* c_source = source.c_str(); | |
return compile(c_source, type); | |
} | |
GLuint GtkGui::ImageViewRenderer::compile(const char* c_source, GLenum type) { | |
GLuint shader = glCreateShader(type); | |
glShaderSource(shader, 1, &c_source, 0); | |
glCompileShader(shader); | |
GLint success = 0; | |
glGetObjectParameterivARB(shader, GL_COMPILE_STATUS, &success); | |
if (!success) { | |
std::cout << "Shader compilation failed" << std::endl; | |
GLchar error[2048]; | |
GLsizei size; | |
glGetShaderInfoLog(shader, 2048, &size, error); | |
std::cout << error << std::endl; | |
exit(1); // TODO - need better error handling | |
} | |
return shader; | |
} | |
void GtkGui::ImageViewRenderer::init_shaders() { | |
std::cout << "OpenGL version is: " << glGetString(GL_VERSION) << std::endl; | |
std::cout << "Shading language version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl; | |
// "out vec3 color;\n" | |
// "out vec3 color;\n" | |
// "uniform sampler2D image_sampler;\n" | |
//\ | |
//void main() {\ | |
//\ | |
// color = texture( image_sampler, UV ).rgb;\ | |
//}\ | |
//"; | |
GLenum glew_status = glewInit(); | |
if (glew_status != GLEW_OK) { | |
std::cout << "glew failed to initialize" << std::endl; | |
exit(1); | |
} | |
shader_program = glCreateProgram(); | |
GLenum error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
vertex_shader = compile(vertex_shader_source, GL_VERTEX_SHADER); | |
std::cout << "Vertex shader compiled" << std::endl; | |
error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
fragment_shader = compile(fragment_shader_source, GL_FRAGMENT_SHADER); | |
std::cout << "Fragment shader compiled" << std::endl; | |
error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
glAttachShader(shader_program, vertex_shader); | |
error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
glAttachShader(shader_program, fragment_shader); | |
error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
glBindAttribLocation(shader_program, 0, "corners"); | |
error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
glLinkProgram(shader_program); | |
GLint success = 0; | |
glGetProgramiv(shader_program, GL_LINK_STATUS, &success); | |
if (!success) { | |
std::cout << "Program linking failed" << std::endl; | |
GLchar error[2048]; | |
GLsizei size; | |
glGetProgramInfoLog(shader_program, 2048, &size, error); | |
std::cout << error << std::endl; | |
exit(1); // TODO - need better error handling | |
} | |
error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
std::cout << "Shader program linked successfully" << std::endl; | |
} | |
GtkGui::ImageViewRenderer::ImageViewRenderer(boost::shared_ptr<Core::Image> im) : | |
Renderer(), | |
image(im), | |
pixels(image->pixels()), | |
gl_tex_id(0), | |
shader_program(0), | |
fragment_shader(0), | |
vertex_shader(0) { | |
} | |
GtkGui::ImageViewRenderer::~ImageViewRenderer() { | |
if (gl_tex_id) { | |
glDeleteTextures(1, &gl_tex_id); | |
// TODO | |
} | |
} | |
void GtkGui::ImageViewRenderer::realize() { | |
GLenum error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
glEnable(GL_DEPTH_TEST); | |
glDepthFunc(GL_LEQUAL); | |
glEnable(GL_CULL_FACE); | |
glCullFace(GL_BACK); | |
glDisable(GL_DITHER); | |
glShadeModel(GL_SMOOTH); | |
error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
init_shaders(); | |
error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
if (!pixels) { return; }; | |
// glGenTextures(1, &gl_tex_id); | |
// glBindTexture(GL_TEXTURE_2D, gl_tex_id); | |
// | |
// // Assume RGB | |
// glTexImage2D(GL_TEXTURE_2D, | |
// 0, | |
// GL_RGB, | |
// pixels->cols, | |
// pixels->rows, | |
// 0, | |
// GL_BGR, | |
// GL_UNSIGNED_BYTE, // Image data type | |
// pixels->ptr() | |
// ); | |
} | |
void GtkGui::ImageViewRenderer::configure(unsigned int width, unsigned int height) { | |
GLenum error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
glViewport(0, 0, width, height); | |
error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
} | |
void GtkGui::ImageViewRenderer::draw() { | |
GLuint vbo, vao; | |
// Yoink. Not a clue how to bind things. | |
// https://www.opengl.org/wiki/Tutorial2:_VAOs,_VBOs,_Vertex_and_Fragment_Shaders_(C_/_SDL) | |
GLenum error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
const float positions[] = { | |
0.1, 0.1, | |
0.9, 0.1, | |
0.9, 0.9, | |
0.1, 0.9 | |
}; | |
glUseProgram(shader_program); | |
glGenVertexArrays(1, &vao); | |
glBindVertexArray(vao); | |
glGenBuffers(1, &vbo); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), positions, GL_STATIC_DRAW); | |
glEnableVertexAttribArray(0); | |
glVertexAttribPointer(0, 2, GL_FLOAT, GL_TRUE, 0, 0); | |
error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
glOrtho(0.0, 1.0, 1.0, 0.0, -1, 1); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glClearColor(0, 1, 0, 1); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glBindVertexArray(vao); | |
glDrawArrays(GL_POINTS, 0, 4); | |
error = glGetError(); | |
if (error != GL_NO_ERROR) std::cout << "Error on line " << __LINE__ << ": " << error << std::endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __IMAGE_VIEW_RENDERER_H__ | |
#define __IMAGE_VIEW_RENDERER_H__ | |
#include <boost/shared_ptr.hpp> | |
#include "renderer.h" | |
#include "../core/image.h" | |
#include <GL/glew.h> | |
#include <GL/glext.h> | |
#include <GL/gl.h> | |
#include <GL/glx.h> | |
#include <GL/glxext.h> | |
namespace GtkGui { | |
class ImageViewRenderer : public Renderer { | |
boost::shared_ptr<Core::Image> image; | |
boost::shared_ptr<cv::Mat> pixels; | |
// TODO - hide ugly impl members | |
GLuint gl_tex_id; | |
GLuint shader_program; | |
GLuint vertex_shader; | |
GLuint fragment_shader; | |
void init_shaders(); | |
GLuint compile(const std::string& shader, GLenum type); | |
GLuint compile(const char* shader, GLenum type); | |
public: | |
ImageViewRenderer(boost::shared_ptr<Core::Image> im); | |
~ImageViewRenderer(); | |
void draw(); | |
void configure(unsigned int width, unsigned int height); | |
void realize(); | |
}; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment