VAO data not being received by shader is only my suspicion because i don't know what's the problem.
I'm simply trying to pass position and color vertex attributes to the shader.
Global vars:
GLuint vao[3];
GLuint vertexLoc, colorLoc;
float vertices1[] = { -3.0f, 0.0f, -5.0f, 1.0f,
-1.0f, 0.0f, -5.0f, 1.0f,
-2.0f, 2.0f, -5.0f, 1.0f};
float colors1[] = { 0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f,0.0f, 0.0f, 1.0f};
Init:
void initRendering() {
std::string vertexInputString="textureVS.txt";
std::string fragmentInputString="textureFS.txt";
//create new shader
InitializeProgram(shaderOne, readFiletoString(vertexInputString), readFiletoString(fragmentInputString));
GLuint buffers[2];
glGenVertexArrays(3, vao);
//
// VAO for first triangle
//
glBindVertexArray(vao[0]);
// Generate two slots for the vertex and color buffers
glGenBuffers(1, buffers);
// bind buffer for vertices and copy data into buffer
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW);
glEnableVertexAttribArray(vertexLoc);
glVertexAttribPointer(vertexLoc, 4, GL_FLOAT, 0, 0, 0);
// bind buffer for colors and copy data into buffer
glBindBuffer(GL_ARRAY_BUFFER, buffers[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors1), colors1, GL_STATIC_DRAW);
glEnableVertexAttribArray(colorLoc);
glVertexAttribPointer(colorLoc, 4, GL_FLOAT, 0, 0, 0);
//enables/disables
glEnable(GL_DEPTH_TEST);
glEnable (GL_BLEND);
//load textures
image = SOIL_load_OGL_texture(
"resources/blank.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
if( 0 == image ){printf( "SOIL loading error: '%s'\n", SOIL_last_result());}
//start shader
glUseProgram(shaderOne);
vertexLoc = glGetAttribLocation(shaderOne,"position");
colorLoc = glGetAttribLocation(shaderOne,"color");
}
Draw:
void drawPerspectiveScene(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, windowWidth, windowHeight);
glClearColor(0.5,5.5,0.25,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float)windowWidth / (float)windowHeight, 0.4, 9000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//set camera
gluLookAt(cam.posX,cam.posY,cam.posZ,cam.viewX,cam.viewY,cam.viewZ,cam.upX,cam.upY,cam.upZ);
glColor3f(0.0,0.0,1.0);
glBindVertexArray(vao[0]);
glDrawArrays(GL_TRIANGLES, 0, 3);
//swap buffers
glutPostRedisplay();
glutSwapBuffers();
}
GLSL:
Vert:
attribute vec4 position;
attribute vec4 color;
varying vec4 Color;
void main() {
Color=color;
gl_Position = gl_ModelViewProjectionMatrix * position;
}
Frag:
varying vec4 color;
void main()
{
gl_FragColor = color;
}
I know im using deprecated function, this is my first step towards modern opengl.
It renders without error, however it uses the color data as vertex position data, so the triangle looks distorted.