gpt4 book ai didi

java - 在一个 3D 对象上应用旋转后更新 View OpenGL ES 2.0

转载 作者:太空宇宙 更新时间:2023-11-04 10:30:14 24 4
gpt4 key购买 nike

我一直在为 Android Studio 开发一个基于 ARCore 示例的应用程序。我已经成功地将我选择的不同对象带入 View 中,现在我尝试仅旋转一个对象,即最后放置的对象。这意味着当选择一个对象时,我应该能够旋转它并改变它的位置,直到我选择另一个对象。我不应该再能够操纵那个物体了。现在这是我的 draw() 方法,为每个放置的对象调用该方法

public void draw(float[] cameraView, float[] cameraPerspective, float lightIntensity) {

ShaderUtil.checkGLError(TAG, "Before draw");

// Build the ModelView and ModelViewProjection matrices
// for calculating object position and light.
Matrix.multiplyMM(modelViewMatrix, 0, cameraView, 0, modelMatrix, 0);
Matrix.multiplyMM(modelViewProjectionMatrix, 0, cameraPerspective, 0, modelViewMatrix, 0);



if(this.movable==true) {
//rotation
Matrix.setRotateM(mRotationMatrix, 0, FactorsClass.rotateF, 0.0f, 1.0f, 0.0f);
Matrix.multiplyMM(mFinalModelViewProjectionMatrix, 0, modelViewProjectionMatrix, 0, mRotationMatrix, 0);
}

GLES20.glUseProgram(program);

// Set the lighting environment properties.
Matrix.multiplyMV(viewLightDirection, 0, modelViewMatrix, 0, LIGHT_DIRECTION, 0);
normalizeVec3(viewLightDirection);
GLES20.glUniform4f(
lightingParametersUniform,
viewLightDirection[0],
viewLightDirection[1],
viewLightDirection[2],
lightIntensity);

// Set the object material properties.
GLES20.glUniform4f(materialParametersUniform, ambient, diffuse, specular, specularPower);

// Attach the object texture.
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glUniform1i(textureUniform, 0);

// Set the vertex attributes.
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBufferId);

GLES20.glVertexAttribPointer(
positionAttribute, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, verticesBaseAddress);
GLES20.glVertexAttribPointer(normalAttribute, 3, GLES20.GL_FLOAT, false, 0, normalsBaseAddress);
GLES20.glVertexAttribPointer(
texCoordAttribute, 2, GLES20.GL_FLOAT, false, 0, texCoordsBaseAddress);

GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

// Set the ModelViewProjection matrix in the shader.
GLES20.glUniformMatrix4fv(modelViewUniform, 1, false, modelViewMatrix, 0);


GLES20.glUniformMatrix4fv(modelViewProjectionUniform, 1, false, modelViewProjectionMatrix, 0);
if(this.movable==true) {
GLES20.glUniformMatrix4fv(modelViewProjectionUniform, 1, false, mFinalModelViewProjectionMatrix, 0);
}


// Enable vertex arrays
GLES20.glEnableVertexAttribArray(positionAttribute);
GLES20.glEnableVertexAttribArray(normalAttribute);
GLES20.glEnableVertexAttribArray(texCoordAttribute);

if (blendMode != null) {
GLES20.glDepthMask(false);
GLES20.glEnable(GLES20.GL_BLEND);
switch (blendMode) {
case Shadow:
// Multiplicative blending function for Shadow.
GLES20.glBlendFunc(GLES20.GL_ZERO, GLES20.GL_ONE_MINUS_SRC_ALPHA);
break;
case Grid:
// Grid, additive blending function.
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
break;
}
}

GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBufferId);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indexCount, GLES20.GL_UNSIGNED_SHORT, 0);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

if (blendMode != null) {
GLES20.glDisable(GLES20.GL_BLEND);
GLES20.glDepthMask(true);
}

// Disable vertex arrays
GLES20.glDisableVertexAttribArray(positionAttribute);
GLES20.glDisableVertexAttribArray(normalAttribute);
GLES20.glDisableVertexAttribArray(texCoordAttribute);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

ShaderUtil.checkGLError(TAG, "After draw");
}
目前,我可以按照自己的意愿旋转一个对象(当可移动变量为 true 时,这意味着它是场景中放置的最后一个对象),但是一旦我选择另一个对象,旧对象就会返回到其初始位置,就好像它从未旋转过一样。如何更新矩阵以便保持旋转?谢谢!

最佳答案

but once I select another one, the old object returns to its initial position, as if it was never rotated. How can I update the matrix so that the rotations remain applied?

我假设mRotationMatrix是该类的成员。确保 mRotationMatrix 由单位矩阵 intilaized。

如果this.movable==true,则更新mRotationMatrix,但在任何情况下都使用mRotationMatrix来计算mFinalModelViewProjectionMatrix。在任何情况下都使用 mFinalModelViewProjectionMatrix 来设置 modelViewProjectionUniform

public void draw(float[] cameraView, float[] cameraPerspective, float lightIntensity) {

.....

Matrix.multiplyMM(modelViewMatrix, 0, cameraView, 0, modelMatrix, 0);
Matrix.multiplyMM(modelViewProjectionMatrix, 0, cameraPerspective, 0, modelViewMatrix, 0);


if(this.movable==true) {
Matrix.setRotateM(mRotationMatrix, 0, FactorsClass.rotateF, 0.0f, 1.0f, 0.0f);
}
Matrix.multiplyMM(mFinalModelViewProjectionMatrix, 0, modelViewProjectionMatrix, 0, mRotationMatrix, 0);

.....

GLES20.glUniformMatrix4fv(modelViewProjectionUniform, 1, false, mFinalModelViewProjectionMatrix, 0);

.....
}

关于java - 在一个 3D 对象上应用旋转后更新 View OpenGL ES 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50102228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com