File: gr3_povray.c

package info (click to toggle)
gr-framework 0.73.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 11,600 kB
  • sloc: ansic: 87,413; cpp: 45,348; objc: 3,057; javascript: 2,647; makefile: 1,129; python: 1,000; sh: 991; yacc: 855; pascal: 554; fortran: 228
file content (405 lines) | stat: -rw-r--r-- 17,090 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#define _POSIX_C_SOURCE 200112L

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "gr3.h"
#include "gr3_internals.h"
#ifndef fabsf
#define fabsf(x) ((x) < 0 ? -(x) : (x))
#endif

int gr3_getpovray_(char *pixels, int width, int height, int use_alpha, int ssaa_factor)
{
  int i;
#ifdef GR3_USE_WIN
  char *povfile = malloc(40);
  char *pngfile = malloc(40);
  snprintf(povfile, 40, "./gr3.%lu.pov", (long unsigned)GetCurrentProcessId());
  snprintf(pngfile, 40, "./gr3.%lu.png", (long unsigned)GetCurrentProcessId());
#else
  char *povfile = malloc(40);
  char *pngfile = malloc(40);
  snprintf(povfile, 40, "/tmp/gr3.%d.pov", getpid());
  snprintf(pngfile, 40, "/tmp/gr3.%d.png", getpid());
#endif
  gr3_export_pov_(povfile, width, height);
  {
    int res;
    char *povray_call = malloc(strlen(povfile) + strlen(povfile) + 80);
#ifdef GR3_USE_WIN
    snprintf(povray_call, strlen(povfile) + strlen(povfile) + 80,
             "megapov +I%s +O%s +W%d +H%d -D +UA +FN +A +R%d 2>NUL", povfile, pngfile, width, height, ssaa_factor);
#else
    snprintf(povray_call, strlen(povfile) + strlen(povfile) + 80,
             "povray +I%s +O%s +W%d +H%d -D +UA +FN +A +R%d 2>/dev/null", povfile, pngfile, width, height, ssaa_factor);
#endif
    res = system(povray_call);
    free(povray_call);
    if (use_alpha)
      {
        res = gr3_readpngtomemory_((int *)pixels, pngfile, width, height);
        if (res)
          {
            RETURN_ERROR(GR3_ERROR_EXPORT);
          }
      }
    else
      {
        char *raw_pixels = malloc(width * height * 4);
        if (!raw_pixels)
          {
            RETURN_ERROR(GR3_ERROR_OUT_OF_MEM);
          }
        res = gr3_readpngtomemory_((int *)raw_pixels, pngfile, width, height);
        if (res)
          {
            free(raw_pixels);
            RETURN_ERROR(GR3_ERROR_EXPORT);
          }
        for (i = 0; i < width * height; i++)
          {
            pixels[3 * i + 0] = raw_pixels[4 * i + 0];
            pixels[3 * i + 1] = raw_pixels[4 * i + 1];
            pixels[3 * i + 2] = raw_pixels[4 * i + 2];
          }
        free(raw_pixels);
      }
  }
  remove(povfile);
  remove(pngfile);
  free(povfile);
  free(pngfile);
  return GR3_ERROR_NONE;
}

static void gr3_write_clipped_by(FILE *povfp)
{
  if (isfinite(context_struct_.clip_xmin) || isfinite(context_struct_.clip_xmax) ||
      isfinite(context_struct_.clip_ymin) || isfinite(context_struct_.clip_ymax) ||
      isfinite(context_struct_.clip_zmin) || isfinite(context_struct_.clip_zmax))
    {
      fprintf(povfp, "clipped_by { intersection {\n");
      if (isfinite(context_struct_.clip_xmin))
        {
          fprintf(povfp, "plane { x, %f inverse }\n", context_struct_.clip_xmin);
        }
      if (isfinite(context_struct_.clip_xmax))
        {
          fprintf(povfp, "plane { x, %f }\n", context_struct_.clip_xmax);
        }
      if (isfinite(context_struct_.clip_ymin))
        {
          fprintf(povfp, "plane { y, %f inverse }\n", context_struct_.clip_ymin);
        }
      if (isfinite(context_struct_.clip_ymax))
        {
          fprintf(povfp, "plane { y, %f }\n", context_struct_.clip_ymax);
        }
      if (isfinite(context_struct_.clip_zmin))
        {
          fprintf(povfp, "plane { z, %f inverse }\n", context_struct_.clip_zmin);
        }
      if (isfinite(context_struct_.clip_zmax))
        {
          fprintf(povfp, "plane { z, %f }\n", context_struct_.clip_zmax);
        }
      fprintf(povfp, "} }\n");
    }
}

