1 //  GDS3D, a program for viewing GDSII files in 3D.
2 //  Created by Jasper Velner and Michiel Soer, http://icd.el.utwente.nl
3 //  Based on code by Roger Light, http://atchoo.org/gds2pov/
4 //
5 //  Copyright (C) 2013 IC-Design Group, University of Twente.
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License as published by
9 //  the Free Software Foundation; either version 2 of the License, or
10 //  (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 
21 #ifndef GDS3D_Renderer_h
22 #define GDS3D_Renderer_h
23 
24 #include "../math/Maths.h"
25 
26 // This is the only place gl.h should be included!
27 #ifdef WIN32
28 	#include <windows.h>
29 #endif
30 #ifdef __APPLE__
31     #include <OpenGL/gl.h>
32 #else
33     #include <GL/gl.h>
34 #endif
35 
36 #ifndef __APPLE__
37 	#define GLhandleARB GLuint
38 #endif
39 
40 #define Renderer_SIZE 1024*64 // 64K buffers max (GLushort limit)
41 #define VERTEX_INDEX_RATIO 5
42 
43 typedef struct drawvert2_t{
44 	GLfloat vertex[3];
45 	GLfloat normal[3];
46 }drawvert2_t;
47 
48 typedef struct VBO2_t{
49 	// VBO extension
50     GLuint vertbuffer;
51     GLuint indexbuffer;
52     int numObjects;
53 
54     struct VBO2_t *next;
55     struct VBO2_t *prev;
56 }VBO2_t;
57 
58 typedef struct renderRecipe_t{
59     VBO2_t   *VBO;
60     int     firstIndex;
61     int     numIndices;
62 
63     // Bounding box of geometry
64     AA_BOUNDING_BOX bounds;
65 
66 	// Fallback display lists
67 	GLuint displaylist;
68 
69     struct  renderRecipe_t* next;
70 }renderRecipe_t;
71 
72 typedef struct renderQueue_t{
73     renderRecipe_t *recipe;
74     MATRIX4X4 mat;
75     VECTOR4D color;
76 }renderQueue_t;
77 
78 class Renderer
79 {
80 private:
81     drawvert2_t  drawverts[Renderer_SIZE]; // Glfloat are 4 bytes each
82     unsigned int         numDrawverts;
83     GLushort    indices[Renderer_SIZE*VERTEX_INDEX_RATIO]; // Gluint (4 bytes) not native to ATI R300. GLushort is 2 bytes.
84     unsigned int         numIndices;
85 
86 	// Framebuffer
87 	GLuint	FBO;
88 	GLuint  RBO_depth;
89 	GLuint  RBO_color;
90 
91 	GLuint	FBO2;
92 	GLuint  RBO2_color;
93 
94     // Vertex creation
95     bool    enableVBO;
96     bool    enableShaders;
97 	bool	enableFBO;
98 	bool	enableMultiSample;
99     VBO2_t   *curVBO;
100     VBO2_t   *firstVBO;
101     renderRecipe_t *curRecipe;
102     VBO2_t  *boundVBO;
103 
104     // Shaders
105     GLhandleARB  vertexProgram;
106     GLhandleARB  fragmentProgram;
107     GLhandleARB  shaderProgram;
108     void	loadGLExtensions();
109     bool    IsExtensionSupported2( char* szTargetExtension );
110     void    emitTriangles();
111     void    deleteVBO(VBO2_t *vbo);
112     void    printShaderInfoLog(GLhandleARB obj);
113     void    printProgramInfoLog(GLhandleARB obj);
114     void    loadShaderProgram();
115 
116     // State
117     GLint       cull_type; // Backface culling
118     MATRIX4X4   modelview; // Modelview matrix
119     FRUSTUM     frustum; // For bounding box culling
120     bool        blending;
121     VECTOR4D    cur_color;
122 
123     // TGA saving
124     int savedImages;
125 
126 public:
127     // Renderer
128                         Renderer();
129                         ~Renderer();
130     void                init();
131     void                beginRender(FRUSTUM frustum);
132     void                endRender();
133     void                setWireframe(bool enable);
134 
135 	// Framebuffer
136 	bool				offlineFramebuffer(int width, int height);
137 	void				blitFramebuffer(int width, int height);
138 	void				onlineFramebuffer();
139 
140     // Vertex creation
141     renderRecipe_t*     beginObject();
142 	renderRecipe_t * beginObject(bool Update);
143     unsigned int        getCurIndex();
144 	unsigned int        addVertex(GLfloat x, GLfloat y, GLfloat z);
145 	void                addTriangle(size_t v1, size_t v2, size_t v3);
146     void                allowFlush();
147     void                endObject();
148     void                renderObject(renderRecipe_t *recipe, MATRIX4X4 *mat, VECTOR4D *color, bool transparent);
149     void                forceFlush();
150     void                deleteRecipe(renderRecipe_t *recipe);
151 
152 	// 2D Rendering
153 	void				start2D(int width, int height);
154 	void				drawSquare(float x1, float y1, float x2, float y2, int filled, VECTOR4D color);
155 	void				drawLines(unsigned int numLines, float *x_coords, float *y_coords, VECTOR4D color);
156 
157     // TGA image save
158     int                 tgaSave(char* filename, short int width, short int height, unsigned char pixelDepth,unsigned char* imageData);
159 	int                 tgaSaveSeries(char* filename, short int width, short int height, unsigned char	pixelDepth,unsigned char* imageData);
160 	int                 tgaGrabScreenSeries(char *filename);
161 
162     bool        wireframe; // Wireframe rendering
163 
164 };
165 
166 extern Renderer renderer;
167 extern unsigned long total_tris;
168 
169 #endif
170