Debian Codegraph @

plugins/actor/pseudotoad_flower/main.c

raw
1 /* Libvisual-plugins - Standard plugins for libvisual
2 *
3 * Copyright (C) 2004, 2005, 2006 Antti Silvast <[email protected]>
4 *
5 * Authors: Antti Silvast <[email protected]>
6 * Dennis Smit <[email protected]>
7 *
8 * $Id: main.c,v 1.3 2006/01/22 13:25:26 synap Exp $
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include <config.h>
26
27 #include <stdio.h>
28 #include <math.h>
29 #include <stdlib.h>
30
31 #ifdef HAVE_GL_GL_H
32 # include <GL/gl.h>
33 #elif defined(HAVE_OPENGL_GL_H)
34 # include <OpenGL/gl.h>
35 #else
36 # error neither GL/gl.h nor OpenGL/gl.h available
37 #endif
38
39 #if defined(HAVE_GL_GLU_H)
40 # include <GL/glu.h>
41 #elif defined(HAVE_OPENGL_GLU_H)
42 # include <OpenGL/glu.h>
43 #else
44 # error neither GL/glu.h nor OpenGL/glu.h available
45 #endif
46
47 #include "main.h"
48
49 void set_vector (Vect v, float x, float y, float z)
50 {
51 v[0] = x;
52 v[1] = y;
53 v[2] = z;
54 }
55
56 int init_flower(FlowerInternal *flower)
57 {
58 Vect *v;
59
60 flower->tension = 0.0;
61 flower->continuity = 0.0;
62 flower->bias = 0.0;
63 flower->tension_new = 0.0;
64 flower->continuity_new = 0.0;
65 flower->bias_new = 0.0;
66
67 flower->rotx = -40.0;
68 flower->roty = 0.0;
69 flower->spd = 1.0;
70 flower->posz = 0.0;
71 flower->posz_new = 0.0;
72 flower->audio_strength = 30.0;
73
74 v = flower->kukka;
75
76 set_vector (v[0], 0.0, 0.5, 0.0);
77 set_vector (v[1], 0.0, 0.0, 0.0);
78 set_vector (v[2], 0.1,-0.2, 0.0);
79 set_vector (v[3], 0.3,-0.3, 0.0);
80 set_vector (v[4], 0.5,-0.3, 0.0);
81 set_vector (v[5], 0.6,-0.2, 0.0);
82 set_vector (v[6], 0.6, 0.5, 0.0);
83
84 v = flower->kukka_morph;
85
86 set_vector (v[0], 0.0, 0.5, 0.0);
87 set_vector (v[1], 0.0, 0.0, 0.0);
88 set_vector (v[2], 0.2,-0.2, 0.0);
89 set_vector (v[3], 0.3,-0.15, 0.0);
90 set_vector (v[4], 0.2, 0.0, 0.0);
91 set_vector (v[5], 0.1, 0.0, 0.0);
92 set_vector (v[6], 0.0, 0.1, 0.0);
93
94 return 0;
95 }
96
97 void splineTCP(FlowerInternal *flower, float u, Vect * control, Vect * result)
98 {
99
100 (*result)[0] = (2*u*u*u - 3*u*u + 1)*control[1][0]
101 + (-2*u*u*u + 3*u*u)*control[2][0]
102 + (u*u*u - 2*u*u + u)*(0.5*(1-flower->tension)*((1+flower->bias)*(1-flower->continuity)*(control[1][0]-control[0][0])
103 + (1-flower->bias)*(1+flower->continuity)*(control[2][0]-control[1][0])))
104 + (u*u*u - u*u)*(0.5*(1-flower->tension)*((1+flower->bias)*(1+flower->continuity)*(control[2][0]-control[1][0])
105 + (1-flower->bias)*(1-flower->continuity)*(control[3][0]-control[2][0])));
106
107 (*result)[1]
108 = (2*u*u*u - 3*u*u + 1)*control[1][1]
109 + (-2*u*u*u + 3*u*u)*control[2][1]
110 + (u*u*u - 2*u*u + u)*(0.5*(1-flower->tension)*((1+flower->bias)*(1-flower->continuity)*(control[1][1]-control[0][1])
111 + (1-flower->bias)*(1+flower->continuity)*(control[2][1]-control[1][1])))
112 + (u*u*u - u*u)*(0.5*(1-flower->tension)*((1+flower->bias)*(1+flower->continuity)*(control[2][1]-control[1][1])
113 + (1-flower->bias)*(1-flower->continuity)*(control[3][1]-control[2][1])));
114
115 // printf ("FUCKERS %f %f %f %f %f, %f %f %f\n", *result[0], *result[1], control[1][0], control[1][1], u,
116 // flower->tension, flower->bias, flower->continuity);
117 }
118
119 void lights(FlowerInternal *flower) {
120 GLfloat light_position[]={10.0,9.0,-12.0,0.0};
121 GLfloat light_diffuse[]={0.8,0.7,0.7,0.0};
122 GLfloat light_specular[]={0.85,0.85,0.8,0.0};
123 GLfloat light_ambient[]={0.0,0.0,0.0,0.0};
124
125 GLfloat mat_diffuse[]={1.0,1.0,1.0,0.0};
126 GLfloat mat_specular[]={1.0,1.0,1.0,0.0};
127 GLfloat mat_ambient[]={0.48,0.45,0.4,0.0};
128 GLfloat mat_emission[] = { 0.00, 0.00, 0.00, 1.0 };
129 GLfloat mat_shininess[] = { 18.0 };
130
131 glMaterialfv(GL_FRONT, GL_EMISSION,mat_emission);
132 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
133 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
134 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
135 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
136
137 glMaterialfv(GL_BACK, GL_EMISSION,mat_emission);
138 glMaterialfv(GL_BACK, GL_AMBIENT, mat_ambient);
139 glMaterialfv(GL_BACK, GL_SPECULAR, mat_specular);
140 glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffuse);
141 glMaterialfv(GL_BACK, GL_SHININESS, mat_shininess);
142
143 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
144 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
145 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
146 glLightfv(GL_LIGHT0,GL_POSITION,light_position);
147 glEnable(GL_LIGHT0);
148
149 }
150
151 void spline3DMorph(FlowerInternal *flower, float factor, float poikkeama) {
152 #define NBTW 12
153 int i,j,k;
154 int size=sizeof(flower->kukka)/sizeof(Vect);
155
156 float vnyt,vnex;
157 float l;
158 float ti=visual_timer_elapsed_msecs(&flower->timer);
159 Vect r,r_morph,n;
160 float rf[17*NBTW*3];
161 int c;
162 c=0;
163 for (i=0; i<size-3; i++) {
164 for (j=0; j<NBTW; j++) {
165 splineTCP(flower, (float)j/NBTW, &flower->kukka[i], &r);
166 splineTCP(flower, (float)j/NBTW, &flower->kukka_morph[i], &r_morph);
167 for (k=0; k<3; k++)
168 rf[c*3+k]=(1.0-factor)*r[k]+factor*r_morph[k];
169
170 rf[c*3+2]=sin(c*M_PI/(NBTW*4))*0.07;
171 c+=1;
172
173 }
174 }
175 for (i=0; i<(c-1); i++) {
176 vnyt=(float)i/(float)(c-1)*(float)(size-3);
177 vnex=(float)(i+1.0)/(float)(c-1)*(float)(size-3);
178
179 rf[i*3]+=+sin((vnyt*2+ti*0.006))*0.02*poikkeama;
180 rf[i*3+1]+=+sin((vnyt*2+ti*0.006))*0.02*poikkeama;
181 rf[i*3+3]+=sin((vnex*2.1+ti*0.006))*0.02*poikkeama;
182 rf[i*3+3+1]+=+sin((vnex*2.0+ti*0.006))*0.02*poikkeama;
183
184 n[1]=-(rf[(i+1)*3]-rf[i*3]);
185 n[0]=(rf[(i+1)*3+2]-rf[i*3+2]);
186 n[2]=rf[(i+1)*3+1]-rf[i*3+1];
187 l=sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
188 n[0]/=l;
189 n[1]/=l;
190 n[2]/=l;
191
192 glEnable(GL_LIGHTING);
193 glColor3f(1.0,1.0,1.0);
194 glPolygonOffset(3.0,2.0);
195 glEnable(GL_POLYGON_OFFSET_FILL);
196 glBegin(GL_POLYGON);
197 glNormal3fv((GLfloat *)n);
198 glVertex3f(1.0*rf[i*3],-rf[i*3+2],rf[i*3+1]);
199 glNormal3fv((GLfloat *)n);
200 glVertex3f(1.0*rf[i*3+3],-rf[i*3+3+2],rf[i*3+3+1]);
201 glNormal3fv((GLfloat *)n);
202 glVertex3f(1.0*rf[i*3+3],+rf[i*3+3+2],rf[i*3+3+1]);
203 glNormal3fv((GLfloat *)n);
204 glVertex3f(1.0*rf[i*3],+rf[i*3+2],rf[i*3+1]);
205 glEnd();
206
207 glEnable(GL_BLEND);
208 glBlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
209 glDisable(GL_BLEND);
210 glPolygonOffset(-1.0,-5.0);
211 glEnable(GL_POLYGON_OFFSET_FILL);
212 glLineWidth(2.0);
213 glDisable(GL_LIGHTING);
214 glColor3f(0.0,0.0,0.0);
215 glBegin(GL_LINE_LOOP);
216 glVertex3f(1.0*rf[i*3],-rf[i*3+2],rf[i*3+1]*1);
217 glVertex3f(1.0*rf[i*3+3],-rf[i*3+3+2],rf[i*3+3+1]*1);
218 glVertex3f(1.0*rf[i*3+3],+rf[i*3+3+2],rf[i*3+3+1]*1);
219 glVertex3f(1.0*rf[i*3],+rf[i*3+2],rf[i*3+1]*1);
220 glEnd();
221 glEnable(GL_LIGHTING);
222 glEnable(GL_DEPTH_TEST);
223 }
224 }
225
226
227 #define F 0.006
228 #define FR 0.006
229
230 void render_flower(FlowerInternal *flower) {
231 int t;
232 int time=visual_timer_elapsed_msecs(&flower->timer);
233
234 flower->tension=F*flower->tension_new+(1.0-F)*flower->tension;
235 flower->continuity=F*flower->continuity_new+(1.0-F)*flower->continuity;
236 flower->bias=F*flower->bias_new+(1.0-F)*flower->bias;
237
238 for (t=0; t<12; t++) {
239 glRotatef(360.0/12.0,0.0,0.0,1.0);
240 spline3DMorph(flower, sin(time*0.001*flower->spd)*0.5+0.5,
241 6.0*flower->audio_bars[(t*8) % 32]*flower->audio_strength
242
243 );
244 }
245
246
247 }
248
249 float col1[]={1,1,1};
250
251 void render_flower_effect(FlowerInternal *flower) {
252
253 glClearColor(col1[0],col1[1],col1[2],0.0);
254 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
255 glMatrixMode (GL_PROJECTION);
256 glLoadIdentity ();
257 gluPerspective(60.0, (float)flower->width/(float)flower->height, 0.01, 1135.0);
258
259
260 glMatrixMode (GL_MODELVIEW);
261 glLoadIdentity ();
262 glTranslatef(0.0,0.0,-1.0);
263
264 lights(flower);
265 glPopMatrix();
266 flower->posz=FR*flower->posz_new+(1.0-FR)*flower->posz;
267 glTranslatef(0.0,0.0,-flower->posz);
268
269 glRotatef(flower->rotx,1.0,0.0,0.0);
270 glRotatef(flower->roty,0.0,1.0,0.0);
271 glRotatef(visual_timer_elapsed_msecs(&flower->timer)*0.02,0.0,0.0,1.0);
272 render_flower(flower);
273 }