35 #ifdef G4OPENGL_VERSION_2
39 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
47 G4OpenGLVboDrawer::G4OpenGLVboDrawer (G4OpenGLViewer* viewer,
55 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
56 G4OpenGLImmediateWtViewer*
v =
dynamic_cast<G4OpenGLImmediateWtViewer*
>(viewer);
58 G4OpenGLImmediateQtViewer* v =
dynamic_cast<G4OpenGLImmediateQtViewer*
>(viewer);
66 "precision highp float;\n"
69 "varying vec3 vLightWeighting;\n"
70 "uniform vec4 uPointColor; // Point Color\n"
73 " vec4 matColor = uPointColor;\n"
74 " gl_FragColor = vec4(matColor.rgb, matColor.a);\n"
78 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
80 "attribute vec3 aVertexPosition;\n"
81 "attribute vec3 aVertexNormal;\n"
83 "uniform mat4 uMVMatrix; // [M]odel[V]iew matrix\n"
84 "uniform mat4 uCMatrix; // Client-side manipulated [C]amera matrix\n"
85 "uniform mat4 uPMatrix; // Perspective [P]rojection matrix\n"
86 "uniform mat4 uNMatrix; // [N]ormal transformation\n"
87 "// uNMatrix is the transpose of the inverse of uCMatrix * uMVMatrix\n"
88 "uniform mat4 uTMatrix; // [T]ransformation matrix\n"
89 "uniform float uPointSize; // Point size\n"
91 "varying vec3 vLightWeighting;\n"
94 " // Calculate the position of this vertex\n"
95 " gl_Position = uPMatrix * uCMatrix * uMVMatrix * uTMatrix * vec4(aVertexPosition, 1.0);\n"
98 " vec3 transformedNormal = normalize((uNMatrix * vec4(normalize(aVertexNormal), 0)).xyz);\n"
99 " vec3 lightingDirection = normalize(vec3(1, 1, 1));\n"
100 " float directionalLightWeighting = max(dot(transformedNormal, lightingDirection), 0.0);\n"
101 " vec3 uAmbientLightColor = vec3(0.2, 0.2, 0.2);\n"
102 " vec3 uDirectionalColor = vec3(0.8, 0.8, 0.8);\n"
103 " gl_PointSize = uPointSize;\n"
104 " vLightWeighting = uAmbientLightColor + uDirectionalColor * directionalLightWeighting;\n"
111 "attribute highp vec4 aVertexPosition;\n"
112 "attribute vec3 aVertexNormal;\n"
113 "uniform highp mat4 uCMatrix;\n"
114 "uniform highp mat4 uPMatrix; // Perspective [P]rojection matrix\n"
115 "uniform highp mat4 uMVMatrix; // [M]odel[V]iew matrix\n"
116 "uniform highp mat4 uTMatrix; // [T]ransformation matrix\n"
117 "uniform float uPointSize; // Point size\n"
120 " gl_Position = uPMatrix * uCMatrix * uMVMatrix * uTMatrix * aVertexPosition;\n"
121 " // Phong shading\n"
123 " vec3 lightingDirection = normalize(vec3(1, 1, 1));\n"
127 " gl_PointSize = uPointSize;\n"
136 G4OpenGLVboDrawer::~G4OpenGLVboDrawer (
146 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
148 void G4OpenGLVboDrawer:: vboGlClear(Wt::WFlags< GLenum >
mask) {
149 if (fVboViewer->isInitialized()) {
150 fVboViewer->clear(mask);
155 void G4OpenGLVboDrawer:: vboGlUniformMatrix4(
const Wt::WGLWidget::UniformLocation &location,
const Wt::WMatrix4x4 &
m) {
157 vboGlUseProgram(fVboViewer->getShaderProgram());
158 fVboViewer->uniformMatrix4(location, m);
163 void G4OpenGLVboDrawer:: vboGlUniformMatrix4(
const Wt::WGLWidget::UniformLocation &location,
const double* matrix) {
166 (
double) matrix[0], (
double) matrix[4], (
double) matrix[8], (
double) matrix[12],
167 (
double) matrix[1], (
double) matrix[5], (
double) matrix[9], (
double) matrix[13],
168 (
double) matrix[2], (
double) matrix[6], (
double) matrix[10],(
double) matrix[14],
169 (
double) matrix[3],(
double) matrix[7],(
double) matrix[11],(
double) matrix[15]);
171 fVboViewer->uniformMatrix4(location, mat);
175 void G4OpenGLVboDrawer:: vboGlUniformMatrix4fv(
const Wt::WGLWidget::UniformLocation &location,
const float* matrix) {
178 (
double) matrix[0], (
double) matrix[4], (
double) matrix[8], (
double) matrix[12],
179 (
double) matrix[1], (
double) matrix[5], (
double) matrix[9], (
double) matrix[13],
180 (
double) matrix[2], (
double) matrix[6], (
double) matrix[10],(
double) matrix[14],
181 (
double) matrix[3],(
double) matrix[7],(
double) matrix[11],(
double) matrix[15]);
183 fVboViewer->uniformMatrix4(location, mat);
187 void G4OpenGLVboDrawer:: vboGlUniformMatrix4(
const Wt::WGLWidget::UniformLocation &location,
const Wt::WGLWidget::JavaScriptMatrix4x4 &m) {
188 if (fVboViewer->isInitialized()) {
189 fVboViewer->uniformMatrix4(location, m);
193 Wt::WGLWidget::Buffer G4OpenGLVboDrawer:: vboGlCreateBuffer(){
195 return fVboViewer->createBuffer();
197 return Wt::WGLWidget::Buffer();
200 void G4OpenGLVboDrawer:: vboGlBindBuffer(GLenum
target, Wt::WGLWidget::Buffer
buffer){
202 fVboViewer->bindBuffer(target,buffer);
206 void G4OpenGLVboDrawer:: vboGlDeleteBuffer(Wt::WGLWidget::Buffer buffer){
207 if (fVboViewer == NULL)
return;
208 fVboViewer->deleteBuffer(buffer);
211 void G4OpenGLVboDrawer:: vboGlVertexAttribPointer(Wt::WGLWidget::AttribLocation location,
int size, GLenum type,
bool normalized,
unsigned stride,
unsigned offset){
212 if (fVboViewer->isInitialized()) {
213 fVboViewer->vertexAttribPointer(location, size,type, normalized, stride, offset);
217 Wt::WGLWidget::Program G4OpenGLVboDrawer:: vboGlCreateProgram(){
219 return fVboViewer->createProgram();
221 return Wt::WGLWidget::Program();
224 void G4OpenGLVboDrawer:: vboGlAttachShader(Wt::WGLWidget::Program program, Shader shader){
226 fVboViewer->attachShader(program,shader);
230 void G4OpenGLVboDrawer:: vboGlLinkProgram(Wt::WGLWidget::Program program){
232 fVboViewer->linkProgram(program);
236 void G4OpenGLVboDrawer:: vboGlUseProgram(Wt::WGLWidget::Program program){
238 fVboViewer->useProgram(program);
242 void G4OpenGLVboDrawer:: vboGlEnableVertexAttribArray(Wt::WGLWidget::AttribLocation pointer){
244 fVboViewer->enableVertexAttribArray(pointer);
248 void G4OpenGLVboDrawer:: vboGlDisableVertexAttribArray(Wt::WGLWidget::AttribLocation pointer){
250 fVboViewer->disableVertexAttribArray(pointer);
254 Wt::WGLWidget::UniformLocation G4OpenGLVboDrawer:: vboGlGetUniformLocation(Wt::WGLWidget::Program programm,
const std::string &src){
256 return fVboViewer->getUniformLocation(programm, src);
258 return Wt::WGLWidget::UniformLocation();
261 Wt::WGLWidget::AttribLocation G4OpenGLVboDrawer:: vboGlGetAttribLocation(Wt::WGLWidget::Program shader,
const std::string &src){
263 return fVboViewer->getAttribLocation(shader, src);
265 return Wt::WGLWidget::AttribLocation();
272 void G4OpenGLVboDrawer::vboGlClearColor (
double r,
double g,
double b,
double a) {
274 if (fVboViewer->isInitialized() ) {
275 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
276 fVboViewer->clearColor(r,g,b,a);
282 void G4OpenGLVboDrawer::vboGlClearDepth(
double depth) {
283 if (fVboViewer->isInitialized()) {
284 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
285 fVboViewer->clearDepth(depth);
293 void G4OpenGLVboDrawer::vboGlFlush() {
294 if (fVboViewer->isInitialized()) {
295 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
302 void G4OpenGLVboDrawer:: vboGlViewport(
int x,
int y,
unsigned width,
unsigned height) {
303 if (fVboViewer->isInitialized()) {
304 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
305 fVboViewer->viewport(x,y,width,height);
311 void G4OpenGLVboDrawer:: vboGlEnable(GLenum cap) {
312 if (fVboViewer->isInitialized()) {
313 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
315 fVboViewer->enable(cap);
322 void G4OpenGLVboDrawer:: vboGlDisable(GLenum cap) {
323 if (fVboViewer->isInitialized()) {
324 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
326 fVboViewer->disable(cap);
333 void G4OpenGLVboDrawer:: vboGlBlendFunc (GLenum sfactor, GLenum dfactor) {
334 if (fVboViewer->isInitialized()) {
335 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
336 fVboViewer->blendFunc(sfactor, dfactor);
343 void G4OpenGLVboDrawer:: vboGlDepthFunc (GLenum func) {
344 if (fVboViewer->isInitialized()) {
345 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
346 fVboViewer->depthFunc(func);
352 void G4OpenGLVboDrawer:: vboGlDepthMask(
bool flag) {
353 if (fVboViewer->isInitialized()) {
354 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
355 fVboViewer->depthMask(flag);
361 void G4OpenGLVboDrawer:: vboGlColorMask (
bool red,
bool green,
bool blue,
bool alpha) {
362 if (fVboViewer->isInitialized()) {
363 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
364 fVboViewer->colorMask(red,green,blue,alpha);
370 void G4OpenGLVboDrawer:: vboGlLineWidth(
double width) {
371 if (fVboViewer->isInitialized()) {
372 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
373 fVboViewer->lineWidth(width);
380 void G4OpenGLVboDrawer:: vboGlDrawArrays(GLenum mode,
int first,
unsigned count){
381 if (fVboViewer->isInitialized()) {
382 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
383 fVboViewer->drawArrays(mode,first, count);
389 void G4OpenGLVboDrawer:: vboGlDrawElements(GLenum mode,
unsigned count, GLenum type,
unsigned offset){
390 if (fVboViewer->isInitialized()) {
391 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
392 fVboViewer->drawElements(mode,count,type,offset);
398 void G4OpenGLVboDrawer:: vboGlBufferDatafv(GLenum target,
const std::vector<double>::iterator begin,
const std::vector<double>::iterator end, GLenum
usage){
400 if (fVboViewer->isInitialized()) {
401 fVboViewer->bufferDatafv(target,begin,end,usage);
406 void G4OpenGLVboDrawer:: vboGlBufferDataiv(GLenum target,
const std::vector<unsigned short>::iterator begin,
const std::vector<unsigned short>::iterator end, GLenum usage, GLenum type){
408 if (fVboViewer->isInitialized()) {
409 fVboViewer->bufferDataiv(target,begin,end,usage,type);
415 void G4OpenGLVboDrawer:: vboGlMultMatrixd(
const GLdouble *matrix){
417 if (fVboViewer->isInitialized()) {
419 (
double) matrix[0], (
double) matrix[4], (
double) matrix[8], (
double) matrix[12],
420 (
double) matrix[1], (
double) matrix[5], (
double) matrix[9], (
double) matrix[13],
421 (
double) matrix[2], (
double) matrix[6], (
double) matrix[10],(
double) matrix[14],
422 (
double) matrix[3],(
double) matrix[7],(
double) matrix[11],(
double) matrix[15]);
425 fVboViewer->uniformMatrix4(fVboViewer->getShaderTransformMatrix(),mat);
426 if (fMatrixMode == GL_MODELVIEW) {
429 G4cerr <<
"glMultMatrixd could only be used in GL_MODELVIEW mode" <<
G4endl;
435 void G4OpenGLVboDrawer:: vboGlMultMatrixf(
const GLfloat *matrix){
437 if (fVboViewer->isInitialized()) {
439 matrix[0], matrix[4], matrix[8], matrix[12],
440 matrix[1], matrix[5], matrix[9], matrix[13],
441 matrix[2], matrix[6], matrix[10], matrix[14],
442 matrix[3], matrix[7], matrix[11], matrix[15]);
444 if (fMatrixMode == GL_MODELVIEW) {
445 fVboViewer->uniformMatrix4(fVboViewer->getShaderTransformMatrix(),mat);
447 G4cerr <<
"glMultMatrixf could only be used in GL_MODELVIEW mode" <<
G4endl;
453 void G4OpenGLVboDrawer:: vboGlShaderSource(Shader shader, GLsizei ,
const GLchar **src,
const GLint *){
455 std::string
s = *src;
456 fVboViewer->shaderSource(shader, s);
460 void G4OpenGLVboDrawer:: vboGlCompileShader(Shader shader){
462 fVboViewer->compileShader(shader);
466 Shader G4OpenGLVboDrawer:: vboGlCreateShader(GLenum shader){
468 return fVboViewer->createShader(shader);
479 void G4OpenGLVboDrawer:: vboGlMultMatrixf(
const GLfloat *matrix){
481 if (fVboViewer->isInitialized()) {
486 if (fMatrixMode == GL_MODELVIEW) {
487 glUniformMatrix4fv(fVboViewer->getShaderTransformMatrix(),1,0,matrix);
489 G4cerr <<
"glMultMatrixf could only be used in GL_MODELVIEW mode" <<
G4endl;
496 void G4OpenGLVboDrawer:: vboGlMultMatrixd(
const GLdouble *matrix){
498 if (fVboViewer->isInitialized()) {
504 matrix[0],matrix[1],matrix[2],matrix[3],
505 matrix[4],matrix[5],matrix[6],matrix[7],
506 matrix[8],matrix[9],matrix[10],matrix[11],
507 matrix[12],matrix[13],matrix[14],matrix[15]
510 glUniformMatrix4fv(fVboViewer->getShaderTransformMatrix(),1,0,mat);
511 GLenum e = glGetError();
512 printf(
"GL error : %d",e);
529 void G4OpenGLVboDrawer::vboGlOrtho(GLdouble
left, GLdouble
right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
531 if (fVboViewer->isInitialized()) {
532 printf(
"glOrtho implemented --- %f %f %f %f %f %f \n",left, right, bottom, top, zNear, zFar);
533 float a = 2.0f / (right -
left);
534 float b = 2.0f / (top - bottom);
535 float c = -2.0f / (zFar - zNear);
537 float tx = - (right +
left)/(right - left);
538 float ty = - (top + bottom)/(top - bottom);
539 float tz = - (zFar + zNear)/(zFar - zNear);
551 if (fMatrixMode == GL_PROJECTION) {
552 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
553 vboGlUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), ortho);
555 glUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), 1, 0, ortho);
558 G4cerr <<
"glFrustum could only be used in GL_PROJECTION mode" <<
G4endl;
565 void G4OpenGLVboDrawer::vboGlFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
567 if (fVboViewer->isInitialized()) {
568 float deltaX = right -
left;
569 float deltaY = top - bottom;
570 float deltaZ = zFar - zNear;
572 float a = 2.0f * zNear / deltaX;
573 float b = 2.0f * zNear / deltaY;
574 float c = (right +
left) / deltaX;
575 float d = (top + bottom) / deltaY;
576 float e = -(zFar + zNear) / (zFar - zNear);
577 float f = -2.0f * zFar * zNear / deltaZ;
586 if (fMatrixMode == GL_PROJECTION) {
587 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
588 vboGlUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), proj);
590 glUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), 1, 0, proj);
593 G4cerr <<
"glFrustrum could only be used in GL_PROJECTION mode" <<
G4endl;
600 void G4OpenGLVboDrawer::vboGlMatrixMode(GLenum a) {
602 if (fVboViewer->isInitialized()) {
603 printf(
"G4OpenGLVboDrawer::vboGlMatrixMode CHANGED :%d \n",a);
610 void G4OpenGLVboDrawer::vboGlColor4d(
int red,
int green,
int blue,
int alpha) {
612 if (fVboViewer->isInitialized()) {
615 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
616 double color [] = {
red, green,
blue, 0.7 };
617 glUniform4fv (fVboViewer->getUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),color);
620 glUniform4f (glGetUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),red, green, blue, alpha);
626 void G4OpenGLVboDrawer:: vboGlColor4fv(
const GLfloat*
data) {
628 if (fVboViewer->isInitialized()) {
629 double color [] = { (data[0]), (data[1]), (data[2]), 0.7};
631 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
632 glUniform4fv (fVboViewer->getUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),color);
634 glUniform4f (glGetUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),color[0],color[1],color[2], color[3]);
640 void G4OpenGLVboDrawer:: vboGlPointSize(
float size) {
642 if (fVboViewer->isInitialized()) {
643 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
644 glUniform1f(fVboViewer->getUniformLocation(fVboViewer->getShaderProgram(),
"uPointSize"),size);
646 glUniform1f (glGetUniformLocation(fVboViewer->getShaderProgram(),
"uPointSize"),size);
std::vector< ExP01TrackerHit * > a
const XML_Char const XML_Char * data
static constexpr double m
static const G4double alpha
G4GLOB_DLL std::ostream G4cerr