Geant4-11
G4OpenGLVboDrawer.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27//
28//
29// Class to provide Vertex Buffer Object (VBO) specific
30// functionality for OpenGL > 2.0 in GEANT4
31
32#include "G4OpenGLViewer.hh"
33#ifdef G4OPENGL_VERSION_2
34
35#include "G4OpenGLVboDrawer.hh"
36
38
39
41G4OpenGLVboDrawer::G4OpenGLVboDrawer (G4OpenGLViewer* viewer,
42 std::string type
43 ):
44fVboViewer(NULL),
45fOGLType(type)
48{
49 G4OpenGLImmediateQtViewer* v = dynamic_cast<G4OpenGLImmediateQtViewer*>(viewer);
50 if (v) {
51 fVboViewer = v;
52 }
53
54 fFragmentShaderSrc =
55 "#ifdef GL_ES\n"
56 "precision highp float;\n"
57 "#endif\n"
58 "\n"
59 "varying vec3 vLightWeighting;\n"
60 "uniform vec4 uPointColor; // Point Color\n"
61 "\n"
62 "void main(void) {\n"
63 " vec4 matColor = uPointColor;\n"
64 " gl_FragColor = vec4(matColor.rgb, matColor.a);\n"
65 "}\n";
66
67
68 fVertexShaderSrc =
69 "attribute highp vec4 aVertexPosition;\n"
70 "attribute vec3 aVertexNormal;\n"
71 "uniform highp mat4 uCMatrix;\n"
72 "uniform highp mat4 uPMatrix; // Perspective [P]rojection matrix\n"
73 "uniform highp mat4 uMVMatrix; // [M]odel[V]iew matrix\n"
74 "uniform highp mat4 uTMatrix; // [T]ransformation matrix\n"
75 "uniform float uPointSize; // Point size\n"
76 "void main(void)\n"
77 "{\n"
78 " gl_Position = uPMatrix * uCMatrix * uMVMatrix * uTMatrix * aVertexPosition;\n"
79 " // Phong shading\n"
80 // " vec3 transformedNormal = normalize((uNMatrix * vec4(normalize(aVertexNormal), 0)).xyz);\n"
81 " vec3 lightingDirection = normalize(vec3(1, 1, 1));\n"
82 // " float directionalLightWeighting = max(dot(transformedNormal, lightingDirection), 0.0);\n"
83 // " vec3 uAmbientLightColor = vec3(0.2, 0.2, 0.2);\n"
84 // " vec3 uDirectionalColor = vec3(0.8, 0.8, 0.8);\n"
85 " gl_PointSize = uPointSize;\n"
86 // " vLightWeighting = uAmbientLightColor + uDirectionalColor * directionalLightWeighting;\n"
87 "}";
88}
89
91G4OpenGLVboDrawer::~G4OpenGLVboDrawer (
92)
95{
96}
97
98// +--------------------------------+
99// + QT (OpenGL ES) case +
100// +--------------------------------+
101
102void G4OpenGLVboDrawer:: vboGlMultMatrixf(const GLfloat *matrix){
103 if (fVboViewer) {
104 if (fVboViewer->isInitialized()) {
105 // FIXME
106 // glUniformMatrix4fv(12, 1, 0, 0x7fff5fbf5d00)
107 // Error: GL_INVALID_OPERATION
108
109 if (fMatrixMode == GL_MODELVIEW) {
110 glUniformMatrix4fv(fVboViewer->getShaderTransformMatrix(),1,0,matrix);
111 } else {
112 G4cerr << "glMultMatrixf could only be used in GL_MODELVIEW mode" << G4endl;
113 }
114 }
115 }
116}
117
118
119void G4OpenGLVboDrawer:: vboGlMultMatrixd(const GLdouble *matrix){
120 if (fVboViewer) {
121 if (fVboViewer->isInitialized()) {
122 // FIXME !
123 // if (fMatrixMode == GL_MODELVIEW) {
124 // printf("G4OpenGLVboDrawer:: vboGlMultMatrixd %d %d\n",fVboViewer->getShaderTransformMatrix(), matrix);
126 float mat[16] = {
127 matrix[0],matrix[1],matrix[2],matrix[3],
128 matrix[4],matrix[5],matrix[6],matrix[7],
129 matrix[8],matrix[9],matrix[10],matrix[11],
130 matrix[12],matrix[13],matrix[14],matrix[15]
131 };
132
133 glUniformMatrix4fv(fVboViewer->getShaderTransformMatrix(),1,0,mat);
134 GLenum e = glGetError();
135 printf("GL error : %d",e);
136 // } else {
137 // G4cerr << "glMultMatrixd could only be used in GL_MODELVIEW mode" << G4endl;
138 // }
139 }
140 }
141}
142
143
144
145// +--------------------------------+
146// + All case +
147// +--------------------------------+
148
149
150
151void G4OpenGLVboDrawer::vboGlOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
152 if (fVboViewer) {
153 if (fVboViewer->isInitialized()) {
154 printf("glOrtho implemented --- %f %f %f %f %f %f \n",left, right, bottom, top, zNear, zFar);
155 float a = 2.0f / (right - left);
156 float b = 2.0f / (top - bottom);
157 float c = -2.0f / (zFar - zNear);
158
159 float tx = - (right + left)/(right - left);
160 float ty = - (top + bottom)/(top - bottom);
161 float tz = - (zFar + zNear)/(zFar - zNear);
162
163 float ortho[16] = {
164 a, 0, 0, 0,
165 0, b, 0, 0,
166 0, 0, c, 0,
167 tx, ty, tz, 1
168 };
169 // FIXME :
170 // glUniformMatrix4fv(0, 1, 0, 0x7fff5fbf5d00)
171 // Error: GL_INVALID_OPERATION
172
173 if (fMatrixMode == GL_PROJECTION) {
174 glUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), 1, 0, ortho);
175 } else {
176 G4cerr << "glFrustum could only be used in GL_PROJECTION mode" << G4endl;
177 }
178 }
179 }
180}
181
182
183void G4OpenGLVboDrawer::vboGlFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
184 if (fVboViewer) {
185 if (fVboViewer->isInitialized()) {
186 float deltaX = right - left;
187 float deltaY = top - bottom;
188 float deltaZ = zFar - zNear;
189
190 float a = 2.0f * zNear / deltaX;
191 float b = 2.0f * zNear / deltaY;
192 float c = (right + left) / deltaX;
193 float d = (top + bottom) / deltaY;
194 float e = -(zFar + zNear) / (zFar - zNear);
195 float f = -2.0f * zFar * zNear / deltaZ;
196
197 float proj[16] = {
198 a, 0, 0, 0,
199 0, b, 0, 0,
200 c, d, e, -1.0f,
201 0, 0, f, 0
202 };
203
204 if (fMatrixMode == GL_PROJECTION) {
205 glUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), 1, 0, proj);
206 } else {
207 G4cerr << "glFrustrum could only be used in GL_PROJECTION mode" << G4endl;
208 }
209 }
210 }
211}
212
213
214void G4OpenGLVboDrawer::vboGlMatrixMode(GLenum a) {
215 if (fVboViewer) {
216 if (fVboViewer->isInitialized()) {
217 printf("G4OpenGLVboDrawer::vboGlMatrixMode CHANGED :%d \n",a);
218 fMatrixMode = a;
219 }
220 }
221}
222
223
224void G4OpenGLVboDrawer::vboGlColor4d(int red,int green,int blue,int alpha) {
225 if (fVboViewer) {
226 if (fVboViewer->isInitialized()) {
227 // double color [] = { red, green, blue, alpha };
228 // FIXME : REMOVE /2 , used to render transparents for testing purpose
229 alpha = 0.7;
230 glUniform4f (glGetUniformLocation(fVboViewer->getShaderProgram(), "uPointColor"),red, green, blue, alpha);
231 }
232 }
233}
234
235void G4OpenGLVboDrawer:: vboGlColor4fv(const GLfloat* data) {
236 if (fVboViewer) {
237 if (fVboViewer->isInitialized()) {
238 double color [] = { (data[0]), (data[1]), (data[2]), 0.7};
239 // FIXME : REMOVE /2 , used to render transparents for testing purpose
240 glUniform4f (glGetUniformLocation(fVboViewer->getShaderProgram(), "uPointColor"),color[0],color[1],color[2], color[3]);
241 }
242 }
243}
244
245void G4OpenGLVboDrawer:: vboGlPointSize(float size) {
246 if (fVboViewer) {
247 if (fVboViewer->isInitialized()) {
248 glUniform1f (glGetUniformLocation(fVboViewer->getShaderProgram(), "uPointSize"),size);
249 }
250 }
251}
252
253#endif
254
static const G4double alpha
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57