Pikcing opengl

znb Messages postés 4 Date d'inscription mercredi 16 février 2011 Statut Membre Dernière intervention 15 mai 2011 - 9 avril 2011 à 02:44
cs_Tibabou Messages postés 129 Date d'inscription mercredi 2 janvier 2002 Statut Membre Dernière intervention 7 juillet 2012 - 18 avril 2011 à 21:01
J 'ai fait un code et ça marche très bien; il détecte les couleurs des objets. Mais je veux que, pour une valeur particulière de la couleur, dessiner un objet (une boule dans ce cas), alors j'ai inséré une condition sur une variable qui contient la couleur dans ma fonction display mais cela ne marche pas :

le code:

#include<GL/glut.h>
#include <stdlib.h>
#include<stdio.h>
int X,Y,r=-1,v=-1;
static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F };
static const float gris[] = { 0.6F,0.6F,0.6F,1.0F };

void changeSize(int w, int h)
{
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0, 0.0,0.0,-1.0,0.0f,1.0f,0.0f);
}
void init(void)
{
const GLfloat mat_shininess[] = { 50.0 };
glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
const GLfloat mat_diffuse[4] = { 1.0F,0.0F,0.0F,1.0F };
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glLightfv(GL_LIGHT0,GL_DIFFUSE,gris);
glLightfv(GL_LIGHT1,GL_DIFFUSE,gris);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_CULL_FACE);

}
void dessinerboules(void)
{
int i,j;
float y=-1.45,x=-1.65;
for ( i = 0 ; i < 10 ; i++ )
{
for(j=0;j<4;j++)
{
glPushMatrix();
glTranslatef(x,y, 0.1);
glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc);
glutSolidSphere(0.1,10000,10000);
glPopMatrix();
x=x+0.3;
}
x=-1.65;
y=y+0.3;
}

}
void drawPickingMode()
{
int i,j;
float y=-1.45,x=-1.65;
for ( i = 0 ; i < 10 ; i++ )
{
for(j=0;j<4;j++)
{
glPushMatrix();
glTranslatef(x,y, 0.1);
glColor3ub(4*i+j+1,0,0);
glutSolidSphere(0.1,10000,10000);
glPopMatrix();
x=x+0.3;
}
x=-1.65;
y=y+0.3;
}
}
void selectionmode(void)
{
GLint viewport[4];
GLubyte pixel[3];
glGetIntegerv(GL_VIEWPORT,viewport);
glDisable(GL_LIGHTING);
glDisable(GL_LESS);
glDisable(GL_DEPTH_TEST);
glDisable(GL_NORMALIZE);
glDisable(GL_AUTO_NORMAL);
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
drawPickingMode();
glReadPixels(X,viewport[3]-Y,1,1,GL_RGB,GL_UNSIGNED_BYTE,(void *)pixel);
r=pixel[0];
v=pixel[1];
printf("&%d,%d&\t%d\t%d\n",X,Y,r,v);
glRenderMode (GL_RENDER);

}
void click(int button, int state, int x, int y)
{
if ((button GLUT_LEFT_BUTTON) && (state GLUT_DOWN))
{
X=x;Y=y;
selectionmode();
}
else return;
}
void display(void)
{
glClearColor(0.0F,0.0F,0.0F,0.0F) ;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
dessinerboules();
if (r==1)
{
glPushMatrix();
glTranslatef(1.75,-1.8,-0.10);
glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc);
glutSolidSphere(0.1,10000,10000);
glPopMatrix();
}
glFlush();
glutSwapBuffers();
}
int main(int argc, char*argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowPosition(0,0);
glutInitWindowSize(700,700);
glutCreateWindow("MasterMind");
init();
glutReshapeFunc(changeSize);
glutDisplayFunc(display);
glutMouseFunc(click);
glutMainLoop();
return(0);
}

je travaille sous windows vista
Merci

1 réponse

cs_Tibabou Messages postés 129 Date d'inscription mercredi 2 janvier 2002 Statut Membre Dernière intervention 7 juillet 2012
18 avril 2011 à 21:01
Salut,

Pourquoi ton test est fait sur r == 1 ?
Je n'ai pas testé ton code mais je m'attends à avoir un pixel blanc sous la souris et donc les composantes rvb à 255 chacune (on est en unsigned char ici, pas en float).

J'aurais fait mon test sur r==255.
Que te renvoie ton printf ?

[i]glReadPixels(X,viewport[3]-Y,1,1,GL_RGB,GL_UNSIGNED_BYTE,(void *)pixel);
r=pixel[0];
v=pixel[1];
printf("&%d,%d&\t%d\t%d\n",X,Y,r,v); /i


Tibabou
Mattally, le scrabble de tous les défis : www.mattally.fr
0
Rejoignez-nous