├── 2D Bezier Curves
├── Bezier1.PNG
├── BezierFormula.png
└── Source.cpp
├── 3D Solar System
├── SolarSystem
│ └── SolarSystem
│ │ ├── a.bmp
│ │ ├── b.bmp
│ │ ├── c.bmp
│ │ ├── d.bmp
│ │ ├── e.bmp
│ │ ├── f.bmp
│ │ ├── g.bmp
│ │ ├── h.bmp
│ │ ├── i.bmp
│ │ ├── im.bmp
│ │ ├── text.bmp
│ │ ├── glut32.dll
│ │ └── rocket.bmp
└── main.cpp
└── README.md
/2D Bezier Curves/Bezier1.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/2D Bezier Curves/Bezier1.PNG
--------------------------------------------------------------------------------
/2D Bezier Curves/BezierFormula.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/2D Bezier Curves/BezierFormula.png
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/a.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/a.bmp
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/b.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/b.bmp
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/c.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/c.bmp
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/d.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/d.bmp
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/e.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/e.bmp
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/f.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/f.bmp
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/g.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/g.bmp
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/h.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/h.bmp
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/i.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/i.bmp
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/im.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/im.bmp
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/text.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/text.bmp
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/glut32.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/glut32.dll
--------------------------------------------------------------------------------
/3D Solar System/SolarSystem/SolarSystem/rocket.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lafifii/Computer_Graphics_Projects/HEAD/3D Solar System/SolarSystem/SolarSystem/rocket.bmp
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Partial Project: Bezier-Curves
2 | Bezier Curves animation using 4 movable control points in OpenGL
3 |
4 | A Bézier curve is a parametric curve used in computer graphics and related fields. The curve, which is related to the Bernstein polynomial, is named after Pierre Bézier, who used it in the 1960s for designing curves for the bodywork of Renault cars. Other uses include the design of computer fonts and animation. Bézier curves can be combined to form a Bézier spline, or generalized to higher dimensions to form Bézier surfaces.
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | # Final Project: Solar System 3D
15 | 3D Animation Solar System using OpenGL
16 |
17 | **Topics:**
18 | * Textures
19 | * Lighting
20 | * 3D and perspective Projection
21 | * Bezier Curves
22 | * 3D Transformation Matrix
23 | * Particles system
24 |
25 |
26 |
27 |
28 |
29 |
30 | ## Instructions:
31 | * Directional keys for moving system on x y and z axis
32 | * F1, F2, and F3 for rotating Saturn orbit around the planet in x y and z axis respectively
33 | * Zoom in and out + and -
34 | * Move camera left right with A and D respectively and up and down with W and S
35 | * V to increase rocket's speed and B to decrease it
36 |
37 |
--------------------------------------------------------------------------------
/2D Bezier Curves/Source.cpp:
--------------------------------------------------------------------------------
1 | #pragma warning(disable : 4996)
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | using namespace std;
13 | #define Frac_Circ 360 // Cien fracciones de circulo
14 | #define PI 3.1415926535897932
15 |
16 |
17 | typedef pair pff;
18 | int SCREEN_HEIGHT = 480;
19 | vector PC(4);
20 | vector PB, PR[3];
21 | pff nuevo;
22 | float mouseX, mouseY;
23 | bool mouseleft;
24 |
25 | float t_ = 1.0;
26 | const int maxi = 100;
27 | int dp[maxi][maxi], dx = 1, t = 0, id = 0, apreto = 0, id_cambio = -1;
28 | float color1, color2, color3;
29 |
30 | void init() {
31 | glClearColor(1.0, 1.0, 1.0, 1.0);
32 | glColor3f(1.0, 0.0, 0.0);
33 | glPointSize(4.0);
34 | glMatrixMode(GL_PROJECTION);
35 | glLoadIdentity();
36 | gluOrtho2D(0.0, 640.0, 0.0, 480.0);
37 | }
38 |
39 |
40 | void DibujaPunto(int x, int y)
41 | {
42 | glPointSize(9);
43 | glBegin(GL_POINTS);
44 | glVertex2i(x, y);
45 | glEnd();
46 | }
47 |
48 | void DibujarPuntoCircular(int x, int y) {
49 | glBegin(GL_POLYGON);
50 | for (int i = 0; i < Frac_Circ + 1; i++) { // +1 para cerrar
51 | glVertex2f(x + 5 * cos(2.0 * PI * i / Frac_Circ),
52 | y + 5 * sin(2.0 * PI * i / Frac_Circ));
53 | }
54 | glEnd();
55 | glColor3f(0.0f, 0.0f, 0.0f);
56 | glLineWidth(1.0);
57 | glBegin(GL_LINES);
58 | for (int i = 0; i < Frac_Circ + 1; i++) { // +1 para cerrar
59 | glVertex2f(x + 5 * cos(2.0 * PI * i / Frac_Circ),
60 | y + 5 * sin(2.0 * PI * i / Frac_Circ));
61 | }
62 | glEnd();
63 | }
64 |
65 | void DibujarPuntoCircularDelgado(int x, int y) {
66 | glBegin(GL_POLYGON);
67 | for (int i = 0; i < Frac_Circ + 1; i++) { // +1 para cerrar
68 | glVertex2f(x + 3 * cos(2.0 * PI * i / Frac_Circ),
69 | y + 3 * sin(2.0 * PI * i / Frac_Circ));
70 | }
71 | glEnd();
72 | glColor3f(0.0f, 0.0f, 0.0f);
73 | glLineWidth(1.0);
74 | glBegin(GL_LINES);
75 | for (int i = 0; i < Frac_Circ + 1; i++) { // +1 para cerrar
76 | glVertex2f(x + 3 * cos(2.0 * PI * i / Frac_Circ),
77 | y + 3 * sin(2.0 * PI * i / Frac_Circ));
78 | }
79 | glEnd();
80 | }
81 |
82 | void DibujaLinea(pff p1, pff p2)
83 | {
84 | glLineWidth(1.0);
85 | glBegin(GL_LINES);
86 | glVertex3f(p1.first, p1.second, 0);
87 | glVertex3f(p2.first, p2.second, 0);
88 | glEnd();
89 | }
90 |
91 | void DibujaLineaGruesa(pff p1, pff p2)
92 | {
93 | glLineWidth(2.5);
94 | glBegin(GL_LINES);
95 | glVertex3f(p1.first, p1.second, 0);
96 | glVertex3f(p2.first, p2.second, 0);
97 | glEnd();
98 | }
99 |
100 | pff DibujaBezier(double t)
101 | {
102 | pff P = { 0.0, 0.0 };
103 | int n = PC.size() - 1;
104 | for (int i = 0; i < PC.size(); i++)
105 | {
106 | P.first += 1.0 * dp[n][i] * (PC[i].first) * (pow((1 - t), n - i)) * (pow(t, i));
107 | P.second += 1.0 * dp[n][i] * (PC[i].second) * (pow((1 - t), n - i)) * (pow(t, i));
108 | }
109 | return P;
110 | }
111 | pff puntoRecta(double t, pff p1, pff p2) {
112 | return { p1.first * t + (1 - t) * p2.first , p1.second * t + (1 - t) * p2.second };
113 | }
114 |
115 | void DrawLineaPunteada(pff p1, pff p2) {
116 | float dist = sqrt(pow((p2.first - p1.first), 2) + pow((p2.second - p1.second), 2));
117 | float inters = dist / 10;
118 | float aumento = 1.0 / inters;
119 | for (float t = 0; t < 1.0; t = t + aumento) {
120 | float x1 = (p2.first - p1.first) * t + p1.first;
121 | float y1 = (p2.second - p1.second) * t + p1.second;
122 | float x2 = (p2.first - p1.first) * (t + (aumento * 0.6)) + p1.first;
123 | float y2 = (p2.second - p1.second) * (t + (aumento * 0.6)) + p1.second;
124 | glBegin(GL_LINES);
125 | glVertex3f(x1, y1, 0);
126 | glVertex3f(x2, y2, 0);
127 | glEnd();
128 | }
129 | }
130 |
131 | void draw()
132 | {
133 | glClear(GL_COLOR_BUFFER_BIT);
134 | glClearColor(1.0, 1.0, 1.0, 1.0);
135 | glPointSize(5);
136 | for (int i = 0; i < 4; i++) {
137 | if (i == 1 || i == 2) {
138 | glColor3f(0.0, 0.0, 1.0);
139 | DibujaPunto(PC[i].first, PC[i].second);
140 | }
141 | else {
142 | glColor3f(1.0, 0.0, 0.0);
143 | DibujarPuntoCircular(PC[i].first, PC[i].second);
144 | }
145 | }
146 |
147 | glColor3f(1.0, 0.0, 0.0);
148 | for (int i = 0; i < PB.size() - 1; i++) DibujaLineaGruesa(PB[i], PB[i + 1]);
149 |
150 | for (int i = 0; i < 3; i++) {
151 | if (i == 1) {
152 | glColor3f(0.33, 0.33, 0.33);
153 | DrawLineaPunteada(PC[i], PC[i + 1]);
154 | }
155 | else {
156 | glColor3f(0.0, 1.0, 0.0);
157 | DibujaLinea(PC[i], PC[i + 1]);
158 | }
159 | }
160 | glColor3f(1.0, 0.0, 0.0);
161 | DibujarPuntoCircularDelgado(PB[id].first, PB[id].second);
162 |
163 | for (int i = 0; i < 3; i++) {
164 | glColor3f(0.0, 0.5, 0.5);
165 | DibujarPuntoCircularDelgado(PR[i][id].first, PR[i][id].second);
166 | if (i < 2) {
167 | if (i == 0) glColor3f(color1, color3, color2); else if (i == 1) glColor3f(color2, color3, color1);
168 | DrawLineaPunteada(PR[i][id], PR[i + 1][id]);
169 | }
170 | }
171 |
172 | pff pc1 = puntoRecta(t_, PR[0][id], PR[1][id]);
173 | pff pc2 = puntoRecta(t_, PR[1][id], PR[2][id]);
174 |
175 | glColor3f(1.0, 0.3, 1.0);
176 | DibujarPuntoCircularDelgado(pc1.first, pc1.second);
177 | DibujarPuntoCircularDelgado(pc2.first, pc2.second);
178 |
179 | glColor3f(color3, color2, color1);
180 | DrawLineaPunteada(pc1, PB[id]);
181 | DrawLineaPunteada(PB[id], pc2);
182 |
183 | glColor3f(0, 0, 0);
184 | for (char letra = 'A'; letra < 'E'; letra++) {
185 | glRasterPos2i(PC[letra - 'A'].first + 10, PC[letra - 'A'].second + 10);
186 | glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, letra);
187 | }
188 | glutSwapBuffers();
189 |
190 | }
191 | void trianguloPascal() {
192 | for (int line = 0; line < maxi; line++)
193 | {
194 | for (int i = 0; i <= line; i++) {
195 | if (line == i || i == 0) dp[line][i] = 1;
196 | else dp[line][i] = dp[line - 1][i - 1] + dp[line - 1][i];
197 | }
198 | }
199 | }
200 | void puntos() {
201 | pff POld = PC[0];
202 | for (double t = 0.0; t <= 1.0; t += 0.005) {
203 | pff P = DibujaBezier(t);
204 | PB.push_back(P);
205 | POld = P;
206 | }
207 | for (int i = 0; i < 3; i++) {
208 | for (double t = 0.0; t <= 1.0; t += 0.005)
209 | PR[i].push_back(puntoRecta(t, PC[i], PC[i + 1]));
210 | reverse(PR[i].begin(), PR[i].end());
211 | }
212 | }
213 | void idle(void) {
214 | t++;
215 | if (t == 2e6) {
216 | if (id + dx >= PB.size() || id + dx < 0) dx *= -1;
217 | t_ += (dx < 0 ? 0.005 : -0.005);
218 | id += dx, t = 0;
219 | glutPostRedisplay();
220 | }
221 | }
222 | void mouseCB(int button, int state, int x, int y) {
223 | mouseX = x;
224 | mouseY = y;
225 | if (button == GLUT_LEFT_BUTTON)
226 | {
227 | if (state == GLUT_DOWN) {
228 | mouseleft = true;
229 | }
230 | if (state == GLUT_UP && apreto)
231 | {
232 | mouseleft = false;
233 | }
234 | }
235 | }
236 |
237 |
238 | void mouseMotionCB(int x, int y) {
239 | if (mouseleft) {
240 | apreto = 1;
241 | for (int i = 0; i < 4; i++) {
242 | nuevo = { x,480 - y };
243 | if (abs(PC[i].first - nuevo.first) < 5 && abs(PC[i].second - nuevo.second) < 5) {
244 | id_cambio = i;
245 | break;
246 | }
247 | }
248 | nuevo = { x,480 - y };
249 | apreto = 0;
250 | if (id_cambio == -1) return;
251 | PC[id_cambio] = nuevo;
252 | id_cambio = -1;
253 | PB.clear();
254 | for (int i = 0; i < 3; i++)
255 | PR[i].clear();
256 | puntos();
257 | }
258 | }
259 |
260 | int main() {
261 |
262 | time_t now = time(0);
263 | tm* time = localtime(&now);
264 | srand(time->tm_sec);
265 | trianguloPascal();
266 | color1 = (rand() % 100) * 0.01; color2 = (rand() % 100) * 0.01; color3 = (rand() % 100) * 0.01;
267 | for (int i = 0; i < 4; i++) PC[i] = { rand() % 600 + 20, rand() % 440 + 20 };
268 | puntos();
269 |
270 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
271 | glutInitWindowSize(640, 480);
272 | glutInitWindowPosition(0, 0);
273 | glutCreateWindow("Bezier Curves Animation");
274 | init();
275 | glutDisplayFunc(draw);
276 | glutPostRedisplay();
277 | glutIdleFunc(idle);
278 | glutMouseFunc(mouseCB);
279 | glutMotionFunc(mouseMotionCB);
280 | glutMainLoop();
281 | return 0;
282 | }
283 |
--------------------------------------------------------------------------------
/3D Solar System/main.cpp:
--------------------------------------------------------------------------------
1 | #pragma warning(disable:4996)
2 | #pragma once
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | double velocidad_cohete = 1;
14 | bool stop = 1;
15 | struct Point {
16 | float x, y, z;
17 | void setxyz(float xa, float ya, float za) {
18 | x = xa, y = ya, z = za;
19 | }
20 | };
21 | struct BMP {
22 | int w, h;
23 | BYTE* data;
24 | static unsigned int textura;
25 | };
26 |
27 |
28 | struct Particula {
29 | Point punto;
30 | float lifespan, tiempovivo, velocidad;
31 | Particula(Point p) {
32 | punto.setxyz(p.x, 9 + rand() % 3, p.z);
33 | tiempovivo = 0;
34 | lifespan = (rand() % 101) / 100.0;
35 | velocidad = velocidad_cohete;
36 | }
37 | void mover() {
38 | tiempovivo += 0.03;
39 | }
40 | void recal(Point p) {
41 | punto.setxyz(p.x, (90 + rand() % 30) / 10.0, p.z);
42 | tiempovivo = 0;
43 | lifespan = (rand() % 100) / 100.0;
44 | velocidad = velocidad_cohete;
45 | }
46 | };
47 |
48 | using namespace std;
49 | unsigned int texture[11];
50 | int actual[7];
51 | bool debe[3] = { 0 };
52 | Point punto_cohete;
53 | float radios[8] = { 1.5,1.8,2,1.2,2.5, 1.6,2.1,2.1 };
54 |
55 | float t_ = 0, dxt = 0.005;
56 | GLfloat girax = 15, giray = 0, zoom[3] = { 0,0,-60 }, rx = 0, ry = 0, rz = 0;
57 | const int maxi = 40;
58 | vector Asteroides;
59 | vector PuntosEstrellas, PC(4), PC2(4);
60 | vector PCE[5];
61 | vector PO[7];
62 | vector Texturas;
63 | vector SistemaParticulas;
64 |
65 | int dp[maxi][maxi];
66 | float j_cohete = 0;
67 | void trianguloPascal() {
68 | for (int line = 0; line < maxi; line++)
69 | {
70 | for (int i = 0; i <= line; i++) {
71 | if (line == i || i == 0) dp[line][i] = 1;
72 | else dp[line][i] = dp[line - 1][i - 1] + dp[line - 1][i];
73 | }
74 | }
75 | }
76 | void mover(void) {
77 | glMatrixMode(GL_MODELVIEW);
78 | glLoadIdentity();
79 | glTranslated(zoom[0], zoom[1], zoom[2]);
80 | glRotated(girax, 1.0, 0.0, 0.0);
81 | glRotated(giray, 0.0, 1.0, 0.0);
82 | glRotated(40, 0, 0, 1);
83 | }
84 |
85 | void cohete_cub() {
86 | glPushMatrix();
87 | glEnable(GL_TEXTURE_2D);
88 | glBindTexture(GL_TEXTURE_2D, texture[10]);
89 | int xx = ((int)j_cohete + 1) % 301;
90 | double ang = 2.0 * 3.14 * xx / 300.0;
91 | float ult[3] = { cos(ang) * 30, 11.5, sin(ang) * 30 };
92 | punto_cohete.y += 2.5;
93 |
94 | glTranslatef(ult[0], ult[1], ult[2]);
95 | glBegin(GL_QUADS);
96 | // Front Face
97 | glTexCoord2f(0.0f, 1.0f);
98 | glVertex3f(-1.0f, 1.0f, 1.0f);
99 | glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
100 | glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
101 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
102 |
103 | // Back Face
104 | glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
105 | glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
106 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
107 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
108 |
109 | // Top Face
110 | glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
111 | glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f);
112 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
113 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
114 |
115 | // Bottom Face
116 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
117 | glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
118 | glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f);
119 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
120 |
121 | glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
122 | glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
123 | glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
124 | glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
125 |
126 |
127 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
128 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
129 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
130 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
131 |
132 | glEnd();
133 |
134 | glTranslatef(0,0,0);
135 | punto_cohete.y -= 2.5;
136 | glPopMatrix();
137 | glDisable(GL_TEXTURE_2D);
138 | }
139 |
140 | void LoadTextures(const char* nombre) {
141 | int width, height;
142 | BYTE* data;
143 | FILE* file;
144 | // open texture data
145 | file = fopen(nombre, "rb");
146 | // allocate buffer
147 | BYTE header[54];
148 | fread(header, 1, 54, file);
149 | size_t dataPos = *(int*) & (header[0x0A]);
150 | size_t imageSize = *(int*) & (header[0x22]);
151 | width = *(int*) & (header[0x12]);
152 | height = *(int*) & (header[0x16]);
153 |
154 | if (imageSize == 0) imageSize = width * height * 3;
155 | if (dataPos == 0) dataPos = 54;
156 | data = new unsigned char[imageSize];
157 | fread(data, 1, imageSize, file);
158 | fclose(file);
159 | BMP aux;
160 | aux.w = width, aux.h = height, aux.data = data;
161 | Texturas.push_back(aux);
162 | }
163 | void nueva_textura(int idx) {
164 | glBindTexture(GL_TEXTURE_2D, texture[idx]);
165 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
166 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
167 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
168 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
169 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
170 | gluBuild2DMipmaps(GL_TEXTURE_2D, 3, Texturas[idx].w, Texturas[idx].h, GL_BGR, GL_UNSIGNED_BYTE, Texturas[idx].data);
171 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
172 | }
173 | void cube() {
174 | glEnable(GL_TEXTURE_2D);
175 | glBindTexture(GL_TEXTURE_2D, texture[0]);
176 | glEnable(GL_LIGHTING);
177 | glBegin(GL_QUADS);
178 | // Front Face
179 | glTexCoord2f(0.0f, 1.0f);
180 | glVertex3f(-60.0f, 60.0f, 60.0f);
181 | glTexCoord2f(1.0f, 1.0f); glVertex3f(60.0f, 60.0f, 60.0f);
182 | glTexCoord2f(1.0f, 0.0f); glVertex3f(60.0f, -60.0f, 60.0f);
183 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-60.0f, -60.0f, 60.0f);
184 |
185 | // Back Face
186 | glTexCoord2f(0.0f, 0.0f); glVertex3f(60.0f, -60.0f, -60.0f);
187 | glTexCoord2f(0.0f, 1.0f); glVertex3f(60.0f, 60.0f, -60.0f);
188 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-60.0f, 60.0f, -60.0f);
189 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-60.0f, -60.0f, -60.0f);
190 |
191 | // Top Face
192 | glTexCoord2f(1.0f, 1.0f); glVertex3f(60.0f, 60.0f, -60.0f);
193 | glTexCoord2f(1.0f, 0.0f); glVertex3f(60.0f, 60.0f, 60.0f);
194 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-60.0f, 60.0f, 60.0f);
195 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-60.0f, 60.0f, -60.0f);
196 |
197 | // Bottom Face
198 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-60.0f, -60.0f, 60.0f);
199 | glTexCoord2f(0.0f, 0.0f); glVertex3f(60.0f, -60.0f, 60.0f);
200 | glTexCoord2f(0.0f, 1.0f); glVertex3f(60.0f, -60.0f, -60.0f);
201 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-60.0f, -60.0f, -60.0f);
202 |
203 | glTexCoord2f(0.0f, 0.0f); glVertex3f(60.0f, -60.0f, 60.0f);
204 | glTexCoord2f(0.0f, 1.0f); glVertex3f(60.0f, 60.0f, 60.0f);
205 | glTexCoord2f(1.0f, 1.0f); glVertex3f(60.0f, 60.0f, -60.0f);
206 | glTexCoord2f(1.0f, 0.0f); glVertex3f(60.0f, -60.0f, -60.0f);
207 |
208 |
209 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-60.0f, 60.0f, -60.0f);
210 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-60.0f, 60.0f, 60.0f);
211 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-60.0f, -60.0f, 60.0f);
212 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-60.0f, -60.0f, -60.0f);
213 |
214 | glEnd();
215 | glDisable(GL_TEXTURE_2D);
216 | }
217 | void estrella(Point p, int tipo) {
218 | glTranslatef(p.x, p.y, p.z);
219 | glBegin(GL_LINE_LOOP);
220 | glVertex3f(0.0 * tipo, 0.2 * tipo, 0.0);
221 | glVertex3f(0.1 * tipo, 0.1 * tipo, 0.0);
222 | glVertex3f(0.2 * tipo, 0.05 * tipo, 0.0);
223 | glVertex3f(0.1 * tipo, 0.0 * tipo, 0.0);
224 | glVertex3f(0.2 * tipo, -0.1 * tipo, 0.0);
225 | glVertex3f(0.0 * tipo, 0.0 * tipo, 0.0);
226 | glVertex3f(-0.2 * tipo, -0.1 * tipo, 0.0);
227 | glVertex3f(-0.1 * tipo, 0.0 * tipo, 0.0);
228 | glVertex3f(-0.2 * tipo, 0.05 * tipo, 0.0);
229 | glVertex3f(-0.1 * tipo, 0.1 * tipo, 0.0);
230 | glEnd();
231 | }
232 | void planeta(float radio, float x, float y, float z, int idx) {
233 | glPushMatrix();
234 | glTranslatef(x, y, z);
235 | GLUquadric* qobj = gluNewQuadric();
236 | gluQuadricTexture(qobj, GL_TRUE);
237 | glEnable(GL_TEXTURE_2D);
238 | glBindTexture(GL_TEXTURE_2D, texture[idx]);
239 | gluSphere(qobj, radio, 50, 50);
240 | gluDeleteQuadric(qobj);
241 | glDisable(GL_TEXTURE_2D);
242 | glPopMatrix();
243 | }
244 | void drawAsteroides() {
245 | for (int i = 0, j = 0; i < Asteroides.size() && j <= 300; i++, j += 6) {
246 | double angle = 2 * 3.14 * j / 300;
247 | planeta(Asteroides[i], cos(angle) * 28, (i & 2 ? -1 : 1), sin(angle) * 28, 9);
248 | planeta(Asteroides[i], cos(angle) * 30, (j & 1 ? 1 : 1), sin(angle) * 30, 9);
249 | }
250 | }
251 | Point DibujaBezier(double t, vector & PCA)
252 | {
253 | Point P; P.setxyz(0, 0, 0);
254 | int n = PCA.size() - 1;
255 | for (int i = 0; i < PCA.size(); i++)
256 | {
257 | P.x += 1.0 * dp[n][i] * (PCA[i].x) * (pow((1 - t), n - i)) * (pow(t, i));
258 | P.y += 1.0 * dp[n][i] * (PCA[i].y) * (pow((1 - t), n - i)) * (pow(t, i));
259 | P.z += 1.0 * dp[n][i] * (PCA[i].z) * (pow((1 - t), n - i)) * (pow(t, i));
260 | }
261 | return P;
262 | }
263 | void orbitas_planetas(vector puntos, int idx) {
264 | glPushMatrix();
265 | glTranslatef(PO[idx][actual[idx]].x, PO[idx][actual[idx]].y, PO[idx][actual[idx]].z);
266 | glRotatef(rx, 1, 0, 0);
267 | glRotatef(ry, 0, 1, 0);
268 | glRotatef(rz, 0, 0, 1);
269 | glTranslatef(-PO[idx][actual[idx]].x, -PO[idx][actual[idx]].y, -PO[idx][actual[idx]].z);
270 | glLineWidth(1.5);
271 | glBegin(GL_LINES);
272 | vector vpa;
273 | vpa.push_back(puntos[0]);
274 | vpa.push_back(puntos[1]);
275 | vpa.push_back(puntos[2]);
276 | Point p1 = DibujaBezier(0, vpa);
277 | for (double t = 0.005; t <= 1; t += 0.005)
278 | {
279 | Point p2 = DibujaBezier(t, vpa);
280 | glVertex3f(p1.x, p1.y, p1.z);
281 | glVertex3f(p2.x, p2.y, p2.z);
282 | p1 = p2;
283 | }
284 | vpa.clear();
285 | vpa.push_back(puntos[2]);
286 | vpa.push_back(puntos[3]);
287 | vpa.push_back(puntos[0]);
288 |
289 | p1 = DibujaBezier(0, vpa);
290 | for (double t = 0.005; t <= 1; t += 0.005)
291 | {
292 | Point p2 = DibujaBezier(t, vpa);
293 | glVertex3f(p1.x, p1.y, p1.z);
294 | glVertex3f(p2.x, p2.y, p2.z);
295 | p1 = p2;
296 | }
297 | glEnd();
298 | glPopMatrix();
299 | }
300 | void display() {
301 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
302 |
303 | GLfloat mat_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
304 | GLfloat mat_diffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
305 | GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
306 | GLfloat high_shininess[] = { 100.0f };
307 | GLfloat emission[] = { 0.0, 0.0, 0.0, 1.0 };
308 | GLfloat emission2[] = { 1.0, 1.0, 1.0, 1.0 };
309 |
310 | GLfloat light_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
311 | GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
312 | GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
313 | GLfloat light_pos[] = { 0.0, 0.0, 0.0, 1.0 };
314 |
315 |
316 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
317 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
318 | glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
319 |
320 | glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
321 | glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
322 | glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular);
323 | glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
324 | glMaterialfv(GL_FRONT, GL_EMISSION, emission2);
325 |
326 | glEnable(GL_LIGHTING);
327 | glEnable(GL_LIGHT0);
328 |
329 | mover();
330 | glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
331 | planeta(4, 0, 0, 0, 1);
332 | glMaterialfv(GL_FRONT, GL_EMISSION, emission);
333 | for (int i = 0; i < 7; i++)
334 | planeta(radios[i], PO[i][actual[i]].x, PO[i][actual[i]].y, PO[i][actual[i]].z, i + 2);
335 | glPushMatrix();
336 | glDisable(GL_LIGHTING);
337 | glPopMatrix();
338 | glPushMatrix();
339 |
340 |
341 | glPointSize(1 + (rand() % 20) / 10.0);
342 | glColor3f(1, 1, 1);
343 | glBegin(GL_POINTS);
344 | for (int i = 0; i < 100; i++) {
345 | glVertex3f(SistemaParticulas[i].punto.x, SistemaParticulas[i].punto.y, SistemaParticulas[i].punto.z);
346 | SistemaParticulas[i].mover();
347 | if (SistemaParticulas[i].tiempovivo > SistemaParticulas[i].lifespan)
348 | SistemaParticulas[i].recal(punto_cohete);
349 | }
350 | glEnd();
351 | glColor3f(0, 0, 0.15);
352 | for (int i = 0; i < 7; i++) {
353 | glBegin(GL_LINE_LOOP);
354 | for (int j = 0; j < PO[i].size(); j++)
355 | glVertex3f(PO[i][j].x, PO[i][j].y, PO[i][j].z);
356 | glEnd();
357 | }
358 | glPopMatrix();
359 | glColor3f(0.2, 0.1, 0.2);
360 | orbitas_planetas(PC, 4);
361 | vector auxP = PC;
362 | for (int i = 0; i < 5; i++) {
363 |
364 | PC[0].x += (radios[4] * 1.4 + i * 0.2), PC[0].z += (radios[4] * 1.4 + i * 0.2);
365 | PC[3].x += (radios[4] * 1.4 + i * 0.2), PC[3].z -= (radios[4] * 1.4 + i * 0.2);
366 | PC[1].x -= (radios[4] * 1.4 + i * 0.2), PC[1].z += (radios[4] * 1.4 + i * 0.2);
367 | PC[2].x -= (radios[4] * 1.4 + i * 0.2), PC[2].z -= (radios[4] * 1.4 + i * 0.2);
368 | orbitas_planetas(PC, 4);
369 | PC = auxP;
370 | }
371 | glColor3f(0, 0.5, 0.5);
372 | orbitas_planetas(PC2, 5);
373 | PC2[0].x += (radios[5] * 1.5), PC2[0].z += (radios[5] * 1.5);
374 | PC2[3].x += (radios[5] * 1.5), PC2[3].z -= (radios[5] * 1.5);
375 | PC2[1].x -= (radios[5] * 1.5), PC2[1].z += (radios[5] * 1.5);
376 | PC2[2].x -= (radios[5] * 1.5), PC2[2].z -= (radios[5] * 1.5);
377 | orbitas_planetas(PC2, 5);
378 |
379 | glEnable(GL_LIGHTING);
380 | drawAsteroides();
381 | cohete_cub();
382 | glDisable(GL_LIGHTING);
383 |
384 | glColor3f(1, 0, 0);
385 | for (int i = 0; i < 5; i++) {
386 | glPushMatrix();
387 | Point p2 = DibujaBezier(t_, PCE[i]);
388 | glColor3f(0.97, 0.95, 0.17);
389 | estrella(p2, 2);
390 | glPopMatrix();
391 | }
392 | for (int j = 0; j < PuntosEstrellas.size(); j++) {
393 | float r = (rand() % 255) / 255.0, g = (rand() % 255) / 255.0, b = (rand() % 255) / 255.0;
394 | glPushMatrix();
395 | glColor3f(r, g, b);
396 | estrella(PuntosEstrellas[j], 1);
397 | glPopMatrix();
398 | }
399 | for (int i = 0; i < 5; i++) {
400 | glColor3f(1, 1, 1);
401 | glPushMatrix();
402 | glBegin(GL_POINTS);
403 | double tt_ = t_;
404 | for (int iter = 0; iter < 15; iter++) {
405 | tt_ -= dxt;
406 | if (tt_ < 0 || tt_ > 1) continue;
407 | Point p2 = DibujaBezier(tt_, PCE[i]);
408 | glVertex3f(p2.x, p2.y, p2.z);
409 | }
410 | glEnd();
411 | glPopMatrix();
412 | }
413 | glPushMatrix();
414 | cube();
415 | glPopMatrix();
416 | glutSwapBuffers();
417 | }
418 | void specialKeyInput(int key, int x, int y) {
419 | switch (key) {
420 | case GLUT_KEY_LEFT: giray -= 5; break;
421 | case GLUT_KEY_RIGHT: giray += 5; break;
422 | case GLUT_KEY_UP: girax -= 5; break;
423 | case GLUT_KEY_DOWN: girax += 5; break;
424 | case GLUT_KEY_F1: debe[0] = !debe[0]; break;
425 | case GLUT_KEY_F2: debe[1] = !debe[1]; break;
426 | case GLUT_KEY_F3: debe[2] = !debe[2]; break;
427 | case GLUT_KEY_F4: stop = !stop; break;
428 | default: break;
429 | }
430 | glutPostRedisplay();
431 | }
432 | void reshape(int ancho, int alto) {
433 | glClearColor(0, 0, 0, 0.0); //black
434 | glMatrixMode(GL_MODELVIEW);
435 | glLoadIdentity();
436 | glEnable(GL_DEPTH_TEST);
437 | glViewport(0, 0, ancho, alto);
438 | glMatrixMode(GL_PROJECTION);
439 | glLoadIdentity();
440 |
441 | gluPerspective(90, 1, 12, 3000);
442 | }
443 | void calcular() {
444 | double angle = 2 * 3.14 * actual[4] / 300.0;
445 | for (int i = 0; i < 4; i++)
446 | PC[i].setxyz(cos(angle) * 35, 0, sin(angle) * 35);
447 | angle = 2 * 3.14 * actual[5] / 300.0;
448 | for (int i = 0; i < 4; i++)
449 | PC2[i].setxyz(cos(angle) * 40, 0, sin(angle) * 40);
450 | }
451 | void idle(void) {
452 | if (stop)giray += 0.5;
453 | if (t_ + dxt < 0 || t_ + dxt > 1) dxt *= -1;
454 | t_ += dxt;
455 | for (int j = 0; j < 7; j++) actual[j] = (actual[j] + 1) % 301;
456 | calcular();
457 | if (debe[0]) rx += 2.5;
458 | if (debe[1]) ry += 2.5;
459 | if (debe[2]) rz += 2.5;
460 | double angle = 2 * 3.14 * j_cohete / 300.0;
461 | punto_cohete.setxyz(cos(angle) * 30, 8, sin(angle) * 30);
462 | j_cohete += velocidad_cohete;
463 | glutPostRedisplay();
464 | }
465 | void iniciar() {
466 | double angle = 2 * 3.14 * j_cohete / 300.0;
467 | punto_cohete.setxyz(cos(angle) * 30, 8, sin(angle) * 30);
468 | j_cohete++;
469 | glGenTextures(12, texture);
470 | for(char i = 'a'; i <= 'i'; i++)
471 | LoadTextures(i + ".bmp");
472 | LoadTextures("rocket.bmp");
473 |
474 | for (int i = 0; i < 11; i++) nueva_textura(i);
475 | for (int i = 0; i < 100; i++) {
476 | Particula pa(punto_cohete);
477 | SistemaParticulas.push_back(pa);
478 | }
479 | for (int i = 0; i < 50; i++) Asteroides.push_back((3 + rand() % 20) / 50.0);
480 | for (int i = 0; i < 500; i++) {
481 | Point aux;
482 | float x_ = (rand() % 100) * (rand() % 2 ? 1 : -1);
483 | float y_ = (rand() % 100) * (rand() % 2 ? 1 : -1);
484 | float z_ = (rand() % 100) * (rand() % 2 ? 1 : -1);
485 | aux.setxyz(x_, y_, z_);
486 | PuntosEstrellas.push_back(aux);
487 | }
488 |
489 | for (int i = 0, orb = 10; i < 7; i++, orb += 5) {
490 | actual[i] = rand() % 100;
491 | if (orb == 30) orb += 5;
492 | for (int j = 0; j <= 300; j++) {
493 | double angle = 2 * 3.14 * j / 300.0;
494 | Point auxp; auxp.setxyz(cos(angle) * orb, 0, sin(angle) * orb);
495 | PO[i].push_back(auxp);
496 | }
497 | }
498 | calcular();
499 | }
500 | void tecla(unsigned char c, int x, int y) {
501 | c = toupper(c);
502 | if (c == '+') zoom[2]++;
503 | else if (c == '-') zoom[2]--;
504 | else if (c == 'A') zoom[0]++;
505 | else if (c == 'D') zoom[0]--;
506 | else if (c == 'W') zoom[1]--;
507 | else if (c == 'S') zoom[1]++;
508 | else if (c == 'V') velocidad_cohete += 0.2;
509 | else if (c == 'B') velocidad_cohete -= 0.2;
510 | glutPostRedisplay();
511 | }
512 | int main(int argc, char** argv) {
513 |
514 | srand(time(NULL));
515 | for (int j = 0; j < 5; j++)
516 | for (int i = 0; i < 4; i++) {
517 | Point aux;
518 | aux.setxyz(rand() % 50 * (rand() % 2 ? 1 : -1), rand() % 50 * (rand() % 2 ? 1 : -1), rand() % 50 * (rand() % 2 ? 1 : -1));
519 | PCE[j].push_back(aux);
520 | }
521 | trianguloPascal();
522 | glutInit(&argc, argv);
523 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
524 | glutInitWindowPosition(100, 0);
525 | glutInitWindowSize(900, 700);
526 |
527 | glutCreateWindow(".:: Space ::.");
528 |
529 | glutReshapeFunc(reshape);
530 | glutDisplayFunc(display);
531 | glutIdleFunc(idle);
532 | glutSpecialFunc(specialKeyInput);
533 | glutKeyboardFunc(tecla);
534 |
535 | iniciar();
536 | glutMainLoop();
537 | return 0;
538 | }
539 |
--------------------------------------------------------------------------------