OpenGL glColor3iv function does not work !

Hi.
This is my program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <GL/glut.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;

typedef struct _POINT
{
    float x, y;
}POINT;

float MinX = -1.0, MaxX = 1.0, MinY = -1.0, MaxY = 1.0;
float scale = 0.03;
int FPS = 30;
int msec = 1000.0 / FPS;
POINT ObjectPoint = {0, 0};
int ptColor[3] = {0, 0, 0};

void init()
{
	// White Background
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(MinX, MaxX, MinY, MaxY);
	glPointSize(10);
}

void DisplayFunction()
{
	glClear(GL_COLOR_BUFFER_BIT);

	glBegin(GL_POINTS);
        glColor3iv(ptColor);
        printf("F1 pressed - [DisplayFunction] {%d, %d, %d}\n", ptColor[0], ptColor[1], ptColor[2]);
        glVertex2f(ObjectPoint.x, ObjectPoint.y);
	glEnd();

	glutSwapBuffers();
}

void KeyboardFunction(unsigned char key, int x, int y)
{
	switch(key)
	{
		case 'q':
		case 'Q':
		case 27: //Esc
			exit(0);
			break;
		default:
			break;
	}
	glutPostRedisplay();
}

void SpecialFunction(int key, int x, int y)
{
    switch(key)
    {
        case GLUT_KEY_UP:
            ObjectPoint.y += scale;
            break;
        case GLUT_KEY_DOWN:
            ObjectPoint.y -= scale;
            break;
        case GLUT_KEY_RIGHT:
            ObjectPoint.x += scale;
            break;
        case GLUT_KEY_LEFT:
            ObjectPoint.x -= scale;
            break;
        case GLUT_KEY_F1:
            ptColor[0] = rand() % 256;
            ptColor[1] = rand() % 256;
            ptColor[2] = rand() % 256;
            printf("F1 pressed - [SpecialFunction] {%d, %d, %d}\n", ptColor[0], ptColor[1], ptColor[2]);
            break;
    }
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    srand((unsigned)time(0));
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowPosition(50, 50);
    glutInitWindowSize(500, 500);
    glutCreateWindow("OpenGL Example");

    init();
    glutDisplayFunc(DisplayFunction);
    glutKeyboardFunc(KeyboardFunction);
    glutSpecialFunc(SpecialFunction);
    glutMainLoop();
}

glColor3iv function does not work and every time I press F1, point remains black while ptColor array is changed in SpecialFunction.
What's the problem?

printfs do Watch's work !!! :D

Thanks ;)
That's because white is mapped to the max value (INT_MAX in this case)
Use glColor3bv() instead (making unsigned char ptColor[3];)

In case of floating types, the range is [0, 1]
Last edited on
Do you mean RGB 0 to 255 maps to 0 to INT_MAX ?
Kind of. You are using more bytes to represent the number, you've got more range.
(255 because that's the max with 8 bits)
Thank you. that worked, but with signed char, because GLByte is typedef of signed char.
Thanks anyway ;)
closed account (o1vk4iN6)
The fixed pipeline has been removed since opengl 3.x, and also was removed from opengl es 2.0 . In your example it is a lot easier to use the fixed pipeline but just giving you a heads up.
@xerzi
Could you please explain what you said? I didn't understand.
Tnx
closed account (o1vk4iN6)
http://www.opengl.org/wiki/Fixed_Function_Pipeline

In other words, this:

1
2
3
4
glBegin(GL_POINTS);
        glColor3iv(ptColor);
        glVertex2f(ObjectPoint.x, ObjectPoint.y);
glEnd();
Last edited on
@xerzi:
Thanks. I didn't know that.
Topic archived. No new replies allowed.