Sign in to follow this  
Wilds

OpenGL OpenGL3.3 Not getting any colors

Recommended Posts

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
[code]
#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);
}
[/code]

FragmentShader
[code]
#version 330
out vec4 outFragColor;
in vec4 inColor;

void main()
{
outFragColor = inColor;
}
[/code]

ArrayBuffer class header
[code]#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[/code]

Draw code
[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);[/code]

Share this post


Link to post
Share on other sites
This is it, I am using a forward compatible core context.

[code] 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);[/code]

Share this post


Link to post
Share on other sites
Oh, just noticed. Variables between shaders are linked by their name. Your [i]outColor[/i] in the vertex shader is not the same as the [i]inColor[/i] 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this