views:

383

answers:

2

Hello,

We are new to OpenGL and GLUT programming and our teacher gave us a homework. The task is, there are 3 rectangles and 1 triangle. Each object have a different color (for easy selection) With these objects, we are suppose to do a home building. I drew the objects but I have big problems with moving. The first method and the easiest one is, selecting the objects with color. In this method, how can I get the color of the object under the mouse pointer?

The second method is general selecting and moving. I have no idea how to do this with GLUT.

I hope you friends understand the situation. Thanks for you answers.

A: 

There's a list on OpenGL.org about how to select objects in OpenGL

Styggentorsken
A: 

You can use the function glReadPixels to read out the color at the mouse pointer:

unsigned char rgba[4];
glReadPixels(mouseX, mouseY, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgba);

There's also a special render mode in OpenGL to do selection: GL_SELECT. Here's the specification.

For the movement part it's hard to give specific code since we don't know what your data structures look like but generally you want to store the relative position of the shape to the mouse pointer and then use this to update the shape position something like this:

//In glutMouseFunc.
//The user has pressed a mouse button and we know which object has been selected
//IMPORTANT: This is done _once_ when the object is selected
relativeX = shape.x - mouseX;
relativeY = shape.y - mouseY;

//In glutMotionFunc
//shape is the selected shape
shape.x = mouseX + relativeX;
shape.y = mouseY + relativeY;
Andreas Brinck