示例#1
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);
}
示例#2
0
WImage *Captcha::GenCap()
{
    size_t n1 = (rand() % 10) + 1;
    size_t n2 = (rand() % 10) + 1;
    int rotate = (rand() % 7 - 3);
    int skew = (rand() % 9 - 4);

    Result = n1 * n2;

    string captcha(lexical_cast<string>(n1));
    captcha += " X ";
    captcha += lexical_cast<string>(n2);

    Image img(Geometry(115, 35), Color("white"));
    list<Drawable> drawList;

    drawList.push_back(DrawableTextAntialias(true));
    drawList.push_back(DrawableFont("../fnt/HAZELN.TTF"));
    drawList.push_back(DrawablePointSize(32));
    drawList.push_back(DrawableStrokeColor(Color("black")));
    drawList.push_back(DrawableFillColor(Color(0, 0, 0, MaxRGB)));
    drawList.push_back(DrawableTextDecoration(UnderlineDecoration));
    drawList.push_back(DrawableGravity(CenterGravity));


    drawList.push_back(DrawableRotation(rotate));
    drawList.push_back(DrawableRotation(skew));
    drawList.push_back(DrawableText(0, 0, captcha));

    img.draw(drawList);


    string capPath;

    do {
        capPath = "../tmp/captcha-";
        capPath += lexical_cast<string>(rand());
        capPath += ".png";
    } while (System::FileExists(capPath));


    img.write(capPath);

    WMemoryResource *capResource = new WMemoryResource("image/png");

    char *buffer = NULL;
    size_t size = 0;
    System::ReadFile(capPath, size, &buffer);

    capResource->setData(reinterpret_cast<const unsigned char*>(buffer),
                         static_cast<int>(size));

    delete buffer;

    System::EraseFile(capPath);

    WImage *capImage = new WImage(capResource, "Captcha");
    capImage->setStyleClass("captcha");

    return capImage;
}