似乎我在使用Opengl ES 2时遇到了一些问题。我创建的程序会显示一个向一个方向移动的方块。我以前做过这件事。我只是在其中放了一个游戏循环,现在我有点腌渍。我希望有人可以帮我解决我遇到的问题。 (我已经环顾了3天以上,无法弄清楚)。
我认为错误但不知道如何解决:以前在GameState类中,在hitIt方法中。我有 playerModel.setPosition(xV,yV); 为 playerAssets.setPosition(xV,yV); 。因为以前出了什么问题。 playerMode已扩展为PlayerModel。我在扩展时直接调用了一个类方法。修好之后。我现在遇到了当前的问题(没有当前上下文的调用OpengGL ES API)。
注意:我在更改之前没有错误消息。
活动
package com.Skit.main;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
public class DaSurfaceView extends GLSurfaceView{
private DaRenderer daRend;
public DaSurfaceView(Context context, GamePhysics gamephy) {
super(context);
setEGLContextClientVersion(2); //Request OpenGL ES 2.0
daRend = new DaRenderer(gamephy, this);
setRenderer(daRend);
//setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
return daRend.onTouchEvent(e);
}
}
SurfaceView
package com.Skit.main;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
public class DaSurfaceView extends GLSurfaceView{
private DaRenderer daRend;
public DaSurfaceView(Context context, GamePhysics gamephy) {
super(context);
setEGLContextClientVersion(2); //Request OpenGL ES 2.0
daRend = new DaRenderer(gamephy, this);
setRenderer(daRend);
//setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
return daRend.onTouchEvent(e);
}
}
渲染
package com.Skit.main;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.opengl.GLSurfaceView.Renderer;
import android.view.MotionEvent;
public class DaRenderer implements Renderer{
static final float mProjectionMatrix[] = new float[16];
private PlayerModel playermodel;
private GamePhysics mgamephysics;
private DaSurfaceView surfview;
public DaRenderer(GamePhysics gamephy, DaSurfaceView surf){
mgamephysics = gamephy;
surfview = surf;
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GamePhysics gamephysics = mgamephysics;
// Set the background color.
GLES20.glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
gamephysics.setAssets();
playermodel = new PlayerModel();
}
@Override
public void onDrawFrame(GL10 gl) {
GamePhysics gamephysics = mgamephysics;
gamephysics.hitIt();
// Redraw background color
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
gamephysics.playerDraw();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 2, 7);
}
public static int loadShader(int glVertexShader, String vertexShaderCode) {
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(glVertexShader);
// add the source code to the shader and compile it
GLES20.glShaderSource(shader, vertexShaderCode);
GLES20.glCompileShader(shader);
return shader;
}
public boolean onTouchEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
游戏状态
package com.Skit.main;
import android.util.Log;
public class GamePhysics {
PlayerMode playerAssets = new PlayerMode();
PlayerModel playerModel = new PlayerModel();
//2D Cords & physics variables
private float xV, yV, v = 0.2f, t = 0.1f;
private int TT = 0;
public void setAssets(){
xV = playerAssets.getXPosition();
Log.d("GamePhysics", "XV = " + String.valueOf(xV));
yV = playerAssets.getYPosition();
Log.d("GamePhysics", "YV = " + String.valueOf(yV));
}
void playerDraw(){
playerModel.draw();
}
void hitIt(){
xV = xV + 1;
yV = yV;
//PlayerAsset = PlayerMode.java
playerModel.setPosition(xV, yV);
Log.d("GamePhysics", String.valueOf(xV));
}
}
PlayerMode
package com.Skit.main;
import android.opengl.Matrix;
import android.util.Log;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
public class PlayerMode {
protected float[] mModelView;
protected PlayerMode(){
mModelView = new float[16];
Matrix.setIdentityM(mModelView, 0);
}
public float getXPosition(){
return mModelView[12];
}
public float getYPosition(){
return mModelView[13];
}
public void setPosition(float x, float y){
mModelView[12] = x;
mModelView[13] = y;
Log.d("PlayerMode", String.valueOf(mModelView[12]));
}
}
PlayerModel
package com.Skit.main;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Log;
import com.Skit.main.DaRenderer;
public class PlayerModel extends PlayerMode {
private FloatBuffer vertexBuffer;
private ShortBuffer drawListBuffer;
private static int mProgram;
private int mPositionHandle;
private int mColorHandle;
private int mMVPMatrixHandle;
static final float mViewMatrix[] = new float[16];
static final float mMVPMatrix[] = new float[16];
static float mTempMVP[] = new float[16];
private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
//Object Shaders
private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main() {" +
" gl_Position = uMVPMatrix * vPosition;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float triangleCoords[] = { // in counterclockwise order:
-0.2f, 0.2f, 0.0f, // top left
-0.2f, -0.2f, 0.0f, // bottom left
0.2f, -0.2f, 0.0f, // bottom right
0.2f, 0.2f, 0.0f }; // top right
// Set color with red, green, blue and alpha (opacity) values
float color[] = { 1.0f, 0.0f, 0.0f, 1.0f };
private short[] drawOrder = { 0, 1, 2, 0, 2, 3 };
public PlayerModel() {
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
triangleCoords.length * 4);
// use the device hardware's native byte order
bb.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer.put(triangleCoords);
// set the buffer to read the first coordinate
vertexBuffer.position(0);
// initialize byte buffer for the draw list
ByteBuffer dlb = ByteBuffer.allocateDirect(
// (# of coordinate values * 2 bytes per short)
drawOrder.length * 2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer = dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
int vertexShader = DaRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
vertexShaderCode);
int fragmentShader = DaRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);
// create empty OpenGL ES Program
mProgram = GLES20.glCreateProgram();
// add the vertex shader to program
GLES20.glAttachShader(mProgram, vertexShader);
// add the fragment shader to program
GLES20.glAttachShader(mProgram, fragmentShader);
// creates OpenGL ES program executables
GLES20.glLinkProgram(mProgram);
}
public void draw() {
float[] mvp = mTempMVP;
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);
// Set the camera position (View matrix)
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// combine the projection and camera view Matrix
Matrix.multiplyMM(mMVPMatrix, 0, DaRenderer.mProjectionMatrix, 0, mViewMatrix, 0);
// Combine the Projection/Camera view with ModelView
Matrix.multiplyMM(mvp, 0, mMVPMatrix, 0, mModelView, 0);
//Log to see if modelview is being passed from playermode
Log.d("PlayerModel", String.valueOf(mModelView[12]));
//pass the projection and view transformation to the shader
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvp, 0);
//get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);
// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
// Draw the triangle
GLES20.glDrawElements(
GLES20.GL_TRIANGLES, drawOrder.length,
GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
}
我对这一切都很陌生。对不起草率的代码。