Beispiel #1
0
void PaintWidget::resizeGL(int width, int height)
{
    // Set the viewport size.
    viewport(0, 0, width, height);

    // Set projection matrix to some fixed values
    WMatrix4x4 proj;
    proj.perspective(45, ((double)width)/height, 1, 40);
    uniformMatrix4(pMatrixUniform_, proj);
}
Beispiel #2
0
void DrawGLWidget::initializeGL()
{
    // Init variables
    useLighting=true;
    ambientColor.assign(0.2,0.2,0.2);
    lightDirection.assign(-1.0,-1.0,-1.0);
    lightDirection.normalize();
    lightDirection.scale(-1.0);
    lightColor.assign(0.8);

    // Init Angles
    angleX = -30.0;
    velAngleX = 0.0;
    angleY = 0.0;
    velAngleY = 0.0;
    accelAngle = 0.1;
    zDepth = -6.0;


    // Init Javascript Matrix (ClientSide)
    WMatrix4x4 worldTransform;
    worldTransform.setToIdentity();
    jsMatrix_ = createJavaScriptMatrix4();
    setJavaScriptMatrix4(jsMatrix_, worldTransform);

    rotMatrix=createJavaScriptMatrix4();
    //setClientSideWalkHandler(rotMatrix,0.01,0.01);
    setClientSideLookAtHandler(rotMatrix,0,0,0,0,1,0,0.007,0.007);
    // Init Texture pointer


    initShaders();
    initBuffers();
    initTextures();




    viewport(0, 0, 500, 500);
    clearColor(0, 0, 0, 1.0);
    enable(DEPTH_TEST);

}
Beispiel #3
0
WMatrix4x4 WGLWidget::JavaScriptMatrix4x4::value() const
{
  if (!hasContext())
    throw WException("JavaScriptMatrix4x4: matrix not assigned to a WGLWidget");

  WMatrix4x4 originalCpy;
  for (unsigned i = 0; i<context_->jsMatrixList_.size(); i++)
    if (context_->jsMatrixList_[i].id == id_)
      originalCpy = context_->jsMatrixList_[i].serverSideCopy;

  // apply all operations
  int nbMult = 0;
  for (unsigned i = 0; i<operations_.size(); i++) {
    switch (operations_[i]) {
    case op::TRANSPOSE:
      originalCpy = originalCpy.transposed();
      break;
    case op::INVERT:
#ifndef WT_TARGET_JAVA
      originalCpy = 
#endif
	originalCpy.inverted();
      break;
    case op::MULTIPLY:
#ifndef WT_TARGET_JAVA
      originalCpy = originalCpy * matrices_[nbMult];
#else
      originalCpy.mul(matrices_[nbMult]);
#endif
      nbMult++;
      break;
    }
  }

  return originalCpy;
}
Beispiel #4
0
  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);

  }
Beispiel #5
0
  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
  }