static void print_pigment_and_finish(GR3_DrawList_t_ *draw, char *alpha_text, float alpha_val, int i, FILE *povfp,
                                     float red, float green, float blue)
{
  if (draw->alpha_mode == 0)
    {
      fprintf(povfp,
              "pigment { color rgb <%f, %f, %f> } finish { ambient %f diffuse %f "
              "specular %f roughness %f } \n",
              draw->colors[i * 3 + 0] * red, draw->colors[i * 3 + 1] * green, draw->colors[i * 3 + 2] * blue,
              context_struct_.light_parameters.ambient, context_struct_.light_parameters.diffuse,
              context_struct_.light_parameters.specular, 1 / (context_struct_.light_parameters.specular_exponent + 1));
    }
  else
    {
      fprintf(povfp,
              "pigment { color rgb <%f, %f, %f> %s %f} finish { ambient %f diffuse %f specular "
              "%f roughness %f} \n",
              draw->colors[i * 3 + 0] * red, draw->colors[i * 3 + 1] * green, draw->colors[i * 3 + 2] * blue,
              alpha_text, alpha_val, context_struct_.light_parameters.ambient * (1 - alpha_val),
              context_struct_.light_parameters.diffuse * (1 - alpha_val),
              context_struct_.light_parameters.specular * (1 - alpha_val),
              (1 - alpha_val) / (context_struct_.light_parameters.specular_exponent + 1));
    }
}


