I came up with code to render a rectangle, but the shaders won't work. It still has the blank white color.
Here I will include the important code
Main:
float verts[] = {
-.5f, -.5f, .0f,
-.5f, .5f, .0f,
.5f, .5f, .0f,
.5f, .5f, .0f,
.5f, -.5f, .0f,
-.5f, -.5f, .0f
};
Shader shader("basicVert.glsl", "basicFrag.glsl");
GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), &verts, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
shader.enable();
Shader.cpp (class functions)
Shader::Shader(const string vpath, const string fpath) {
Shader();
current_vpath = vpath;
current_fpath = fpath;
shaderID = init();
}
Shader::Shader(const char *vpath, const char *fpath) {
Shader(string(vpath), string(fpath));
}
Shader::~Shader() {
shaderID = NULL;
glDeleteProgram(shaderID);
}
void Shader::enable() {
glUseProgram(shaderID);
}
GLuint Shader::makeVertextShader(const char* source) {
GLuint vertShaderID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShaderID, 1, &source, NULL);
glCompileShader(vertShaderID);
GLint r;
glGetShaderiv(vertShaderID, GL_COMPILE_STATUS, &r);
if (r == GL_FALSE) {
GLint l;
glGetShaderiv(vertShaderID, GL_INFO_LOG_LENGTH, &l);
cout << l << endl;
char *bfer = new char[l];
glGetShaderInfoLog(vertShaderID, l, &l, bfer);
cerr << "Failed to compile VERTEXT SHADER! FILE NAME: " <<
current_vpath << endl;
cerr << bfer << endl;
glDeleteShader(vertShaderID);
delete[] bfer;
return NULL;
}
return vertShaderID;
}
GLuint Shader::makeFragmentShader(const char* source) {
GLuint fragShaderID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShaderID, 1, &source, NULL);
glCompileShader(fragShaderID);
GLint r;
glGetShaderiv(fragShaderID, GL_COMPILE_STATUS, &r);
if (r == GL_FALSE) {
GLint l;
glGetShaderiv(fragShaderID, GL_INFO_LOG_LENGTH, &l);
char *bfer = new char[l];
glGetShaderInfoLog(fragShaderID, l, &l, bfer);
cerr << "Failed to compile FRAGMENT SHADER! FILE NAME: " <<
current_fpath << endl;
cerr << bfer << endl;
glDeleteShader(fragShaderID);
delete[] bfer;
return NULL;
}
return fragShaderID;
}
GLuint Shader::init() {
GLuint program = glCreateProgram();
const string vs = readFile(current_vpath);
const string vf = readFile(current_fpath);
const char *vertexsrc = vs.c_str();
const char *fragmentsrc = vf.c_str();
GLuint vertShaderID = this->makeVertextShader(vertexsrc);
GLuint fragShaderID = this->makeFragmentShader(fragmentsrc);
glAttachShader(program, vertShaderID);
glAttachShader(program, fragShaderID);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vertShaderID);
glDeleteShader(fragShaderID);
return program;
}
GLSL Vertex Shader
#version 330 core
layout(location = 0) in vec3 position;
void main(){
gl_Position = position;
}
GLSL Fragment Shader
#version 330 core
layout(location = 0) out vec4 color;
void main(){
color = vec4(1.0, 0.0, 1.0, 1.0);
gl_FragColor = color;
}
gl_FragColor
is no longer supported in modern versions of GLSL
so it will be in Vertex Shader,
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}
in FS,
layout(location = 0) out vec4 color;
void main()
{
color = vec4(1.0, 0.0, 1.0, 1.0);
//gl_FragColor is no longer supported in modern versions of GLSL
}
The vertex and fragment shader have to look like this:
#version 330 core
layout(location = 0) in vec3 position;
void main()
{
gl_Position = vec4( position.xyz, 1.0 );
}
#version 330 core
layout(location = 0) out vec4 color;
void main()
{
color = vec4(1.0, 0.0, 1.0, 1.0);
}
Explanation:
There are 2 issues in your code:
1.) While the vertex attribute position
, in the Vertex Shader has the type vec3
, the Built-in Variable (GLSL) gl_Position
has the type vec4
.
Either the type of the vertex attribute has to be changed:
layout(location = 0) in vec4 position;
or the assignment to gl_Position
has to be adapted:
gl_Position = vec4( position.xyz, 1.0 );
2.) In the Fragment Shader either can be used the Built-in output Variable (GLSL) gl_FragColor
:
void main()
{
gl_FragColor = [...];
}
or an explicit output variable has to be declared:
out vec4 color;
void main()
{
color = [...];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With