0
我正在android中使用opengl制作2D游戏。我有一个方形纹理的精灵,我打算用它作为弹跳球。 我面临的问题是关于精灵的翻译。我使用单个模型矩阵作为我的顶点着色器的统一体。我在渲染每个精灵之前更新该矩阵。这是否是正确的方法?翻译在opengl中做得不正确
我想通过使用重力效应使球加速,但它只能以恒定速度转换。
这里是精灵类的更新功能: -
public Ball(int textureID) {
texture = textureID;
//Stores location of the center of the ball
location = new Vector(300,350);
//The velocity of ball
speed = new Vector(0, 0);
//gravity acceleration
accel = new Vector(0, 2);
//Geometry of ball
rect = new Geometry.Rectangle(new Geometry.Point(location.getI() - RADIUS,location.getJ() - RADIUS, 0), 2*RADIUS, 2*RADIUS);
//Builder class to create vertex coordinates
builder = new ObjectBuilder(ObjectBuilder.RECTANGLE2D, true);
builder.generateData(rect, 0);
//Vertex Array holds the coordinates
vertexArray = new VertexArray(builder.vertexData);
}
public void update(float[] modelMatrix) {
Matrix.setIdentityM(modelMatrix, 0);
location.addVector(speed);
Matrix.translateM(modelMatrix, 0, speed.getI(), speed.getJ(), 0);
accel.setJ(1);
speed.addVector(accel);
accel.setI(-(0.3f * speed.getI()));
}
我的顶点着色器: -
uniform mat4 u_Matrix;
uniform mat4 u_ModelMatrix;
attribute vec4 a_Position;
attribute vec2 a_TextureCoordinates;
varying vec2 v_TextureCoordinates;
void main() {
v_TextureCoordinates = a_TextureCoordinates;
gl_Position = u_Matrix * u_ModelMatrix * a_Position;
}
我OnDrawFrame功能: -
public void onDrawFrame(GL10 gl) {
textureShaderProgram.useProgram();
ball.update(modelMatrix);
textureShaderProgram.setUniforms(projectionMatrix, modelMatrix);
ball.bindData(textureShaderProgram);
ball.render();
}
谢谢,这解决了我的问题。 btw是我翻译对象的方法吗? 我将这个模型矩阵传递给每个对象渲染前的制服。 – AribAlam
我是否必须为每个对象提供他们自己的模型矩阵,或者只是改变一个可以做到的模式 – AribAlam
这是一个正确的方法。你可以每次改变一个矩阵(但是,每次改变它时,你仍然会将它作为统一函数发送给着色器),但是我总是更喜欢为每个对象保留一个模型矩阵。 – Reigertje