# picking and changing alpha .......Help

This topic is 4230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everybody; I have some confusion to do the picking one face of icosahedron to change its color or alpha value ,so what I did I draw the icosahedron by triangles and normals and assigned a different color to each face with value that can be able to control through picking procedure but I don't know weather I have to make this value out of from array with board[20] ,because 20 face , or not but when I tried the following code ,there is never interaction by click the mouse button .....and I don't know if the part 1 and 2 is suitable to each other Please tell me what is the right way .......soon .....please : #include <math.h> #include <stdlib.h> #include <stdio.h> #include <GL/glut.h> #define X .325731112119133606 #define Z .50650808352039932 float xx=0.1 ,yy=0.1, zz=0.1, ww=0.1,xxx=0.0; float alpha=0.75; /* amount of color for each square */ int board[20]; /****icosahedron *******//*****1*******/ 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); } void drawTriangle(void) { GLuint i, j; glBegin(GL_TRIANGLES); //glColor4f ((GLfloat) i/6.0,(GLfloat) i/17.0,(GLfloat) board/3.0,alpha); icoNormVec(i); glVertex3fv(&vdata[tindices[0]] [0]); glVertex3fv(&vdata[tindices[1]] [0]); glVertex3fv(&vdata[tindices[2]] [0]); glEnd(); } void drawIcosahedron(GLenum mode) { GLuint i, j; for (i = 0; i < 20; i++) { glPushMatrix (); if (mode == GL_SELECT) glLoadName (i); glColor4f ((GLfloat) i/6.0,(GLfloat) i/17.0,(GLfloat) board/3.0,alpha); drawTriangle(); if (mode == GL_SELECT) glPopName (); glPopMatrix(); } } /**************************end 1*************/ /***** initial the array that will be use to change the color or alpha****/ void init(void) { int i, j; for ( i = 0; i <20; i++) board = 0; glClearColor (0.0, 0.0, 0.0, 0.0); } /***procedures for selection and hits*******2****/ void processHits (GLint hits, GLuint buffer[]) { unsigned int i, j; GLuint ii, jj, names, *ptr; printf ("hits = %d\n", hits); ptr = (GLuint *) buffer; for (i = 0; i < hits; i++) { /* for each hit */ names = *ptr; printf (" number of names for this hit = %d\n", names); ptr++; printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++; printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++; printf (" names are "); printf ("%d ", *ptr); ii=*ptr; ptr++; } printf ("\n"); board[ii] = (board[ii] + 1) % 3; } } /* pickTriangle() sets up selection mode, name stack, * and projection matrix for picking. Then the * objects are drawn. */ #define BUFSIZE 512 void pickTriangle(int button, int state, int x, int y) { GLuint selectBuf[BUFSIZE]; GLint hits; GLint viewport[4]; if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return; glGetIntegerv (GL_VIEWPORT, viewport); glSelectBuffer (BUFSIZE, selectBuf); (void) glRenderMode (GL_SELECT); glInitNames(); glPushName(0); glMatrixMode (GL_PROJECTION); glPushMatrix (); glLoadIdentity (); /* create 5x5 pixel picking region near cursor location */ gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport); gluOrtho2D (0.0, 3.0, 30.0, 3.0); drawIcosahedron (GL_SELECT); glMatrixMode (GL_PROJECTION); glPopMatrix (); glFlush (); hits = glRenderMode (GL_RENDER); processHits (hits, selectBuf); glutPostRedisplay(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); drawIcosahedron (GL_RENDER); glFlush(); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D (3.0, 3.0, 3.0, 3.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; } } /* Main Loop */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (1000, 1000); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutReshapeFunc (reshape); glutDisplayFunc(display); glutMouseFunc (pickTriangle); glutKeyboardFunc (keyboard); glutMainLoop(); return 0; } Thanks S H E R E E

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632995
• Total Posts
3009775
• ### Who's Online (See full list)

There are no registered users currently online

×