1 #ifndef COMMON_RENDER_INTERFACE_H
2 #define COMMON_RENDER_INTERFACE_H
3 
4 struct CommonCameraInterface;
5 
6 enum
7 {
8 	B3_GL_TRIANGLES = 1,
9 	B3_GL_POINTS
10 };
11 
12 enum
13 {
14 	B3_INSTANCE_TRANSPARANCY = 1,
15 	B3_INSTANCE_TEXTURE = 2,
16 	B3_INSTANCE_DOUBLE_SIDED = 4,
17 };
18 
19 enum
20 {
21 	B3_DEFAULT_RENDERMODE = 1,
22 	//B3_WIREFRAME_RENDERMODE,
23 	B3_CREATE_SHADOWMAP_RENDERMODE,
24 	B3_USE_SHADOWMAP_RENDERMODE,
25 	B3_USE_SHADOWMAP_RENDERMODE_REFLECTION,
26 	B3_USE_SHADOWMAP_RENDERMODE_REFLECTION_PLANE,
27 	B3_USE_PROJECTIVE_TEXTURE_RENDERMODE,
28 	B3_SEGMENTATION_MASK_RENDERMODE,
29 };
30 
31 struct GfxVertexFormat0
32 {
33 	float x, y, z, w;
34 	float unused0, unused1, unused2, unused3;
35 	float u, v;
36 };
37 
38 struct GfxVertexFormat1
39 {
40 	float x, y, z, w;
41 	float nx, ny, nz;
42 	float u, v;
43 };
44 
45 struct CommonRenderInterface
46 {
~CommonRenderInterfaceCommonRenderInterface47 	virtual ~CommonRenderInterface() {}
48 	virtual void init() = 0;
49 	virtual void updateCamera(int upAxis) = 0;
50 	virtual void removeAllInstances() = 0;
51 	virtual void removeGraphicsInstance(int instanceUid) = 0;
52 
53 	virtual const CommonCameraInterface* getActiveCamera() const = 0;
54 	virtual CommonCameraInterface* getActiveCamera() = 0;
55 	virtual void setActiveCamera(CommonCameraInterface* cam) = 0;
56 
57 	virtual void setLightPosition(const float lightPos[3]) = 0;
58 	virtual void setLightPosition(const double lightPos[3]) = 0;
59 	virtual void setBackgroundColor(const double rgbBackground[3]) = 0;
60 
61 	virtual void setShadowMapResolution(int shadowMapResolution) = 0;
62 	virtual void setShadowMapIntensity(double shadowMapIntensity) = 0;
63 
64 	virtual void setShadowMapWorldSize(float worldSize) = 0;
65 
setProjectiveTextureMatricesCommonRenderInterface66 	virtual void setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16]){};
setProjectiveTextureCommonRenderInterface67 	virtual void setProjectiveTexture(bool useProjectiveTexture){};
68 
69 	virtual void renderScene() = 0;
70 	virtual void renderSceneInternal(int renderMode = B3_DEFAULT_RENDERMODE){};
71 	virtual int getScreenWidth() = 0;
72 	virtual int getScreenHeight() = 0;
73 
74 	virtual void resize(int width, int height) = 0;
75 
76 	virtual int registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling) = 0;
77 	virtual int registerGraphicsInstance(int shapeIndex, const double* position, const double* quaternion, const double* color, const double* scaling) = 0;
78 	virtual void drawLines(const float* positions, const float color[4], int numPoints, int pointStrideInBytes, const unsigned int* indices, int numIndices, float pointDrawSize) = 0;
79 	virtual void drawLine(const float from[4], const float to[4], const float color[4], float lineWidth) = 0;
80 	virtual void drawLine(const double from[4], const double to[4], const double color[4], double lineWidth) = 0;
81 	virtual void drawPoint(const float* position, const float color[4], float pointDrawSize) = 0;
82 	virtual void drawPoint(const double* position, const double color[4], double pointDrawSize) = 0;
83 	virtual void drawTexturedTriangleMesh(float worldPosition[3], float worldOrientation[4], const float* vertices, int numvertices, const unsigned int* indices, int numIndices, float color[4], int textureIndex = -1, int vertexLayout = 0) = 0;
84 
85 	virtual int registerShape(const float* vertices, int numvertices, const int* indices, int numIndices, int primitiveType = B3_GL_TRIANGLES, int textureIndex = -1) = 0;
86 	virtual void updateShape(int shapeIndex, const float* vertices, int numVertices) = 0;
87 
88 	virtual int registerTexture(const unsigned char* texels, int width, int height, bool flipPixelsY = true) = 0;
89 	virtual void updateTexture(int textureIndex, const unsigned char* texels, bool flipPixelsY = true) = 0;
90 	virtual void activateTexture(int textureIndex) = 0;
replaceTextureCommonRenderInterface91 	virtual void replaceTexture(int shapeIndex, int textureIndex){};
92 	virtual void removeTexture(int textureIndex) = 0;
93 
setPlaneReflectionShapeIndexCommonRenderInterface94 	virtual void setPlaneReflectionShapeIndex(int index) {}
95 
getShapeIndexFromInstanceCommonRenderInterface96 	virtual int getShapeIndexFromInstance(int srcIndex) { return -1; }
97 
98 	virtual bool readSingleInstanceTransformToCPU(float* position, float* orientation, int srcIndex) = 0;
99 
100 	virtual void writeSingleInstanceTransformToCPU(const float* position, const float* orientation, int srcIndex) = 0;
101 	virtual void writeSingleInstanceTransformToCPU(const double* position, const double* orientation, int srcIndex) = 0;
102 	virtual void writeSingleInstanceColorToCPU(const float* color, int srcIndex) = 0;
103 	virtual void writeSingleInstanceColorToCPU(const double* color, int srcIndex) = 0;
104 	virtual void writeSingleInstanceScaleToCPU(const float* scale, int srcIndex) = 0;
105 	virtual void writeSingleInstanceScaleToCPU(const double* scale, int srcIndex) = 0;
106 	virtual void writeSingleInstanceSpecularColorToCPU(const double* specular, int srcIndex) = 0;
107 	virtual void writeSingleInstanceSpecularColorToCPU(const float* specular, int srcIndex) = 0;
108 	virtual void writeSingleInstanceFlagsToCPU(int flags, int srcIndex) = 0;
109 
110 	virtual int getTotalNumInstances() const = 0;
111 
112 	virtual void writeTransforms() = 0;
113 
114 	virtual void clearZBuffer() = 0;
115 
116 	//This is internal access to OpenGL3+ features, mainly used for OpenCL-OpenGL interop
117 	//Only the GLInstancingRenderer supports it, just return 0 otherwise.
118 	virtual struct GLInstanceRendererInternalData* getInternalData() = 0;
119 };
120 
121 template <typename T>
projectWorldCoordToScreen(T objx,T objy,T objz,const T modelMatrix[16],const T projMatrix[16],const int viewport[4],T * winx,T * winy,T * winz)122 inline int projectWorldCoordToScreen(T objx, T objy, T objz,
123 									 const T modelMatrix[16],
124 									 const T projMatrix[16],
125 									 const int viewport[4],
126 									 T* winx, T* winy, T* winz)
127 {
128 	int i;
129 	T in2[4];
130 	T tmp[4];
131 
132 	in2[0] = objx;
133 	in2[1] = objy;
134 	in2[2] = objz;
135 	in2[3] = T(1.0);
136 
137 	for (i = 0; i < 4; i++)
138 	{
139 		tmp[i] = in2[0] * modelMatrix[0 * 4 + i] + in2[1] * modelMatrix[1 * 4 + i] +
140 				 in2[2] * modelMatrix[2 * 4 + i] + in2[3] * modelMatrix[3 * 4 + i];
141 	}
142 
143 	T out[4];
144 	for (i = 0; i < 4; i++)
145 	{
146 		out[i] = tmp[0] * projMatrix[0 * 4 + i] + tmp[1] * projMatrix[1 * 4 + i] + tmp[2] * projMatrix[2 * 4 + i] + tmp[3] * projMatrix[3 * 4 + i];
147 	}
148 
149 	if (out[3] == T(0.0))
150 		return 0;
151 	out[0] /= out[3];
152 	out[1] /= out[3];
153 	out[2] /= out[3];
154 	/* Map x, y and z to range 0-1 */
155 	out[0] = out[0] * T(0.5) + T(0.5);
156 	out[1] = out[1] * T(0.5) + T(0.5);
157 	out[2] = out[2] * T(0.5) + T(0.5);
158 
159 	/* Map x,y to viewport */
160 	out[0] = out[0] * viewport[2] + viewport[0];
161 	out[1] = out[1] * viewport[3] + viewport[1];
162 
163 	*winx = out[0];
164 	*winy = out[1];
165 	*winz = out[2];
166 	return 1;
167 }
168 
169 #endif  //COMMON_RENDER_INTERFACE_H
170