Beispiel #6
0
// The initializeGL() captures all JS commands that are to be executed
// before the scene is rendered for the first time. It is executed only
// once. It is re-executed when the WebGL context is restored after it
// was lost.
// In general, it should be used to set up shaders, create VBOs, initialize
// matrices, ...
void PaintWidget::initializeGL()
{
    // In order to know where to look at, calculate the centerpoint of the
    // scene
    double cx, cy, cz;
    centerpoint(cx, cy, cz);

    // Transform the world so that we look at the centerpoint of the scene
    WMatrix4x4 worldTransform;
    worldTransform.lookAt(
        cx, cy, cz + 10, // camera position
        cx, cy, cz,      // looking at
        0, 1, 0);        // 'up' vector

    // We want to be able to change the camera position client-side. In
    // order to do so, the world transformation matrix must be stored in
    // a matrix that can be manipulated from JavaScript.
    if (!initialized_) {
        initJavaScriptMatrix4(jsMatrix_);
        setJavaScriptMatrix4(jsMatrix_, worldTransform);

        // This installs a client-side mouse handler that modifies the
        // world transformation matrix. Like WMatrix4x4::lookAt, this works
        // by specifying a center point and an up direction; mouse movements
        // will allow the camera to be moved around the center point.
        setClientSideLookAtHandler(jsMatrix_, // the name of the JS matrix
                                   cx, cy, cz,                       // the center point
                                   0, 1, 0,                          // the up direction
                                   0.005, 0.005);                    // 'speed' factors
        // Alternative: this installs a client-side mouse handler that allows
        // to 'walk' around: go forward, backward, turn left, turn right, ...
        //setClientSideWalkHandler(jsMatrix_, 0.05, 0.005);
        initialized_ = true;
    }

    // First, load a simple shader
    Shader fragmentShader = createShader(FRAGMENT_SHADER);
    shaderSource(fragmentShader, fragmentShader_);
    compileShader(fragmentShader);
    Shader vertexShader = createShader(VERTEX_SHADER);
    shaderSource(vertexShader, vertexShader_);
    compileShader(vertexShader);
    shaderProgram_ = createProgram();
    attachShader(shaderProgram_, vertexShader);
    attachShader(shaderProgram_, fragmentShader);
    linkProgram(shaderProgram_);
    useProgram(shaderProgram_);

    // Extract the references to the attributes from the shader.
    vertexNormalAttribute_   =
        getAttribLocation(shaderProgram_, "aVertexNormal");
    vertexPositionAttribute_ =
        getAttribLocation(shaderProgram_, "aVertexPosition");
    enableVertexAttribArray(vertexPositionAttribute_);
    enableVertexAttribArray(vertexNormalAttribute_);

    // Extract the references the uniforms from the shader
    pMatrixUniform_  = getUniformLocation(shaderProgram_, "uPMatrix");
    cMatrixUniform_  = getUniformLocation(shaderProgram_, "uCMatrix");
    mvMatrixUniform_ = getUniformLocation(shaderProgram_, "uMVMatrix");
    nMatrixUniform_  = getUniformLocation(shaderProgram_, "uNMatrix");

    // Create a Vertex Buffer Object (VBO) and load all polygon's data
    // (points, normals) into it. In this case we use one VBO that contains
    // all data (6 per point: vx, vy, vz, nx, ny, nz); alternatively you
    // can use multiple VBO's (e.g. one VBO for normals, one for points,
    // one for texture coordinates).
    // Note that if you use indexed buffers, you cannot have indexes
    // larger than 65K, due to the limitations of WebGL.
    objBuffer_ = createBuffer();
    bindBuffer(ARRAY_BUFFER, objBuffer_);

    if (!useBinaryBuffers_)
    {
        // embed the buffer directly in the JavaScript stream.
        bufferDatafv(ARRAY_BUFFER, data.begin(), data.end(), STATIC_DRAW);
    } else
    {
        //Alternatively uncomment the following lines to directly transfer the array
        //as a binary data resource.
        //The binary data is prepared as a downloadable resource which is requested
        //by an XHR of the javascript client
        WMemoryResource * mem = new WMemoryResource("application/octet", this);
        // cast the doubles to an unsigned char array to serve it by the memory resource
        mem->setData(
            reinterpret_cast<unsigned char*>(&(data[0])),
            data.size() * sizeof(float));
        // create client side identifier for the buffer resource and set up for preloading
        ArrayBuffer clientBufferResource = createAndLoadArrayBuffer(mem->generateUrl());
        bufferData(ARRAY_BUFFER, clientBufferResource, STATIC_DRAW);
    }





    // Set the clear color to a transparant background
    clearColor(0, 0, 0, 0);

    // Reset Z-buffer, enable Z-buffering
    clearDepth(1);
    enable(DEPTH_TEST);
    depthFunc(LEQUAL);
}
Beispiel #7
0
void DrawGLWidget::paintGL()
{
    clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT);
    // Reset Z-buffer, enable Z-buffering

    // Draw Cube
    // set perspective matrix
    WMatrix4x4 pMatrix;
    pMatrix.perspective(45,1,0.1,100.0);
    uniformMatrix4(pMatrixUniform_, pMatrix);
    //set model-view matrix
    WMatrix4x4 mvMatrix;
    mvMatrix.setToIdentity();
    mvMatrix.translate(0.0,0.0,zDepth);

    //WMatrix4x4 rotMatrix;
    //rotMatrix.rotate(angleX,-1.0, 0.0, 0.0);
    //rotMatrix.rotate(angleY, 0.0, 1.0, 0.0);

    uniformMatrix4(cMatrixUniform_, rotMatrix);
    //set model-view matrix
    uniformMatrix4(mvMatrixUniform_, mvMatrix);
    //
    uniformMatrix4(nMatrixUniform_, rotMatrix.inverted().transposed());



    activeTexture(TEXTURE0);
    bindTexture(TEXTURE_2D,texture);
    uniform1i(samplerUniform_,0);


    uniform1i(useLightingUniform_,useLighting);
    uniform3f(ambientColorUniform_,ambientColor.values[0],ambientColor.values[1],ambientColor.values[2]);
    uniform3f(lightingDirectionUniform_,lightDirection.values[0],lightDirection.values[1],lightDirection.values[2]);
    uniform3f(directionalColorUniform_,lightColor.values[0],lightColor.values[1],lightColor.values[2]);

    bindBuffer(ARRAY_BUFFER, cubeVertexPositionBuffer);
    vertexAttribPointer(vertexPositionAttribute_,
        cubeVertices.itemsize_,     // size: Every vertex has an X, Y and Z component
        FLOAT, // type: They are floats
        false, // normalized: Please, do NOT normalize the vertices
        0, // stride: The first byte of the next vertex is located this
        //         amount of bytes further. The format of the VBO is
        //         vx, vy, vz, nx, ny, nz and every element is a
        //         Float32, hence 4 bytes large
        0);    // offset: The byte position of the first vertex in the buffer
    bindBuffer(ARRAY_BUFFER, cubeVertexNormalBuffer);
    vertexAttribPointer(vertexNormalAttribute_,
        cubeNormals.itemsize_,     // size: Every vertex has an X, Y and Z component
        FLOAT, // type: They are floats
        false, // normalized: Please, do NOT normalize the vertices
        0, // stride: The first byte of the next vertex is located this
        //         amount of bytes further. The format of the VBO is
        //         vx, vy, vz, nx, ny, nz and every element is a
        //         Float32, hence 4 bytes large
        0);    // offset: The byte position of the first vertex in the buffer
    bindBuffer(ARRAY_BUFFER, cubeTextureCoordBuffer);
    vertexAttribPointer(textureCoordAttribute_,
        cubeTexCoord.itemsize_,     // size: Every vertex has RGBA components
        FLOAT, // type: They are floats
        false, // normalized: Please, do NOT normalize the vertices
        0, // stride: The first byte of the next vertex is located this
        //         amount of bytes further. The format of the VBO is
        //         vx, vy, vz, nx, ny, nz and every element is a
        //         Float32, hence 4 bytes large
        0);    // offset: The byte position of the first vertex in the buffer*/
    bindBuffer(ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
    drawElements(TRIANGLES,cubeIndices.numel_,UNSIGNED_SHORT,0);
}
Beispiel #8
0
void DrawGLWidget::paintGL()
{
    clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT);
    // Reset Z-buffer, enable Z-buffering

    bindBuffer(ARRAY_BUFFER, triangleVertexPositionBuffer);
    // set perspective matrix
    WMatrix4x4 pMatrix;
    pMatrix.perspective(45,1,0.1,100.0);
    jsMatrix_ = createJavaScriptMatrix4();
    setJavaScriptMatrix4(jsMatrix_, pMatrix);
    uniformMatrix4(pMatrixUniform_, jsMatrix_);
    //set model-view matrix
    WMatrix4x4 mvMatrix;
    mvMatrix.setToIdentity();
    mvMatrix.translate(-1.5,0.0,-7.0);
    setJavaScriptMatrix4(jsMatrix_, mvMatrix);
    uniformMatrix4(mvMatrixUniform_, jsMatrix_);

    vertexAttribPointer(vertexPositionAttribute_,
        3,     // size: Every vertex has an X, Y and Z component
        FLOAT, // type: They are floats
        false, // normalized: Please, do NOT normalize the vertices
        3*4, // stride: The first byte of the next vertex is located this
        //         amount of bytes further. The format of the VBO is
        //         vx, vy, vz, nx, ny, nz and every element is a
        //         Float32, hence 4 bytes large
        0);    // offset: The byte position of the first vertex in the buffer
    
    bindBuffer(ARRAY_BUFFER, triangleVertexColorBuffer);
    vertexAttribPointer(vertexColorAttribute_,
        4,     // size: Every vertex has an X, Y and Z component
        FLOAT, // type: They are floats
        false, // normalized: Please, do NOT normalize the vertices
        4*4, // stride: The first byte of the next vertex is located this
        //         amount of bytes further. The format of the VBO is
        //         vx, vy, vz, nx, ny, nz and every element is a
        //         Float32, hence 4 bytes large
        0);    // offset: The byte position of the first vertex in the buffer
    
    drawArrays(TRIANGLES, 0, 3);


    bindBuffer(ARRAY_BUFFER, squareVertexPositionBuffer);
    // set perspective matrix
    setJavaScriptMatrix4(jsMatrix_, pMatrix);
    uniformMatrix4(pMatrixUniform_, jsMatrix_);
    //set model-view matrix
    mvMatrix.translate(3.0,0.0,0.0);
    setJavaScriptMatrix4(jsMatrix_, mvMatrix);
    uniformMatrix4(mvMatrixUniform_, jsMatrix_);

    vertexAttribPointer(vertexPositionAttribute_,
        3,     // size: Every vertex has an X, Y and Z component
        FLOAT, // type: They are floats
        false, // normalized: Please, do NOT normalize the vertices
        3*4, // stride: The first byte of the next vertex is located this
        //         amount of bytes further. The format of the VBO is
        //         vx, vy, vz, nx, ny, nz and every element is a
        //         Float32, hence 4 bytes large
        0);    // offset: The byte position of the first vertex in the buffer
    
    bindBuffer(ARRAY_BUFFER, squareVertexColorBuffer);
    vertexAttribPointer(vertexColorAttribute_,
        4,     // size: Every vertex has an X, Y and Z component
        FLOAT, // type: They are floats
        false, // normalized: Please, do NOT normalize the vertices
        4*4, // stride: The first byte of the next vertex is located this
        //         amount of bytes further. The format of the VBO is
        //         vx, vy, vz, nx, ny, nz and every element is a
        //         Float32, hence 4 bytes large
        0);    // offset: The byte position of the first vertex in the buffer
    drawArrays(TRIANGLE_STRIP, 0, 4); // What are you going to draw, Offset , Number of elements
}