File: ray.c

package info (click to toggle)
viewmol 2.3-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,424 kB
  • ctags: 2,239
  • sloc: ansic: 29,098; sh: 909; makefile: 513; python: 238
file content (262 lines) | stat: -rw-r--r-- 9,202 bytes parent folder | download
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
/*******************************************************************************
*                                                                              *
*                                   Viewmol                                    *
*                                                                              *
*                                  R A Y . C                                   *
*                                                                              *
*                 Copyright (c) Joerg-R. Hill, December 2000                   *
*                                                                              *
********************************************************************************
*
* $Id: ray.c,v 1.5 2000/12/10 15:14:33 jrh Exp $
* $Log: ray.c,v $
* Revision 1.5  2000/12/10 15:14:33  jrh
* Release 2.3
*
* Revision 1.4  1999/05/24 01:27:03  jrh
* Release 2.2.1
*
* Revision 1.3  1999/02/07 21:55:11  jrh
* Release 2.2
*
* Revision 1.2  1998/01/26 00:49:07  jrh
* Release 2.1
*
* Revision 1.1  1996/12/10  18:43:24  jrh
* Initial revision
*
*/
#include<math.h>
#include<stdio.h>
#include<unistd.h>
#include<GL/glu.h>
#include "viewmol.h"
#include "dialog.h"

extern void transformCoordinates(int, float input[4], float output[4]);
extern void *getmem(size_t, size_t);
extern void fremem(void **);

extern struct WINDOW windows[];
extern struct MOLECULE *molecules;
extern struct ELEMENT *elements;
extern double tmat[4][4];
extern GLfloat light0p[], light1p[];
extern float *transObject;
extern int ne, iwavef, lights, projectionMode;

static struct ELEMENT *material;
static double n[3];
static int printNormal=FALSE;
static FILE *f;

FILE *raytraceInit(char *filename, Dimension width, Dimension height)
{
  struct MOLECULE *mol;
  struct ELEMENT *element;
  FILE *file;
  double todeg=45.0/atan(1.0);
  GLfloat xview, zview, light[4], lookat[4]={0.0, 0.0, -1.0, 1.0}, up[4]={0.0, 1.0, 0.0, 1.0};
  int *done, hbond;
  register int i, j;

  if ((file=fopen(filename, "w")) == NULL) return(NULL);
  fprintf(file, "screen %d %d\n", width, height);
  if (2.0*windows[VIEWER].far > transObject[3*VIEWPOINT+2])
    zview=2.0*windows[VIEWER].far-transObject[3*VIEWPOINT+2];
  else
    zview=0.1;
  xview=-0.75*windows[VIEWER].near*windows[VIEWER].top*(GLfloat)(width)/(windows[VIEWER].far
       *(GLfloat)(height));
  fprintf(file, "eyep %12.7f %12.7f %12.7f\n", (-transObject[3*VIEWPOINT]),
          (-transObject[3*VIEWPOINT+1]), zview+windows[VIEWER].far);
  transformCoordinates(VIEWPOINT, lookat, light);
  light[0]*=(float)(10.0*windows[VIEWER].far-transObject[3*VIEWPOINT+2]);
  light[1]*=(float)(10.0*windows[VIEWER].far-transObject[3*VIEWPOINT+2]);
  light[2]*=(float)(10.0*windows[VIEWER].far-transObject[3*VIEWPOINT+2]);
  fprintf(file, "lookp %12.7f %12.7f %12.7f\n", light[0]-transObject[3*VIEWPOINT],
          light[1]-transObject[3*VIEWPOINT+1], light[2]-transObject[3*VIEWPOINT+2]);
  fprintf(file, "fov %f\n", 2.0*todeg*atan((double)(xview/zview)));
  transformCoordinates(VIEWPOINT, up, light);
  fprintf(file, "up %12.7f %12.7f %12.7f\n", light[0], light[1], light[2]);
  fprintf(file, "background %6.3f %6.3f %6.3f\n", windows[VIEWER].background_rgb[0],
          windows[VIEWER].background_rgb[1], windows[VIEWER].background_rgb[2]);
  if (lights & 0x1)
  {
    transformCoordinates(LIGHTNO0, light0p, light);
    fprintf(file, "light 1.0 directional %10.5f %10.5f %10.5f noshadow\n",
            light[0], light[1], light[2]);
  }
  if (lights & 0x2)
  {
    transformCoordinates(LIGHTNO1, light1p, light);
    fprintf(file, "light 0.5 directional %10.5f %10.5f %10.5f\n", light[0],
            light[1], light[2]);
  }
  if (windows[VIEWER].set >= 0)
    mol=&molecules[windows[VIEWER].set];
  else
    mol=&molecules[0];
  hbond=FALSE;
  for (i=0; i<mol->nb; i++)
  {
    if (mol->bonds[i].order == (-1))
    {
      hbond=TRUE;
      break;
    }
  }
  done=(int *)getmem(mol->na, sizeof(int));
  for (i=0; i<mol->na; i++)
  {
    if (!done[i])
    {
      element=mol->atoms[i].element;
      fprintf(file, "surface %s\n", element->symbol);
      fprintf(file, "  diffuse %5.3f %5.3f %5.3f\n",
              (element->dark[0]+element->light[0])*0.5,
              (element->dark[1]+element->light[1])*0.5,
              (element->dark[2]+element->light[2])*0.5);
      /* Use only 1/10 of the ambient color since rayshade looses contrast
	   otherwise */
      fprintf(file, "  ambient %5.3f %5.3f %5.3f\n", 0.1*element->ambient[0],
		  0.1*element->ambient[1], 0.1*element->ambient[2]);
      fprintf(file, "  specular %5.3f %5.3f %5.3f\n", element->specular[0],
		  element->specular[1], element->specular[2]);
      fprintf(file, "  specpow %7.3f\n", element->shininess);
      fprintf(file, "  transp  %7.3f\n", 1.0-element->alpha);
      for (j=i; j<mol->na; j++)
      {
        if (mol->atoms[j].element == element) done[j]=TRUE;
      }
    }
  }
  if (hbond)
  {
    element=mol->bondStyle;
    fprintf(file, "surface %s\n", element->symbol);
    fprintf(file, "  diffuse %5.3f %5.3f %5.3f\n",
            (element->dark[0]+element->light[0])*0.5,
            (element->dark[1]+element->light[1])*0.5,
            (element->dark[2]+element->light[2])*0.5);
    fprintf(file, "  ambient %5.3f %5.3f %5.3f\n", 0.1*element->ambient[0],
	      0.1*element->ambient[1], 0.1*element->ambient[2]);
    fprintf(file, "  specular %5.3f %5.3f %5.3f\n", element->specular[0],
		element->specular[1], element->specular[2]);
    fprintf(file, "  specpow %7.3f\n", element->shininess);
    fprintf(file, "  transp  %7.3f\n", 1.0-element->alpha);
  }
  if (iwavef != ALL_OFF)
  {
    for (i=0; i<ne; i++)
    {
      if (!strcmp(elements[i].symbol, "Ps") || !strcmp(elements[i].symbol, "Ms"))
      {
        fprintf(file, "surface %s\n", elements[i].symbol);
        fprintf(file, "  diffuse %5.3f %5.3f %5.3f\n",
                (elements[i].dark[0]+elements[i].light[0])*0.5,
                (elements[i].dark[1]+elements[i].light[1])*0.5,
                (elements[i].dark[2]+elements[i].light[2])*0.5);
        fprintf(file, "  ambient %5.3f %5.3f %5.3f\n", 0.1*elements[i].ambient[0],
		    0.1*elements[i].ambient[1], 0.1*elements[i].ambient[2]);
        fprintf(file, "  specular %5.3f %5.3f %5.3f\n", elements[i].specular[0],
		    elements[i].specular[1], elements[i].specular[2]);
        fprintf(file, "  specpow %7.3f\n", elements[i].shininess);
        fprintf(file, "  transp  %7.3f\n", 1.0-elements[i].alpha);
      }
    }
  }
  fremem((void **)&done);
  if (projectionMode == PERSPECTIVE)
  {
    fprintf(file, "surface ground\n");
    fprintf(file, "  diffuse %5.3f %5.3f %5.3f\n", windows[VIEWER].foreground_rgb[0],
            windows[VIEWER].foreground_rgb[1], windows[VIEWER].foreground_rgb[2]);
    fprintf(file, "  ambient %5.3f %5.3f %5.3f\n", 0.5*windows[VIEWER].foreground_rgb[0],
            0.5*windows[VIEWER].foreground_rgb[1], 0.5*windows[VIEWER].foreground_rgb[2]);
    fprintf(file, "plane ground 0.0 %10.5f 0.0 0.0 1.0 0.0\n", windows[VIEWER].bottom);
  }
  fprintf(file, "name molecule list\n");
  f=file;
  return(file);
}