int gr3_export_pov_(const char *filename, int width, int height)
{
  int i, j, k, l;
  FILE *povfp;
  GR3_DrawList_t_ *draw;


  povfp = fopen(filename, "w");
  if (!povfp)
    {
      RETURN_ERROR(GR3_ERROR_CANNOT_OPEN_FILE);
    }

  fprintf(povfp, "camera {\n");
  if (context_struct_.projection_type == GR3_PROJECTION_ORTHOGRAPHIC)
    {
      fprintf(povfp, "  orthographic\n");
    }
  fprintf(povfp, "  location <%f, %f, %f>\n", context_struct_.camera_x, context_struct_.camera_y,
          context_struct_.camera_z);
  fprintf(povfp, "  look_at <%f, %f, %f>\n", context_struct_.center_x, context_struct_.center_y,
          context_struct_.center_z);
  fprintf(povfp, "  sky <%f, %f, %f>\n", context_struct_.up_x, context_struct_.up_y, context_struct_.up_z);
  if (context_struct_.projection_type != GR3_PROJECTION_ORTHOGRAPHIC)
    {
      fprintf(povfp, "  up <0,1,0>\n");
      fprintf(povfp, "  right <-%f,0,0>\n", 1.0 * width / height);
      fprintf(povfp, "  angle %f\n", context_struct_.vertical_field_of_view * 1.0 * width / height);
    }
  else
    {
      fprintf(povfp, "  up <0 %f 0>\n", fabsf(context_struct_.top) + fabsf(context_struct_.bottom));
      fprintf(povfp, "  right <-%f,0,0>\n", fabsf(context_struct_.right) + fabsf(context_struct_.left));
    }
  fprintf(povfp, "}\n");

  if (context_struct_.num_lights == 0)
    {
      GLfloat camera_pos[3];
      camera_pos[0] = context_struct_.camera_x;
      camera_pos[1] = context_struct_.camera_y;
      camera_pos[2] = context_struct_.camera_z;
      fprintf(povfp, "light_source { <%f, %f, %f> color rgb <1.0, 1.0, 1.0> parallel point_at <0,0,0>}\n",
              camera_pos[0], camera_pos[1], camera_pos[2]);
    }
  else
    {
      for (i = 0; i < context_struct_.num_lights; i++)
        {
          if (context_struct_.light_sources[i].x == 0 && context_struct_.light_sources[i].y == 0 &&
              context_struct_.light_sources[i].z == 0)
            {
              GLfloat camera_pos[3];
              camera_pos[0] = context_struct_.camera_x;
              camera_pos[1] = context_struct_.camera_y;
              camera_pos[2] = context_struct_.camera_z;
              fprintf(povfp, "light_source { <%f, %f, %f> color rgb <%f, %f, %f> parallel point_at <0,0,0>}\n",
                      camera_pos[0], camera_pos[1], camera_pos[2], context_struct_.light_sources[i].r,
                      context_struct_.light_sources[i].g, context_struct_.light_sources[i].b);
            }
          else
            {
              fprintf(povfp, "light_source { <%f, %f, %f> color rgb <%f, %f, %f> parallel point_at <0,0,0>}\n",
                      context_struct_.light_sources[i].x, context_struct_.light_sources[i].y,
                      context_struct_.light_sources[i].z, context_struct_.light_sources[i].r,
                      context_struct_.light_sources[i].g, context_struct_.light_sources[i].b);
            }
        }
    }
  fprintf(povfp, "background { color rgb <%f, %f, %f> }\n", context_struct_.background_color[0],
          context_struct_.background_color[1], context_struct_.background_color[2]);
  draw = context_struct_.draw_list_;
  while (draw)
    {
      float alpha_val;
      char *alpha_text;
      if (draw->alpha_mode == 0)
        {
          alpha_text = "";
        }
      else if (draw->alpha_mode == 1)
        {
          alpha_text = "transmit";
          alpha_val = 1 - draw->alphas[0];
        }
      else
        {
          alpha_text = "filter";
          alpha_val = 1 - (draw->alphas[0] + draw->alphas[1] + draw->alphas[2]) / 3;
        }
      gr3_sortindexedmeshdata(draw->mesh);
      switch (context_struct_.mesh_list_[draw->mesh].data.type)
        {
        case kMTSphereMesh:
          for (i = 0; i < draw->n; i++)
            {
              fprintf(povfp, "sphere {\n");
              fprintf(povfp, "  <%f, %f, %f>, %f\n", draw->positions[i * 3 + 0], draw->positions[i * 3 + 1],
                      draw->positions[i * 3 + 2], draw->scales[i * 3 + 0]);
              fprintf(povfp, "  texture {\n");
              print_pigment_and_finish(draw, alpha_text, alpha_val, i, povfp, 1, 1, 1);
              fprintf(povfp, "  }\n");
              gr3_write_clipped_by(povfp);
              fprintf(povfp, " no_shadow \n");
              fprintf(povfp, "}\n");
            }
          break;
        case kMTCylinderMesh:
          for (i = 0; i < draw->n; i++)
            {
              float len_sq = draw->directions[i * 3 + 0] * draw->directions[i * 3 + 0] +
                             draw->directions[i * 3 + 1] * draw->directions[i * 3 + 1] +
                             draw->directions[i * 3 + 2] * draw->directions[i * 3 + 2];
              float len = sqrt(len_sq);
              fprintf(povfp, "cylinder {\n");
              fprintf(povfp, "  <%f, %f, %f>, <%f, %f, %f>, %f\n", draw->positions[i * 3 + 0],
                      draw->positions[i * 3 + 1], draw->positions[i * 3 + 2],
                      draw->positions[i * 3 + 0] + draw->directions[i * 3 + 0] / len * draw->scales[i * 3 + 2],
                      draw->positions[i * 3 + 1] + draw->directions[i * 3 + 1] / len * draw->scales[i * 3 + 2],
                      draw->positions[i * 3 + 2] + draw->directions[i * 3 + 2] / len * draw->scales[i * 3 + 2],
                      draw->scales[i * 3 + 0]);
              fprintf(povfp, "  texture {\n");
              print_pigment_and_finish(draw, alpha_text, alpha_val, i, povfp, 1, 1, 1);
              fprintf(povfp, "  }\n");
              gr3_write_clipped_by(povfp);
              fprintf(povfp, " no_shadow \n");
              fprintf(povfp, "}\n");
            }
          break;
        case kMTConeMesh:
          for (i = 0; i < draw->n; i++)
            {
              float len_sq = draw->directions[i * 3 + 0] * draw->directions[i * 3 + 0] +
                             draw->directions[i * 3 + 1] * draw->directions[i * 3 + 1] +
                             draw->directions[i * 3 + 2] * draw->directions[i * 3 + 2];
              float len = sqrt(len_sq);
              fprintf(povfp, "cone {\n");
              fprintf(povfp, "  <%f, %f, %f>, %f, <%f, %f, %f>, %f\n", draw->positions[i * 3 + 0],
                      draw->positions[i * 3 + 1], draw->positions[i * 3 + 2], draw->scales[i * 3 + 0],
                      draw->positions[i * 3 + 0] + draw->directions[i * 3 + 0] / len * draw->scales[i * 3 + 2],
                      draw->positions[i * 3 + 1] + draw->directions[i * 3 + 1] / len * draw->scales[i * 3 + 2],
                      draw->positions[i * 3 + 2] + draw->directions[i * 3 + 2] / len * draw->scales[i * 3 + 2], 0.0);
              fprintf(povfp, "  texture {\n");
              print_pigment_and_finish(draw, alpha_text, alpha_val, i, povfp, 1, 1, 1);
              fprintf(povfp, "  }\n");
              gr3_write_clipped_by(povfp);
              fprintf(povfp, " no_shadow \n");
              fprintf(povfp, "}\n");
            }
          break;
        case kMTIndexedMesh:
        case kMTNormalMesh:
          for (i = 0; i < draw->n; i++)
            {
              GLfloat model_matrix[4][4] = {{0}};
              const float *vertices = context_struct_.mesh_list_[draw->mesh].data.vertices;
              const float *normals = context_struct_.mesh_list_[draw->mesh].data.normals;
              const float *colors = context_struct_.mesh_list_[draw->mesh].data.colors;
              {
                int m;
                GLfloat forward[3], up[3], left[3];
                float tmp;
                /* Calculate an orthonormal base in IR^3, correcting the up vector
                 * in case it is not perpendicular to the forward vector. This base
                 * is used to create the model matrix as a base-transformation
                 * matrix.
                 */
                /* forward = normalize(&directions[i*3]); */
                tmp = 0;
                for (m = 0; m < 3; m++)
                  {
                    tmp += draw->directions[i * 3 + m] * draw->directions[i * 3 + m];
                  }
                tmp = sqrt(tmp);
                for (m = 0; m < 3; m++)
                  {
                    forward[m] = draw->directions[i * 3 + m] / tmp;
                  } /* up = normalize(&ups[i*3]); */
                tmp = 0;
                for (m = 0; m < 3; m++)
                  {
                    tmp += draw->ups[i * 3 + m] * draw->ups[i * 3 + m];
                  }
                tmp = sqrt(tmp);
                for (m = 0; m < 3; m++)
                  {
                    up[m] = draw->ups[i * 3 + m] / tmp;
                  }
                /* left = cross(forward,up); */
                for (m = 0; m < 3; m++)
                  {
                    left[m] = forward[(m + 1) % 3] * up[(m + 2) % 3] - up[(m + 1) % 3] * forward[(m + 2) % 3];
                  }
                /* up = cross(left,forward); */
                for (m = 0; m < 3; m++)
                  {
                    up[m] = left[(m + 1) % 3] * forward[(m + 2) % 3] - forward[(m + 1) % 3] * left[(m + 2) % 3];
                  }
                for (m = 0; m < 3; m++)
                  {
                    model_matrix[0][m] = -left[m];
                    model_matrix[1][m] = up[m];
                    model_matrix[2][m] = forward[m];
                    model_matrix[3][m] = draw->positions[i * 3 + m];
                  }
                model_matrix[3][3] = 1;
              }
              fprintf(povfp, "mesh {\n");
              for (j = 0; j < context_struct_.mesh_list_[draw->mesh].data.number_of_vertices / 3; j++)
                {
                  float red = (colors[j * 9 + 0] + colors[j * 9 + 3] + colors[j * 9 + 6]) / 3.0;
                  float green = (colors[j * 9 + 1] + colors[j * 9 + 4] + colors[j * 9 + 7]) / 3.0;
                  float blue = (colors[j * 9 + 2] + colors[j * 9 + 5] + colors[j * 9 + 8]) / 3.0;
                  fprintf(povfp, "#local tex = texture { ");
                  print_pigment_and_finish(draw, alpha_text, alpha_val, i, povfp, red, green, blue);
                  fprintf(povfp, "}\n");
                  fprintf(povfp, "  smooth_triangle {\n");
                  for (k = 0; k < 3; k++)
                    {
                      float vertex1[4];
                      float vertex2[4];
                      float normal1[3];
                      float normal2[3];
                      for (l = 0; l < 3; l++)
                        {
                          vertex1[l] = draw->scales[i * 3 + l] * vertices[j * 9 + k * 3 + l];
                        }
                      vertex1[3] = 1;
                      for (l = 0; l < 4; l++)
                        {
                          vertex2[l] = model_matrix[0][l] * vertex1[0] + model_matrix[1][l] * vertex1[1] +
                                       model_matrix[2][l] * vertex1[2] + model_matrix[3][l] * vertex1[3];
                        }
                      for (l = 0; l < 3; l++)
                        {
                          normal1[l] = normals[j * 9 + k * 3 + l];
                        }
                      vertex1[3] = 1;
                      for (l = 0; l < 3; l++)
                        {
                          normal2[l] = model_matrix[0][l] * normal1[0] + model_matrix[1][l] * normal1[1] +
                                       model_matrix[2][l] * normal1[2];
                        }
                      fprintf(povfp, "    <%f, %f, %f>,", vertex2[0], vertex2[1], vertex2[2]);
                      fprintf(povfp, " <%f, %f, %f>", normal2[0], normal2[1], normal2[2]);
                      if (k < 2)
                        {
                          fprintf(povfp, ",");
                        }
                      fprintf(povfp, "\n");
                    }
                  fprintf(povfp, "    texture { tex }\n");
                  fprintf(povfp, "  }\n");
                }
              gr3_write_clipped_by(povfp);
              fprintf(povfp, " no_shadow \n");
              fprintf(povfp, "}\n");
            }
          break;
        default:
          gr3_log_("Unknown mesh type");
          break;
        }
      draw = draw->next;
    }
  fclose(povfp);
  return 0;
}