Last active
May 19, 2016 21:13
-
-
Save a10y/4069710 to your computer and use it in GitHub Desktop.
OpenGL Odd Row Fragment Shader
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 <iostream> | |
#include <stdio.h> | |
//This is nice: http://stackoverflow.com/questions/3907818/opengl-headers-for-os-x-linux | |
//OS X headers | |
#ifdef __APPLE__ | |
#include <OpenGL/gl.h> | |
#include <OpenGL/glu.h> | |
#include <GLUT/glut.h> | |
#else | |
//Windows Headers | |
#ifdef _WIN32 | |
#include <windows.h> | |
#endif | |
//Regular Linux Headers | |
#include <GL/gl.h> | |
#include <GL/glu.h> | |
#include <GL/glut.h> | |
#endif | |
void DrawGLScene(){ | |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | |
// Draw some points | |
glBegin(GL_TRIANGLES); | |
glColor3f(0.1, 0.2, 0.3); | |
glVertex3f(0, 0, 0); | |
glVertex3f(1, 0, 0); | |
glVertex3f(0, 1, 0); | |
glEnd(); | |
glFlush(); | |
glutSwapBuffers(); | |
} | |
void Shading(){ | |
//Fragment shader we want to use | |
GLuint oddRowShaderId = glCreateShader(GL_FRAGMENT_SHADER); | |
std::cout << "Creating the fragment shader with id " << oddRowShaderId << std::endl; | |
const GLchar *source = | |
"void main(){ \n" | |
" if (mod(gl_FragCoord.y-0.5, 2.0) == 0.0){\n" | |
" gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 );\n" | |
" }\n" | |
"}\n\n"; | |
std::cout << "Shader source:\n" << source << std::endl; | |
std::cout << "Gathering shader source code" << std::endl; | |
glShaderSource(oddRowShaderId, 1, &source, NULL); | |
std::cout << "Compiling the shader" << std::endl; | |
glCompileShader(oddRowShaderId); | |
GLint params; | |
glGetShaderiv(oddRowShaderId, GL_COMPILE_STATUS, ¶ms); | |
if(params){ | |
std::cout << "Compiled Successfully" << std::endl; | |
} else { | |
std::cout << "Errors compiling" << std::endl; | |
GLchar infoLog[256]; | |
int infoLogLength = 0; | |
glGetShaderInfoLog(oddRowShaderId, 256, &infoLogLength, infoLog); | |
std::cout << "Info Log: " << std::endl << infoLog << std::endl; | |
} | |
std::cout << "Creating new glCreateProgram() program" << std::endl; | |
GLuint shaderProgramId = glCreateProgram(); //Shader program id | |
std::cout << "Attaching shader to the new program" << std::endl; | |
glAttachShader(shaderProgramId, oddRowShaderId); //Add the fragment shader to the program | |
std::cout << "Linking the program " << std::endl; | |
glLinkProgram(shaderProgramId); //Link the program | |
GLint err; | |
glGetProgramiv(shaderProgramId, GL_LINK_STATUS, &err); | |
if(err){ | |
std::cout << "Linked Successfully" << std::endl; | |
} else { | |
std::cout << "Errors Linking" << std::endl; | |
GLchar errors[256]; | |
int errLength; | |
glGetProgramInfoLog(shaderProgramId, 256, &errLength, errors); | |
std::cout << "Program Error Log: " << std::endl << errors << std::endl; | |
} | |
std::cout << "Using the shader program for rendering" << std::endl; | |
glUseProgram(shaderProgramId); //Start using the shader | |
} | |
void keyboard(int key, int x, int y){ | |
switch(key){ | |
case 's': | |
Shading(); | |
break; | |
case 'q': | |
exit(0); | |
break; | |
} | |
} | |
void idleFunc(){ | |
glutPostRedisplay(); //Redraw the scene. | |
} | |
int main(int argc, char** argv){ | |
glutInit(&argc, argv); | |
glutInitWindowPosition(100, 100); | |
glutInitWindowSize(1000,1000); | |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); | |
glutCreateWindow("anaglyph test"); | |
glutDisplayFunc(DrawGLScene); | |
glutSpecialFunc(keyboard); | |
glutIdleFunc(idleFunc); | |
glutMainLoop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated on 5/19/2016. Fixed a bunch of issues from back when I didn't really know C++.
Can successfully compile on OSX 10.11 with the following command: