-
-
Save tindzk/edc5b1167e894c8ec50f to your computer and use it in GitHub Desktop.
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
void initialiseGL() { | |
const char *fragment = " \ | |
varying vec2 v_texcoord; \ | |
uniform sampler2D s_texture_y; \ | |
uniform sampler2D s_texture_u; \ | |
uniform sampler2D s_texture_v; \ | |
void main() { \ | |
float y = texture2D(s_texture_y, v_texcoord).r; \ | |
float u = texture2D(s_texture_u, v_texcoord).r - 0.5; \ | |
float v = texture2D(s_texture_v, v_texcoord).r - 0.5; \ | |
\ | |
\ | |
float r = y + 1.402 * v; \ | |
float g = y - 0.344 * u - 0.714 * v; \ | |
float b = y + 1.772 * u; \ | |
\ | |
gl_FragColor = vec4(r,g,b,1.0); \ | |
} \ | |
"; | |
const char *vertex = " \ | |
attribute vec4 position; \ | |
attribute vec2 texcoord; \ | |
uniform mat4 modelViewProjectionMatrix; \ | |
varying vec2 v_texcoord; \ | |
\ | |
void main() { \ | |
gl_Position = modelViewProjectionMatrix * position; \ | |
v_texcoord = texcoord.xy; \ | |
} \ | |
"; | |
GLint status, length; | |
// Vertex shader | |
shader.vertex = glCreateShader(GL_VERTEX_SHADER); | |
length = strlen(vertex); | |
glShaderSource(shader.vertex, 1, (const GLchar **)&vertex, &length); | |
glCompileShader(shader.vertex); | |
glGetShaderiv(shader.vertex, GL_COMPILE_STATUS, &status); | |
if (!status) { | |
GLsizei length; char message[5000]; | |
glGetShaderInfoLog(shader.vertex, 5000, &length, message); | |
printf("%s\n", message); | |
assert(false); | |
} | |
// Fragment shader | |
shader.fragment = glCreateShader(GL_FRAGMENT_SHADER); | |
length = strlen(fragment); | |
glShaderSource(shader.fragment, 1, (const GLchar **)&fragment, &length); | |
glCompileShader(shader.fragment); | |
glGetShaderiv(shader.fragment, GL_COMPILE_STATUS, &status); | |
if (!status) { | |
GLsizei length; char message[5000]; | |
glGetShaderInfoLog(shader.fragment, 5000, &length, message); | |
printf("%s\n", message); | |
assert(false); | |
} | |
// Create program, attach both shaders and link | |
shader.program = glCreateProgram(); | |
glAttachShader(shader.program, shader.vertex); | |
glAttachShader(shader.program, shader.fragment); | |
glBindAttribLocation(shader.program, ATTRIBUTE_VERTEX, "position"); | |
glBindAttribLocation(shader.program, ATTRIBUTE_TEXCOORD, "texcoord"); | |
glLinkProgram(shader.program); | |
glGetProgramiv(shader.program, GL_LINK_STATUS, &status); | |
if (!status) { | |
GLsizei length; char message[5000]; | |
glGetProgramInfoLog(shader.program, 5000, &length, message); | |
printf("%s\n", message); | |
assert(false); | |
} | |
// Validate | |
glValidateProgram(shader.program); | |
glGetProgramiv(shader.program, GL_VALIDATE_STATUS, &status); | |
if (!status) { | |
GLsizei length; char message[5000]; | |
glGetProgramInfoLog(shader.program, 5000, &length, message); | |
printf("%s\n", message); | |
assert(false); | |
} | |
// Get the locations of the shader variables | |
shader.matrix = glGetUniformLocation(shader.program, "modelViewProjectionMatrix"); | |
shader.textures[0] = glGetUniformLocation(shader.program, "s_texture_y"); | |
shader.textures[1] = glGetUniformLocation(shader.program, "s_texture_u"); | |
shader.textures[2] = glGetUniformLocation(shader.program, "s_texture_v"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment