Skip to content

Instantly share code, notes, and snippets.

@feresr
Created Aug 31, 2020
Embed
What would you like to do?
Simple openGL GLSL vertex+fragment shader
void checkCompileErrors(unsigned int object, std::string type) {
int success;
char infoLog[1024];
if (type != "PROGRAM") {
glGetShaderiv(object, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(object, 1024, NULL, infoLog);
std::cout << "| ERROR::SHADER: Compile-time error: Type: " << type << "\n"
<< infoLog << "\n -- --------------------------------------------------- -- "
<< std::endl;
}
} else {
glGetProgramiv(object, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(object, 1024, NULL, infoLog);
std::cout << "| ERROR::Shader: Link-time error: Type: " << type << "\n"
<< infoLog << "\n -- --------------------------------------------------- -- "
<< std::endl;
}
}
}
//vertex shader
const char* vertexSource = "#version 330 core\n"
"layout (location = 0) in vec3 vertex;\n"
"void main() {\n"
" gl_Position = vec4(vertex, 1.0);\n"
"}\n";
unsigned int sVertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(sVertex, 1, &vertexSource, NULL);
glCompileShader(sVertex);
checkCompileErrors(sVertex, "VERTEX");
// fragment Shader
const char* fragmentSource = "#version 330 core\n"
"out vec4 colorOut;\n"
"void main() {\n"
" colorOut = vec4(1.0, 1.0, 0.0, 1.0);\n"
"}\n";
unsigned int sFragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(sFragment, 1, &fragmentSource, NULL);
glCompileShader(sFragment);
checkCompileErrors(sFragment, "FRAGMENT");
unsigned int program = glCreateProgram();
glAttachShader(program, sVertex);
glAttachShader(program, sFragment);
glLinkProgram(program);
glDeleteShader(sVertex);
glDeleteShader(sFragment);
glUseProgram(program);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment