Problem with glColorPointer

Hello,

I have this following code


#define GLEW_STATIC
#include <stdio.h>
#include <gl/glew.h>
#include <glfw.h>


#define BUFFER_OFFSET(offset)((char*)NULL+offset)

void init(void)
{
	int glewinitialize;
	float aspect_ratio;
	glClearColor(0.0,0.0,0.0,0.0);
	glShadeModel(GL_FLAT);
    glewinitialize=glewInit();
	if(glewinitialize==GLEW_OK)
	{
		printf("GLEW is available
");
	}
	//glViewport(0,0,640,480);
}

void GLFWCALL reshape(int width,int height)
{
   glViewport(0,0,width,height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluOrtho2D(0.0,(GLdouble)width,0.0,(GLdouble)height);
}
void display(void)
{
	GLuint buffers[3];
	
	GLfloat vertices[13][2]={{200,350},{230,300},{200,250},
	{170,300},{230,200},{200,150},{170,200},{250,280},{300,250},{250,220},{150,280},{100,250},{150,220}};
	GLfloat colors[4][3]={{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}};
	GLuint indices[4][4]={{0,1,2,3},{2,4,5,6},{2,7,8,9},{2,10,11,12}};

	
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,0.0,0.0);
	glGenBuffers(3,buffers);
	glBindBuffer(GL_ARRAY_BUFFER,buffers[0]);
	glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(2,GL_FLOAT,0,BUFFER_OFFSET(0));


	
	glBindBuffer(GL_ARRAY_BUFFER,buffers[1]);
	glBufferData(GL_ARRAY_BUFFER,sizeof(colors),colors,GL_STATIC_DRAW);
	
    glColorPointer(3,GL_FLOAT,0,BUFFER_OFFSET(0));
		glEnableClientState(GL_COLOR_ARRAY);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,buffers[2]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
	
	glDrawElements(GL_QUADS,16,GL_UNSIGNED_INT,BUFFER_OFFSET(0));
	//glDrawArrays(GL_QUADS,0,16);
	
    glfwSwapBuffers();
}
int main(int argc,char** argv)
{
 
	int wndstate,wndopenstate;
	
	wndstate=glfwInit();
	
	if(wndstate==1)
	{
		wndopenstate=glfwOpenWindow(640,480,0,0,0,0,4,0,GLFW_WINDOW);
		
		if(wndopenstate==0)
		{
			glfwTerminate();
		}
		else
		{
			glfwSetWindowTitle("Hello Opengl");
			glfwSetWindowPos(30,30);
			init();
			glfwSetWindowSizeCallback(reshape);
			
			while(1)
			{
				display();
				 if(glfwGetKey(GLFW_KEY_ESC))
	             {
		             glfwTerminate();
		
	             }
                
			}
		}
	}
	glfwTerminate();
	return 0;
 
  }

I have attached two images of the expected output and the output which i got .

I want the 4 quads in the expectedoutput.png to have different colors but the output which i got is only one quad with blue color(outputwithcolorpointer.png).

where is the mistake?
-swetha

Hello,

I solved the problem , the color matrix did not hold values for all the vertices.

That raises another question:

Even though color is specified for each of the vertices,the polygon takes only the color of the last specified vertex.

To make things clear:

1.0,0.0,0.0-color for vertex 0
1.0,1.0,0.0-color for vertex 1
0.0,1.0,0.0-color for vertex 2
0.0,0.0,1.0-color for vertex 3
1.0,0.0,1.0-color for vertex 4
0.0,1.0,1.0-color for vertex 5
1.0,1.0,1.0-color for vertex 6
0.0,0.0,0.0-color for vertex 7
0.5,1.0,0.0-color for vertex 8
0.5,0.0,1.0-color for vertex 9
0.0,0.5,1.0-color for vertex 10
0.5,0.5,0.5-color for vertex 11
0.5,0.5,0.0-color for vertex 12

For the 1st quad sharing the vertex 0,1,2,3 the color is 0.0,0.0,1.0

for the 2nd quad sharing the vertex 2,4,5,6 the color is 1.0,1.0,1.0

for the 3rd quad sharing the vertex 2,7,8,9 the color is
0.5,0.0,1.0

for the 4th quad sharing the vertex 2,10,11,12 the color is
0.5,0.5,0.0

Why cant we hold only 4 values in the color matrix instead of 13 values for each vertex?
-swetha

That’s where you disable color interpolation, leave it at GL_SMOOTH and you should get color gradients.

Hi,

Thankyou .got it.
-swetha