void raytraceClose(FILE *file)
{
  fprintf(file, "end\n");
  fprintf(file, "object molecule\n");
  fclose(file);
}

void raytracerBegin(GLenum what)
{
  fprintf(f, "triangle %s ", material->symbol);
  glGetDoublev(GL_MODELVIEW_MATRIX, &tmat[0][0]);
}

void raytracerEnd()
{
  fprintf(f, "\n");
}

void raytracerVertex3d(double vx, double vy, double vz)
{
  double t[3];

  t[0]=vx*tmat[0][0]+vy*tmat[1][0]+vz*tmat[2][0]+tmat[3][0];
  t[1]=vx*tmat[0][1]+vy*tmat[1][1]+vz*tmat[2][1]+tmat[3][1];
  t[2]=vx*tmat[0][2]+vy*tmat[1][2]+vz*tmat[2][2]+tmat[3][2];
  fprintf(f, "%10.6f %10.6f %10.6f ", t[0], t[1], t[2]);

  if (printNormal)
  {
    t[0]=n[0]*tmat[0][0]+n[1]*tmat[1][0]+n[2]*tmat[2][0]+tmat[3][0];
    t[1]=n[0]*tmat[0][1]+n[1]*tmat[1][1]+n[2]*tmat[2][1]+tmat[3][1];
    t[2]=n[0]*tmat[0][2]+n[1]*tmat[1][2]+n[2]*tmat[2][2]+tmat[3][2];
    fprintf(f, "%10.6f %10.6f %10.6f ", t[0], t[1], t[2]);
    printNormal=FALSE;
  }
}

void raytracerNormal3d(double vx, double vy, double vz)
{
  n[0]=vx;
  n[1]=vy;
  n[2]=vz;
  printNormal=TRUE;
}

void raytracerSphere(GLUquadricObj *object, GLdouble radius, GLint dummy1,
                     GLint dummy2)
{
  double matrix[4][4];

  glGetDoublev(GL_MODELVIEW_MATRIX, &matrix[0][0]);
  fprintf(f, "sphere %s %f %10.6f %10.6f %10.6f\n", material->symbol, radius,
          matrix[3][0], matrix[3][1], matrix[3][2]);
}

void raytracerCylinder(GLUquadricObj *object, GLdouble top, GLdouble bottom,
                       GLdouble height, GLint dummy1, GLint dummy2)
{
  double matrix[4][4];

  glGetDoublev(GL_MODELVIEW_MATRIX, &matrix[0][0]);
  fprintf(f, "cylinder %s %f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f\n",
          material->symbol, bottom, matrix[3][0], matrix[3][1], matrix[3][2],
          height*matrix[2][0]+matrix[3][0], height*matrix[2][1]+matrix[3][1],
          height*matrix[2][2]+matrix[3][2]);
}

void raytracerColor4fv(const GLfloat *color)
{
}

void raytracerClearColor(GLclampf red, GLclampf green , GLclampf blue, GLclampf alpha)
{
}

void raytracerMaterial(struct ELEMENT *e)
{
  material=e;
}