so i have this code and i think theres a problem the mouse method? because i the colors dont change when i click the object:( please can someone help me i really dont know what the problem is
this is the code:
#include <windows.h>
#include <glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
GLint windW = 300, windH = 300;
GLuint selectBuf[4];
GLfloat feedBuf[4];
GLint vp[4];
GLint objectCount;
GLint numObjects;
struct object {
float v[360][2];
float color[3];
} objects[4];
static void Init(void)
{
GLint i;
GLint j;
GLint k;
float x, y;
objectCount = 4;
srand((unsigned int)43);//0 to 50
objects[0].v[0][0] = 10;
objects[0].v[0][1] = 10;
objects[0].v[1][0] = 0;
objects[0].v[1][1] = 30;
objects[0].v[2][0] = -10;
objects[0].v[2][1] = 10;
objects[1].v[0][0] = 20;
objects[1].v[0][1] = 50;
objects[1].v[1][0] = 40;
objects[1].v[1][1] = 50;
objects[1].v[2][0] = 40;
objects[1].v[2][1] = 30;
objects[1].v[3][0] = 20;
objects[1].v[3][1] = 30;
for (int i = 0; i < 360; i++) {
float Angle = i * (2.0 * 3.1415 / 360);
float x = cos(Angle) * 12.0;
float y = sin(Angle) * 12.0;
objects[2].v[i][0] = x - 30;
objects[2].v[i][1] = y + 30;
}
objects[3].v[0][0] = -30;
objects[3].v[0][1] = 70;
objects[3].v[1][0] = 10;
objects[3].v[1][1] = 70;
objects[3].v[2][0] = 10;
objects[3].v[2][1] = 50;
objects[3].v[3][0] = -30;
objects[3].v[3][1] = 50;
for (i = 0; i < 4; i++) {
objects[i].color[0] = 0;
objects[i].color[1] = 0;
objects[i].color[2] = 0.8;
}
}
static void Reshape(int width, int height)
{
windW = width;
windH = height;
glViewport(0, 0, windW, windH);
glGetIntegerv(GL_VIEWPORT, vp);
}
static void Render(GLenum mode)
{
GLint i;
GLint j;
for (i = 0; i < 3; i++) {
if (mode == GL_SELECT) {
glLoadName(0);
}
glColor3fv(objects[0].color);
glBegin(GL_TRIANGLES);
glVertex2d(objects[0].v[i][0], objects[0].v[i][1]);
glEnd();
}
for (i = 1; i < objectCount; i++) {
if (i == 2)
continue;
if (mode == GL_SELECT) {
glLoadName(i);
}
glColor3fv(objects[i].color);
glBegin(GL_POLYGON);
for (j = 0; j < 4; j++)
glVertex2d(objects[i].v[j][0], objects[i].v[j][1]);
glEnd();
}
for (i = 0; i < 360; i++) {
if (mode == GL_SELECT) {
glLoadName(2);
}
glColor3fv(objects[2].color);
glBegin(GL_POLYGON);
glVertex2d(objects[2].v[i][0], objects[2].v[i][1]);
glEnd();
}
}
static GLint DoSelect(GLint x, GLint y)
{
GLint hits;
glSelectBuffer(7, selectBuf);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(~0);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPickMatrix(x, windH - y, 4, 4, vp);
gluOrtho2D(-100, 100, 0, 100);
glMatrixMode(GL_MODELVIEW);
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glRotatef(90, 0, 0, 1);
Render(GL_SELECT);
glPopMatrix();
hits = glRenderMode(GL_RENDER);
if (hits <= 0) {
return -1;
}
return selectBuf[(hits - 1)
* 4 + 3];
}
static void RecolorShape()
{
objects[0].color[0] = 1;
objects[0].color[1] = 1;
objects[0].color[2] = 0;
objects[1].color[0] = 0.5;
objects[1].color[1] = 1;
objects[1].color[2] = 0.5;
objects[2].color[0] = 1;
objects[2].color[1] = 0;
objects[2].color[2] = 0;
objects[3].color[0] = 0;
objects[3].color[1] = 1;
objects[3].color[2] = 1;
}
static void Mouse(int button, int state, int mouseX, int mouseY)
{
GLint hit;
if (state == GLUT_DOWN) {
hit = DoSelect((GLint)mouseX, (GLint)mouseY);
if (hit != -1) {
if (button == GLUT_LEFT_BUTTON) {
RecolorShape();
}
glutPostRedisplay();
}
}
}
static void Draw(void)
{
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-175, 175, -175, 175);
glMatrixMode(GL_MODELVIEW);
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glRotatef(90, 0, 0, 1);
Render(GL_RENDER);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500, 250);
glutCreateWindow("Project 2: Picking by color coding");
Init();
glutReshapeFunc(Reshape);
glutMouseFunc(Mouse);
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}