IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

OpenGL Discussion :

Probleme de picking avec openGL ES via un système de lancer de rayon sur triangle


Sujet :

OpenGL

  1. #1
    Membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2006
    Messages
    44
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Novembre 2006
    Messages : 44
    Points : 52
    Points
    52
    Par défaut Probleme de picking avec openGL ES via un système de lancer de rayon sur triangle
    Bonjour, je suis en train de développer un algorithme sur lequel je bute depuis pas mal de temps. Je pense pas que ce soit la théorie mais plutôt une erreur dans le code. Je développe en Objective-C/C et avec OpenGL ES 1.0.
    Je suis passé à autre chose car j'ai buté sur ce problème pendant 1 semaine.
    Pouvez vous m'aider, j'en perd des cheveux...

    La matrice viewPort n'est pas changé et de même pour la matrice projection.

    Voici le code sur les calculs matriciels:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    // Compute the dot product of two vector
    float dotProduct(const Vector3D a,const Vector3D b) 
    {
    	return a.x*b.x+a.y*b.y+a.z*b.z;
    }
     
    // Compute the cross product of two vector
    Vector3D crossProduct(const Vector3D a,const Vector3D b)
    {
    	Vector3D tmp={a.y*b.z - a.z*b.y,a.z*b.x - a.x*b.z,a.x*b.y - a.y*b.x};
    	return tmp;
    }
    /*
     * Perform a 4x4 matrix multiplication  (product = a x b).
     * Input:  a, b - matrices to multiply
     * Output:  product - product of a and b
     */
    void matmul(GLfloat * product, const GLfloat * a, const GLfloat * b)
    {
    	// Pour éviter 
    	GLfloat temp[16];
    	GLint i;
    	//<<2 =*4
    #define A(row,col)  a[(col<<2)+row]
    #define B(row,col)  b[(col<<2)+row]
    #define T(row,col)  temp[(col<<2)+row]
     
    	/* i-te Zeile */
    	for (i = 0; i < 4; ++i) 
    	{
    		T(i, 0) = A(i, 0) * B(0, 0) + A(i, 1) * B(1, 0) + A(i, 2) * B(2, 0) + A(i, 3) * B(3, 0);
    		T(i, 1) = A(i, 0) * B(0, 1) + A(i, 1) * B(1, 1) + A(i, 2) * B(2, 1) + A(i, 3) * B(3, 1);
    		T(i, 2) = A(i, 0) * B(0, 2) + A(i, 1) * B(1, 2) + A(i, 2) * B(2, 2) + A(i, 3) * B(3, 2);
    		T(i, 3) = A(i, 0) * B(0, 3) + A(i, 1) * B(1, 3) + A(i, 2) * B(2, 3) + A(i, 3) * B(3, 3);
    	}
     
    #undef A
    #undef B
    #undef T
    	memcpy(product, temp, 16 * sizeof(GLfloat));
    }
     
    /*
     * Compute inverse of 4x4 transformation matrix.
     * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
     */
    GLboolean invert_matrix(const GLfloat * m, GLfloat * out)
    {
    	/* NB. OpenGL Matrices are COLUMN major.{ a^= b; b^=a; a^=b; } */
    #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; } 
    #define MAT(m,r,c) (m)[((c)<<2)+(r)]
     
    	GLfloat wtmp[4][8];
    	GLfloat m0, m1, m2, m3, s;
    	GLfloat *r0, *r1, *r2, *r3;
     
    	r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
     
    	r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1),
    	r0[2] = MAT(m, 0, 2), r0[3] = MAT(m, 0, 3),
    	r0[4] = 1.0f, r0[5] = r0[6] = r0[7] = 0.0f,
    	r1[0] = MAT(m, 1, 0), r1[1] = MAT(m, 1, 1),
    	r1[2] = MAT(m, 1, 2), r1[3] = MAT(m, 1, 3),
    	r1[5] = 1.0f, r1[4] = r1[6] = r1[7] = 0.0f,
    	r2[0] = MAT(m, 2, 0), r2[1] = MAT(m, 2, 1),
    	r2[2] = MAT(m, 2, 2), r2[3] = MAT(m, 2, 3),
    	r2[6] = 1.0f, r2[4] = r2[5] = r2[7] = 0.0f,
    	r3[0] = MAT(m, 3, 0), r3[1] = MAT(m, 3, 1),
    	r3[2] = MAT(m, 3, 2), r3[3] = MAT(m, 3, 3),
    	r3[7] = 1.0f, r3[4] = r3[5] = r3[6] = 0.0f;
     
    	/* choose pivot - or die */
    	if (fabsf(r3[0]) > fabsf(r2[0]))
    		SWAP_ROWS(r3, r2);
    	if (fabsf(r2[0]) > fabsf(r1[0]))
    		SWAP_ROWS(r2, r1);
    	if (fabsf(r1[0]) > fabsf(r0[0]))
    		SWAP_ROWS(r1, r0);
    	if (0.0f == r0[0])
    		return GL_FALSE;
     
    	/* eliminate first variable     */
    	m1 = r1[0] / r0[0];
    	m2 = r2[0] / r0[0];
    	m3 = r3[0] / r0[0];
    	s = r0[1];
    	r1[1] -= m1 * s;
    	r2[1] -= m2 * s;
    	r3[1] -= m3 * s;
    	s = r0[2];
    	r1[2] -= m1 * s;
    	r2[2] -= m2 * s;
    	r3[2] -= m3 * s;
    	s = r0[3];
    	r1[3] -= m1 * s;
    	r2[3] -= m2 * s;
    	r3[3] -= m3 * s;
    	s = r0[4];
    	if (s != 0.0f) {
    		r1[4] -= m1 * s;
    		r2[4] -= m2 * s;
    		r3[4] -= m3 * s;
    	}
    	s = r0[5];
    	if (s != 0.0f) {
    		r1[5] -= m1 * s;
    		r2[5] -= m2 * s;
    		r3[5] -= m3 * s;
    	}
    	s = r0[6];
    	if (s != 0.0f) {
    		r1[6] -= m1 * s;
    		r2[6] -= m2 * s;
    		r3[6] -= m3 * s;
    	}
    	s = r0[7];
    	if (s != 0.0f) {
    		r1[7] -= m1 * s;
    		r2[7] -= m2 * s;
    		r3[7] -= m3 * s;
    	}
     
    	/* choose pivot - or die */
    	if (fabsf(r3[1]) > fabsf(r2[1]))
    		SWAP_ROWS(r3, r2);
    	if (fabsf(r2[1]) > fabsf(r1[1]))
    		SWAP_ROWS(r2, r1);
    	if (0.0f == r1[1])
    		return GL_FALSE;
     
    	/* eliminate second variable */
    	m2 = r2[1] / r1[1];
    	m3 = r3[1] / r1[1];
    	r2[2] -= m2 * r1[2];
    	r3[2] -= m3 * r1[2];
    	r2[3] -= m2 * r1[3];
    	r3[3] -= m3 * r1[3];
    	s = r1[4];
    	if (0.0f != s) {
    		r2[4] -= m2 * s;
    		r3[4] -= m3 * s;
    	}
    	s = r1[5];
    	if (0.0f != s) {
    		r2[5] -= m2 * s;
    		r3[5] -= m3 * s;
    	}
    	s = r1[6];
    	if (0.0f != s) {
    		r2[6] -= m2 * s;
    		r3[6] -= m3 * s;
    	}
    	s = r1[7];
    	if (0.0f != s) {
    		r2[7] -= m2 * s;
    		r3[7] -= m3 * s;
    	}
     
    	/* choose pivot - or die */
    	if (fabsf(r3[2]) > fabsf(r2[2]))
    		SWAP_ROWS(r3, r2);
    	if (0.0f == r2[2])
    		return GL_FALSE;
     
    	/* eliminate third variable */
    	m3 = r3[2] / r2[2];
    	r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
    	r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], r3[7] -= m3 * r2[7];
     
    	/* last check */
    	if (0.0f == r3[3])
    		return GL_FALSE;
     
    	s = 1.0f / r3[3];		/* now back substitute row 3 */
    	r3[4] *= s;
    	r3[5] *= s;
    	r3[6] *= s;
    	r3[7] *= s;
     
    	m2 = r2[3];			/* now back substitute row 2 */
    	s = 1.0f / r2[2];
    	r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
    	r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
    	m1 = r1[3];
    	r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
    	r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
    	m0 = r0[3];
    	r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
    	r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
     
    	m1 = r1[2];			/* now back substitute row 1 */
    	s = 1.0f / r1[1];
    	r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
    	r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
    	m0 = r0[2];
    	r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
    	r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
     
    	m0 = r0[1];			/* now back substitute row 0 */
    	s = 1.0f / r0[0];
    	r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
    	r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
     
    	MAT(out, 0, 0) = r0[4];
    	MAT(out, 0, 1) = r0[5], MAT(out, 0, 2) = r0[6];
    	MAT(out, 0, 3) = r0[7], MAT(out, 1, 0) = r1[4];
    	MAT(out, 1, 1) = r1[5], MAT(out, 1, 2) = r1[6];
    	MAT(out, 1, 3) = r1[7], MAT(out, 2, 0) = r2[4];
    	MAT(out, 2, 1) = r2[5], MAT(out, 2, 2) = r2[6];
    	MAT(out, 2, 3) = r2[7], MAT(out, 3, 0) = r3[4];
    	MAT(out, 3, 1) = r3[5], MAT(out, 3, 2) = r3[6];
    	MAT(out, 3, 3) = r3[7];
     
    	return GL_TRUE;
     
    #undef MAT
    #undef SWAP_ROWS
    }
     
    /* projection of the point (objx,objy,obz) on the screen (winx,winy,winz) */
    //GLint GLAPIENTRY;
    GLboolean gluProject(GLfloat objx, GLfloat objy, GLfloat objz,
    							const GLfloat model[16], const GLfloat proj[16],
    							const GLint viewport[4],
    							GLfloat * winx, GLfloat * winy, GLfloat * winz)
    {
    	/* transformation matrix */
    	GLfloat in[4], out[4];
     
    	/* initialize the matrix and the vector a transformer */
    	in[0] = objx;
    	in[1] = objy;
    	in[2] = objz;
    	in[3] = 1.0f;
    	transform_point(out, model, in);
    	transform_point(in, proj, out);
    	/* To avoid a error of the type division by ZERO */
    	if (in[3] == 0.0f)
    		return GL_FALSE;
     
    	in[0] /= in[3];
    	in[1] /= in[3];
    	in[2] /= in[3];
     
    	/* screen coordinate */
    	*winx = viewport[0] + (1.0f + in[0]) * viewport[2] / 2.0f;
    	*winy = viewport[1] + (1.0f + in[1]) * viewport[3] / 2.0f;
    	/* between 0 and 1 next z */
    	*winz = (1.0f + in[2]) / 2.0f;
    	return GL_TRUE;
    }
     
    /* transformation du point ecran (winx,winy,winz) en point objet */
    //GLint GLAPIENTRY
    GLboolean gluUnProject(GLfloat winx, GLfloat winy, GLfloat winz,
    							  const GLfloat model[16], const GLfloat proj[16],
    							  const GLint viewport[4],
    							  GLfloat * objx, GLfloat * objy, GLfloat * objz)
    {
    	/* transformation matrices */
    	GLfloat m[16], A[16];
    	GLfloat in[4], out[4];
     
    	/* Normalization between -1 et 1 */
    	in[0] = (winx - viewport[0]) * 2.0f / viewport[2] - 1.0f;
    	in[1] = (winy - viewport[1]) * 2.0f / viewport[3] - 1.0f;
    	in[2] = 2.0f * winz - 1.0f;
    	in[3] = 1.0f;
     
    	/* We calcul the inverse transformation*/
    	matmul(A, proj, model);
    	invert_matrix(A, m);
     
    	/* whence the coordinatennees objets */
    	transform_point(out, m, in);
    	if (out[3] == 0.0f)
    		return GL_FALSE;
    	*objx = out[0] / out[3];
    	*objy = out[1] / out[3];
    	*objz = out[2] / out[3];
    	return GL_TRUE;
    }
    et voici le code concernant le raytracer qui lance un rayon à partir de l'endroit où on a cliqué:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    -(Boolean) checkFaces:(CGPoint)winPos
    {
    	checking=TRUE;
    	//opengl 0,0 is at the bottom not at the top
    	winPos.y = (float)viewport[3] - winPos.y;
     
    	Vector3D nearPoint;
    	Vector3D rayOrigin;
    	Vector3D farPoint;
    	Vector3D rayVector;
    	//glGetFloatv( GL_PROJECTION_MATRIX,projection );
    	glGetFloatv( GL_MODELVIEW_MATRIX,modelview );
     
    	// float winZ;
    	//we cannot do the following in openGL ES due to tile rendering
    	// glReadPixels( (int)winPos.x, (int)winPos.y, 1, 1, GL_DEPTH_COMPONENT24_OES, GL_FLOAT, &winZ );
     
    	//Retreiving position projected on near plan
     
    	gluUnProject( winPos.x, winPos.y ,0, modelview, projection, viewport, &nearPoint.x, &nearPoint.y, &nearPoint.z);
    	rayOrigin=nearPoint;
     
    	//Retreiving position projected on far plan
    	gluUnProject( winPos.x, winPos.y,  1, modelview, projection, viewport, &farPoint.x, &farPoint.y, &farPoint.z);
     
     
    	//Processing ray vector
    	rayVector.x = farPoint.x - nearPoint.x;
    	rayVector.y = farPoint.y - nearPoint.y;
    	rayVector.z = farPoint.z - nearPoint.z;
     
    	float rayLength = sqrtf(POW2(rayVector.x) + POW2(rayVector.y) + POW2(rayVector.z));
     
    	//normalizing ray vector
    	rayVector.x /= rayLength;
    	rayVector.y /= rayLength;
    	rayVector.z /= rayLength;
     
     
    	static const GLfloat cubeIn[24][4] ={
     
            // face de devant
            {-1.0, 1.0, 1.0,1.0},             
            {-1.0, -1.0, 1.0,1.0},           
            {1.0, -1.0, 1.0,1.0},             
            {1.0, 1.0, 1.0,1.0},              
     
            // haut
            {-1.0, 1.0, -1.0,1.0},           
            {-1.0, 1.0, 1.0,1.0},             
            {1.0, 1.0, 1.0,1.0},              
            {1.0, 1.0, -1.0,1.0},             
     
            // arrière
            {1.0, 1.0, -1.0,1.0},         
            {1.0, -1.0, -1.0,1.0},           
            {-1.0, -1.0, -1.0,1.0},           
            {-1.0, 1.0, -1.0,1.0},            
     
            // dessous
            {-1.0, -1.0, 1.0,1.0},
            {-1.0, -1.0, -1.0,1.0},
            {1.0, -1.0, -1.0,1.0},
            {1.0, -1.0, 1.0,1.0},
     
            // gauche
            {-1.0, 1.0, -1.0,1.0},
            {-1.0, 1.0, 1.0,1.0},
            {-1.0, -1.0, 1.0,1.0},
            {-1.0, -1.0, -1.0,1.0},
     
            // droit
            {1.0, 1.0, 1.0,1.0},
            {1.0, 1.0, -1.0,1.0},
            {1.0, -1.0, -1.0,1.0},
            {1.0, -1.0, 1.0,1.0}
        };  
    	GLfloat cubeOut[24][4]; 
    	short unsigned i,j=0;
    	iVideo=-1;
    	CGFloat distanceMin=1e30f;
    	Vector3D vertices[3];
     
    	//We keep the coordinates of the cube in the model view system
    	for (i=0; i<24; ++i) 
    	{
    		transform_point(cubeOut[i], modelview, cubeIn[i]);
    		Vector3D temp={cubeOut[i][0],cubeOut[i][1],cubeOut[i][2]};
    		vertices[j]=temp;
    		++j;
    		if (j==3) 
    		{
    			j=0;
    			float t=[self intersectionSurface:rayOrigin rayVector:rayVector Vector1:vertices[0] Vector2:vertices[1] Vector3:vertices[2] distance:distanceMin];
    			if (t<distanceMin) 
    			{
    				distanceMin=t;
    				//=>i>>2
    				iVideo=i/4;
     
    			}
    			++i;
    			temp.x=cubeOut[i][0];
    			temp.y=cubeOut[i][1];
    			temp.z=cubeOut[i][2];
    			t=[self intersectionSurface:rayOrigin rayVector:rayVector Vector1:vertices[2] Vector2:temp Vector3:vertices[0] distance:distanceMin];
    			if(t<distanceMin) 
    			{
    				distanceMin=t;
    				//=>i>>2
    				iVideo=i/4;
    			}
    		}
    	}
     
    	checking=FALSE;
     
    	if (iVideo>-1) 
    	{
    		short unsigned b=iVideo*4,i=0;
    		short unsigned count=b+5;
    		coordinates[0]=cubeOut[b][0];
    		coordinates[1]=cubeOut[b][0];
    		coordinates[2]=cubeOut[b][1];
    		coordinates[3]=cubeOut[b][1];
    		for (i=b; i<count; ++i) 
    		{
    			if (coordinates[0]>cubeOut[i][0]) 
    			{
    				coordinates[0]=cubeOut[i][0];
    			}
     
    			if (coordinates[1]<cubeOut[i][0]) 
    			{
    				coordinates[1]=cubeOut[i][0];
    			}
    			if (coordinates[2]<cubeOut[i][1]) 
    			{
    				coordinates[2]=cubeOut[i][1];
    			}
     
    			if (coordinates[3]>cubeOut[i][1]) 
    			{
    				coordinates[3]=cubeOut[i][1];
    			}
     
    			if (coordinates[4]<cubeOut[i][2]) 
    			{
    				coordinates[4]=cubeOut[i][2];
    			}
     
    			if (coordinates[5]>cubeOut[i][2]) 
    			{
    				coordinates[5]=cubeOut[i][2];
    			}
    		}
    		coordinates[0]=-1-coordinates[0];
    		coordinates[1]=1-coordinates[1];
    		coordinates[2]=-1-coordinates[2];
    		coordinates[3]=1-coordinates[3];
     
    		return TRUE;
    	}
    	else 
    	{
    		return FALSE;	
    	}
     
    }
     
    -(float) intersectionSurface:(Vector3D)rayOrigin rayVector:(Vector3D) rayVector Vector1:(Vector3D) p0 Vector2:(Vector3D) p1 Vector3:(Vector3D) p2 distance:(float) distanceMin
    {
    	//Value which means that the method return an error or value laid aside
    	const float kNoIntersection=1e30f;
     
    	//Vectors of the plane
    	Vector3D a={p1.x-p0.x,p1.y-p0.y,p1.z-p0.z};
    	Vector3D b={p2.x-p1.x,p2.y-p1.y,p2.z-p1.z};
     
    	//Normal at the surface
    	Vector3D normal=crossProduct(a, b);
     
    	//We verify if the result designs a parallel ray or not
    	CGFloat dot=dotProduct(normal,rayVector);
    	if(!(dot<0.0f))
    	{
    		return kNoIntersection;	
    	}
     
    	//equivalente of the equation of the surface in one point
    	CGFloat d=dotProduct(normal,p0);
    	//distance between the origin and the surface
    	CGFloat t=d-dotProduct(normal,rayOrigin);
    	if(!(t<=0.0f))
    	{
    		return kNoIntersection;	
    	}
     
    	if (!(t>=dot*distanceMin)) 
    	{
    		return kNoIntersection;
    	}
     
     
    	t/=dot;
    	assert(t>=0.0f);
    	assert(t<=distanceMin);
     
     
    	//Compute 3D point of intersection
    	Vector3D p;
    	p.x=rayOrigin.x+rayVector.x*t;
    	p.y=rayOrigin.y+rayVector.y*t;
    	p.z=rayOrigin.z+rayVector.z*t;
     
    	//Find dominant axis to select which plane to project onto, and compute u's and v's
    	float u0,u1,u2;
    	float v0,v1,v2;
    	if (fabs(normal.x)>fabs(normal.y)) 
    	{
    		if (fabs(normal.x)>fabs(normal.z)) 
    		{
    			u0=p.y-p0.y;
    			u1=p1.y-p0.y;
    			u2=p2.y-p0.y;
     
    			v0=p.z-p0.z;
    			v1=p1.z-p0.z;
    			v2=p2.z-p0.z;
    		}
    		else 
    		{
    			u0=p.x-p0.x;
    			u1=p1.x-p0.x;
    			u2=p2.x-p0.x;
     
    			v0=p.y-p0.y;
    			v1=p1.y-p0.y;
    			v2=p2.y-p0.y;	
    		}
    	}
    	else 
    	{
    		if (fabs(normal.y)>fabs(normal.z)) 
    		{
    			u0=p.x-p0.x;
    			u1=p1.x-p0.x;
    			u2=p2.x-p0.x;
     
    			v0=p.z-p0.z;
    			v1=p1.z-p0.z;
    			v2=p2.z-p0.z;
    		}
    		else 
    		{
    			u0=p.x-p0.x;
    			u1=p1.x-p0.x;
    			u2=p2.x-p0.x;
     
    			v0=p.y-p0.y;
    			v1=p1.y-p0.y;
    			v2=p2.y-p0.y;
    		}
    	}
    	//Compute denominator, check for invalid
     
    	float temp=u1*v2-v1*u2;
    	if(!(temp!=0.0f))
    	{
    		return kNoIntersection;
    	}
    	temp=1.0f/temp;
     
    	//Compute barycentric coords, checking for out-of-range
    	//at each step
    	float alpha=(u0*v2-v0*u2)*temp;
    	if(!(alpha>=0.0f))
    	{
    		return kNoIntersection;	
    	}
     
    	float beta=(u1*v0-v1*u0)*temp;
    	if(!(beta>=0.0f))
    	{
    		return kNoIntersection;	
    	}
     
    	float gamma=1.0f-alpha-beta;
    	if(!(gamma>=0.0f))
    	{
    		return kNoIntersection;	
    	}
    	//Return parametric point of intersection
    	return t;
    }

  2. #2
    Membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2006
    Messages
    44
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Novembre 2006
    Messages : 44
    Points : 52
    Points
    52
    Par défaut
    Bon, j'ai changé les coordonnées du cube afin qu'ils soient tous construit dans le sens trigonométrique. J'ai aussi changé la façon dont sont transmis les vectrices pour être sûr que je prenne la normale à la surface qui soit frontal à chaque triangle.

    Mais cela ne résout toujours pas le problème...

    Voici le code sur les calculs matriciels:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    // Compute the dot product of two vector
    float dotProduct(const Vector3D a,const Vector3D b) 
    {
    	return a.x*b.x+a.y*b.y+a.z*b.z;
    }
     
    // Compute the cross product of two vector
    Vector3D crossProduct(const Vector3D a,const Vector3D b)
    {
    	Vector3D tmp={a.y*b.z - a.z*b.y,a.z*b.x - a.x*b.z,a.x*b.y - a.y*b.x};
    	return tmp;
    }
    /*
     * Perform a 4x4 matrix multiplication  (product = a x b).
     * Input:  a, b - matrices to multiply
     * Output:  product - product of a and b
     */
    void matmul(GLfloat * product, const GLfloat * a, const GLfloat * b)
    {
    	// Pour éviter 
    	GLfloat temp[16];
    	GLint i;
    	//<<2 =*4
    #define A(row,col)  a[(col<<2)+row]
    #define B(row,col)  b[(col<<2)+row]
    #define T(row,col)  temp[(col<<2)+row]
     
    	/* i-te Zeile */
    	for (i = 0; i < 4; ++i) 
    	{
    		T(i, 0) = A(i, 0) * B(0, 0) + A(i, 1) * B(1, 0) + A(i, 2) * B(2, 0) + A(i, 3) * B(3, 0);
    		T(i, 1) = A(i, 0) * B(0, 1) + A(i, 1) * B(1, 1) + A(i, 2) * B(2, 1) + A(i, 3) * B(3, 1);
    		T(i, 2) = A(i, 0) * B(0, 2) + A(i, 1) * B(1, 2) + A(i, 2) * B(2, 2) + A(i, 3) * B(3, 2);
    		T(i, 3) = A(i, 0) * B(0, 3) + A(i, 1) * B(1, 3) + A(i, 2) * B(2, 3) + A(i, 3) * B(3, 3);
    	}
     
    #undef A
    #undef B
    #undef T
    	memcpy(product, temp, 16 * sizeof(GLfloat));
    }
     
    /*
     * Compute inverse of 4x4 transformation matrix.
     * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
     */
    GLboolean invert_matrix(const GLfloat * m, GLfloat * out)
    {
    	/* NB. OpenGL Matrices are COLUMN major.{ a^= b; b^=a; a^=b; } */
    #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; } 
    #define MAT(m,r,c) (m)[((c)<<2)+(r)]
     
    	GLfloat wtmp[4][8];
    	GLfloat m0, m1, m2, m3, s;
    	GLfloat *r0, *r1, *r2, *r3;
     
    	r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
     
    	r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1),
    	r0[2] = MAT(m, 0, 2), r0[3] = MAT(m, 0, 3),
    	r0[4] = 1.0f, r0[5] = r0[6] = r0[7] = 0.0f,
    	r1[0] = MAT(m, 1, 0), r1[1] = MAT(m, 1, 1),
    	r1[2] = MAT(m, 1, 2), r1[3] = MAT(m, 1, 3),
    	r1[5] = 1.0f, r1[4] = r1[6] = r1[7] = 0.0f,
    	r2[0] = MAT(m, 2, 0), r2[1] = MAT(m, 2, 1),
    	r2[2] = MAT(m, 2, 2), r2[3] = MAT(m, 2, 3),
    	r2[6] = 1.0f, r2[4] = r2[5] = r2[7] = 0.0f,
    	r3[0] = MAT(m, 3, 0), r3[1] = MAT(m, 3, 1),
    	r3[2] = MAT(m, 3, 2), r3[3] = MAT(m, 3, 3),
    	r3[7] = 1.0f, r3[4] = r3[5] = r3[6] = 0.0f;
     
    	/* choose pivot - or die */
    	if (fabsf(r3[0]) > fabsf(r2[0]))
    		SWAP_ROWS(r3, r2);
    	if (fabsf(r2[0]) > fabsf(r1[0]))
    		SWAP_ROWS(r2, r1);
    	if (fabsf(r1[0]) > fabsf(r0[0]))
    		SWAP_ROWS(r1, r0);
    	if (0.0f == r0[0])
    		return GL_FALSE;
     
    	/* eliminate first variable     */
    	m1 = r1[0] / r0[0];
    	m2 = r2[0] / r0[0];
    	m3 = r3[0] / r0[0];
    	s = r0[1];
    	r1[1] -= m1 * s;
    	r2[1] -= m2 * s;
    	r3[1] -= m3 * s;
    	s = r0[2];
    	r1[2] -= m1 * s;
    	r2[2] -= m2 * s;
    	r3[2] -= m3 * s;
    	s = r0[3];
    	r1[3] -= m1 * s;
    	r2[3] -= m2 * s;
    	r3[3] -= m3 * s;
    	s = r0[4];
    	if (s != 0.0f) {
    		r1[4] -= m1 * s;
    		r2[4] -= m2 * s;
    		r3[4] -= m3 * s;
    	}
    	s = r0[5];
    	if (s != 0.0f) {
    		r1[5] -= m1 * s;
    		r2[5] -= m2 * s;
    		r3[5] -= m3 * s;
    	}
    	s = r0[6];
    	if (s != 0.0f) {
    		r1[6] -= m1 * s;
    		r2[6] -= m2 * s;
    		r3[6] -= m3 * s;
    	}
    	s = r0[7];
    	if (s != 0.0f) {
    		r1[7] -= m1 * s;
    		r2[7] -= m2 * s;
    		r3[7] -= m3 * s;
    	}
     
    	/* choose pivot - or die */
    	if (fabsf(r3[1]) > fabsf(r2[1]))
    		SWAP_ROWS(r3, r2);
    	if (fabsf(r2[1]) > fabsf(r1[1]))
    		SWAP_ROWS(r2, r1);
    	if (0.0f == r1[1])
    		return GL_FALSE;
     
    	/* eliminate second variable */
    	m2 = r2[1] / r1[1];
    	m3 = r3[1] / r1[1];
    	r2[2] -= m2 * r1[2];
    	r3[2] -= m3 * r1[2];
    	r2[3] -= m2 * r1[3];
    	r3[3] -= m3 * r1[3];
    	s = r1[4];
    	if (0.0f != s) {
    		r2[4] -= m2 * s;
    		r3[4] -= m3 * s;
    	}
    	s = r1[5];
    	if (0.0f != s) {
    		r2[5] -= m2 * s;
    		r3[5] -= m3 * s;
    	}
    	s = r1[6];
    	if (0.0f != s) {
    		r2[6] -= m2 * s;
    		r3[6] -= m3 * s;
    	}
    	s = r1[7];
    	if (0.0f != s) {
    		r2[7] -= m2 * s;
    		r3[7] -= m3 * s;
    	}
     
    	/* choose pivot - or die */
    	if (fabsf(r3[2]) > fabsf(r2[2]))
    		SWAP_ROWS(r3, r2);
    	if (0.0f == r2[2])
    		return GL_FALSE;
     
    	/* eliminate third variable */
    	m3 = r3[2] / r2[2];
    	r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
    	r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], r3[7] -= m3 * r2[7];
     
    	/* last check */
    	if (0.0f == r3[3])
    		return GL_FALSE;
     
    	s = 1.0f / r3[3];		/* now back substitute row 3 */
    	r3[4] *= s;
    	r3[5] *= s;
    	r3[6] *= s;
    	r3[7] *= s;
     
    	m2 = r2[3];			/* now back substitute row 2 */
    	s = 1.0f / r2[2];
    	r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
    	r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
    	m1 = r1[3];
    	r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
    	r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
    	m0 = r0[3];
    	r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
    	r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
     
    	m1 = r1[2];			/* now back substitute row 1 */
    	s = 1.0f / r1[1];
    	r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
    	r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
    	m0 = r0[2];
    	r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
    	r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
     
    	m0 = r0[1];			/* now back substitute row 0 */
    	s = 1.0f / r0[0];
    	r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
    	r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
     
    	MAT(out, 0, 0) = r0[4];
    	MAT(out, 0, 1) = r0[5], MAT(out, 0, 2) = r0[6];
    	MAT(out, 0, 3) = r0[7], MAT(out, 1, 0) = r1[4];
    	MAT(out, 1, 1) = r1[5], MAT(out, 1, 2) = r1[6];
    	MAT(out, 1, 3) = r1[7], MAT(out, 2, 0) = r2[4];
    	MAT(out, 2, 1) = r2[5], MAT(out, 2, 2) = r2[6];
    	MAT(out, 2, 3) = r2[7], MAT(out, 3, 0) = r3[4];
    	MAT(out, 3, 1) = r3[5], MAT(out, 3, 2) = r3[6];
    	MAT(out, 3, 3) = r3[7];
     
    	return GL_TRUE;
     
    #undef MAT
    #undef SWAP_ROWS
    }
     
    /* projection of the point (objx,objy,obz) on the screen (winx,winy,winz) */
    //GLint GLAPIENTRY;
    GLboolean gluProject(GLfloat objx, GLfloat objy, GLfloat objz,
    							const GLfloat model[16], const GLfloat proj[16],
    							const GLint viewport[4],
    							GLfloat * winx, GLfloat * winy, GLfloat * winz)
    {
    	/* transformation matrix */
    	GLfloat in[4], out[4];
     
    	/* initialize the matrix and the vector a transformer */
    	in[0] = objx;
    	in[1] = objy;
    	in[2] = objz;
    	in[3] = 1.0f;
    	transform_point(out, model, in);
    	transform_point(in, proj, out);
    	/* To avoid a error of the type division by ZERO */
    	if (in[3] == 0.0f)
    		return GL_FALSE;
     
    	in[0] /= in[3];
    	in[1] /= in[3];
    	in[2] /= in[3];
     
    	/* screen coordinate */
    	*winx = viewport[0] + (1.0f + in[0]) * viewport[2] / 2.0f;
    	*winy = viewport[1] + (1.0f + in[1]) * viewport[3] / 2.0f;
    	/* between 0 and 1 next z */
    	*winz = (1.0f + in[2]) / 2.0f;
    	return GL_TRUE;
    }
     
    /* transformation du point ecran (winx,winy,winz) en point objet */
    //GLint GLAPIENTRY
    GLboolean gluUnProject(GLfloat winx, GLfloat winy, GLfloat winz,
    							  const GLfloat model[16], const GLfloat proj[16],
    							  const GLint viewport[4],
    							  GLfloat * objx, GLfloat * objy, GLfloat * objz)
    {
    	/* transformation matrices */
    	GLfloat m[16], A[16];
    	GLfloat in[4], out[4];
     
    	/* Normalization between -1 et 1 */
    	in[0] = (winx - viewport[0]) * 2.0f / viewport[2] - 1.0f;
    	in[1] = (winy - viewport[1]) * 2.0f / viewport[3] - 1.0f;
    	in[2] = 2.0f * winz - 1.0f;
    	in[3] = 1.0f;
     
    	/* We calcul the inverse transformation*/
    	matmul(A, proj, model);
    	invert_matrix(A, m);
     
    	/* whence the coordinatennees objets */
    	transform_point(out, m, in);
    	if (out[3] == 0.0f)
    		return GL_FALSE;
    	*objx = out[0] / out[3];
    	*objy = out[1] / out[3];
    	*objz = out[2] / out[3];
    	return GL_TRUE;
    }
    et voici le code concernant le raytracer qui lance un rayon à partir de l'endroit où on a cliqué:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    -(Boolean) checkFaces:(CGPoint)winPos
    {
    	checking=TRUE;
    	//opengl 0,0 is at the bottom not at the top
    	winPos.y = (float)viewport[3] - winPos.y;
     
    	Vector3D nearPoint;
    	Vector3D rayOrigin;
    	Vector3D farPoint;
    	Vector3D rayVector;
    	glGetFloatv( GL_PROJECTION_MATRIX,projection );
    	glGetFloatv( GL_MODELVIEW_MATRIX,modelview );
     
    	// float winZ;
    	//we cannot do the following in openGL ES due to tile rendering
    	// glReadPixels( (int)winPos.x, (int)winPos.y, 1, 1, GL_DEPTH_COMPONENT24_OES, GL_FLOAT, &winZ );
     
    	//Retreiving position projected on near plan
     
    	gluUnProject( winPos.x, winPos.y ,0, modelview, projection, viewport, &nearPoint.x, &nearPoint.y, &nearPoint.z);
    	rayOrigin=nearPoint;
     
    	//Retreiving position projected on far plan
    	gluUnProject( winPos.x, winPos.y,  1, modelview, projection, viewport, &farPoint.x, &farPoint.y, &farPoint.z);
     
     
    	//Processing ray vector
    	rayVector.x = farPoint.x - nearPoint.x;
    	rayVector.y = farPoint.y - nearPoint.y;
    	rayVector.z = farPoint.z - nearPoint.z;
     
    	float rayLength = sqrtf(POW2(rayVector.x) + POW2(rayVector.y) + POW2(rayVector.z));
     
    	//normalizing ray vector
    	rayVector.x /= rayLength;
    	rayVector.y /= rayLength;
    	rayVector.z /= rayLength;
     
     
    	static const GLfloat cubeIn[24][4] ={
     
            {1.0, -1.0, -1.0,1.0},             
            { 1.0, -1.0, 1.0,1.0},           
            {-1.0, -1.0, 1.0,1.0},             
            {-1.0, -1.0, -1.0,1.0},              
     
            {1.0, 1.0, -1.0,1.0},           
            {-1.0, 1.0, -1.0,1.0},             
            {-1.0, 1.0, 1.0,1.0},              
            {1.0, 1.0, 1.0,1.0},             
     
            {1.0, -1.0, -1.0,1.0},         
            {1.0, 1.0, -1.0,1.0},           
            {1.0, 1.0, 1.0,1.0},           
            {1.0, -1.0, 1.0,1.0},            
     
            {1.0, -1.0, 1.0,1.0},
            {1.0, 1.0, 1.0,1.0},
            {-1.0, 1.0, 1.0,1.0},
            {-1.0, -1.0, 1.0,1.0},
     
            {-1.0, -1.0, 1.0,1.0},
            {-1.0, 1.0, 1.0,1.0},
            {-1.0, 1.0, -1.0,1.0},
            {-1.0, -1.0, -1.0,1.0},
     
            {1.0, 1.0, -1.0,1.0},
            {1.0, -1.0, -1.0,1.0},
            {-1.0, -1.0, -1.0,1.0},
            {-1.0, 1.0, -1.0,1.0}
        };  
    	GLfloat cubeOut[24][4]; 
    	short unsigned i,j=0;
    	iVideo=-1;
    	CGFloat distanceMin=1e30f;
    	Vector3D vertices[3];
     
    	//We keep the coordinates of the cube in the model view system
    	for (i=0; i<24; ++i) 
    	{
    		transform_point(cubeOut[i], modelview, cubeIn[i]);
    		Vector3D temp={cubeOut[i][0],cubeOut[i][1],cubeOut[i][2]};
    		vertices[j]=temp;
    		++j;
    		if (j==3) 
    		{
    			j=0;
    			float t=[self intersectionSurface:rayOrigin rayVector:rayVector Vector1:vertices[2] Vector2:vertices[1] Vector3:vertices[0] distance:distanceMin];
    			if (t<distanceMin) 
    			{
    				distanceMin=t;
    				//=>i>>2
    				iVideo=i/4;
     
    			}
    			++i;
    			temp.x=cubeOut[i][0];
    			temp.y=cubeOut[i][1];
    			temp.z=cubeOut[i][2];
    			t=[self intersectionSurface:rayOrigin rayVector:rayVector Vector1:temp Vector2:vertices[2] Vector3:vertices[1] distance:distanceMin];
    			if(t<distanceMin) 
    			{
    				distanceMin=t;
    				//=>i>>2
    				iVideo=i/4;
    			}
    		}
    	}
     
    	checking=FALSE;
     
    	if (iVideo>-1) 
    	{
    		short unsigned b=iVideo*4,i=0;
    		short unsigned count=b+5;
    		coordinates[0]=cubeOut[b][0];
    		coordinates[1]=cubeOut[b][0];
    		coordinates[2]=cubeOut[b][1];
    		coordinates[3]=cubeOut[b][1];
    		for (i=b; i<count; ++i) 
    		{
    			if (coordinates[0]>cubeOut[i][0]) 
    			{
    				coordinates[0]=cubeOut[i][0];
    			}
     
    			if (coordinates[1]<cubeOut[i][0]) 
    			{
    				coordinates[1]=cubeOut[i][0];
    			}
    			if (coordinates[2]<cubeOut[i][1]) 
    			{
    				coordinates[2]=cubeOut[i][1];
    			}
     
    			if (coordinates[3]>cubeOut[i][1]) 
    			{
    				coordinates[3]=cubeOut[i][1];
    			}
     
    			if (coordinates[4]<cubeOut[i][2]) 
    			{
    				coordinates[4]=cubeOut[i][2];
    			}
     
    			if (coordinates[5]>cubeOut[i][2]) 
    			{
    				coordinates[5]=cubeOut[i][2];
    			}
    		}
    		coordinates[0]=-1-coordinates[0];
    		coordinates[1]=1-coordinates[1];
    		coordinates[2]=-1-coordinates[2];
    		coordinates[3]=1-coordinates[3];
     
    		return TRUE;
    	}
    	else 
    	{
    		return FALSE;	
    	}
     
    }
     
    -(float) intersectionSurface:(Vector3D)rayOrigin rayVector:(Vector3D) rayVector Vector1:(Vector3D) p0 Vector2:(Vector3D) p1 Vector3:(Vector3D) p2 distance:(float) distanceMin
    {
    	//Value which means that the method return an error or value laid aside
    	const float kNoIntersection=1e30f;
     
    	//Vectors of the plane
    	Vector3D a={p1.x-p0.x,p1.y-p0.y,p1.z-p0.z};
    	Vector3D b={p2.x-p1.x,p2.y-p1.y,p2.z-p1.z};
    	//Normal at the surface
    	Vector3D normal=crossProduct(a, b);
     
    	//We verify if the result designs a parallel ray or not
    	CGFloat dot=dotProduct(normal,rayVector);
    	if(!(dot<0.0f))
    	{
    		return kNoIntersection;	
    	}
     
    	//equivalente of the equation of the surface in one point
    	CGFloat d=dotProduct(normal,p0);
    	//distance between the origin and the surface
    	CGFloat t=d-dotProduct(normal,rayOrigin);
    	if(!(t<=0.0f))
    	{
    		return kNoIntersection;	
    	}
     
    	if (!(t>=dot*distanceMin)) 
    	{
    		return kNoIntersection;
    	}
     
     
    	t/=dot;
    	assert(t>=0.0f);
    	assert(t<=distanceMin);
     
     
    	//Compute 3D point of intersection
    	Vector3D p;
    	p.x=rayOrigin.x+rayVector.x*t;
    	p.y=rayOrigin.y+rayVector.y*t;
    	p.z=rayOrigin.z+rayVector.z*t;
     
    	//Find dominant axis to select which plane to project onto, and compute u's and v's
    	float u0,u1,u2;
    	float v0,v1,v2;
    	if (fabs(normal.x)>fabs(normal.y)) 
    	{
    		if (fabs(normal.x)>fabs(normal.z)) 
    		{
    			u0=p.y-p0.y;
    			u1=p1.y-p0.y;
    			u2=p2.y-p0.y;
     
    			v0=p.z-p0.z;
    			v1=p1.z-p0.z;
    			v2=p2.z-p0.z;
    		}
    		else 
    		{
    			u0=p.x-p0.x;
    			u1=p1.x-p0.x;
    			u2=p2.x-p0.x;
     
    			v0=p.y-p0.y;
    			v1=p1.y-p0.y;
    			v2=p2.y-p0.y;	
    		}
    	}
    	else 
    	{
    		if (fabs(normal.y)>fabs(normal.z)) 
    		{
    			u0=p.x-p0.x;
    			u1=p1.x-p0.x;
    			u2=p2.x-p0.x;
     
    			v0=p.z-p0.z;
    			v1=p1.z-p0.z;
    			v2=p2.z-p0.z;
    		}
    		else 
    		{
    			u0=p.x-p0.x;
    			u1=p1.x-p0.x;
    			u2=p2.x-p0.x;
     
    			v0=p.y-p0.y;
    			v1=p1.y-p0.y;
    			v2=p2.y-p0.y;
    		}
    	}
    	//Compute denominator, check for invalid
     
    	float temp=u1*v2-v1*u2;
    	if(!(temp!=0.0f))
    	{
    		return kNoIntersection;
    	}
    	temp=1.0f/temp;
     
    	//Compute barycentric coords, checking for out-of-range
    	//at each step
    	float alpha=(u0*v2-v0*u2)*temp;
    	if(!(alpha>=0.0f))
    	{
    		return kNoIntersection;	
    	}
     
    	float beta=(u1*v0-v1*u0)*temp;
    	if(!(beta>=0.0f))
    	{
    		return kNoIntersection;	
    	}
     
    	float gamma=1.0f-alpha-beta;
    	if(!(gamma>=0.0f))
    	{
    		return kNoIntersection;	
    	}
    	//Return parametric point of intersection
    	return t;
    }

Discussions similaires

  1. Réponses: 0
    Dernier message: 30/11/2010, 16h46
  2. picking avec opengl
    Par steebatc dans le forum Débuter
    Réponses: 2
    Dernier message: 16/03/2009, 14h39
  3. picking avec opengl
    Par refka_eei dans le forum OpenGL
    Réponses: 0
    Dernier message: 11/04/2008, 13h52
  4. Probleme de Nbre avec un Userform Via Excel
    Par Didpa dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 10/08/2006, 09h29
  5. probleme de reaffichage avec openGL [MFC]
    Par ryu20 dans le forum MFC
    Réponses: 9
    Dernier message: 22/01/2005, 20h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo