opengl - matrix transformation for multiple objects

278 views Asked by At

I can't get myobject1, myobject2, and myobject3 to have their own rotation rendered. They rotate but all facing the same direction.

rotate() stops working after glDrawElements gets called.

It only starts working again if I call glutSwapBuffers() in render(). But then my screen flickers, but the models are rendered the way I want them.

In other words: before any render() is called, rotate() works fine... but using rotate() after any render() is called, rotate() does nothing... because for rotate() to work again, I need to call glutSwapBuffer() every time I draw an object, which makes rotate() work correctly for myobject1, myobject2, and myobject3, but then screen then flickers.

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

There are 0 answers