由于某种原因,我的代码(这是整个事情)除了红色屏幕之外实际上没有呈现任何东西..谁能告诉我为什么?
package com.ntu.way2fungames.earth.testbed;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
public class projectiles extends Activity {
GLSurfaceView lGLView;
Renderer lGLRenderer;
float projectilesX[]= new float[5001];
float projectilesY[]= new float[5001];
float projectilesXa[]= new float[5001];
float projectilesYa[]= new float[5001];
float projectilesTheta[]= new float[5001];
float projectilesSpeed[]= new float[5001];
private static FloatBuffer drawBuffer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SetupProjectiles();
Context mContext = this.getWindow().getContext();
lGLView= new MyView(mContext);
lGLRenderer= new MyRenderer();
lGLView.setRenderer(lGLRenderer);
setContentView(lGLView);
}
private void SetupProjectiles() {
int i=0;
for (i=5000;i>0;i=i-1){
projectilesX[i] = 240;
projectilesY[i] = 427;
float theta = (float) ((i/5000)*Math.PI*2);
projectilesXa[i] = (float) Math.cos(theta);
projectilesYa[i] = (float) Math.sin(theta);
projectilesTheta[i]= theta;
projectilesSpeed[i]= (float) (Math.random()+1);
}
}
public class MyView extends GLSurfaceView{
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}
public class MyRenderer implements Renderer{
private float[] projectilecords = new float[] {
.0f, .5f, 0,
-.5f, 0f, 0,
.5f, 0f, 0,
0, -5f, 0,
};
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
//gl.glLoadIdentity();
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
for (int i=5000;i>4500;i=i-1){
//drawing section
gl.glLoadIdentity();
gl.glColor4f(.9f, .9f,.9f,.9f);
gl.glTranslatef(projectilesY[i], projectilesX[i],1);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, drawBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 12);
//physics section
projectilesX[i]=projectilesX[i]+projectilesXa[i];
projectilesY[i]=projectilesY[i]+projectilesYa[i];
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (height == 0) height = 1;
// draw on the entire screen
gl.glViewport(0, 0, width, height);
// setup projection matrix
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0,width,height,0, -100, 100);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(1f, .01f, .01f, 1f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
drawBuffer = FloatBuffer.wrap(projectilecords);
}
}
}
答案 0 :(得分:0)
原来,射弹初始化代码已被破坏
int i=0;
for (i=5000;i>0;i=i-1){
projectilesX[i] = 240;
projectilesY[i] = 427;
float theta = (float) ((i/5000)*Math.PI*2);
projectilesXa[i] = (float) Math.cos(theta);
projectilesYa[i] = (float) Math.sin(theta);
projectilesTheta[i]= theta;
projectilesSpeed[i]= (float) (Math.random()+1);
}
不确定为什么,但当我用
替换它时int i=0;
float pi2=(float)(Math.PI *2);
for (i=5000;i>0;i=i-1){
projectilesX[i] = 160;
projectilesY[i] = 320;
float theta = (float)(Math.random());
projectilesSpeed[i]= (float) (Math.random()+1);
projectilesXa[i] = (float) Math.cos(theta*pi2)*projectilesSpeed[i];
projectilesYa[i] = (float) Math.sin(theta*pi2)*projectilesSpeed[i];
projectilesTheta[i]= (float) (theta*360);
}
事情开始起作用......