Opengl es 如何在opengl(android平台)中的特定位置(例如1.0,2.0,0.5)绘制对象(球体或三角形)
我是opengl(Android平台)的新手。我想画一个opengl对象,比如说一个3D中特定位置的球体。我怎么做?画完之后,我想在触摸事件中旋转它。那么我该如何做这两件事呢?提前感谢。您可以使用变换矩阵来实现这一点。 你需要三个矩阵来做你想做的事情,一个对象矩阵(给出它的位置和旋转),一个相机矩阵(给出相机在空间中的位置和方向)和一个投影矩阵(产生透视,即远的东西是小的,大的东西是近的) 在android中,您可以:Opengl es 如何在opengl(android平台)中的特定位置(例如1.0,2.0,0.5)绘制对象(球体或三角形),opengl-es,Opengl Es,我是opengl(Android平台)的新手。我想画一个opengl对象,比如说一个3D中特定位置的球体。我怎么做?画完之后,我想在触摸事件中旋转它。那么我该如何做这两件事呢?提前感谢。您可以使用变换矩阵来实现这一点。 你需要三个矩阵来做你想做的事情,一个对象矩阵(给出它的位置和旋转),一个相机矩阵(给出相机在空间中的位置和方向)和一个投影矩阵(产生透视,即远的东西是小的,大的东西是近的) 在android中,您可以: float[] model = new float[16]; float[]
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()分别加载modelview和投影矩阵(无需计算mvp)。不要使用内置的OpenGL ES 1.0 glRotatef()/glTranslatef(),它们只会引起悲伤和不安
注意上面的代码包含perspective()函数。我怀疑它在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
}
我希望这有助于…您可以使用gluLookAt()方法轻松定位。GLU.gluLookAt(gl,0,0,5,moveToX,moveToY,moveToZ,0,1,0);对。尽管它很容易出错。也不确定它是否符合OP的要求。