/usr/lib/Wt/examples/webgl/webgl.C is in witty-examples 3.3.0-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | /*
* Copyright (C) 2010 Emweb bvba, Heverlee, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WText>
#include <Wt/WGLWidget>
#include <Wt/WMatrix4x4>
#include <Wt/WGenericMatrix>
using namespace Wt;
const char *fragmentShaderSrc =
"#ifdef GL_ES\n"
"precision highp float;\n"
"#endif\n"
"\n"
"varying vec4 vColor;\n"
"\n"
"void main(void) {\n"
" gl_FragColor = vColor;\n"
"}\n";
const char *vertexShaderSrc =
"attribute vec3 aVertexPosition;\n"
"attribute vec4 aVertexColor;\n"
"\n"
"uniform mat4 uMVMatrix;\n"
"uniform mat4 uCMatrix;\n"
"uniform mat4 uPMatrix;\n"
"\n"
"varying vec4 vColor;\n"
"\n"
"void main(void) {\n"
" gl_Position = uPMatrix * uCMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);\n"
" vColor = aVertexColor;\n"
"}\n";
const char *texFragmentShaderSrc =
"#ifdef GL_ES\n"
"precision highp float;\n"
"#endif\n"
""
"varying vec2 vTextureCoord;\n"
""
"uniform sampler2D uSampler;\n"
""
"void main(void) {\n"
" gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));\n"
"}\n";
const char *texVertexShaderSrc =
"attribute vec3 aVertexPosition;\n"
"attribute vec2 aTextureCoord;\n"
""
"uniform mat4 uMVMatrix;\n"
"uniform mat4 uCMatrix;\n"
"uniform mat4 uPMatrix;\n"
""
"varying vec2 vTextureCoord;\n"
""
"void main(void) {\n"
" gl_Position = uPMatrix * uCMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);\n"
" vTextureCoord = aTextureCoord;\n"
"}\n";
class PaintWidget: public WGLWidget
{
public:
PaintWidget(WContainerWidget *root):
WGLWidget(root)
{
}
void initializeGL()
{
jsMatrix_ = createJavaScriptMatrix4();
WMatrix4x4 worldTransform;
worldTransform.lookAt(0, 0, 5, 0, 0, -1, 0, 1, 0);
setJavaScriptMatrix4(jsMatrix_, worldTransform);
//setClientSideWalkHandler(jsMatrix_, 1./20, 1./100);
setClientSideLookAtHandler(jsMatrix_, 0, 0, 0, 0, 1, 0, 0.005, 0.005);
// First, load a simple shader
Shader fragmentShader = createShader(FRAGMENT_SHADER);
shaderSource(fragmentShader, fragmentShaderSrc);
compileShader(fragmentShader);
Shader vertexShader = createShader(VERTEX_SHADER);
shaderSource(vertexShader, vertexShaderSrc);
compileShader(vertexShader);
shaderProgram_ = createProgram();
attachShader(shaderProgram_, vertexShader);
attachShader(shaderProgram_, fragmentShader);
linkProgram(shaderProgram_);
useProgram(shaderProgram_);
vertexPositionAttribute_ = getAttribLocation(shaderProgram_, "aVertexPosition");
enableVertexAttribArray(vertexPositionAttribute_);
vertexColorAttribute_ = getAttribLocation(shaderProgram_, "aVertexColor");
enableVertexAttribArray(vertexColorAttribute_);
pMatrixUniform_ = getUniformLocation(shaderProgram_, "uPMatrix");
cMatrixUniform_ = getUniformLocation(shaderProgram_, "uCMatrix");
mvMatrixUniform_ = getUniformLocation(shaderProgram_, "uMVMatrix");
// Next, load a texture shader
Shader texFragmentShader = createShader(FRAGMENT_SHADER);
shaderSource(texFragmentShader, texFragmentShaderSrc);
compileShader(texFragmentShader);
Shader texVertexShader = createShader(VERTEX_SHADER);
shaderSource(texVertexShader, texVertexShaderSrc);
compileShader(texVertexShader);
texShaderProgram_ = createProgram();
attachShader(texShaderProgram_, texVertexShader);
attachShader(texShaderProgram_, texFragmentShader);
linkProgram(texShaderProgram_);
useProgram(texShaderProgram_);
texVertexPositionAttribute_ = getAttribLocation(texShaderProgram_, "aVertexPosition");
enableVertexAttribArray(texVertexPositionAttribute_);
texCoordAttribute_ = getAttribLocation(texShaderProgram_, "aTextureCoord");
enableVertexAttribArray(texCoordAttribute_);
texPMatrixUniform_ = getUniformLocation(texShaderProgram_, "uPMatrix");
texCMatrixUniform_ = getUniformLocation(shaderProgram_, "uCMatrix");
texMvMatrixUniform_ = getUniformLocation(texShaderProgram_, "uMVMatrix");
texSamplerUniform_ = getUniformLocation(texShaderProgram_, "uSampler");
// Now, preload buffers
triangleVertexPositionBuffer_ = createBuffer();
bindBuffer(ARRAY_BUFFER, triangleVertexPositionBuffer_);
double trianglePosition[] = {
0.0, 1.0, 0.0,
-1.0,-1.0, 0.0,
1.0,-1.0, 0.0
};
bufferDatafv(ARRAY_BUFFER, trianglePosition, 9, STATIC_DRAW);
triangleVertexColorBuffer_ = createBuffer();
bindBuffer(ARRAY_BUFFER, triangleVertexColorBuffer_);
double triangleColor[] = {
1.0,0.0,0.0,1.0,
0.0,1.0,0.0,1.0,
0.0,0.0,1.0,1.0
};
bufferDatafv(ARRAY_BUFFER, triangleColor, 12, STATIC_DRAW);
squareVertexPositionBuffer_ = createBuffer();
bindBuffer(ARRAY_BUFFER, squareVertexPositionBuffer_);
double squarePosition[] = {
1.0, 1.0,0.0,
-1.0, 1.0,0.0,
1.0,-1.0,0.0,
-1.0,-1.0,0.0
};
bufferDatafv(ARRAY_BUFFER, squarePosition, 12, STATIC_DRAW);
squareVertexColorBuffer_ = createBuffer();
bindBuffer(ARRAY_BUFFER, squareVertexColorBuffer_);
double squareColor[] = {
1.0,0.0,0.0,1.0,
0.0,1.0,0.0,1.0,
0.0,0.0,1.0,1.0,
0.0,1.0,1.0,1.0
};
bufferDatafv(ARRAY_BUFFER, squareColor, 16, STATIC_DRAW);
squareElementBuffer_ = createBuffer();
bindBuffer(ELEMENT_ARRAY_BUFFER, squareElementBuffer_);
int elements[] = {
0, 1, 2, 3
};
bufferDataiv(ELEMENT_ARRAY_BUFFER, elements, 4, STATIC_DRAW, DT_UNSIGNED_BYTE);
texture_ = createTextureAndLoad("texture.jpg");
bindTexture(TEXTURE_2D, texture_);
pixelStorei(UNPACK_FLIP_Y_WEBGL, true);
texImage2D(TEXTURE_2D, 0, RGB, RGB, PT_UNSIGNED_BYTE, texture_);
texParameteri(TEXTURE_2D, TEXTURE_MAG_FILTER, LINEAR);
texParameteri(TEXTURE_2D, TEXTURE_MIN_FILTER, LINEAR);
textureCoordBuffer_ = createBuffer();
bindBuffer(ARRAY_BUFFER, textureCoordBuffer_);
float textureCoords[] = {
1, 1,
0, 1,
1, 0,
0, 0
};
bufferDatafv(ARRAY_BUFFER, textureCoords, 8, STATIC_DRAW);
}
void paintGL()
{
// Drawing starts here!
//clearColor(0.8, 0.8, 0.8, 1);
clearColor(0, 0, 0, 1);
clearDepth(1);
disable(DEPTH_TEST);
disable(CULL_FACE);
depthFunc(LEQUAL);
viewport(0, 0, 640, 360);
clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT);
WMatrix4x4 proj;
proj.perspective(90, 1, 1, 40);
useProgram(shaderProgram_);
// Set projection matrix
uniformMatrix4(pMatrixUniform_, proj);
uniformMatrix4(cMatrixUniform_, jsMatrix_);
// Draw the scene
#if 0
bindBuffer(ARRAY_BUFFER, triangleVertexPositionBuffer_);
vertexAttribPointer(vertexPositionAttribute_, 3, FLOAT, false, 0, 0);
bindBuffer(ARRAY_BUFFER, triangleVertexColorBuffer_);
vertexAttribPointer(vertexColorAttribute_, 4, FLOAT, false, 0, 0);
WMatrix4x4 modelMatrix1(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
-2.0, 0, 0, 1
);
modelMatrix1.rotate(45, 0, 1, 0);
uniformMatrix4fv(mvMatrixUniform_, false, modelMatrix1);
drawArrays(TRIANGLES, 0, 3);
#endif
bindBuffer(ARRAY_BUFFER, squareVertexPositionBuffer_);
vertexAttribPointer(vertexPositionAttribute_, 3, FLOAT, false, 0, 0);
bindBuffer(ARRAY_BUFFER, squareVertexColorBuffer_);
vertexAttribPointer(vertexColorAttribute_, 4, FLOAT, false, 0, 0);
WMatrix4x4 modelMatrix2(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
//modelMatrix2.rotate(45, 1, 0, 0);
//modelMatrix2.translate(0, 0, -2);
uniformMatrix4(mvMatrixUniform_, modelMatrix2);
//drawArrays(TRIANGLE_STRIP, 0, 4);
bindBuffer(ELEMENT_ARRAY_BUFFER, squareElementBuffer_);
drawElements(TRIANGLE_STRIP, 4, DT_UNSIGNED_BYTE, 0);
#if 0
// now draw something textured
useProgram(texShaderProgram_);
uniformMatrix4fv(texPMatrixUniform_, false, projectionMatrix);
//WMatrix4x4 mm3(
// 2, 0, 0, 0,
// 0, 1, 0, -2.5,
// 0, 0, 1, 0,
// 0, 0, 0, 1
// );
WMatrix4x4 mm3;
//mm3.scale(.5, 2, 1);
mm3.rotate(45, 0, 0, 1);
uniformMatrix4fv(texMvMatrixUniform_, false, mm3.data());
bindBuffer(ARRAY_BUFFER, textureCoordBuffer_);
vertexAttribPointer(texCoordAttribute_, 2, FLOAT, false, 0, 0);
activeTexture(TEXTURE0);
bindTexture(TEXTURE_2D, texture_);
uniform1i(texSamplerUniform_, 0);
bindBuffer(ARRAY_BUFFER, squareVertexPositionBuffer_);
vertexAttribPointer(texVertexPositionAttribute_, 3, FLOAT, false, 0, 0);
drawArrays(TRIANGLE_STRIP, 0, 4);
#endif
}
void resizeGL(int width, int height)
{
}
private:
Program shaderProgram_;
AttribLocation vertexPositionAttribute_;
AttribLocation vertexColorAttribute_;
UniformLocation pMatrixUniform_;
UniformLocation cMatrixUniform_;
UniformLocation mvMatrixUniform_;
Program texShaderProgram_;
AttribLocation texVertexPositionAttribute_;
AttribLocation texCoordAttribute_;
UniformLocation texPMatrixUniform_;
UniformLocation texCMatrixUniform_;
UniformLocation texMvMatrixUniform_;
UniformLocation texSamplerUniform_;
JavaScriptMatrix4x4 jsMatrix_;
Buffer triangleVertexPositionBuffer_;
Buffer triangleVertexColorBuffer_;
Buffer squareVertexPositionBuffer_;
Buffer squareVertexColorBuffer_;
Buffer squareElementBuffer_;
Buffer textureCoordBuffer_;
Texture texture_;
};
/*
* A simple WebGL demo application
*/
class WebGLDemo : public WApplication
{
public:
WebGLDemo(const WEnvironment& env);
};
WebGLDemo::WebGLDemo(const WEnvironment& env)
: WApplication(env)
{
setTitle("WebGL Demo");
root()->addWidget(new WText("This is a preliminary WebGL demo. It will "
"become more spectacular in time. This technology preview has only "
"been tested on Chrome Canary builds. No 3D because I did "
"not yet invest in 3D projection matrices."));
root()->addWidget(new WBreak());
PaintWidget *gl = new PaintWidget(root());
gl->resize(640, 640);
}
WApplication *createApplication(const WEnvironment& env)
{
return new WebGLDemo(env);
}
int main(int argc, char **argv)
{
WGenericMatrix<double, 3, 6> gm1;
WGenericMatrix<double, 3, 6> gm2(gm1);
WGenericMatrix<double, 3, 6> gm3;
WGenericMatrix<double, 6, 3> gm4;
double foo[] = {1, 2, 3, 4};
WGenericMatrix<double, 2, 2> gm5(foo);
std::cout << gm5.constData()[0] << std::endl;
std::cout << gm5(0, 0) << std::endl;
std::cout << gm5 << std::endl;
gm5.copyDataTo(foo);
WGenericMatrix<double, 2, 2> const gm6(foo);
std::cout << "equality test: " << (gm5 == gm6 ? "OK" : "NOK") << std::endl;
std::cout << "inequality test: " << (gm5 != gm6 ? "NOK" : "OK") << std::endl;
std::cout << gm5.data()[0] << std::endl;
std::cout << gm6.constData()[0] << std::endl;
gm3.fill(42);
std::cout << gm3 << std::endl;
std::cout << gm3.transposed() << std::endl;
std::cout << "isidentity non-square: " << (gm2.isIdentity() ? "OK" : "NOK") << std::endl;
std::cout << "isidentity non-square: " << (!gm3.isIdentity() ? "OK" : "NOK") << std::endl;
gm3.setToIdentity();
std::cout << gm3 << std::endl;
std::cout << "isidentity non-square: " << (gm3.isIdentity() ? "OK" : "NOK") << std::endl;
#if 1
std::cout << gm1 << std::endl;
gm3 = gm1 + gm2;
std::cout << gm3 << std::endl;
gm3 = gm1 - gm2;
std::cout << gm3 << std::endl;
gm3 = -gm2;
std::cout << gm3 << std::endl;
gm3 = gm2 * 2.0;
std::cout << gm3 << std::endl;
gm3 = gm2 / 2.0;
std::cout << gm3 << std::endl;
gm3 = 2.0 * gm2;
std::cout << gm3 << std::endl;
std::cout << gm3 * gm4 << std::endl;
WGenericMatrix<double, 3, 3> gm7= gm3 * gm4;
#endif
gm3 *= 2.0;
std::cout << gm3 << std::endl;
gm3 /= 2.0;
std::cout << gm3 << std::endl;
gm3 += gm1;
std::cout << gm3 << std::endl;
gm3 -= gm1;
std::cout << gm3 << std::endl;
WMatrix4x4 m1;
std::cout << m1 << std::endl;
std::cout << "Determinant: " << m1.determinant() << std::endl;
std::cout << "Inverted: ";
std::cout << m1.inverted() << std::endl;
std::cout << "Identity? " << m1.isIdentity() << std::endl;
WMatrix4x4 m2;
m2.rotate(0, 0, 0, 1);
std::cout << m2 << std::endl;
m2.rotate(0, 0, 0, 2);
std::cout << m2 << std::endl;
m2.rotate(0, 0, 2, 0);
std::cout << m2 << std::endl;
m2.rotate(0, 2, 0, 0);
std::cout << m2 << std::endl;
m2.rotate(0, 1, 1, 1);
std::cout << m2 << std::endl;
m2.rotate(90, 1, 0, 0);
std::cout << m2 << std::endl;
m2.rotate(-90, 1, 0, 0);
std::cout << m2 << std::endl;
m2.rotate(45, 1, 0, 0);
std::cout << m2 << std::endl;
m2.rotate(-45, 1, 0, 0);
std::cout << m2 << std::endl;
WMatrix4x4 m3;
m3 = m1 * m2;
WMatrix4x4 m4(m1*m2);
m3.setToIdentity();
m3.lookAt(0, 0, 0, 0, 0, -1, 0, 1, 0);
std::cout << m3 << std::endl;
m3.setToIdentity();
m3.lookAt(0, 0, 0, 0, 0, -2, 0, 8, 0);
std::cout << m3 << std::endl;
m3.setToIdentity();
m3.lookAt(0, 0, -1, 0, 0, -2, 0, 1, 0);
std::cout << m3 << std::endl;
m3.setToIdentity();
m3.lookAt(0, 0, 0, 0, 0, 0, 0, 1, 0);
std::cout << m3 << std::endl;
m3.setToIdentity();
m3.lookAt(0, 0, 0,1, 1, 1, 0, 1, 0);
std::cout << m3 << std::endl;
return WRun(argc, argv, &createApplication);
}
|