35 #ifdef G4OPENGL_VERSION_2
37 #define G4DEBUG_VIS_OGL 1
41 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
49 G4OpenGLVboDrawer::G4OpenGLVboDrawer (G4OpenGLViewer* viewer,
57 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
58 G4OpenGLImmediateWtViewer* v =
dynamic_cast<G4OpenGLImmediateWtViewer*
>(viewer);
60 G4OpenGLImmediateQtViewer* v =
dynamic_cast<G4OpenGLImmediateQtViewer*
>(viewer);
68 "precision highp float;\n"
71 "varying vec3 vLightWeighting;\n"
72 "uniform vec4 uPointColor; // Point Color\n"
75 " vec4 matColor = uPointColor;\n"
76 " gl_FragColor = vec4(matColor.rgb, matColor.a);\n"
80 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
82 "attribute vec3 aVertexPosition;\n"
83 "attribute vec3 aVertexNormal;\n"
85 "uniform mat4 uMVMatrix; // [M]odel[V]iew matrix\n"
86 "uniform mat4 uCMatrix; // Client-side manipulated [C]amera matrix\n"
87 "uniform mat4 uPMatrix; // Perspective [P]rojection matrix\n"
88 "uniform mat4 uNMatrix; // [N]ormal transformation\n"
89 "// uNMatrix is the transpose of the inverse of uCMatrix * uMVMatrix\n"
90 "uniform mat4 uTMatrix; // [T]ransformation matrix\n"
91 "uniform float uPointSize; // Point size\n"
93 "varying vec3 vLightWeighting;\n"
96 " // Calculate the position of this vertex\n"
97 " gl_Position = uPMatrix * uCMatrix * uMVMatrix * uTMatrix * vec4(aVertexPosition, 1.0);\n"
100 " vec3 transformedNormal = normalize((uNMatrix * vec4(normalize(aVertexNormal), 0)).xyz);\n"
101 " vec3 lightingDirection = normalize(vec3(1, 1, 1));\n"
102 " float directionalLightWeighting = max(dot(transformedNormal, lightingDirection), 0.0);\n"
103 " vec3 uAmbientLightColor = vec3(0.2, 0.2, 0.2);\n"
104 " vec3 uDirectionalColor = vec3(0.8, 0.8, 0.8);\n"
105 " gl_PointSize = uPointSize;\n"
106 " vLightWeighting = uAmbientLightColor + uDirectionalColor * directionalLightWeighting;\n"
113 "attribute highp vec4 aVertexPosition;\n"
114 "attribute vec3 aVertexNormal;\n"
115 "uniform highp mat4 uCMatrix;\n"
116 "uniform highp mat4 uPMatrix; // Perspective [P]rojection matrix\n"
117 "uniform highp mat4 uMVMatrix; // [M]odel[V]iew matrix\n"
118 "uniform highp mat4 uTMatrix; // [T]ransformation matrix\n"
119 "uniform float uPointSize; // Point size\n"
122 " gl_Position = uPMatrix * uCMatrix * uMVMatrix * uTMatrix * aVertexPosition;\n"
123 " // Phong shading\n"
125 " vec3 lightingDirection = normalize(vec3(1, 1, 1));\n"
129 " gl_PointSize = uPointSize;\n"
138 G4OpenGLVboDrawer::~G4OpenGLVboDrawer (
148 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
150 void G4OpenGLVboDrawer:: vboGlClear(Wt::WFlags< GLenum >
mask) {
151 if (fVboViewer->isInitialized()) {
152 fVboViewer->clear(mask);
157 void G4OpenGLVboDrawer:: vboGlUniformMatrix4(
const Wt::WGLWidget::UniformLocation &location,
const Wt::WMatrix4x4 &
m) {
159 vboGlUseProgram(fVboViewer->getShaderProgram());
160 fVboViewer->uniformMatrix4(location, m);
165 void G4OpenGLVboDrawer:: vboGlUniformMatrix4(
const Wt::WGLWidget::UniformLocation &location,
const double* matrix) {
168 (
double) matrix[0], (
double) matrix[4], (
double) matrix[8], (
double) matrix[12],
169 (
double) matrix[1], (
double) matrix[5], (
double) matrix[9], (
double) matrix[13],
170 (
double) matrix[2], (
double) matrix[6], (
double) matrix[10],(
double) matrix[14],
171 (
double) matrix[3],(
double) matrix[7],(
double) matrix[11],(
double) matrix[15]);
173 fVboViewer->uniformMatrix4(location, mat);
177 void G4OpenGLVboDrawer:: vboGlUniformMatrix4fv(
const Wt::WGLWidget::UniformLocation &location,
const float* matrix) {
180 (
double) matrix[0], (
double) matrix[4], (
double) matrix[8], (
double) matrix[12],
181 (
double) matrix[1], (
double) matrix[5], (
double) matrix[9], (
double) matrix[13],
182 (
double) matrix[2], (
double) matrix[6], (
double) matrix[10],(
double) matrix[14],
183 (
double) matrix[3],(
double) matrix[7],(
double) matrix[11],(
double) matrix[15]);
185 fVboViewer->uniformMatrix4(location, mat);
189 void G4OpenGLVboDrawer:: vboGlUniformMatrix4(
const Wt::WGLWidget::UniformLocation &location,
const Wt::WGLWidget::JavaScriptMatrix4x4 &m) {
190 if (fVboViewer->isInitialized()) {
191 fVboViewer->uniformMatrix4(location, m);
195 Wt::WGLWidget::Buffer G4OpenGLVboDrawer:: vboGlCreateBuffer(){
197 return fVboViewer->createBuffer();
199 return Wt::WGLWidget::Buffer();
202 void G4OpenGLVboDrawer:: vboGlBindBuffer(GLenum target, Wt::WGLWidget::Buffer
buffer){
204 fVboViewer->bindBuffer(target,buffer);
208 void G4OpenGLVboDrawer:: vboGlDeleteBuffer(Wt::WGLWidget::Buffer buffer){
209 if (fVboViewer == NULL)
return;
210 fVboViewer->deleteBuffer(buffer);
213 void G4OpenGLVboDrawer:: vboGlVertexAttribPointer(Wt::WGLWidget::AttribLocation location,
int size, GLenum type,
bool normalized,
unsigned stride,
unsigned offset){
214 if (fVboViewer->isInitialized()) {
215 fVboViewer->vertexAttribPointer(location, size,type, normalized, stride, offset);
219 Wt::WGLWidget::Program G4OpenGLVboDrawer:: vboGlCreateProgram(){
221 return fVboViewer->createProgram();
223 return Wt::WGLWidget::Program();
226 void G4OpenGLVboDrawer:: vboGlAttachShader(Wt::WGLWidget::Program program, Shader shader){
228 fVboViewer->attachShader(program,shader);
232 void G4OpenGLVboDrawer:: vboGlLinkProgram(Wt::WGLWidget::Program program){
234 fVboViewer->linkProgram(program);
238 void G4OpenGLVboDrawer:: vboGlUseProgram(Wt::WGLWidget::Program program){
240 fVboViewer->useProgram(program);
244 void G4OpenGLVboDrawer:: vboGlEnableVertexAttribArray(Wt::WGLWidget::AttribLocation pointer){
246 fVboViewer->enableVertexAttribArray(pointer);
250 void G4OpenGLVboDrawer:: vboGlDisableVertexAttribArray(Wt::WGLWidget::AttribLocation pointer){
252 fVboViewer->disableVertexAttribArray(pointer);
256 Wt::WGLWidget::UniformLocation G4OpenGLVboDrawer:: vboGlGetUniformLocation(Wt::WGLWidget::Program programm,
const std::string &src){
258 return fVboViewer->getUniformLocation(programm, src);
260 return Wt::WGLWidget::UniformLocation();
263 Wt::WGLWidget::AttribLocation G4OpenGLVboDrawer:: vboGlGetAttribLocation(Wt::WGLWidget::Program shader,
const std::string &src){
265 return fVboViewer->getAttribLocation(shader, src);
267 return Wt::WGLWidget::AttribLocation();
274 void G4OpenGLVboDrawer::vboGlClearColor (
double r,
double g,
double b,
double a) {
276 if (fVboViewer->isInitialized() ) {
277 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
278 fVboViewer->clearColor(r,g,b,a);
284 void G4OpenGLVboDrawer::vboGlClearDepth(
double depth) {
285 if (fVboViewer->isInitialized()) {
286 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
287 fVboViewer->clearDepth(depth);
295 void G4OpenGLVboDrawer::vboGlFlush() {
296 if (fVboViewer->isInitialized()) {
297 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
304 void G4OpenGLVboDrawer:: vboGlViewport(
int x,
int y,
unsigned width,
unsigned height) {
305 if (fVboViewer->isInitialized()) {
306 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
307 fVboViewer->viewport(x,y,width,height);
313 void G4OpenGLVboDrawer:: vboGlEnable(GLenum cap) {
314 if (fVboViewer->isInitialized()) {
315 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
317 fVboViewer->enable(cap);
324 void G4OpenGLVboDrawer:: vboGlDisable(GLenum cap) {
325 if (fVboViewer->isInitialized()) {
326 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
328 fVboViewer->disable(cap);
335 void G4OpenGLVboDrawer:: vboGlBlendFunc (GLenum sfactor, GLenum dfactor) {
336 if (fVboViewer->isInitialized()) {
337 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
338 fVboViewer->blendFunc(sfactor, dfactor);
345 void G4OpenGLVboDrawer:: vboGlDepthFunc (GLenum func) {
346 if (fVboViewer->isInitialized()) {
347 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
348 fVboViewer->depthFunc(func);
354 void G4OpenGLVboDrawer:: vboGlDepthMask(
bool flag) {
355 if (fVboViewer->isInitialized()) {
356 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
357 fVboViewer->depthMask(flag);
363 void G4OpenGLVboDrawer:: vboGlColorMask (
bool red,
bool green,
bool blue,
bool alpha) {
364 if (fVboViewer->isInitialized()) {
365 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
366 fVboViewer->colorMask(red,green,blue,alpha);
372 void G4OpenGLVboDrawer:: vboGlLineWidth(
double width) {
373 if (fVboViewer->isInitialized()) {
374 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
375 fVboViewer->lineWidth(width);
382 void G4OpenGLVboDrawer:: vboGlDrawArrays(GLenum mode,
int first,
unsigned count){
383 if (fVboViewer->isInitialized()) {
384 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
385 fVboViewer->drawArrays(mode,first, count);
391 void G4OpenGLVboDrawer:: vboGlDrawElements(GLenum mode,
unsigned count, GLenum type,
unsigned offset){
392 if (fVboViewer->isInitialized()) {
393 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
394 fVboViewer->drawElements(mode,count,type,offset);
400 void G4OpenGLVboDrawer:: vboGlBufferDatafv(GLenum target,
const std::vector<double>::iterator begin,
const std::vector<double>::iterator end, GLenum
usage){
402 if (fVboViewer->isInitialized()) {
403 fVboViewer->bufferDatafv(target,begin,end,usage);
408 void G4OpenGLVboDrawer:: vboGlBufferDataiv(GLenum target,
const std::vector<unsigned short>::iterator begin,
const std::vector<unsigned short>::iterator end, GLenum usage, GLenum type){
410 if (fVboViewer->isInitialized()) {
411 fVboViewer->bufferDataiv(target,begin,end,usage,type);
417 void G4OpenGLVboDrawer:: vboGlMultMatrixd(
const GLdouble *matrix){
419 if (fVboViewer->isInitialized()) {
421 (
double) matrix[0], (
double) matrix[4], (
double) matrix[8], (
double) matrix[12],
422 (
double) matrix[1], (
double) matrix[5], (
double) matrix[9], (
double) matrix[13],
423 (
double) matrix[2], (
double) matrix[6], (
double) matrix[10],(
double) matrix[14],
424 (
double) matrix[3],(
double) matrix[7],(
double) matrix[11],(
double) matrix[15]);
427 fVboViewer->uniformMatrix4(fVboViewer->getShaderTransformMatrix(),mat);
428 if (fMatrixMode == GL_MODELVIEW) {
431 G4cerr <<
"glMultMatrixd could only be used in GL_MODELVIEW mode" <<
G4endl;
437 void G4OpenGLVboDrawer:: vboGlMultMatrixf(
const GLfloat *matrix){
439 if (fVboViewer->isInitialized()) {
441 matrix[0], matrix[4], matrix[8], matrix[12],
442 matrix[1], matrix[5], matrix[9], matrix[13],
443 matrix[2], matrix[6], matrix[10], matrix[14],
444 matrix[3], matrix[7], matrix[11], matrix[15]);
446 if (fMatrixMode == GL_MODELVIEW) {
447 fVboViewer->uniformMatrix4(fVboViewer->getShaderTransformMatrix(),mat);
449 G4cerr <<
"glMultMatrixf could only be used in GL_MODELVIEW mode" <<
G4endl;
455 void G4OpenGLVboDrawer:: vboGlShaderSource(Shader shader, GLsizei ,
const GLchar **src,
const GLint *){
457 std::string
s = *src;
458 fVboViewer->shaderSource(shader, s);
462 void G4OpenGLVboDrawer:: vboGlCompileShader(Shader shader){
464 fVboViewer->compileShader(shader);
468 Shader G4OpenGLVboDrawer:: vboGlCreateShader(GLenum shader){
470 return fVboViewer->createShader(shader);
481 void G4OpenGLVboDrawer:: vboGlMultMatrixf(
const GLfloat *matrix){
483 if (fVboViewer->isInitialized()) {
488 if (fMatrixMode == GL_MODELVIEW) {
489 glUniformMatrix4fv(fVboViewer->getShaderTransformMatrix(),1,0,matrix);
491 G4cerr <<
"glMultMatrixf could only be used in GL_MODELVIEW mode" <<
G4endl;
498 void G4OpenGLVboDrawer:: vboGlMultMatrixd(
const GLdouble *matrix){
500 if (fVboViewer->isInitialized()) {
506 matrix[0],matrix[1],matrix[2],matrix[3],
507 matrix[4],matrix[5],matrix[6],matrix[7],
508 matrix[8],matrix[9],matrix[10],matrix[11],
509 matrix[12],matrix[13],matrix[14],matrix[15]
512 glUniformMatrix4fv(fVboViewer->getShaderTransformMatrix(),1,0,mat);
513 GLenum e = glGetError();
514 printf(
"GL error : %d",e);
531 void G4OpenGLVboDrawer::vboGlOrtho(GLdouble
left, GLdouble
right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
533 if (fVboViewer->isInitialized()) {
534 printf(
"glOrtho implemented --- %f %f %f %f %f %f \n",left, right, bottom, top, zNear, zFar);
535 float a = 2.0f / (right -
left);
536 float b = 2.0f / (top - bottom);
537 float c = -2.0f / (zFar - zNear);
539 float tx = - (right +
left)/(right - left);
540 float ty = - (top + bottom)/(top - bottom);
541 float tz = - (zFar + zNear)/(zFar - zNear);
553 if (fMatrixMode == GL_PROJECTION) {
554 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
555 vboGlUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), ortho);
557 glUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), 1, 0, ortho);
560 G4cerr <<
"glFrustum could only be used in GL_PROJECTION mode" <<
G4endl;
567 void G4OpenGLVboDrawer::vboGlFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
569 if (fVboViewer->isInitialized()) {
570 float deltaX = right -
left;
571 float deltaY = top - bottom;
572 float deltaZ = zFar - zNear;
574 float a = 2.0f * zNear / deltaX;
575 float b = 2.0f * zNear / deltaY;
576 float c = (right +
left) / deltaX;
577 float d = (top + bottom) / deltaY;
578 float e = -(zFar + zNear) / (zFar - zNear);
579 float f = -2.0f * zFar * zNear / deltaZ;
588 if (fMatrixMode == GL_PROJECTION) {
589 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
590 vboGlUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), proj);
592 glUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), 1, 0, proj);
595 G4cerr <<
"glFrustrum could only be used in GL_PROJECTION mode" <<
G4endl;
602 void G4OpenGLVboDrawer::vboGlMatrixMode(GLenum a) {
604 if (fVboViewer->isInitialized()) {
605 printf(
"G4OpenGLVboDrawer::vboGlMatrixMode CHANGED :%d \n",a);
612 void G4OpenGLVboDrawer::vboGlColor4d(
int red,
int green,
int blue,
int alpha) {
614 if (fVboViewer->isInitialized()) {
617 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
618 double color [] = {
red, green,
blue, 0.7 };
619 glUniform4fv (fVboViewer->getUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),color);
622 glUniform4f (glGetUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),red, green, blue, alpha);
628 void G4OpenGLVboDrawer:: vboGlColor4fv(
const GLfloat* data) {
630 if (fVboViewer->isInitialized()) {
631 double color [] = { (data[0]), (data[1]), (data[2]), 0.7};
633 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
634 glUniform4fv (fVboViewer->getUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),color);
636 glUniform4f (glGetUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),color[0],color[1],color[2], color[3]);
642 void G4OpenGLVboDrawer:: vboGlPointSize(
float size) {
644 if (fVboViewer->isInitialized()) {
645 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
646 glUniform1f(fVboViewer->getUniformLocation(fVboViewer->getShaderProgram(),
"uPointSize"),size);
648 glUniform1f (glGetUniformLocation(fVboViewer->getShaderProgram(),
"uPointSize"),size);
static const G4double alpha
G4GLOB_DLL std::ostream G4cerr