opengl - 多个对象的矩阵变换

时间:2013-12-02 21:56:55

标签: opengl vao

我无法让myobject1myobject2myobject3呈现自己的轮播。它们旋转但都面向同一方向。

rotate()被调用后,

glDrawElements停止工作。

如果我在glutSwapBuffers()中拨打render(),它才会重新开始工作。但随后我的屏幕闪烁,但模型以我想要的方式呈现。

换句话说:在调用任何render()之前,rotate()工作正常...... 但在调用任何rotate()之后使用render()rotate()什么都不做...... 因为要让rotate()再次使用,我需要每次调用一个对象时调用glutSwapBuffer() ,这样rotate()才能正常使用myobject1myobject2myobject3,然后屏幕闪烁。

void setModelMatrix() {
    glBindBuffer(GL_UNIFORM_BUFFER, matricesUniBuffer);
    glBufferSubData(GL_UNIFORM_BUFFER, 
    ModelMatrixOffset, MatrixSize, modelMatrix);
    glBindBuffer(GL_UNIFORM_BUFFER,0);
}

void setIdentityMatrix(float *mat, int size) {  
    for (int i = 0; i < size * size; ++i)
            mat[i] = 0.0f;

    for (int i = 0; i < size; ++i)
        mat[i + i * size] = 1.0f;
}

void multMatrix(float *a, float *b) {
    float res[16];

    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            res[j*4 + i] = 0.0f;
            for (int k = 0; k < 4; ++k) {
                res[j*4 + i] += a[k*4 + i] * b[j*4 + k]; 
            }
        }
    }
    memcpy(a, res, 16 * sizeof(float));
}

void rotate(float angle, float x, float y, float z) {
    float aux[16];

    setRotationMatrix(aux,angle,x,y,z);
    multMatrix(modelMatrix,aux);
    setModelMatrix();
}

void setRotationMatrix(float *mat, float angle, float x, float y, float z) {
    float radAngle = DegToRad(angle);
    float co = cos(radAngle);
    float si = sin(radAngle);
    float x2 = x*x;
    float y2 = y*y;
    float z2 = z*z;

    mat[0] = x2 + (y2 + z2) * co; 
    mat[4] = x * y * (1 - co) - z * si;
    mat[8] = x * z * (1 - co) + y * si;
    mat[12]= 0.0f;

    mat[1] = x * y * (1 - co) + z * si;
    mat[5] = y2 + (x2 + z2) * co;
    mat[9] = y * z * (1 - co) - x * si;
    mat[13]= 0.0f;

    mat[2] = x * z * (1 - co) - y * si;
    mat[6] = y * z * (1 - co) + x * si;
    mat[10]= z2 + (x2 + y2) * co;
    mat[14]= 0.0f;

    mat[3] = 0.0f;
    mat[7] = 0.0f;
    mat[11]= 0.0f;
    mat[15]= 1.0f;
}


void recursive_render () {    
    //myobject->ry+=0.0001f;

    for (unsigned int n=0; n < scene->mNumMeshes; ++n)  {
        glBindBufferRange(GL_UNIFORM_BUFFER, materialUniLoc, myobject->Meshes[scene->mMeshes[n]].uniformBlockIndex, 0, sizeof(struct MyMaterial));    
        glBindTexture(GL_TEXTURE_2D, myobject->Meshes[scene->mMeshes[n]].texIndex);
        glBindVertexArray(myobject->Meshes[nd->mMeshes[n]].vao);
        glDrawElements(GL_TRIANGLES,myobject->Meshes[scene->mMeshes[n]].numFaces*3,GL_UNSIGNED_INT,0);    
    }
}

void render()
{
    setIdentityMatrix(modelMatrix,4);

    rotate(myobject->ry, 0.0, 1.0, 0.0);

    scale(scaleFactor, scaleFactor, scaleFactor);

    recursive_render(scene->mRootNode);
}


void renderScene(void) {
    myobject1.ry = 50.0f;
    myobject2.ry = 150.0f;
    myobject3.ry = 270.0f;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    setCamera(camX,camY,camZ,0,0,0);

    glUseProgram(program);
    glUniform1i(texUnit,0);

    myobject = &myobject1;
    render();

    myobject = &myobject2;
    render();

    myobject = &myobject3;
    render();

    glutSwapBuffers();
}

0 个答案:

没有答案