我正在使用OpenGL ES 2构建一个Android应用程序来绘制分子。我画了分子,但我不知道如何添加触摸事件来旋转我的原始原子周围的分子位于(0,0,0),就像我想在Oxy周围旋转2原子Hidro与Oxy位于(0,0) ,0)
我已阅读了一些指南touch和Rotate the sphere around another,但我不明白它是如何运作的。这是我渲染分子的代码,希望有人可以帮助我!
public class MoleculeRenderer extends Activity implements Renderer {
private final Context context;
private String t="1";
private final float[] projectionMatrix = new float[16];
private final float[] modelMatrix = new float[16];
private final float[] viewMatrix = new float[16];
private final float[] viewProjectionMatrix = new float[16];
private final float[] modelViewProjectionMatrix = new float[16];
private Atom atom;
public Float height;
public Float radius;
String amount,amount1;
private Cylinder cylinder;
public ArrayList<Point> apl;
public ArrayList<CoCylinder> acl;
public ArrayList<Float> angle;
public MainRenderer id;
InputStream is;
// private TextureShaderProgram textureProgram;
private ColorShaderProgram colorProgram;
// private int texture;
public MoleculeRenderer(Context context) {
this.context = context;
id=(MainRenderer) context;
apl=id.lp;
acl=id.lc;
angle=id.angle;
amount=id.amount;
amount1=id.amount1;
height=id.link;
radius=id.radius;
}
public void setT(String l){
t = l;
}
public String getT(){
return t;
}
@Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
atom = new Atom(radius, 200);
cylinder = new Cylinder(0.01f, height-2*radius, 64);
// textureProgram = new TextureShaderProgram(context);
colorProgram = new ColorShaderProgram(context);
// texture = TextureHelper.loadTexture(context,
// R.drawable.air_hockey_surface);
}
@Override
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
// Set the OpenGL viewport to fill the entire surface.
glViewport(0, 0, width, height);
MatrixHelper.perspectiveM(projectionMatrix, 45, (float) width
/ (float) height, 1f, 10f);
setLookAtM(viewMatrix, 0, 0.6f, 0.8f, 1.9f, 0f, 0f, 0f, 0f, 1f, 0f);
}
@Override
public void onDrawFrame(GL10 glUnused) {
// Clear the rendering surface.
glClear(GL_COLOR_BUFFER_BIT);
multiplyMM(viewProjectionMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
colorProgram.useProgram();
drawsphere();
drawCylinder();
}
private void positionObjectInScene1(float x, float y, float z) {
setIdentityM(modelMatrix, 0);
translateM(modelMatrix, 0, x, y, z);
multiplyMM(modelViewProjectionMatrix, 0, viewProjectionMatrix, 0,
modelMatrix, 0);
}
private void positionObjectInScene2(float angle, float x, float y, float z) {
setIdentityM(modelViewProjectionMatrix, 0);
rotateM(modelMatrix, 0, angle, x, y, z);
multiplyMM(modelViewProjectionMatrix, 0, viewProjectionMatrix, 0,
modelMatrix, 0);
}
private void drawsphere() {
for (int i = 0; i < apl.size(); i++) {
float x = apl.get(i).getx();
float y = apl.get(i).gety();
float z = apl.get(i).getz();
//Log.d("ANDROID", "X = " + x + " Y = " + y + " Z = " + z);
positionObjectInScene1(x, y, z);
if(i<Integer.parseInt(amount))
{
colorProgram.setUniforms(modelViewProjectionMatrix, 0.5f, 0.5f,0.5f);
}
else if (i>=Integer.parseInt(amount) &&i<(Integer.parseInt(amount1)+ Integer.parseInt(amount)))
{
colorProgram.setUniforms(modelViewProjectionMatrix, 0.2f, 0.3f, 0.3f);
}
else
colorProgram.setUniforms(modelViewProjectionMatrix, 0f, 0f, 1f);
atom.bindData(colorProgram);
atom.draw();
}
}
private void drawCylinder() {
for (int i = 0; i < acl.size(); i++) {
Point point = acl.get(i).getCylinder();
Log.d("cylinder", "X = " + point.x + " Y = " + point.y + " Z = " + point.z);
Point convert = convert(point);
//Log.d("ANDROID", "X = " + convert.x + " Y = " +convert.y + " Z = " + convert.z);
//Log.d("cylinder", "x " + convert.x + "y " + convert.y + "z " + convert.z);
positionObjectInScene1(point.x, point.y , point.z );
if(convert.x==0 && convert.y==0 && convert.z ==0){
positionObjectInScene2(90, 0 , 0 , 1f );
}
else
positionObjectInScene2(angle.get(i), convert.x , convert.y , convert.z );
colorProgram.setUniforms(modelViewProjectionMatrix, 1f, 1f, 1f);
cylinder.bindData(colorProgram);
cylinder.draw();
}
}
public Point convert(Point point) {
Point orgin = new Point(0, (float) (point.y + 0.1), 0);
float x, y, z;
x = (orgin.y * point.z) - (orgin.z * point.y);
y = (orgin.z * point.x) - (orgin.x * point.z);
z = (orgin.x * point.y) - (orgin.y * point.x);
return new Point(x, y, z);
}
}