Beispiel #1
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 #2
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 #3
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
}