summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/glrender.h
blob: 0a0276e3392063b3ac25cbb3e429ca5e8e08efcf (plain)
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
/*****
 * glrender.h
 * Render 3D Bezier paths and surfaces.
 *****/

#ifndef GLRENDER_H
#define GLRENDER_H

#include "common.h"
#include "triple.h"

#ifdef HAVE_LIBGLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/transform.hpp>
#endif

#ifdef HAVE_GL

#include <csignal>

#define GLEW_NO_GLU
//#define GLEW_OSMESA

#ifdef __MSDOS__
#define GLEW_STATIC
#define _WIN32
#endif

#include "GL/glew.h"

#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl.h>
#ifdef HAVE_LIBGLUT
#include <GLUT/glut.h>
#endif
#ifdef HAVE_LIBOSMESA
#include <GL/osmesa.h>
#endif
#else
#ifdef __MSDOS__
#undef _WIN32
#include <GL/gl.h>
#include <GL/wglew.h>
#include <GL/wglext.h>
#endif
#ifdef HAVE_LIBGLUT
#include <GL/glut.h>
#endif
#ifdef HAVE_LIBOSMESA
#include <GL/osmesa.h>
#endif
#endif

#else
typedef unsigned int GLuint;
typedef int GLint;
typedef float GLfloat;
typedef double GLdouble;
#endif

#ifdef HAVE_LIBGLM
#include "material.h"
#endif

namespace camp {
class picture;

inline void store(GLfloat *f, double *C)
{
  f[0]=C[0];
  f[1]=C[1];
  f[2]=C[2];
}

inline void store(GLfloat *control, const camp::triple& v)
{
  control[0]=v.getx();
  control[1]=v.gety();
  control[2]=v.getz();
}

inline void store(GLfloat *control, const triple& v, double weight)
{
  control[0]=v.getx()*weight;
  control[1]=v.gety()*weight;
  control[2]=v.getz()*weight;
  control[3]=weight;
}

}

namespace gl {

extern bool outlinemode;
extern bool wireframeMode;
extern size_t maxvertices;
extern bool forceRemesh;

extern bool orthographic;
extern double xmin,xmax;
extern double ymin,ymax;
extern double zmin,zmax;
extern int fullWidth,fullHeight;
extern double Zoom0;
extern double Angle;
extern camp::pair Shift;
extern camp::pair Margin;

extern camp::triple *Lights; 
extern size_t nlights;
extern double *Diffuse;
extern double *Background;

struct projection 
{
public:
  bool orthographic;
  camp::triple camera;
  camp::triple up;
  camp::triple target;
  double zoom;
  double angle;
  camp::pair viewportshift;
  
  projection(bool orthographic=false, camp::triple camera=0.0,
             camp::triple up=0.0, camp::triple target=0.0,
             double zoom=0.0, double angle=0.0,
             camp::pair viewportshift=0.0) : 
    orthographic(orthographic), camera(camera), up(up), target(target),
    zoom(zoom), angle(angle), viewportshift(viewportshift) {}
};

#ifdef HAVE_GL
extern GLuint ubo;
GLuint initHDR();

void setUniforms(GLint shader);
void deleteUniforms();
#endif

projection camera(bool user=true);

void glrender(const string& prefix, const camp::picture* pic,
              const string& format, double width, double height, double angle,
              double zoom, const camp::triple& m, const camp::triple& M,
              const camp::pair& shift, const camp::pair& margin, double *t,
              double *background, size_t nlights, camp::triple *lights,
              double *diffuse, double *specular, bool view, int oldpid=0);

extern const double *dprojView;

void initshader();
void deleteshader();

extern double BBT[9];

}

namespace camp {

struct Billboard {
  double cx,cy,cz;
  
  void init(const triple& center) {
    cx=center.getx();
    cy=center.gety();
    cz=center.getz();
  }
    
