Bonjour à tous !
Je débute avec OpenGl et j’aimerais savoir si l’un d’entre vous pourrait me fournir des sources d’un rubik’s cube basique.
Tenant sur un seul fichier c ou cpp d’environ 15ko ?
ça serait cool !
Ou encore un fichier source d’un simple cube (non rubik’s Cube) ou sphère qu’on fait tourner par rapport à des axes fixe (global) et non local à l’objet.
Merci d’avance !
EDIT :
A ceux qui veulent un code utilisant les quaternions pour effectuer une rotation global à la scène :
Le header quaternion.h :
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 #include <GL/glut.h> #include <stdlib.h> #include <math.h> #include "quaternion.h" float ANGL = 0; Quaternion QXYZ; int axe = 0; void init(void) { glClearColor (0.0f, 0.0f, 0.0f, 0.0f); glShadeModel (GL_SMOOTH); } void display(void){ glMatrixMode (GL_MODELVIEW); glLoadIdentity (); gluLookAt (0.5f, 1.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f (0.8, 0.6, 0.0); mat4 matrice; Quaternion QX, QY, QZ, QQ; if(axe == 0) { QX.createFromAxisAngle(1, 0, 0, ANGL); QQ = QX;} if(axe == 1) { QY.createFromAxisAngle(0, 1, 0, ANGL); QQ = QY;} if(axe == 2) { QZ.createFromAxisAngle(0, 0, 1, ANGL); QQ = QZ;} QXYZ = QXYZ * QQ; QXYZ.normalise(); QXYZ.quaterToMat(&matrice); glMultMatrixf((GLfloat*)&matrice); glutSolidCube (1.0); glColor3f (0.8, 0.4, 0.2); glutWireCube (1.0); glutSwapBuffers(); glFlush (); } void reshape (int w, int h){ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); } void keyboard(unsigned char key, int x, int y){ switch (key) { case 27: exit(0); break; case 'x' : ANGL = 1.0f; axe = 0; glutPostRedisplay(); break; case 'y' : ANGL = 1.0f; axe = 1; glutPostRedisplay(); break; case 'z' : ANGL = 1.0f; axe = 2; glutPostRedisplay(); break; } } int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }
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 typedef struct{ float x, y, z, w; } vec4; typedef struct{ vec4 x, y, z, w; } mat4; typedef class Quaternion{ public: float x, y , z, w; public : Quaternion::Quaternion(){ x = y = z = 0; w = 1; }; void Quaternion::quaterToMat(mat4 *result){ mat4 *mat = result; float xx, xy, xz, xw, yy, yz, yw, zz, zw, X, Y, Z, W; X = x; Y = y; Z = z; W = w; xx = X * X; xy = X * Y; xz = X * Z; xw = X * W; yy = Y * Y; yz = Y * Z; yw = Y * W; zz = Z * Z; zw = Z * W; (*mat).x.x = 1 - 2 * ( yy + zz ); (*mat).x.y = 2 * ( xy - zw ); (*mat).x.z = 2 * ( xz + yw ); (*mat).y.x = 2 * ( xy + zw ); (*mat).y.y = 1 - 2 * ( xx + zz ); (*mat).y.z = 2 * ( yz - xw ); (*mat).z.x = 2 * ( xz - yw ); (*mat).z.y = 2 * ( yz + xw ); (*mat).z.z = 1 - 2 * ( xx + yy ); (*mat).x.w = (*mat).y.w = (*mat).z.w = (*mat).w.x = (*mat).w.y = (*mat).w.z = 0; (*mat).w.w = 1; }; Quaternion Quaternion::operator *(Quaternion q){ Quaternion r; r.w = w*q.w - x*q.x - y*q.y - z*q.z; r.x = w*q.x + x*q.w + y*q.z - z*q.y; r.y = w*q.y + y*q.w + z*q.x - x*q.z; r.z = w*q.z + z*q.w + x*q.y - y*q.x; r.normalise(); return r; }; float Quaternion:: magnitude(){ return sqrt(w * w + x * x + y * y + z * z); }; void Quaternion::normalise(){ float m = magnitude(); if(m>0){ x /= m; y /= m; z /= m; w /= m;} }; void Quaternion::createFromAxisAngle(float _x, float _y, float _z, float angle) { angle = float((angle / 180.0) * M_PI); float result = (float)sin( angle / 2.0 ); w = (float)cos( angle / 2.0 ); x = float(_x * result); y = float(_y * result); z = float(_z * result); normalise(); }; } Quaternion;
dans mon cas j'utilise devc++ pour la compilation et les linkers suivant :
-lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32
![]()
Partager