added summerhack
[summerhack] / src / parts / dist.cpp
1 #include <iostream>
2 #include "dist.hpp"
3
4 using namespace std;
5
6 static Vertex *varray;
7 static Index *iarray;
8 static TexCoord *tc;
9
10 static const int subd = 50;
11 static const int vsz = subd + 2;
12 static int vcount, icount;
13
14 DistFx::DistFx() : Part("distort") {
15         set_clear(false);
16
17         TriMesh mesh;
18         create_plane(&mesh, Vector3(0, 0, -1), Vector2(1.0, 1.0), subd);
19
20         vcount = mesh.get_vertex_array()->get_count();
21         icount = mesh.get_index_array()->get_count();
22
23         varray = new Vertex[vcount];
24         Vertex *vptr = varray;
25         for(int i=0; i<vcount; i++) {
26                 *vptr = mesh.get_vertex_array()->get_data()[i];
27                 vptr->pos += Vector3(0.5, 0.5);
28                 vptr++;
29         }
30
31         iarray = new Index[icount];
32         Index *iptr = iarray;
33         for(int i=0; i<icount; i++) {
34                 *iptr++ = mesh.get_index_array()->get_data()[i];
35         }
36
37         tc = new TexCoord[vcount];
38         TexCoord *tptr = tc;
39         for(int y=0; y<vsz; y++) {
40                 for(int x=0; x<vsz; x++) {
41                         *tptr++ = TexCoord((float)x / (float)(vsz - 1), 1.0 - (float)y / (float)(vsz - 1));
42                 }
43         }
44         
45 }
46
47 DistFx::~DistFx() {
48         delete [] varray;
49         delete [] iarray;
50 }
51
52 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
53 #define MAX(a, b)       ((a) > (b) ? (a) : (b))
54 #define CLAMP(x, a, b)  MIN(MAX(x, a), b)
55
56 void DistFx::draw_part() {
57         // update
58         float t = (float)time / 1000.0;
59         float cost = cos(t * 2.0);
60         float sint = sin(t * 2.0);
61         
62         Vertex *vptr = varray;
63         for(int y=0; y<vsz; y++) {
64                 for(int x=0; x<vsz; x++) {
65                         float u = (float)x / (float)(vsz - 1);
66                         float v = (1.0 - (float)y / (float)(vsz - 1));
67
68                         float pu = u * 4.0;
69                         float pv = v * 4.0;
70
71                         float du = sin(pu + cost) * 10.0 +
72                                                 cos((pu + sint) * 2.0) * 5.0 +
73                                                 sin((pu + cost) * 3.0) * 3.33 + 
74                                                 cos((pv + sint) * 2.0) * 5.0 +
75                                                 sin(pv + cost) * 10.0;
76
77                         float dv = cos(pv + sint) * 10.0 +
78                                                 sin((pv + cost) * 2.0) * 5.0 +
79                                                 cos((pu + sint) * 3.0) * 3.33 +
80                                                 sin((pv + cost) * 2.0) * 6.0;
81
82                         du *= sin(t / 2.0) * 0.003 + 0.003;
83                         dv *= sin(t / 2.0) * 0.003 + 0.003;
84
85                         vptr->tex[0].u = CLAMP(u + du, 0.0, 1.0);
86                         vptr->tex[0].v = CLAMP(v + dv, 0.0, 1.0);
87                         vptr++;
88                 }
89         }
90         
91         // draw
92         set_matrix(XFORM_TEXTURE, Matrix4x4::identity_matrix);
93         load_xform_matrices();
94
95         glMatrixMode(GL_MODELVIEW);
96         glLoadIdentity();
97         glMatrixMode(GL_PROJECTION);
98         glLoadIdentity();
99         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
100
101         set_lighting(false);
102         set_zbuffering(false);
103         set_backface_culling(false);
104
105         set_texture(0, dsys::tex[0]);
106         enable_texture_unit(0);
107
108         glBegin(GL_TRIANGLES);
109         glColor4f(1.0, 1.0, 1.0, 0.5);
110         for(int i=0; i<icount; i++) {
111                 Vertex *vptr = &varray[iarray[i]];
112                 glTexCoord2f(tc[iarray[i]].u, tc[iarray[i]].v);
113                 glVertex3f(vptr->pos.x, vptr->pos.y, vptr->pos.z);
114         }
115         glEnd();
116
117         set_alpha_blending(true);
118         set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
119
120         glBegin(GL_TRIANGLES);
121         glColor4f(1.0, 1.0, 1.0, 0.7);
122         for(int i=0; i<icount; i++) {
123                 Vertex *vptr = &varray[iarray[i]];
124                 glTexCoord2f(vptr->tex[0].u, vptr->tex[0].v);
125                 glVertex3f(vptr->pos.x, vptr->pos.y, vptr->pos.z);
126         }
127         glEnd();
128
129         set_alpha_blending(false);
130
131         disable_texture_unit(0);
132         
133         set_lighting(true);
134         set_zbuffering(true);
135         set_backface_culling(true);
136 }