PDA

View Full Version : glColorPointer drawing only in black color



igrekun
08-28-2011, 06:43 AM
#include <GL\glew.h>
#include <GL\freeglut.h>


#define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + (bytes))

GLuint ChunkVBO, ColorVBO;
GLint data[] = {
0.0, 0.0, -1.0,
1.0, 0.0, -1.0,
1.0, 1.0, 0.0,
0.0, 1.0, 0.0,
};
GLint color[] = {
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0
};
float z = 0.0;
void init(void)
{
glewInit();
glClearColor(1.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}

void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
z += 1.0;
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
z -= 1.0;
break;
default:
break;
}
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity(); /* clear the matrix */
/* viewing transformation */
gluLookAt(z, 0.0, 5.0, z, 0.0, 0.0, 0.0, 1.0, 0.0);
glVertexPointer(3, GL_INT, 0, data);
glColorPointer(3, GL_INT, 0, color);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glPopMatrix();
glutSwapBuffers();
}

int main (int argc, char** argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}


Hi.
Have been struggling with VBO's trying to display polygons in color. The problem is that it doesn't work even without VBO.
The polygon is black.
Does anyone know what to do, or have some complete sample code to test whether color pointer at all works on my machine?
TIA

mhagain
08-28-2011, 07:19 AM
glColorPointer works but I don't think GL_INT is what you want to use for it's type here. From the looks of it, you should be using GL_FLOAT for both your color and your vertex pointer (and change those declarations for data and color from GLint to GLfloat too).

igrekun
08-28-2011, 07:37 AM
Thanks!
GL_FLOAT works. I just expected that I could cut down memory usage.
So, only GL_FLOAT is correct?

_arts_
08-28-2011, 08:07 AM
GL_FLOAT and GL_INT generally have the same size (4 bytes).

If you want to keep integers, then 0 is full black, 1 is still very black (but a bit less)... and "max_val(int)" will be full color in red, blue, green or alpha.

mhagain
08-28-2011, 12:44 PM
Don't optimize before you know that you have a problem!

Valuable lesson here: pre-emptively trying to save memory can cause more trouble than it solves. ;)

If you really wanted to, you could use GL_UNSIGNED_BYTE as your colour format (but make it 4 component - some drivers choke badly if you use 3) and use a scale in each channel of 0 (full dark) to 255 (full bright). For your test program that would save a total of 32 bytes of memory (see how silly that sounds - the time to save memory is when you know that you need to, not before).