I’ve been coding a shaders program thats supposed to display a magenta window, but instead, it is gray.
There’s is no compile, linker or any type of error, everything works just “fine” but even thought i set rgb to it’s correspondent values, i can’t seem to make it work.
Here is the main code:
Sprite.cpp:
...
void Sprite::init(...) {
if (_vboID == 0) {
glGenBuffers(1, &_vboID);
}
Vertex vertexData[6];
vertexData[0].position.x = _x;
... //set the vertex positions
for (int i = 0; i << 6; i++) {
vertexData[i].color.r = 255;
vertexData[i].color.g = 0;
vertexData[i].color.b = 255;
vertexData[i].color.a = 255;
} //set the vertex color
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Sprite::draw() {
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color)); //assign vertex attributes
glDrawArrays(GL_TRIANGLES, 0, 6); //draw the triangles (2, to fill the entire window)
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
GLSL_Program.cpp:
void linkShaders() {
... //link shaders, i'm pretty sure there are no errors here as the code worked perfectly before using shaders
}
void compileShaders() {
... //compile shaders, i'm pretty sure there are no errors here as the code worked perfectly before using shaders
}
void GLSL_Program::use() {
glUseProgram(_programID);
for (unsigned int i = 0; i < _numAttributes; i++) {
glEnableVertexAttribArray(i);
}
}
void GLSL_Program::unuse() {
glUseProgram(0);
for (unsigned int i = 0; i < _numAttributes; i++) {
glDisableVertexAttribArray(i);
}
}
void GLSL_Program::addAttribute(const std::string& attributeName) {
glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str()); //add attribute
}
MainGame.cpp:
...
void MainGame::initShaders() {
_colorProgram.compileShaders("Shaders/colorShading.vert", "Shaders/colorShading.frag");
_colorProgram.addAttribute("vertexPosition");
_colorProgram.addAttribute("vertexColor");
...
}
void MainGame::drawGame() {
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_colorProgram.use();
_sprite.draw();
_colorProgram.unuse();
SDL_GL_SwapWindow(_window); //swap windows
}
Finally, these are my shader files:
colorShading.frag:
#version 130
in vec4 fragmentColor;
out vec4 color;
void main() {
color = fragmentColor;
}
colorShading.vert:
#version 130
in vec2 vertexPosition;
in vec4 vertexColor;
out vec4 fragmentColor;
void main() {
gl_Position.xy = vertexPosition;
gl_Position.z = 0.0;
gl_Position.w = 1.0;
fragmentColor = vertexColor;
}
In case any more code is needed just tell me, i tryied to summarize the important parts the most i could, sorry if it is too long, it’s just that i’m not sure where’s the code error.