34 #ifdef G4OPENGL_VERSION_2
38 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
46 G4OpenGLVboDrawer::G4OpenGLVboDrawer (G4OpenGLViewer* viewer,
54 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
55 G4OpenGLImmediateWtViewer*
v =
dynamic_cast<G4OpenGLImmediateWtViewer*
>(viewer);
57 G4OpenGLImmediateQtViewer* v =
dynamic_cast<G4OpenGLImmediateQtViewer*
>(viewer);
65 "precision highp float;\n"
68 "varying vec3 vLightWeighting;\n"
69 "uniform vec4 uPointColor; // Point Color\n"
72 " vec4 matColor = uPointColor;\n"
73 " gl_FragColor = vec4(matColor.rgb, matColor.a);\n"
77 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
79 "attribute vec3 aVertexPosition;\n"
80 "attribute vec3 aVertexNormal;\n"
82 "uniform mat4 uMVMatrix; // [M]odel[V]iew matrix\n"
83 "uniform mat4 uCMatrix; // Client-side manipulated [C]amera matrix\n"
84 "uniform mat4 uPMatrix; // Perspective [P]rojection matrix\n"
85 "uniform mat4 uNMatrix; // [N]ormal transformation\n"
86 "// uNMatrix is the transpose of the inverse of uCMatrix * uMVMatrix\n"
87 "uniform mat4 uTMatrix; // [T]ransformation matrix\n"
88 "uniform float uPointSize; // Point size\n"
90 "varying vec3 vLightWeighting;\n"
93 " // Calculate the position of this vertex\n"
94 " gl_Position = uPMatrix * uCMatrix * uMVMatrix * uTMatrix * vec4(aVertexPosition, 1.0);\n"
97 " vec3 transformedNormal = normalize((uNMatrix * vec4(normalize(aVertexNormal), 0)).xyz);\n"
98 " vec3 lightingDirection = normalize(vec3(1, 1, 1));\n"
99 " float directionalLightWeighting = max(dot(transformedNormal, lightingDirection), 0.0);\n"
100 " vec3 uAmbientLightColor = vec3(0.2, 0.2, 0.2);\n"
101 " vec3 uDirectionalColor = vec3(0.8, 0.8, 0.8);\n"
102 " gl_PointSize = uPointSize;\n"
103 " vLightWeighting = uAmbientLightColor + uDirectionalColor * directionalLightWeighting;\n"
110 "attribute highp vec4 aVertexPosition;\n"
111 "attribute vec3 aVertexNormal;\n"
112 "uniform highp mat4 uCMatrix;\n"
113 "uniform highp mat4 uPMatrix; // Perspective [P]rojection matrix\n"
114 "uniform highp mat4 uMVMatrix; // [M]odel[V]iew matrix\n"
115 "uniform highp mat4 uTMatrix; // [T]ransformation matrix\n"
116 "uniform float uPointSize; // Point size\n"
119 " gl_Position = uPMatrix * uCMatrix * uMVMatrix * uTMatrix * aVertexPosition;\n"
120 " // Phong shading\n"
122 " vec3 lightingDirection = normalize(vec3(1, 1, 1));\n"
126 " gl_PointSize = uPointSize;\n"
135 G4OpenGLVboDrawer::~G4OpenGLVboDrawer (
145 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
147 void G4OpenGLVboDrawer:: vboGlClear(Wt::WFlags< GLenum >
mask) {
148 if (fVboViewer->isInitialized()) {
149 fVboViewer->clear(mask);
154 void G4OpenGLVboDrawer:: vboGlUniformMatrix4(
const Wt::WGLWidget::UniformLocation &location,
const Wt::WMatrix4x4 &
m) {
156 vboGlUseProgram(fVboViewer->getShaderProgram());
157 fVboViewer->uniformMatrix4(location, m);
162 void G4OpenGLVboDrawer:: vboGlUniformMatrix4(
const Wt::WGLWidget::UniformLocation &location,
const double* matrix) {
165 (
double) matrix[0], (
double) matrix[4], (
double) matrix[8], (
double) matrix[12],
166 (
double) matrix[1], (
double) matrix[5], (
double) matrix[9], (
double) matrix[13],
167 (
double) matrix[2], (
double) matrix[6], (
double) matrix[10],(
double) matrix[14],
168 (
double) matrix[3],(
double) matrix[7],(
double) matrix[11],(
double) matrix[15]);
170 fVboViewer->uniformMatrix4(location,
mat);
174 void G4OpenGLVboDrawer:: vboGlUniformMatrix4fv(
const Wt::WGLWidget::UniformLocation &location,
const float* matrix) {
177 (
double) matrix[0], (
double) matrix[4], (
double) matrix[8], (
double) matrix[12],
178 (
double) matrix[1], (
double) matrix[5], (
double) matrix[9], (
double) matrix[13],
179 (
double) matrix[2], (
double) matrix[6], (
double) matrix[10],(
double) matrix[14],
180 (
double) matrix[3],(
double) matrix[7],(
double) matrix[11],(
double) matrix[15]);
182 fVboViewer->uniformMatrix4(location,
mat);
186 void G4OpenGLVboDrawer:: vboGlUniformMatrix4(
const Wt::WGLWidget::UniformLocation &location,
const Wt::WGLWidget::JavaScriptMatrix4x4 &m) {
187 if (fVboViewer->isInitialized()) {
188 fVboViewer->uniformMatrix4(location, m);
192 Wt::WGLWidget::Buffer G4OpenGLVboDrawer:: vboGlCreateBuffer(){
194 return fVboViewer->createBuffer();
196 return Wt::WGLWidget::Buffer();
199 void G4OpenGLVboDrawer:: vboGlBindBuffer(GLenum
target, Wt::WGLWidget::Buffer
buffer){
201 fVboViewer->bindBuffer(target,buffer);
205 void G4OpenGLVboDrawer:: vboGlDeleteBuffer(Wt::WGLWidget::Buffer buffer){
206 if (fVboViewer == NULL)
return;
207 fVboViewer->deleteBuffer(buffer);
210 void G4OpenGLVboDrawer:: vboGlVertexAttribPointer(Wt::WGLWidget::AttribLocation location,
int size, GLenum type,
bool normalized,
unsigned stride,
unsigned offset){
211 if (fVboViewer->isInitialized()) {
212 fVboViewer->vertexAttribPointer(location, size,type, normalized, stride, offset);
216 Wt::WGLWidget::Program G4OpenGLVboDrawer:: vboGlCreateProgram(){
218 return fVboViewer->createProgram();
220 return Wt::WGLWidget::Program();
223 void G4OpenGLVboDrawer:: vboGlAttachShader(Wt::WGLWidget::Program program, Shader shader){
225 fVboViewer->attachShader(program,shader);
229 void G4OpenGLVboDrawer:: vboGlLinkProgram(Wt::WGLWidget::Program program){
231 fVboViewer->linkProgram(program);
235 void G4OpenGLVboDrawer:: vboGlUseProgram(Wt::WGLWidget::Program program){
237 fVboViewer->useProgram(program);
241 void G4OpenGLVboDrawer:: vboGlEnableVertexAttribArray(Wt::WGLWidget::AttribLocation pointer){
243 fVboViewer->enableVertexAttribArray(pointer);
247 void G4OpenGLVboDrawer:: vboGlDisableVertexAttribArray(Wt::WGLWidget::AttribLocation pointer){
249 fVboViewer->disableVertexAttribArray(pointer);
253 Wt::WGLWidget::UniformLocation G4OpenGLVboDrawer:: vboGlGetUniformLocation(Wt::WGLWidget::Program programm,
const std::string &src){
255 return fVboViewer->getUniformLocation(programm, src);
257 return Wt::WGLWidget::UniformLocation();
260 Wt::WGLWidget::AttribLocation G4OpenGLVboDrawer:: vboGlGetAttribLocation(Wt::WGLWidget::Program shader,
const std::string &src){
262 return fVboViewer->getAttribLocation(shader, src);
264 return Wt::WGLWidget::AttribLocation();
271 void G4OpenGLVboDrawer::vboGlClearColor (
double r,
double g,
double b,
double a) {
273 if (fVboViewer->isInitialized() ) {
274 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
275 fVboViewer->clearColor(r,g,b,a);
281 void G4OpenGLVboDrawer::vboGlClearDepth(
double depth) {
282 if (fVboViewer->isInitialized()) {
283 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
284 fVboViewer->clearDepth(depth);
292 void G4OpenGLVboDrawer::vboGlFlush() {
293 if (fVboViewer->isInitialized()) {
294 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
301 void G4OpenGLVboDrawer:: vboGlViewport(
int x,
int y,
unsigned width,
unsigned height) {
302 if (fVboViewer->isInitialized()) {
303 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
304 fVboViewer->viewport(x,y,width,height);
310 void G4OpenGLVboDrawer:: vboGlEnable(GLenum cap) {
311 if (fVboViewer->isInitialized()) {
312 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
314 fVboViewer->enable(cap);
321 void G4OpenGLVboDrawer:: vboGlDisable(GLenum cap) {
322 if (fVboViewer->isInitialized()) {
323 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
325 fVboViewer->disable(cap);
332 void G4OpenGLVboDrawer:: vboGlBlendFunc (GLenum sfactor, GLenum dfactor) {
333 if (fVboViewer->isInitialized()) {
334 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
335 fVboViewer->blendFunc(sfactor, dfactor);
342 void G4OpenGLVboDrawer:: vboGlDepthFunc (GLenum
func) {
343 if (fVboViewer->isInitialized()) {
344 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
345 fVboViewer->depthFunc(func);
351 void G4OpenGLVboDrawer:: vboGlDepthMask(
bool flag) {
352 if (fVboViewer->isInitialized()) {
353 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
354 fVboViewer->depthMask(flag);
360 void G4OpenGLVboDrawer:: vboGlColorMask (
bool red,
bool green,
bool blue,
bool alpha) {
361 if (fVboViewer->isInitialized()) {
362 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
363 fVboViewer->colorMask(red,green,blue,alpha);
369 void G4OpenGLVboDrawer:: vboGlLineWidth(
double width) {
370 if (fVboViewer->isInitialized()) {
371 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
372 fVboViewer->lineWidth(width);
379 void G4OpenGLVboDrawer:: vboGlDrawArrays(GLenum mode,
int first,
unsigned count){
380 if (fVboViewer->isInitialized()) {
381 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
382 fVboViewer->drawArrays(mode,first, count);
388 void G4OpenGLVboDrawer:: vboGlDrawElements(GLenum mode,
unsigned count, GLenum type,
unsigned offset){
389 if (fVboViewer->isInitialized()) {
390 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
391 fVboViewer->drawElements(mode,count,type,offset);
397 void G4OpenGLVboDrawer:: vboGlBufferDatafv(GLenum target,
const std::vector<double>::iterator begin,
const std::vector<double>::iterator end, GLenum
usage){
399 if (fVboViewer->isInitialized()) {
400 fVboViewer->bufferDatafv(target,begin,end,usage);
405 void G4OpenGLVboDrawer:: vboGlBufferDataiv(GLenum target,
const std::vector<unsigned short>::iterator begin,
const std::vector<unsigned short>::iterator end, GLenum usage, GLenum type){
407 if (fVboViewer->isInitialized()) {
408 fVboViewer->bufferDataiv(target,begin,end,usage,type);
414 void G4OpenGLVboDrawer:: vboGlMultMatrixd(
const GLdouble *matrix){
416 if (fVboViewer->isInitialized()) {
418 (
double) matrix[0], (
double) matrix[4], (
double) matrix[8], (
double) matrix[12],
419 (
double) matrix[1], (
double) matrix[5], (
double) matrix[9], (
double) matrix[13],
420 (
double) matrix[2], (
double) matrix[6], (
double) matrix[10],(
double) matrix[14],
421 (
double) matrix[3],(
double) matrix[7],(
double) matrix[11],(
double) matrix[15]);
424 fVboViewer->uniformMatrix4(fVboViewer->getShaderTransformMatrix(),
mat);
425 if (fMatrixMode == GL_MODELVIEW) {
428 G4cerr <<
"glMultMatrixd could only be used in GL_MODELVIEW mode" <<
G4endl;
434 void G4OpenGLVboDrawer:: vboGlMultMatrixf(
const GLfloat *matrix){
436 if (fVboViewer->isInitialized()) {
438 matrix[0], matrix[4], matrix[8], matrix[12],
439 matrix[1], matrix[5], matrix[9], matrix[13],
440 matrix[2], matrix[6], matrix[10], matrix[14],
441 matrix[3], matrix[7], matrix[11], matrix[15]);
443 if (fMatrixMode == GL_MODELVIEW) {
444 fVboViewer->uniformMatrix4(fVboViewer->getShaderTransformMatrix(),
mat);
446 G4cerr <<
"glMultMatrixf could only be used in GL_MODELVIEW mode" <<
G4endl;
452 void G4OpenGLVboDrawer:: vboGlShaderSource(Shader shader, GLsizei ,
const GLchar **src,
const GLint *){
454 std::string
s = *src;
455 fVboViewer->shaderSource(shader, s);
459 void G4OpenGLVboDrawer:: vboGlCompileShader(Shader shader){
461 fVboViewer->compileShader(shader);
465 Shader G4OpenGLVboDrawer:: vboGlCreateShader(GLenum shader){
467 return fVboViewer->createShader(shader);
478 void G4OpenGLVboDrawer:: vboGlMultMatrixf(
const GLfloat *matrix){
480 if (fVboViewer->isInitialized()) {
485 if (fMatrixMode == GL_MODELVIEW) {
486 glUniformMatrix4fv(fVboViewer->getShaderTransformMatrix(),1,0,matrix);
488 G4cerr <<
"glMultMatrixf could only be used in GL_MODELVIEW mode" <<
G4endl;
495 void G4OpenGLVboDrawer:: vboGlMultMatrixd(
const GLdouble *matrix){
497 if (fVboViewer->isInitialized()) {
503 matrix[0],matrix[1],matrix[2],matrix[3],
504 matrix[4],matrix[5],matrix[6],matrix[7],
505 matrix[8],matrix[9],matrix[10],matrix[11],
506 matrix[12],matrix[13],matrix[14],matrix[15]
509 glUniformMatrix4fv(fVboViewer->getShaderTransformMatrix(),1,0,
mat);
510 GLenum
e = glGetError();
511 printf(
"GL error : %d",e);
528 void G4OpenGLVboDrawer::vboGlOrtho(GLdouble
left, GLdouble
right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
530 if (fVboViewer->isInitialized()) {
531 printf(
"glOrtho implemented --- %f %f %f %f %f %f \n",left, right, bottom, top, zNear, zFar);
532 float a = 2.0f / (right -
left);
533 float b = 2.0f / (top - bottom);
534 float c = -2.0f / (zFar - zNear);
536 float tx = - (right +
left)/(right - left);
537 float ty = - (top + bottom)/(top - bottom);
538 float tz = - (zFar + zNear)/(zFar - zNear);
550 if (fMatrixMode == GL_PROJECTION) {
551 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
552 vboGlUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), ortho);
554 glUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), 1, 0, ortho);
557 G4cerr <<
"glFrustum could only be used in GL_PROJECTION mode" <<
G4endl;
564 void G4OpenGLVboDrawer::vboGlFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
566 if (fVboViewer->isInitialized()) {
567 float deltaX = right -
left;
568 float deltaY = top - bottom;
569 float deltaZ = zFar - zNear;
571 float a = 2.0f * zNear / deltaX;
572 float b = 2.0f * zNear / deltaY;
573 float c = (right +
left) / deltaX;
574 float d = (top + bottom) / deltaY;
575 float e = -(zFar + zNear) / (zFar - zNear);
576 float f = -2.0f * zFar * zNear / deltaZ;
585 if (fMatrixMode == GL_PROJECTION) {
586 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
587 vboGlUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(),
proj);
589 glUniformMatrix4fv(fVboViewer->getShaderProjectionMatrix(), 1, 0,
proj);
592 G4cerr <<
"glFrustrum could only be used in GL_PROJECTION mode" <<
G4endl;
599 void G4OpenGLVboDrawer::vboGlMatrixMode(GLenum a) {
601 if (fVboViewer->isInitialized()) {
602 printf(
"G4OpenGLVboDrawer::vboGlMatrixMode CHANGED :%d \n",a);
609 void G4OpenGLVboDrawer::vboGlColor4d(
int red,
int green,
int blue,
int alpha) {
611 if (fVboViewer->isInitialized()) {
614 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
615 double color [] = { red, green, blue, 0.7 };
616 glUniform4fv (fVboViewer->getUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),color);
619 glUniform4f (glGetUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),red, green, blue, alpha);
625 void G4OpenGLVboDrawer:: vboGlColor4fv(
const GLfloat*
data) {
627 if (fVboViewer->isInitialized()) {
628 double color [] = { (data[0]), (data[1]), (data[2]), 0.7};
630 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
631 glUniform4fv (fVboViewer->getUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),color);
633 glUniform4f (glGetUniformLocation(fVboViewer->getShaderProgram(),
"uPointColor"),color[0],color[1],color[2], color[3]);
639 void G4OpenGLVboDrawer:: vboGlPointSize(
float size) {
641 if (fVboViewer->isInitialized()) {
642 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
643 glUniform1f(fVboViewer->getUniformLocation(fVboViewer->getShaderProgram(),
"uPointSize"),size);
645 glUniform1f (glGetUniformLocation(fVboViewer->getShaderProgram(),
"uPointSize"),size);