我如何在opengl(android平台)中的特定位置(例如1.0,2.0,0.5)绘制对象(球体或三角形)

发布于 2024-11-07 09:47:05 字数 106 浏览 0 评论 0原文

我是 opengl(Android 平台)的新手。我想在 3D 中的特定位置绘制一个 opengl 对象,例如一个球体。我该怎么做?绘制后我想在触摸事件上旋转它。那么我该如何做这两件事呢?提前致谢。

I am new to opengl(Android Platform). I want to draw an opengl object say a sphere at a specific loaction in 3D. How can i do it? After drawing it i want to rotate it on touch event.So How can i do these two things? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋檐 2024-11-14 09:47:05

您可以使用变换矩阵来做到这一点。
您需要三个矩阵来完成您想要的操作,一个对象矩阵(给出它的位置和旋转),一个相机矩阵(给出相机在空间中的位置和方向)和一个投影矩阵(导致透视,即远处的东西很小,很大事情就近了)。

在 android 中,您需要执行以下操作:

float[] model = new float[16];
float[] camera = new float[16];
float[] projection = new float[16];
// those are the "basic" matrices

float[] modelview = new float[16];
float[] mvp = new float[16];
// those are their products

float f_scene_rotation_x = 10, f_scene_rotation_y = 30;
Matrix.setIdentityM(camera, 0);
Matrix.translateM(camera, 0, 0, 0, -2.5f);
Matrix.rotateM(camera, 0, f_scene_rotation_y, 1, 0, 0);
Matrix.rotateM(camera, 0, f_scene_rotation_x, 0, 1, 0);
// set up a camera, orbitting the scene

Matrix.setIdentityM(model, 0);
Matrix.translateM(model, 0, 1, 2, 0.5f); // set your desired position for the object (1.0,2.0,0.5)
//Matrix.rotateM(model, 0, angle, axisX, axisY, axisZ); // can rotate you object by defined angle arround defined axis
// set up model matrix

int mWidth = 640, mHeight = 480; // display dimensions
perspective(projection, 0, 90, (float)mWidth / mHeight, .1f, 1000.0f);
// set up perspective projection

Matrix.multiplyMM(modelview, 0, camera, 0, model, 0); // modelview = camera * model
Matrix.multiplyMM(mvp, 0, projection, 0, modelview, 0); // mvp = projection * modelview
// fuse matrices together

GLES20.glUseProgram(xxx); // bind your shader
GLES20.glUniformMatrix4fv(yyy, 1, false, GLES20.GL_FALSE, mvp); // pass the final matrix to the shader
// xxx is your program object, yyy is location of the modelview-projection matrix uniform inside your vertex shader

最后两行代码依赖于您使用 OpenGL ES 2.0。如果不是这种情况,请使用 glLoadMatrix() 分别加载模型视图和投影矩阵(无需计算 mvp)。不要使用内置的 OpenGL ES 1.0 glRotatef() / glTranslatef(),它们只会引起悲伤和刺激。

请注意,上面的代码包含透视图()函数。我怀疑它在 Android 库中的某个地方,但你也可以使用:

public static void perspective(float[] matrix, int moffset, float f_fov, float f_aspect, float f_near, float f_far)
{
    float f_w, f_h;

    f_h = (float)(Math.tan(f_fov * Math.PI / 180 * .5f)) * f_near;
    f_w = f_h * f_aspect;
    // calc half width of frustum in x-y plane at z = f_near

    Matrix.frustumM(matrix, moffset, -f_w, f_w, -f_h, f_h, f_near, f_far);
    // set frustum
}

我希望这有帮助......

You use transformation matrices to do that.
You need three matrices to do what you want, an object matrix (giving it position and rotation), a camera matrix (giving camera position and orientation in space), and a projection matrix (that causes perspective, i.e. far things are small, large things are near).

In android, you do:

float[] model = new float[16];
float[] camera = new float[16];
float[] projection = new float[16];
// those are the "basic" matrices

float[] modelview = new float[16];
float[] mvp = new float[16];
// those are their products

float f_scene_rotation_x = 10, f_scene_rotation_y = 30;
Matrix.setIdentityM(camera, 0);
Matrix.translateM(camera, 0, 0, 0, -2.5f);
Matrix.rotateM(camera, 0, f_scene_rotation_y, 1, 0, 0);
Matrix.rotateM(camera, 0, f_scene_rotation_x, 0, 1, 0);
// set up a camera, orbitting the scene

Matrix.setIdentityM(model, 0);
Matrix.translateM(model, 0, 1, 2, 0.5f); // set your desired position for the object (1.0,2.0,0.5)
//Matrix.rotateM(model, 0, angle, axisX, axisY, axisZ); // can rotate you object by defined angle arround defined axis
// set up model matrix

int mWidth = 640, mHeight = 480; // display dimensions
perspective(projection, 0, 90, (float)mWidth / mHeight, .1f, 1000.0f);
// set up perspective projection

Matrix.multiplyMM(modelview, 0, camera, 0, model, 0); // modelview = camera * model
Matrix.multiplyMM(mvp, 0, projection, 0, modelview, 0); // mvp = projection * modelview
// fuse matrices together

GLES20.glUseProgram(xxx); // bind your shader
GLES20.glUniformMatrix4fv(yyy, 1, false, GLES20.GL_FALSE, mvp); // pass the final matrix to the shader
// xxx is your program object, yyy is location of the modelview-projection matrix uniform inside your vertex shader

The last two lines of code rely on you using OpenGL ES 2.0. If that's not the case, use glLoadMatrix() to load modelview and projection matrices separately (no need to calculate mvp). Do not use built-in OpenGL ES 1.0 glRotatef() / glTranslatef(), those only cause sadness and irritation.

Note the code above contains the perspective() function. I suspect it is somewhere in Android libraries, but you could also use:

public static void perspective(float[] matrix, int moffset, float f_fov, float f_aspect, float f_near, float f_far)
{
    float f_w, f_h;

    f_h = (float)(Math.tan(f_fov * Math.PI / 180 * .5f)) * f_near;
    f_w = f_h * f_aspect;
    // calc half width of frustum in x-y plane at z = f_near

    Matrix.frustumM(matrix, moffset, -f_w, f_w, -f_h, f_h, f_near, f_far);
    // set frustum
}

I hope this helps ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文