  triple transform(const triple& v) const {
    double x=v.getx()-cx;
    double y=v.gety()-cy;
    double z=v.getz()-cz;
    
    return triple(x*gl::BBT[0]+y*gl::BBT[3]+z*gl::BBT[6]+cx,
                  x*gl::BBT[1]+y*gl::BBT[4]+z*gl::BBT[7]+cy,
                  x*gl::BBT[2]+y*gl::BBT[5]+z*gl::BBT[8]+cz);
  }
};

extern Billboard BB;

#ifdef HAVE_LIBGLM
typedef mem::map<CONST Material,size_t> MaterialMap;

extern mem::vector<Material> material;
extern MaterialMap materialMap;
extern size_t materialIndex;
extern int MaterialIndex;
#endif

#ifdef HAVE_GL

extern const size_t Nbuffer; // Initial size of 2D dynamic buffers
extern const size_t nbuffer; // Initial size of 0D & 1D dynamic buffers

class vertexData 
{
public:
  GLfloat position[3];
  GLfloat normal[3];
  GLint material;
  vertexData() {};
  vertexData(const triple& v, const triple& n) {
    position[0]=v.getx();
    position[1]=v.gety();
    position[2]=v.getz();
    normal[0]=n.getx();
    normal[1]=n.gety();
    normal[2]=n.getz();
    material=MaterialIndex;
  }
};

class VertexData
{
public:
  GLfloat position[3];
  GLfloat normal[3];
  GLint material;
  GLubyte color[4];
  VertexData() {};
  VertexData(const triple& v, const triple& n) {
    position[0]=v.getx();
    position[1]=v.gety();
    position[2]=v.getz();
    normal[0]=n.getx();
    normal[1]=n.gety();
    normal[2]=n.getz();
    material=MaterialIndex;
  }
  VertexData(const triple& v, const triple& n, GLfloat *c) {
    position[0]=v.getx();
    position[1]=v.gety();
    position[2]=v.getz();
    normal[0]=n.getx();
    normal[1]=n.gety();
    normal[2]=n.getz();
    material=MaterialIndex;
    color[0]=(int)(bytescale*c[0]);
    color[1]=(int)(bytescale*c[1]);
    color[2]=(int)(bytescale*c[2]);
    color[3]=(int)(bytescale*c[3]);
  }
};

class vertexData1 {
public:
  GLfloat position[3];
  GLint material;
  vertexData1() {};
  vertexData1(const triple& v) {
    position[0]=v.getx();
    position[1]=v.gety();
    position[2]=v.getz();
    material=MaterialIndex;
  }
};

class vertexData0 {
public:
  GLfloat position[3];
  GLfloat width;
  GLint  material;
  vertexData0() {};
  vertexData0(const triple& v, double width) : width(width) {
    position[0]=v.getx();
    position[1]=v.gety();
    position[2]=v.getz();
    material=MaterialIndex;
  }
};

class vertexBuffer {
public:  
  std::vector<vertexData> vertices;
  std::vector<VertexData> Vertices;
  std::vector<vertexData1> vertices1;
  std::vector<vertexData0> vertices0;
  std::vector<GLuint> indices;
  void clear() {
    vertices.clear();
    Vertices.clear();
    vertices1.clear();
    vertices0.clear();
    indices.clear();
    vertices.reserve(Nbuffer);
    Vertices.reserve(Nbuffer);
    vertices1.reserve(nbuffer);
    vertices0.reserve(nbuffer);
    indices.reserve(Nbuffer);
  }
  
// Store the vertex v and its normal vector n.
  GLuint vertex(const triple &v, const triple& n) {
    size_t nvertices=vertices.size();
    vertices.push_back(vertexData(v,n));
    return nvertices;
  }     
  
// Store the vertex v and its normal vector n, without an explicit color.
  GLuint tvertex(const triple &v, const triple& n) {
    size_t nvertices=Vertices.size();
    Vertices.push_back(VertexData(v,n));
    return nvertices;
  }
  
// Store the vertex v, its normal vector n, and colors c.
  GLuint Vertex(const triple &v, const triple& n, GLfloat *c) {
    size_t nvertices=Vertices.size();
    Vertices.push_back(VertexData(v,n,c));
    return nvertices;
  }     
  
// Store the vertex v.
  GLuint vertex1(const triple &v) {
    size_t nvertices=vertices1.size();
    vertices1.push_back(vertexData1(v));
    return nvertices;
  }     
  
// Store the pixel v and its width.
  GLuint vertex0(const triple &v, double width) {
    size_t nvertices=vertices0.size();
    vertices0.push_back(vertexData0(v,width));
    return nvertices;
  }     
  
  // append array b onto array a with offset
  void appendOffset(std::vector<GLuint>& a,
                    const std::vector<GLuint>& b, size_t offset) {
    size_t n=a.size();
    size_t m=b.size();
    a.resize(n+m);
    for(size_t i=0; i < m; ++i)
      a[n+i]=b[i]+offset;
  }

  // append array b onto array a
  void append(const vertexBuffer& b) {
    appendOffset(indices,b.indices,vertices.size());
    vertices.insert(vertices.end(),b.vertices.begin(),b.vertices.end());
  }
  
  void Append(const vertexBuffer& b) {
    appendOffset(indices,b.indices,Vertices.size());
    Vertices.insert(Vertices.end(),b.Vertices.begin(),b.Vertices.end());
  }
  
  void append1(const vertexBuffer& b) {
    appendOffset(indices,b.indices,vertices1.size());
    vertices1.insert(vertices1.end(),b.vertices1.begin(),b.vertices1.end());
  }
  
  void append0(const vertexBuffer& b) {
    appendOffset(indices,b.indices,vertices0.size());
    vertices0.insert(vertices0.end(),b.vertices0.begin(),b.vertices0.end());
  }

};

extern GLint pixelShader;
extern GLint noNormalShader;
extern GLint materialShader;
extern GLint colorShader;
extern GLint transparentShader;

extern vertexBuffer material0Data;   // pixels
extern vertexBuffer material1Data;   // material Bezier curves
extern vertexBuffer materialData;    // material Bezier patches & triangles
extern vertexBuffer colorData;       // colored Bezier patches & triangles
extern vertexBuffer transparentData; // transparent patches & triangles
extern vertexBuffer triangleData;    // opaque indexed triangles

extern void drawBuffer(vertexBuffer& data, GLint shader);
extern void drawBuffers(); 
extern void clearBuffers();
extern void clearMaterialBuffer(bool draw=false);

#endif

}

#endif