Tachyon (current)  Current Main Branch
animspheres.c
Go to the documentation of this file.
1 /*
2  * animspheres.c - This file contains a Tachyon demo program/driver
3  *
4  * (C) Copyright 1994-2022 John E. Stone
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * $Id: animspheres.c,v 1.17 2022/02/18 18:18:36 johns Exp $
8  *
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include "tachyon.h"
15 #include "glwin.h"
16 
17 #if defined(USEOPENGL)
18 #include <GL/gl.h>
19 #endif
20 
21 int rt_mynode(void); /* proto */
22 
23 /* Number of frames to render */
24 #define MAXFRAMES 400
25 
26 /* Number of bouncing spheres */
27 #define NUMSP 16
28 
29 /* NTSC Resolution */
30 /* #define XRES 640
31  #define YRES 480
32 */
33 
34 /* MPEG-1 Resolution */
35 #define XRES 352
36 #define YRES 240
37 
38 #define MAXX 1.0
39 #define MAXY 1.0
40 #define MAXZ 1.0
41 
42 #define MINX -1.0
43 #define MINY -1.0
44 #define MINZ -1.0
45 
46 #define LOOP 200.0
47 #define LOOP2 100.0
48 #define RAD 6.28
49 
50 typedef struct {
55  void * voidtex;
56 } asphere;
57 
59 
60 apiflt randflt(void) {
61  long a;
62  apiflt f;
63 
64  a=rand() % 1000;
65 
66  f=(a*1.0) / 1000.0;
67  return f;
68 }
69 
70 
71 void initspheres(void) {
72  int i;
73  apiflt t1;
74 
75  for (i=0; i<NUMSP; i++) {
76  sp[i].tex.col.r=randflt() / 3.0 + 0.66;
77  sp[i].tex.col.g=randflt() / 3.0 + 0.66;
78  sp[i].tex.col.b=randflt() / 3.0 + 0.66;
79  t1=randflt()*0.9;
80 
81  sp[i].tex.ambient=0.1;
82  sp[i].tex.diffuse=t1;
83  sp[i].tex.specular=0.9 - t1;
84  sp[i].tex.opacity=1.0;
85 
86  sp[i].tex.scale.x=1.0;
87  sp[i].tex.scale.y=1.0;
88  sp[i].tex.scale.z=1.0;
89 
90  sp[i].tex.rot.x=0.0;
91  sp[i].tex.rot.y=0.0;
92  sp[i].tex.rot.z=0.0;
93  sp[i].tex.texturefunc=rand() % 7;
94 
95  sp[i].ctr.x=randflt() * 2.0 - 1.0;
96  sp[i].ctr.y=randflt() * 2.0 - 1.0;
97  sp[i].ctr.z=randflt() * 2.0 - 1.0;
98 
99  sp[i].rad=randflt()*0.5 + 0.05;
100 
101  sp[i].dir.x=randflt() * 0.05 - 0.02;
102  sp[i].dir.y=randflt() * 0.05 - 0.02;
103  sp[i].dir.z=randflt() * 0.05 - 0.02;
104  }
105 }
106 
107 void movesp(void) {
108  int i;
109 
110  for (i=0; i<NUMSP; i++) {
111  sp[i].ctr.x += sp[i].dir.x;
112  sp[i].ctr.y += sp[i].dir.y;
113  sp[i].ctr.z += sp[i].dir.z;
114 
115  if (sp[i].ctr.x > MAXX) {
116  sp[i].ctr.x = MAXX;
117  sp[i].dir.x = -sp[i].dir.x;
118  }
119  if (sp[i].ctr.x < MINX) {
120  sp[i].ctr.x = MINX;
121  sp[i].dir.x = -sp[i].dir.x;
122  }
123 
124  if (sp[i].ctr.y > MAXY) {
125  sp[i].ctr.y = MAXY;
126  sp[i].dir.y = -sp[i].dir.y;
127  }
128  if (sp[i].ctr.y < MINY) {
129  sp[i].ctr.y = MINY;
130  sp[i].dir.y = -sp[i].dir.y;
131  }
132 
133  if (sp[i].ctr.z > MAXZ) {
134  sp[i].ctr.z = MAXZ;
135  sp[i].dir.z = -sp[i].dir.z;
136  }
137  if (sp[i].ctr.z < MINZ) {
138  sp[i].ctr.z = MINZ;
139  sp[i].dir.z = -sp[i].dir.z;
140  }
141  sp[i].tex.ctr.x = sp[i].dir.x;
142  sp[i].tex.ctr.y = sp[i].dir.y;
143  sp[i].tex.ctr.z = sp[i].dir.z;
144  }
145 }
146 
147 void drawsp(SceneHandle scene) {
148  int i;
149  apitexture p1;
150  apivector ct1, n1;
151 
152  for (i=0; i<NUMSP; i++) {
153  sp[i].voidtex = rt_texture(scene, &sp[i].tex);
154  rt_sphere(scene, sp[i].voidtex, sp[i].ctr, sp[i].rad);
155  }
156 
157  p1.col.r=1.0;
158  p1.col.g=1.0;
159  p1.col.b=1.0;
160  p1.ambient=0.1;
161  p1.diffuse=0.5;
162  p1.specular=0.4;
163  p1.opacity=1.0;
164 
165  ct1.x=0.0;
166  ct1.y=-1.10;
167  ct1.z=0.0;
168 
169  n1.x=0.0;
170  n1.y=1.0;
171  n1.z=0.0;
172 
173  rt_plane(scene, rt_texture(scene, &p1), ct1, n1);
174 }
175 
176 int main(int argc, char **argv) {
177  SceneHandle scene;
178  int i, xres, yres, maxframes;
179  apivector Ccenter, Cview, Cup;
180  apivector ctr1, ctr2;
181  apitexture tex1, tex2;
182  void * vtx1, * vtx2;
183  char fname[100];
184  int nosave, opengl;
185  void *glwin = NULL;
186  unsigned char *img = NULL;
187 
188  rt_initialize(&argc, &argv);
189 
190  nosave=0;
191  opengl=0;
192  xres=XRES;
193  yres=XRES;
194  maxframes=MAXFRAMES;
195  for (i=1; i<argc; i++) {
196  if (!strcmp("-res", argv[i])) {
197  if (i+2<argc) {
198  i++;
199  sscanf(argv[i], "%d", &xres);
200  i++;
201  sscanf(argv[i], "%d", &xres);
202  }
203  continue;
204  }
205  if (!strcmp("-frames", argv[i])) {
206  if (i+1<argc) {
207  i++;
208  sscanf(argv[i], "%d", &maxframes);
209  }
210  continue;
211  }
212  if (!strcmp("-nosave", argv[i])) {
213  nosave=1;
214  continue;
215  }
216  if (!strcmp("-opengl", argv[i])) {
217  opengl=1;
218  continue;
219  }
220  }
221 
222  if (opengl) {
223  img = (unsigned char *) calloc(1, xres*yres*3);
224  if (img)
225  glwin = glwin_create(argv[0], xres, yres);
226  }
227 
228  Ccenter.x=0.0; Ccenter.y=0.0; Ccenter.z=-3.0;
229  Cview.x=0.0; Cview.y=0.0; Cview.z=1.0;
230  Cup.x=0.0; Cup.y=1.0; Cup.z=0.0;
231 
232  ctr1.x=20.0; ctr1.y=20.0; ctr1.z=-40.0;
233  ctr2.x=-20.0; ctr2.y=20.0; ctr2.z=-40.0;
234 
235  tex1.col.r=1.0;
236  tex1.col.g=0.5;
237  tex1.col.b=0.0;
238  tex1.ambient=1.0;
239  tex1.opacity=1.0;
240  tex2=tex1;
241  tex2.col.r=0.0;
242  tex2.col.b=1.0;
243 
244  initspheres();
245 
246  for (i=0; i<MAXFRAMES; i++) {
247  scene = rt_newscene();
248  vtx1=rt_texture(scene, &tex1);
249  vtx2=rt_texture(scene, &tex2);
250 
251 
252  if (!nosave) {
253  sprintf(fname,"outfile.%4.4d.tga",i);
254  if (rt_mynode()==0) printf("Rendering: %s\n", fname);
255  rt_outputfile(scene, fname);
256  } else {
257  printf("\rRendering %d... ", i);
258  fflush(stdout);
259  }
260 
261  if (img != NULL)
262  rt_rawimage_rgb24(scene, img);
263 
264  rt_resolution(scene, xres, yres);
265  rt_verbose(scene, 0);
266 
267  rt_camera_setup(scene, 1.0, 1.0, 0, 5, Ccenter, Cview, Cup);
268 
269  movesp();
270  drawsp(scene);
271 
272  rt_light(scene, vtx1, ctr1, 1.0);
273  rt_light(scene, vtx2, ctr2, 1.0);
274 
275  rt_renderscene(scene);
276 
277 #ifdef USEOPENGL
278  if (opengl) {
279  float wscalex, wscaley, wminscale;
280  float wxoffset, wyoffset;
281  int wsx, wsy, instereo, maxx, maxy;
282 
283  glwin_get_wininfo(glwin, &instereo, NULL);
284  glwin_get_winsize(glwin, &wsx, &wsy);
285  maxx=xres;
286  maxy=yres;
287  wscalex = wsx / (float) maxx;
288  wscaley = wsy / (float) maxy;
289  wminscale = (wscalex < wscaley) ? wscalex : wscaley;
290  wxoffset = ((wminscale * maxx) - wsx) / 2.0f;
291  wyoffset = ((wminscale * maxy) - wsy) / 2.0f;
292 
293  glDrawBuffer(GL_BACK);
294  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
295  glClearColor(0.0, 0.0, 0.0, 1.0); /* black */
296  glViewport(0, 0, wsx, wsy);
297  glClear(GL_COLOR_BUFFER_BIT);
298 
299  glShadeModel(GL_FLAT);
300  glViewport((int) -wxoffset, (int) -wyoffset, wsx, wsy);
301  glMatrixMode(GL_PROJECTION);
302  glLoadIdentity();
303  glOrtho(0.0, wsx, 0.0, wsy, -1.0, 1.0); /* flip upside-down image */
304 
305  glMatrixMode(GL_MODELVIEW);
306  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
307  glPixelZoom(wminscale, wminscale); /* flip upside-down Tachyon image */
308 
309  glwin_draw_image(glwin, xres, yres, img);
310  }
311 #endif
312 
313  rt_deletescene(scene);
314 
315 #if 0
316  for (j=0; j<NUMSP; j++)
317  free(sp[i].voidtex);
318 #endif
319  }
320 
321  rt_finalize();
322 
323  if (opengl) {
324  glwin_destroy(glwin);
325  if (img)
326  free(img);
327  }
328 
329  return 0;
330 }
331 
float g
Green color component.
Definition: tachyon.h:61
void * glwin_create(const char *wintitle, int width, int height)
Definition: glwin.c:3315
apivector ctr
Definition: animspheres.c:52
void rt_camera_setup(SceneHandle voidscene, flt zoom, flt aspectratio, int antialiasing, int raydepth, apivector camcent, apivector viewvec, apivector upvec)
Define a camera for a perspective projection, given the specified zoom factor, aspect ratio...
Definition: api.c:229
float r
Red color component.
Definition: tachyon.h:60
void * rt_texture(SceneHandle sc, apitexture *apitex)
Translate a texture definition into the internal format used by Tachyon, and returns an opaque pointe...
Definition: api.c:933
apicolor col
base object color
Definition: tachyon.h:67
void drawsp(SceneHandle scene)
Definition: animspheres.c:147
apivector rot
rotation of texture around origin
Definition: tachyon.h:74
void rt_resolution(SceneHandle voidscene, int hres, int vres)
Set the horizontal and vertical resolution (in pixels) for the specified scene.
Definition: api.c:372
void glwin_destroy(void *voidhandle)
Definition: glwin.c:3319
flt diffuse
diffuse reflection
Definition: tachyon.h:70
flt opacity
how opaque the object is
Definition: tachyon.h:72
void glwin_draw_image(void *voidhandle, int xsize, int ysize, unsigned char *img)
Definition: glwin.c:3359
flt specular
specular reflection
Definition: tachyon.h:71
#define MAXY
Definition: animspheres.c:39
asphere sp[NUMSP]
Definition: animspheres.c:58
void movesp(void)
Definition: animspheres.c:107
apivector ctr
origin of texture
Definition: tachyon.h:73
#define MINX
Definition: animspheres.c:42
void * voidtex
Definition: animspheres.c:55
void rt_renderscene(SceneHandle voidscene)
Render the current scene.
Definition: api.c:180
void rt_sphere(SceneHandle scene, void *tex, apivector ctr, flt rad)
Define a sphere with associated texture, center, and radius.
Definition: api.c:1212
#define MINZ
Definition: animspheres.c:44
apiflt rad
Definition: animspheres.c:53
int glwin_get_winsize(void *voidhandle, int *xsize, int *ysize)
Definition: glwin.c:3335
#define MAXFRAMES
Definition: animspheres.c:24
void initspheres(void)
Definition: animspheres.c:71
apitexture tex
Definition: animspheres.c:51
int rt_initialize(int *argc, char ***argv)
Initialize Tachyon library, must be first Tachyon API called.
Definition: api.c:70
flt x
X coordinate or direction component.
Definition: tachyon.h:54
int rt_mynode(void)
distributed memory parallel node rank
Definition: api.c:49
int main(int argc, char **argv)
Definition: animspheres.c:176
flt apiflt
for backward compatibility
Definition: tachyon.h:49
flt y
Y coordinate or direction component.
Definition: tachyon.h:55
apivector scale
scale of texture in x,y,z
Definition: tachyon.h:75
SceneHandle rt_newscene(void)
Allocate, initialize, and return a handle for a new scene.
Definition: api.c:698
void rt_rawimage_rgb24(SceneHandle voidscene, unsigned char *img)
Have Tachyon save the output image in the specified memory area, in raw 24-bit, packed, pixel interleaved, unsigned RGB bytes.
Definition: api.c:419
void rt_outputfile(SceneHandle voidscene, const char *outname)
Set the filename for the output image for the specified scene.
Definition: api.c:350
void rt_deletescene(SceneHandle voidscene)
Destroy and deallocate the specified scene.
Definition: api.c:784
#define MAXZ
Definition: animspheres.c:40
#define NUMSP
Definition: animspheres.c:27
void * SceneHandle
Definition: tachyon.h:51
apiflt randflt(void)
Definition: animspheres.c:60
int texturefunc
which texture function to use
Definition: tachyon.h:66
flt ambient
ambient lighting
Definition: tachyon.h:69
#define XRES
Definition: animspheres.c:35
#define MAXX
Definition: animspheres.c:38
void rt_finalize(void)
Shut down Tachyon library for good, at final use before program termination.
Definition: api.c:153
#define MINY
Definition: animspheres.c:43
float b
Blue color component.
Definition: tachyon.h:62
apivector dir
Definition: animspheres.c:54
void rt_plane(SceneHandle scene, void *tex, apivector ctr, apivector norm)
Define a plane.
Definition: api.c:1186
Tachyon public API function prototypes and declarations used to drive the ray tracing engine...
void * rt_light(SceneHandle voidscene, void *tex, apivector ctr, flt rad)
Define a point light with associated texture, position, and radius.
Definition: api.c:1049
int glwin_get_wininfo(void *voidhandle, int *instereo, int *havestencil)
Definition: glwin.c:3331
flt z
Z coordinate or direction component.
Definition: tachyon.h:56
void rt_verbose(SceneHandle voidscene, int v)
Enables or Disables verbose messages from the Tachyon library during rendering.
Definition: api.c:414