Skip to content

Instantly share code, notes, and snippets.

@a10y
Created November 10, 2012 18:22
Show Gist options
  • Save a10y/4052017 to your computer and use it in GitHub Desktop.
Save a10y/4052017 to your computer and use it in GitHub Desktop.
OpenGL Fragment Shader
void InitShading(){
//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(){"
" if (mod(gl_FragCoord.y-0.5, 2.0) == 1.0){"
" gl_FragColor = gl_Color;"
" } else {"
" gl_FragColor = vert4(0.0, 0.0, 0.0, 1.0);"
" }"
"}";
std::cout << "Shader source:\n" << source << std::endl;
std::cout << "Gathering shader source code" << std::endl;
glShaderSource(oddRowShaderId, 1, &source, 0);
std::cout << "Compiling the shader" << std::endl;
glCompileShader(oddRowShaderId);
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
std::cout << "Using the program for rendering" << std::endl;
glUseProgram(shaderProgramId); //Start using the shader
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment