コード例 #1
0
ファイル: webgl.C プロジェクト: 913862627/wt
  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);

  }
コード例 #2
0
ファイル: PaintWidget.C プロジェクト: mickythump/wt
// 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);
}