Greetings!
I’m trying to draw colored 3D lines. I got the lines part working, but not the color. (See image below)
Here’s my shaders:
layout (location = 0) in vec3 VertPos;
layout (location = 1) in vec3 VertColor;
uniform mat4 V;
uniform mat4 P;
out vec4 FragColor;
void main()
{
gl_Position = P * V * vec4(VertPos, 1);
FragColor = vec4(VertColor, 1);
}
#version 330 core
out vec4 FinalColor;
in vec4 Color;
void main()
{
FinalColor = vec4(Color.r, 1, Color.b, 1); // Setting green to 1 for debugging
}
Here’s my line struct:
struct dbg_line
{
v3 Start;
v3 StartColor;
v3 End;
v3 EndColor;
};
Here’s how I’m initializing and drawing:
// Initializing
Data->DebugVBO = GLGenArrayBuffer(sizeof(dbg_line) * MAX_DBG_LINES, 0, GL_DYNAMIC_DRAW);
Data->DebugVAO = GLGenVertexArray();
GLAttribute(0, 3, GL_FLOAT, 2 * sizeof(v3), 0);
GLAttribute(1, 3, GL_FLOAT, 2 * sizeof(v3), 1 * sizeof(v3));
// Drawing (Note: Lines get multiplied by ModelToWorld matrix in CPU, not in the shader but I didn't show it here)
u32 Shader = Data->Shaders.Debug;
ShaderUse(Shader);
u32 LineCount = ARRAY_COUNT(Data->DBGLines);
GLBindVAO(Data->DebugVAO);
GLSetArrayBuffer(Data->DebugVBO, LineCount * sizeof(dbg_line), Data->DBGLines);
{
ShaderM4(Shader, "V", &WorldToView);
ShaderM4(Shader, "P", &ViewToProjection);
GLDrawLines(LineCount);
}
Helper functions I’m using:
internal inline void
GLSetArrayBuffer(u32 ArrayBuffer, u32 Size, void *Data)
{
glBindBuffer(GL_ARRAY_BUFFER, ArrayBuffer);
glBufferSubData(GL_ARRAY_BUFFER, 0, Size, Data);
}
internal u32
GLGenArrayBuffer(u32 SizeInBytes, void *Data, u32 Mode)
{
u32 ArrayBuffer;
glGenBuffers(1, &ArrayBuffer);
glBindBuffer(GL_ARRAY_BUFFER, ArrayBuffer);
glBufferData(GL_ARRAY_BUFFER, SizeInBytes, Data, Mode);
return (ArrayBuffer);
}
internal inline void
GLAttribute(u32 Index, u32 Count, u32 Type = GL_FLOAT, u32 Stride = 0, u32 Offset = 0)
{
glEnableVertexAttribArray(Index);
glVertexAttribPointer(Index, Count, Type, GL_FALSE, Stride, (char *)0 + Offset);
}
internal inline void
GLBindVBO(u32 VBO)
{
glBindBuffer(GL_ARRAY_BUFFER, VBO);
}
internal inline void
GLBindVAO(u32 VAO)
{
glBindVertexArray(VAO);
}
internal inline void
GLDrawLines(u32 NumLines)
{
glDrawArrays(GL_LINES, 0, NumLines * 2);
}
internal inline u32
GLGenVertexArray()
{
u32 VertexArray;
glGenVertexArrays(1, &VertexArray);
glBindVertexArray(VertexArray);
return (VertexArray);
}
I’ve been stuck at this for a while. All I get is the green lines on my bounding boxes even though I’m pretty sure I set the line color to red (when I push a line, I set the Start and End colors to the same value). I probably messed something up with the strides/offsets but I can’t see it…
Image [http://i.imgur.com/xhEdbHY.png]
Any help is appreciated!
Thanks!
-Keithster