Soundbomber's picture

Line Color

How do I set the color of a line drawn using VBO's and Interleaved arrays?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

You have to place the color into the interleaved array as a vertex attribute. ColorPointer is the function, IIRC.

Soundbomber's picture

Ok. I'm using lighting, do the line vertices need normals?
Scuse my ignorance but what does the IIRC abbreviation mean?

the Fiddler's picture

IIRC = if I remember correctly

If you expect the lines to take part in the lighting equations, then yes. Otherwise, you can disable lighting when you draw the lines and use vertex colors instead.

It depends on the effect you want to achieve.

Soundbomber's picture

I am happy enough for the lines to be a constant color and not take part in any lighting equations. How do I implement vertex colors? (Im am rendering using DrawElements)

the Fiddler's picture

Do you need the color to change per-vertex or is it enough to change once per DrawElements call?

If it is the former, you need to augment your vertex structure to hold color information. For example:

<StructLayout(LayoutKind.Sequential, Pack=1)>
Struct VertexPositionColor
    Public Position As Vector3;
    Public Color As Color4;
 
    Public Shared ReadOnly SizeInBytes As Integer = Marshal.SizeOf(GetType(VertexPositionColor))
End Struct

(My VB.Net skills are a little rusty and I don't have a compiler installed, but I hope this makes sense)

You upload your vertices as before, taking care to specify the correct value for the size parameter ("number_of_vertices * VertexPositionColor.SizeInBytes").

Finally, you bind your VBO and call:

' Disable lighting, otherwise vertex colors will be ignored
GL.Disable(EnableCap.Lighting)
 
' The last parameter indicates the offset of the relevant information inside the
' VertexPositionColor structure
GL.VertexPointer(3, VertexPointerType.Float, VertexPositionColor.SizeInBytes, IntPtr.Zero)
GL.ColorPointer(4, VertexPointerType.Float, VertexPositionColor.SizeInBytes, Vector3.SizeInBytes)
 
' Just like before
GL.DrawElements(...)
Soundbomber's picture

I am currently using InterleavedArrayFormat.T2fC4fN3fV3f to hold my vertex colors, I thought that using interleaved arrays automatically takes care of the color/position/normal pointers?

objarni's picture

Soundbomber - given one outline-polygon, how many different colors are involved?

the Fiddler's picture

Edit: as objarni asks, do you really need per-vertex colors?

In that case it's simply a matter of specifying the correct InterleavedArrayFormat for your vertex structure. InterleavedArrayFormat.T2fC4fN3fV3f implies a structure like:

<StructLayout(LayoutKind.Sequential, Pack=1)>
Struct VertexT2C4N3V3
    Public TexCoords As Vector2;
    Public Color As Color4;
    Public Normal As Vector3;
    Public Position As Vector3;
 
    Public Shared ReadOnly SizeInBytes As Integer = Marshal.SizeOf(GetType(VertexT2C4N3V3))
End Struct

Simply specify the colors you want before uploading the data with BufferData and it will work.

Edit 2: Just to clarify, GL.InterleavedArrays is just one way to create an interleaved array. GL.InterleavedArrays is a very limited function that gives you access to a few specific interleaved formats. GL.Vertex/Color/NormalPointer give you more flexibility (e.g. you can choose the order of the attributes or use multiple sets of texture coordinates). GL.VertexAttribPointer gives you ultimate flexibility: the vertex shader is free to interpret each attribute as it sees fit.

Soundbomber's picture

At the moment I am coloring all lines the same so no I don't need per vertex colors. At some point in the future however, I would like to color different face outlines with different colors and maybe hidden detail lines a different color. So I would like the flexibility.

objarni's picture

Soundbomber: my advice to you is begin with the simplest thing (single color per polygon), that is what you need now.

When and if the time comes you want several different colors, change your code then. Just a humble advice.