Dr_Asik's picture

Vertex/Color pointers issue

I'm just learning how to use vertex pointers, and I'm having a weird issue with OpenTK. I'm just trying to display a red-blue-green triangle in a GameWindow, with this code:

       protected override void OnRenderFrame(FrameEventArgs e) {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            var matrix = Matrix4.CreatePerspectiveFieldOfView((float)(Math.PI / 4.0), 1.3333f, 1, 1000);
            GL.LoadMatrix(ref matrix);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
 
            var vertices = new float[] { 0.0f, 1.0f, -10.0f, -1.0f, -1.0f, -10.0f, 1.0f, -1.0f, -10.0f };
            var colors = new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
 
            GL.EnableClientState(ArrayCap.VertexArray);
	    GL.EnableClientState(ArrayCap.ColorArray);
 
            GL.ColorPointer(3, ColorPointerType.Float, 0, colors);
            GL.VertexPointer(3, VertexPointerType.Float, 0, vertices);
	    GL.DrawArrays(BeginMode.Triangles, 0, 3);
 
            GL.DisableClientState(ArrayCap.VertexArray);
            GL.DisableClientState(ArrayCap.ColorArray);
 
            SwapBuffers();
        }

The problem is, the vertex that is supposed to be red appears black instead. I've translated the code in C++ and it displays fine. Here's how it looks like with OpenTK:

Here's the equivalent native OpenGL code and the result:

#include <GL/freeglut.h>
 
float vertices[] = { 0.0f, 1.0f, -10.0f, -1.0f, -1.0f, -10.0f, 1.0f, -1.0f, -10.0f };
float colors[] = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
 
void render() {
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, 1.3333, 1, 1000);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();		
 
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
 
	glColorPointer(3, GL_FLOAT, 0, colors);
	glVertexPointer(3, GL_FLOAT, 0, vertices);
	glDrawArrays(GL_TRIANGLES, 0, 3);
 
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
 
	glutSwapBuffers();
 
}
 
int main(int argc,char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
	glutInitWindowSize(800, 600);
	glutCreateWindow("test");
	glutDisplayFunc(render);
	glutMainLoop();
	return EXIT_SUCCESS;
}

I am using the OpenTK Branch 1.0 revision 2713 (latest at the time of writing this).


Comments

Comment viewing options

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

Ok so I've nailed it down to calling InitTexturing() on the class TexUtil from TexLib. This is what the function does:

    public static void InitTexturing()
    {
      GL.Disable(EnableCap.CullFace);
      GL.Enable(EnableCap.Texture2D);
      GL.Enable(EnableCap.Blend);
      GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
      GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
    }

So if I disable Texture2D, rendering looks fine again. Guess I just solved my own problem. :P