Tachyon (current)  Current Main Branch
animspheres2.c
Go to the documentation of this file.
1 /*
2  * animspheres2.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: animspheres2.c,v 1.12 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 <math.h>
15 #include "tachyon.h"
16 
17 #define XRES 352
18 #define YRES 240
19 #define NUMSP 12
20 
21 #define MYPI 3.1415926
22 #define MYTPI 6.282
23 
24 int rt_mynode(void);
25 
26 #define MAXFRAMES 400
27 #define MAXX 1.0
28 #define MAXY 1.0
29 #define MAXZ 1.0
30 
31 #define MINX -1.0
32 #define MINY -1.0
33 #define MINZ -1.0
34 
35 #define LOOP 200.0
36 #define LOOP2 100.0
37 #define RAD 6.28
38 
39 #ifdef cube
40 #define RFILE "/cfs/johns/anim/frame"
41 #endif
42 
43 #ifndef cube
44 #define RFILE "outfile"
45 #endif
46 
47 
48 typedef struct {
49  apitexture tex;
50  apivector ctr;
51  apiflt rad;
52  apivector dir;
53 } asphere;
54 
56 
57 apiflt randflt(void) {
58  long a;
59  apiflt f;
60 
61  a=rand() % 1000;
62 
63  f=(a*1.0) / 1000.0;
64  return f;
65 }
66 
67 
68 void initspheres(void) {
69  int i;
70  apiflt t1;
71 
72  for (i=0; i<NUMSP; i++) {
73  sp[i].tex.col.r=randflt() / 3.0 + 0.66;
74  sp[i].tex.col.g=randflt() / 3.0 + 0.66;
75  sp[i].tex.col.b=randflt() / 3.0 + 0.66;
76  t1=randflt()*0.9;
77 
78  sp[i].tex.ambient=0.1;
79  sp[i].tex.diffuse=t1;
80  sp[i].tex.specular=0.9 - t1;
81  sp[i].tex.opacity=1.0;
82 
83  sp[i].tex.scale.x=1.0;
84  sp[i].tex.scale.y=1.0;
85  sp[i].tex.scale.z=1.0;
86 
87  sp[i].tex.rot.x=0.0;
88  sp[i].tex.rot.y=0.0;
89  sp[i].tex.rot.z=0.0;
90  sp[i].tex.texturefunc=rand() % 7;
91 
92 
93  sp[i].ctr.x=randflt() * 2.0 - 1.0;
94  sp[i].ctr.y=randflt() * 2.0 - 1.0;
95  sp[i].ctr.z=randflt() * 2.0 - 1.0;
96 
97  sp[i].rad=1.00;
98 
99  sp[i].dir.x=randflt() * 0.05 - 0.02;
100  sp[i].dir.y=randflt() * 0.05 - 0.02;
101  sp[i].dir.z=randflt() * 0.05 - 0.02;
102  }
103 }
104 
105 
106 void movesp(int frame) {
107  int i;
108 
109  for (i=0; i<6; i++) {
110  sp[i].ctr.x = 3.0 * sin(((i + 1.0)/6.0)*MYTPI + MYTPI*((1.0 * frame) / MAXFRAMES));
111  sp[i].ctr.y = 3.0 * cos(((i + 1.0)/6.0)*MYTPI + MYTPI*((1.0 * frame) / MAXFRAMES));
112  sp[i].ctr.z = 1.0;
113  }
114 
115  for (i=0; i<6; i++) {
116  sp[i+6].ctr.x = 3.0 + 3.0 * sin(((i + 1.0)/6.0)*MYTPI + MYTPI*((1.0 * frame) / MAXFRAMES));
117  sp[i+6].ctr.y = 1.0;
118  sp[i+6].ctr.z = 3.0 * cos(((i + 1.0)/6.0)*MYTPI + MYTPI*((1.0 * frame) / MAXFRAMES));
119  }
120 
121 }
122 
123 void drawsp(SceneHandle scene) {
124  int i;
125  apitexture p1;
126  apivector ct1, n1;
127 
128  for (i=0; i<NUMSP; i++) {
129  rt_sphere(scene, rt_texture(scene, &sp[i].tex), sp[i].ctr, sp[i].rad);
130  }
131 
132  p1.col.r=1.0;
133  p1.col.g=1.0;
134  p1.col.b=1.0;
135  p1.ambient=0.1;
136  p1.diffuse=0.5;
137  p1.specular=0.4;
138  p1.opacity=1.0;
139 
140  ct1.x=0.0;
141  ct1.y=-5.10;
142  ct1.z=0.0;
143 
144  n1.x=0.0;
145  n1.y=1.0;
146  n1.z=0.0;
147 
148  rt_plane(scene, rt_texture(scene, &p1), ct1, n1);
149 
150 }
151 
152 int main(int argc, char **argv) {
153  SceneHandle scene;
154  int i;
155  int xres, yres, raydepth, antialiasing, verbosemode;
156  apiflt zoom, aspectratio;
157  apivector Ccenter, Cview, Cup;
158  apivector ctr1, ctr2;
159  apitexture tex1, tex2;
160  char fname[100];
161  char fname2[200];
162 
163  scene = rt_newscene();
164 
165  xres=XRES;
166  yres=YRES;
167  aspectratio=1.0;
168  raydepth=18;
169  antialiasing=0;
170  verbosemode=0;
171  zoom=1.0;
172 
173  Ccenter.x=1.5; Ccenter.y=3.0; Ccenter.z=-8.0;
174  Cview.x=0.0; Cview.y=-0.2; Cview.z=0.8;
175  Cup.x=0.0; Cup.y=1.0; Cup.z=0.0;
176 
177  ctr1.x=20.0; ctr1.y=20.0; ctr1.z=-40.0;
178  ctr2.x=-20.0; ctr2.y=20.0; ctr2.z=-40.0;
179 
180  tex1.col.r=1.0;
181  tex1.col.g=0.5;
182  tex1.col.b=0.0;
183  tex1.ambient=1.0;
184  tex1.opacity=1.0;
185  tex2=tex1;
186  tex2.col.r=0.0;
187  tex2.col.b=1.0;
188 
189  initspheres();
190 
191  for (i=0; i<MAXFRAMES; i++) {
192  sprintf(fname,".%4.4d.tga",i);
193  strcpy(fname2,RFILE);
194  strcat(fname2, fname);
195 
196  rt_initialize(&argc, &argv);
197  if (rt_mynode()==0) printf("Rendering: %s \n",fname2);
198 
199  rt_outputfile(scene, fname2);
200  rt_resolution(scene, xres, yres);
201  rt_verbose(scene, verbosemode);
202 
203  rt_camera_setup(scene, zoom, aspectratio, antialiasing, raydepth,
204  Ccenter, Cview, Cup);
205 
206  rt_light(scene, rt_texture(scene, &tex1), ctr1, 1.0);
207  rt_light(scene, rt_texture(scene, &tex2), ctr2, 1.0);
208  movesp(i);
209  drawsp(scene);
210 
211  rt_renderscene(scene);
212  }
213 
214  return 0;
215 }
216 
float g
Green color component.
Definition: tachyon.h:61
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
apiflt randflt(void)
Definition: animspheres2.c:57
void movesp(int frame)
Definition: animspheres2.c:106
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
flt diffuse
diffuse reflection
Definition: tachyon.h:70
flt opacity
how opaque the object is
Definition: tachyon.h:72
flt specular
specular reflection
Definition: tachyon.h:71
#define XRES
Definition: animspheres2.c:17
void rt_renderscene(SceneHandle voidscene)
Render the current scene.
Definition: api.c:180
#define MYTPI
Definition: animspheres2.c:22
void rt_sphere(SceneHandle scene, void *tex, apivector ctr, flt rad)
Define a sphere with associated texture, center, and radius.
Definition: api.c:1212
apiflt rad
Definition: animspheres.c:53
#define MAXFRAMES
Definition: animspheres2.c:26
apitexture tex
Definition: animspheres.c:51
void drawsp(SceneHandle scene)
Definition: animspheres2.c:123
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
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_outputfile(SceneHandle voidscene, const char *outname)
Set the filename for the output image for the specified scene.
Definition: api.c:350
int main(int argc, char **argv)
Definition: animspheres2.c:152
void initspheres(void)
Definition: animspheres2.c:68
void * SceneHandle
Definition: tachyon.h:51
int texturefunc
which texture function to use
Definition: tachyon.h:66
flt ambient
ambient lighting
Definition: tachyon.h:69
#define NUMSP
Definition: animspheres2.c:19
#define RFILE
Definition: animspheres2.c:44
float b
Blue color component.
Definition: tachyon.h:62
int rt_mynode(void)
distributed memory parallel node rank
Definition: api.c:49
#define YRES
Definition: animspheres2.c:18
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
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
asphere sp[NUMSP]
Definition: animspheres2.c:55