Balili's picture

Drawarrays vertices colors problem

Hi All,

I just started using the OpenTK library in C#.
And I have some problem with the DrawArrays function, especially with the colors of the vertices.
I'm trying to show simple colored linestrips with the DrawArrays function which seems working all good, except that some colors not showing up correctly.
Some of the colors showing up in white in my below code instead of the defined color.
In the below code snipped I fixed the colors to Color.YellowGreen to all vertices which then all appears in white.
The same code works OK if the color is forexample Color.Red, or Color.Blue etc.
I'm fighting with this problem about 2 days now, so I thought to ask for some help here.
Any advices appritiated!

The code:

for (int i = 0; i < mk.colortablepath.Length; i++)
{
      //Color col = Color.FromArgb(mk.colortablepath[i]);
      Color col = Color.YellowGreen;
      colort[i] = new Vector3((float)col.R, (float)col.B, (float)col.G);
}
 
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.ColorArray);
 
GL.ColorPointer(3, ColorPointerType.Float, 0, colort);
GL.VertexPointer(3, VertexPointerType.Float, 0, vertexbuffer);
 
GL.DrawArrays(BeginMode.LineStrip, 0, (vertexbuffer.Length/3));
GL.Finish();

Comments

Comment viewing options

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

Color.R is a byte in the range [0, 255]. ColorPointerType.Float requires a float between [0.0, 1.0].

Instead of casting byte -> float, try dividing by 255.0f

Balili's picture

Hi Fiddler,

Thank you for the idea, but unfortunately this not works. As I was struggling for the 3rd day now I already tried the idea you mentioned, I did it like this:

Color col = Color.YellowGreen;
colort[i] = new Vector3(col.R/255, col.G/255, col.B/255);

What changes is that this gives a black color instead of white which is different (as it was white if not divided, but casted to float), but still not what the color should be and what I want.

Please note that for Color.Red, Color.Blue, Color.Green and for many other colors this code works good, but not with some colors like the Color.YellowGreen and Color.DarkPink etc.

Also please note that these mentioned special colors (YellowGreen etc.) shows up OK if using them simply between GL.Begin and GL.End etc. but they are not working when adding them with the DrawArray function.
Unfortunately I have to work with the Drawarray because it is fast enough for my application and it works really great, except these not working colors. :(

Balili's picture

Hey Fiddler,

I tried to divide with 255f now and it solved the issue. :)
Previously I divided just with 255 which did not work, but I tried to divide not with the '255f' which really solved the issue.

Thanks a lot for your help and I really love your OpenTK library.

flopoloco's picture

Yep indeed, you need to pass the division as a float.
e.g. (float)(0.5/255) or shorthand what you did.

Balili's picture

Hi,

Thank you for the suggestion about casting the number to float and passing it this way to the function and sure, I did it this way.
However it also works if dividing the integer with the 255f and without having the (float) cast marker before the value.
I think it works too because the C# compiler already casting the number to floating point when it sees that the division is with a floating number... but this is just a guess from me.

Anyway, my code is working fine now, colors are nice and shinny thanks to the help of you guys!!!

Balili's picture

Flopoloco,

I just realised what you talking about. :)
I missed that you removed the 'f' after the number and casting the result of the division, and yes this solution seems also working good.

Thanks again for your help guys!!!