Skip to main content
gamedev.net
🔒 Locked

picking & selection.....Question.2?

Started by Sheree Mar 29, 2007 at 1:47 PM 3 replies 950+ views
Original Post
Sheree
Sheree
Hello every body , thanks for DG :yes DG this is exactly what I made but I didn't use gluUnproject to get the 3D world position ....this exactly what I meant in my last question ... because I learnt that it is very necessary to use gluUnproject to determine exactly the 3d position otherwise it doesn't work . my question2 is :Can I to picke one face from icosahedron's faces and change the whole color/alpha of that face without using gluUnproject if yes or no please explaine!????????? please ..anybody know the answer of my question let me know whay?! S H E R E E
Disgruntled Gamer
Disgruntled Gamer
How are you drawing your icosahedron? Is it hardcoded into the program or is it loaded into some kind of mesh structure or class? If it's hardcoded into your program then you are going to have trouble making changes to it without recompiling the code. But if you have it loaded into a mesh you can just add a few fields to the face information like a variable for the the glLoadName call so when you do make a selection you can go through the faces until you find the right name. You can also store the color and alpha information for each face as well and when someone selects that face you can make the changes you need to those variables and the next time it gets drawn it will have new color or alpha values. I hope this helps, if not you may want to post some code that shows how you actually draw the icosahedron.

DG
Sheree
Sheree
thanks DG ,I'll send to where is my problem
Sheree
Sheree
HI again ,
the icosahedron is from this part of the program

static GLfloat vdata[12][3] = {
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
{0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
{Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
};
static GLuint tindices[20][3] = {
{1,4,0}, {4,9,0}, {4,9,5}, {8,5,4}, {1,8,4},
{1,10,8}, {10,3,8}, {8,3,5}, {3,2,5}, {3,7,2},
{3,10,7}, {10,6,7}, {6,11,7}, {6,0,11}, {6,1,0},
{10,1,6}, {11,0,9}, {2,11,9}, {5,2,9}, {11,2,7}
};
GLfloat d1[3], d2[3], n[3];

void normalize(float v[3]) {
GLfloat d = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
v[0] /= d; v[1] /= d; v[2] /= d;
};
void normCrossProd(float u[3], float v[3], float out[3]) {
out[0]=u[1]* v[2]-u[2]* v[1];
out[1] = u[2]* v[0]-u[0]* v[2];
out[2] = u[0]* v[1]-u[1]* v[0];
normalize(out);
}
void icoNormVec (int i) {
for (int k = 0; k < 3; k++) {
d1[k] = vdata[tindices[0]][k]-vdata[tindices[1]][k];
d2[k] = vdata[tindices[1]][k]-vdata[tindices[2]][k];
}
normCrossProd(d1, d2, n);
glNormal3fv(n);
}
so then I drew it in this part :
static void
Render(GLenum mode)
{ GLint i;
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
for (i = 0; i < 20; i++) {
if (mode == GL_SELECT) {
glLoadName(i);
}
glColor4dv(objects.color);
glBegin(GL_POLYGON);
icoNormVec(i);
glVertex3fv(&vdata[tindices[0]] [0]);
glVertex3fv(&vdata[tindices[1]] [0]);
glVertex3fv(&vdata[tindices[2]] [0]);
glEnd();
}
if (mode == GL_SELECT) {
glPopName();}
}
this "objects.color" what I need to manipulate it by selection .
And I made the selection by this way :
static GLint
DoSelect(GLint x, GLint y)
{ GLint hits,i;
GLfloat winX,winY,winZ;

glSelectBuffer(MAXSELECT, selectBuf);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(~0);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPickMatrix(x, windH - y, 4, 4, vp);
GLdouble modelMatrix[16];
glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
GLdouble projMatrix[16];
glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
int viewport[4];
glGetIntegerv(GL_VIEWPORT,viewport);
winX=(float)x;
winY=(float) viewport[3] - (float)y;

glReadPixels(winX,int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelMatrix,
projMatrix,viewport,&objects.color1[0],
&objects.color1[1],&objects.color1[2]);

glOrtho(3.0, 3.0, 3.0, 3.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glScalef(zoom, zoom, zoom);
glRotatef(rotatex, 1, 0, 0);
Render(GL_SELECT);
glPopMatrix();
hits = glRenderMode(GL_RENDER);
if (hits <= 0) {
return -1;
}
return selectBuf[(hits - 1) * 4 + 3];
}

but the result is selection well for some faces but other they don't interactive with the mouse selection ,and when I try to add some rotation by :
glRotatef(rotatex, 1, 0, 0);
before Render(GL_SELECT); in Doselection and in the same time I used it in draw also that is :

static void
Draw(void)
{ Render(GL_SELECT);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D (3.0, 3.0, 3.0, 3.0);
glOrtho(3.0, 3.0, 3.0, 3.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glScalef(zoom, zoom, zoom);
glRotatef(rotatex, 1, 0, 0);

Render(GL_RENDER);
glPopMatrix();
glutSwapBuffers();
}


Can you Please tel me where is the bug in this program ..?

thanks


S H E R E E


Disgruntled Gamer
Disgruntled Gamer
Ok, I really don't think you need the gluUnProject stuff in there if the selection is working properly. I see that you name the faces based on there index. I'm not sure if starting the naming at zero has any ill effects since you initially glPushName(0), I've always started at 1 when I do this but it may not matter. If it did matter you could always name the faces like this glLoadName(i+1); then they will start at 1 instead of zero.

Now if you can click on one of these faces and your DoSelect returns one of the faces indices, like say face number 4. You can just go into your color array, objects[4].color and change it to what you want it to be. If you change it to the i+1 stuff in the glLoadName then you want to subract 1 from whatever DoSelect returns so you have the proper index to the face. Since it looks like your color array has a slot for each face.

DG

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.