Branch
Hash :
94046104
Author :
Thomas de Grivel
Date :
2020-02-24T15:19:27
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
/*
This is part of Patchwork13! the real-time data synthesis toolkit.
http://patchwork13.sourceforge.net/
pw13_std - the patchwork13 standard patches library
Copyright (C) 2005 Bruno Malaquin <broen.42@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
vbo_cube.pw13p.c - SDL opengl bezier patch
patch_class_LDFLAGS = -lm
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <math.h>
#include <pw13/pw13.h>
#include <std/bool.pw13dt.h>
typedef struct point_3d {
GLfloat x, y, z;
} POINT_3D;
POINT_3D grid[4][4];
GLuint dlB;
int divs = 7;
POINT_3D pointAdd(POINT_3D p, POINT_3D q)
{
p.x += q.x;
p.y += q.y;
p.z += q.z;
return p;
}
POINT_3D pointTimes (double c, POINT_3D p)
{
p.x *= c;
p.y *= c;
p.z *= c;
return p;
}
POINT_3D makePoint (double a, double b, double c)
{
POINT_3D p;
p.x = a;
p.y = b;
p.z = c;
return p;
}
void initBezier( void ) {
grid[0][0] = makePoint(-0.75, -0.75, -0.50);
grid[0][1] = makePoint(-0.25, -0.75, 0.00);
grid[0][2] = makePoint( 0.25, -0.75, 0.00);
grid[0][3] = makePoint( 0.75, -0.75, -0.50);
grid[1][0] = makePoint(-0.75, -0.25, -0.75);
grid[1][1] = makePoint(-0.25, -0.25, 0.50);
grid[1][2] = makePoint( 0.25, -0.25, 0.50);
grid[1][3] = makePoint( 0.75, -0.25, -0.75);
grid[2][0] = makePoint(-0.75, 0.25, 0.00);
grid[2][1] = makePoint(-0.25, 0.25, -0.50);
grid[2][2] = makePoint( 0.25, 0.25, -0.50);
grid[2][3] = makePoint( 0.75, 0.25, 0.00);
grid[3][0] = makePoint(-0.75, 0.75, -0.50);
grid[3][1] = makePoint(-0.25, 0.75, -1.00);
grid[3][2] = makePoint( 0.25, 0.75, -1.00);
grid[3][3] = makePoint( 0.75, 0.75, -0.50);
}
POINT_3D Bernstein(float u, POINT_3D *p)
{
POINT_3D a, b, c, d, r;
a = pointTimes(pow(u,3), p[0]);
b = pointTimes(3*pow(u,2)*(1-u), p[1]);
c = pointTimes(3*u*pow((1-u),2), p[2]);
d = pointTimes(pow((1-u),3), p[3]);
r = pointAdd(pointAdd(a, b), pointAdd(c, d));
return r;
}
GLuint genBezier( int divs)
{
int u = 0, v;
float py, px, pyold;
GLuint drawlist = glGenLists(1);
POINT_3D temp[4];
POINT_3D *last = (POINT_3D*)malloc(sizeof(POINT_3D)*(divs+1));
temp[0] = grid[0][3];
temp[1] = grid[1][3];
temp[2] = grid[2][3];
temp[3] = grid[3][3];
for (v=0;v<=divs;v++) {
px = ((float)v)/((float)divs);
last[v] = Bernstein(px, temp);
}
glNewList(drawlist, GL_COMPILE);
for (u=1;u<=divs;u++) {
py = ((float)u)/((float)divs);
pyold = ((float)u-1.0f)/((float)divs);
temp[0] = Bernstein(py, grid[0]);
temp[1] = Bernstein(py, grid[1]);
temp[2] = Bernstein(py, grid[2]);
temp[3] = Bernstein(py, grid[3]);
glBegin(GL_TRIANGLE_STRIP);
for (v=0;v<=divs;v++) {
px = ((float)v)/((float)divs);
glVertex3d(last[v].x, last[v].y, last[v].z);
last[v] = Bernstein(px, temp);
glVertex3d(last[v].x, last[v].y, last[v].z);
}
glEnd();
}
glEndList();
free(last);
return drawlist;
}
static void pw13_sdl_bezier_pump(Pw13_Patch *p, Pw13_Time *time)
{
int i,j;
pw13_pump_sources (p, time);
glCallList (dlB);
glColor3f(1.0f,0.0f,0.0f);
if ( pw13_bool_input_val(p->input+1) ) {
for(i=0;i<4;i++) {
glBegin(GL_LINE_STRIP);
for(j=0;j<4;j++)
glVertex3d(grid[i][j].x, grid[i][j].y, grid[i][j].z);
glEnd();
}
for(i=0;i<4;i++) {
glBegin(GL_LINE_STRIP);
for(j=0;j<4;j++)
glVertex3d(grid[j][i].x, grid[j][i].y, grid[j][i].z);
glEnd();
}
}
glColor3f(0.0f,0.0f,1.0f);
}
static int pw13_sdl_bezier_start(Pw13_Patch *p, Pw13_Time *time)
{
p = p;
time = time;
initBezier();
dlB = genBezier(divs);
return 1;
}
static void pw13_sdl_bezier_stop (Pw13_Patch *p)
{
p=p;
//TODO: CLEAN UP !?!!!
}
PW13_EXPORT
int pw13_dl_patch_class_init (const char *name, Pw13_Patch *p, void *param)
{
param = param;
pw13_patch_init (name, p, 2, 1);
pw13_void_input ("()", p->input, p);
pw13_bool_input ("grid", p->input+1, p, 1);
pw13_void_output ("()", p->output, p);
p->pump = pw13_sdl_bezier_pump;
p->start = pw13_sdl_bezier_start;
p->stop = pw13_sdl_bezier_stop;
return 1;
}