public
Created

OpenGL Fragment Shader

  • Download Gist
gistfile1.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
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
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.