-
Notifications
You must be signed in to change notification settings - Fork 22
/
VEX_Ocean.C
266 lines (218 loc) · 7.95 KB
/
VEX_Ocean.C
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Yo emacs, this is -*- c++ -*- code.
//
// VEX_Ocean.cpp - Vex functions for building Ocean waves (see
// Ocean.h for more details).
//
// March 2005.
//
// $Id: SOP_Ocean.C 132 2005-08-23 04:56:06Z drw900 $
//
// Houdini Ocean Toolkit
// Copyright (C) 2005 Drew Whitehouse, ANU Supercomputer Facility
// 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
//
// Modified by Hans Joergen Kjaernet <[email protected]>, 2012.03.12, to work in H12.
// The modification is rather small and based upon pieces of code from Christian Schnellhammers multithreaded hot port to H12.
// This modification is not multi-threaded.
#include <limits.h>
#include <string>
#include "Ocean.h"
#include <UT/UT_DSOVersion.h>
#include <CMD/CMD_Manager.h>
#include <CMD/CMD_Args.h>
#include <VEX/VEX_VexOp.h>
#include <OP/OP_Director.h>
#include <UT/UT_Math.h>
#include <UT/UT_Interrupt.h>
#include <GU/GU_Detail.h>
#include <GU/GU_PrimPoly.h>
#include <CH/CH_LocalVariable.h>
#include <PRM/PRM_Include.h>
#include <OP/OP_Operator.h>
#include <OP/OP_OperatorTable.h>
//
// vex functions implementing Tessendorf's ocean model
//
struct OceanHolder
{
drw::Ocean *ocean;
drw::OceanContext *context;
float normalize_factor;
float now;
OceanHolder() : ocean(0),context(0)
{
// nothing
}
~OceanHolder()
{
if (ocean) delete ocean;
if (context) delete context;
}
};
static void*
ocean_init()
{
OceanHolder *data = new OceanHolder;
//std::cout << "Ocean VEX Init() " << std::endl << std::flush;
return reinterpret_cast<void*>(data);
}
static void
ocean_cleanup(void* data)
{
//std::cout << "Ocean VEX Cleanup()\n" << std::flush; // never called ?
if (data)
{
delete reinterpret_cast<OceanHolder*>(data);
}
}
static drw::Ocean*
ocean_from_argv(void *argv[])
{
// consumes "IFFFFFFFI" ...
int res = 1 << *(int *)argv[0]; // I
float size = *(float *)argv[1]; // F
float V = *(float *)argv[2]; // F
float l = *(float *)argv[3]; // F
float w = *(float *)argv[4]; // F
float damp = *(float *)argv[5]; // F
float align= *(float *)argv[6]; // F
float depth= *(float *)argv[7]; // F
int seed = *(int *)argv[8]; // I
return new drw::Ocean(res,res,size/float(res),size/float(res),
V,l,0.000001,UTdegToRad(w),
1-damp,align,depth,seed);
}
static void
ocean_eval_ij(int, void *argv[], void *data)
{
OceanHolder *oh = reinterpret_cast<OceanHolder*>(data);
int i = *(int *)argv[0]; // I
int j = *(int *)argv[1]; // I
float now = *(float *)argv[2]; // F
float height_scale = *(float *)argv[3]; // F
int do_chop = *(int *)argv[4]; // I
float chop_amount = *(float *)argv[5]; // F
float *displacement = (float *)argv[6]; // &V
int do_normal = *(int*)argv[7]; // I
float *normal = (float*)argv[8]; // &V
int do_jacobian = *(int*)argv[9]; // I
float *Jminus = (float*)argv[10]; // &F
float *Jplus = (float*)argv[11]; // &F
float *Eminus = (float*)argv[12]; // &V
float *Eplus = (float*)argv[13]; // &V
if (!oh->ocean)
{
oh->ocean = ocean_from_argv(argv+14);
oh->normalize_factor = oh->ocean->get_height_normalize_factor();
oh->context = oh->ocean->new_context(true,do_chop,do_normal,do_jacobian);
oh->ocean->update (now,*oh->context, true,do_chop,do_normal,do_jacobian,
height_scale * oh->normalize_factor,chop_amount);
oh->now = now;
}
if ( oh->now != now ) {
oh->ocean->update (now,*oh->context, true,do_chop,do_normal,do_jacobian,
height_scale * oh->normalize_factor,chop_amount);
oh->now = now;
}
oh->context->eval_ij(i,j);
displacement[0] = oh->context->disp[0];
displacement[1] = oh->context->disp[1];
displacement[2] = oh->context->disp[2];
if (do_normal)
{
normal[0] = oh->context->normal[0];
normal[1] = oh->context->normal[1];
normal[2] = oh->context->normal[2];
}
if (do_jacobian)
{
*Jminus = oh->context->Jminus;
*Jplus = oh->context->Jplus;
Eminus[0] = oh->context->Eminus[0];
Eminus[1] = oh->context->Eminus[1];
Eminus[2] = oh->context->Eminus[2];
Eplus[0] = oh->context->Eplus[0];
Eplus[1] = oh->context->Eplus[1];
Eplus[2] = oh->context->Eplus[2];
}
}
static void
ocean_eval(int, void *argv[], void *data)
{
OceanHolder *oh = reinterpret_cast<OceanHolder*>(data);
float x = *(float *)argv[0]; // F
float z = *(float *)argv[1]; // F
float now = *(float *)argv[2]; // F
float height_scale = *(float *)argv[3]; // F
int do_chop = *(int *)argv[4]; // I
float chop_amount = *(float *)argv[5]; // F
float *displacement = (float *)argv[6]; // &V
int do_normal = *(int*)argv[7]; // I
float *normal = (float*)argv[8]; // &V
int do_jacobian = *(int*)argv[9]; // I
float *Jminus = (float*)argv[10]; // &F
float *Jplus = (float*)argv[11]; // &F
float *Eminus = (float*)argv[12]; // &V
float *Eplus = (float*)argv[13]; // &V
if (!oh->ocean)
{
oh->ocean = ocean_from_argv(argv+14);
oh->normalize_factor = oh->ocean->get_height_normalize_factor();
oh->context = oh->ocean->new_context(true,do_chop,do_normal,do_jacobian);
oh->ocean->update (now,*oh->context, true,do_chop,do_normal,do_jacobian,
height_scale * oh->normalize_factor,chop_amount);
oh->now = now;
}
if ( oh->now != now ) {
oh->ocean->update (now,*oh->context, true,do_chop,do_normal,do_jacobian,
height_scale * oh->normalize_factor,chop_amount);
oh->now = now;
}
// We always choose the catmull rom version here, the linear
// option is primarily for the SOP where realtime feedback is more
// important.
oh->context->eval2_xz(x,z);
displacement[0] = oh->context->disp[0];
displacement[1] = oh->context->disp[1];
displacement[2] = oh->context->disp[2];
if (do_normal)
{
normal[0] = oh->context->normal[0];
normal[1] = oh->context->normal[1];
normal[2] = oh->context->normal[2];
}
if (do_jacobian)
{
*Jminus = oh->context->Jminus;
*Jplus = oh->context->Jplus;
Eminus[0] = oh->context->Eminus[0];
Eminus[1] = oh->context->Eminus[1];
Eminus[2] = oh->context->Eminus[2];
Eplus[0] = oh->context->Eplus[0];
Eplus[1] = oh->context->Eplus[1];
Eplus[2] = oh->context->Eplus[2];
}
}
void
newVEXOp(void *)
{
new VEX_VexOp("ocean_eval_ij@IIFFIF&VI&VI&F&F&V&VIFFFFFFFI",
ocean_eval_ij,
VEX_ALL_CONTEXT,
ocean_init,ocean_cleanup);
new VEX_VexOp("ocean_eval@FFFFIF&VI&VI&F&F&V&VIFFFFFFFI",
ocean_eval,
VEX_ALL_CONTEXT,
ocean_init,ocean_cleanup);
}