OpenGL3.3 Not getting any colors

Started by
3 comments, last by Wilds 12 years, 2 months ago
I am pulling my hairs out with this one, not sure what I am doing wrong.
Using 2 simple shaders, compile and links correctly.

VertexShader

#version 330 core
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;
out vec4 outColor;

void main()
{
gl_Position = vec4(inPosition.xyz, 1.0);
outColor = vec4(inColor, 1.0);
}


FragmentShader

#version 330
out vec4 outFragColor;
in vec4 inColor;

void main()
{
outFragColor = inColor;
}


ArrayBuffer class header
#ifndef _ARRAYBUFFEROBJECT_H_
#define _ARRAYBUFFEROBJECT_H_
#include <GL/glew.h>
// representing a openGL buffer object
class ArrayBufferObject
{
public:
ArrayBufferObject(): mID(0){}
~ArrayBufferObject() {}
// create the buffer
void Create(float T[], GLenum usage)
{
// generate buffer
glGenBuffers(1, &mID);
glBindBuffer(GL_ARRAY_BUFFER, mID);
// fill with data
int byteSize = sizeof(float) * 18;
glBufferData(GL_ARRAY_BUFFER, byteSize, T, usage);
// unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// dispose of the buffer
void Dispose()
{
// unbind buffer and delete
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &mID);
}
void Bind() { glBindBuffer(GL_ARRAY_BUFFER, mID); }
void Unbind() { glBindBuffer(GL_ARRAY_BUFFER, 0); }
unsigned int GetID() { return mID; }
private:
GLuint mID;
};
#endif


Draw code
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// use program
defaultShader.Bind();
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
PVB.Bind();
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)36);

glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
PVB.Unbind();
defaultShader.Unbind();
// swap the buffers
SwapBuffers(mWndDC);
Advertisement
What is the contents of the vertex buffer?
This is it, I am using a forward compatible core context.

float mPolygon[] =
{
-0.8f, -0.8f, 0.0f,
0.8f, -0.8f, 0.0f,
0.0f, 0.8f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
// create buffer for the triangle
PVB.Create(mPolygon, GL_STATIC_DRAW);
Oh, just noticed. Variables between shaders are linked by their name. Your outColor in the vertex shader is not the same as the inColor in the pixel shader. Thus, you are writing to an unused output variable, and reading from an uninitialized input variable. They must have the same name if you want to link them together.
Thank you so much it worked!! I haven't read a tutorial that said this piece of information!
Glad I asked here.

This topic is closed to new replies.

Advertisement