PDA

View Full Version : Select vertice with mouse cursor

bremenpl
09-07-2014, 12:36 PM
Hello there,
I am new to openGL. I am trying to create an application for solving rubics cube. So far I have coded the cube drawing with changable nr of squares on a single wall.
I can rotate the cube, my problem is I have no idea how to select a certain vertice on the image. I can read the mouse position from mousePressEvent but those coordinates dont give me anything. I need to select a vertice to highlight it and after click be abble to rotate a certain row to solve the cube. Actually I would only swap colors.
This is a screen from my app:

https://dl.dropboxusercontent.com/u/14154631/cube1.png

This is how I draw the cube, I create 6 big squares for the background and many little ones for each wall. This is my way of getting borders between vertices.

void MyGLWidget::draw()
{
// define piece lengh:
GLfloat pieceLengh = CUBE_LENGH / sqrLengh;
GLfloat innerPieceLengh = pieceLengh - CUBE_LINE_WIDTH;

GLfloat x;
GLfloat y;
GLfloat z;

// --------------------------------------------------------------

// start drawing
for (int wall = 0; wall < CUBE_WALLS; wall++) // whole wall draw
{

qglColor(Qt::darkGray);
rotateVetice(1, wall);

glVertex3f(x, y, z);

y -= (CUBE_LENGH + CUBE_LINE_WIDTH);
glVertex3f(x, y, z);

x += (CUBE_LENGH + CUBE_LINE_WIDTH);
glVertex3f(x, y, z);

y += (CUBE_LENGH + CUBE_LINE_WIDTH);
glVertex3f(x, y, z);

glEnd();
rotateVetice(-1, wall);

for (int row = 0; row < sqrLengh; row++) // single row
{
y = CUBE_RADIUS - (pieceLengh * row) - CUBE_LINE_WIDTH;

for(int column = 0; column < sqrLengh; column++) // single cell
{
x = (CUBE_RADIUS * -1) + (pieceLengh * column) + CUBE_LINE_WIDTH;

qglColor(refSquareMatrix[wall][row][column]);
rotateVetice(1, wall);

glVertex3f(x, y, z);

y -= innerPieceLengh;
glVertex3f(x, y, z);

x += innerPieceLengh;
glVertex3f(x, y, z);

y += innerPieceLengh;
glVertex3f(x, y, z);

glEnd();
rotateVetice(-1, wall);
}
}
}
}

I would really aprichiate any help in this matter. I have only one day lefy to finish the project and im really stuck.