Java OpenGL ES 2.0仅绘制一次对象
首先,我很抱歉今天问了这么多问题。 所以,我有一个圆圈课。我有一个arraylist,有3个圆实例,每个都有不同的x坐标。不管出于什么原因,OpenGL ES 2.0只绘制了其中一个,尽管我将它们全部称为绘制。我看了几个小时的代码,尝试了很多不同的东西,但都没有用。所以,我的问题是:要让OpenGLES2.0绘制多个对象,我需要做什么特殊的事情? 这是我的代码(我知道它很乱。我稍后会解决:): 这是我的GLCircle课程:Java OpenGL ES 2.0仅绘制一次对象,java,android,object,matrix,opengl-es-2.0,Java,Android,Object,Matrix,Opengl Es 2.0,首先,我很抱歉今天问了这么多问题。 所以,我有一个圆圈课。我有一个arraylist,有3个圆实例,每个都有不同的x坐标。不管出于什么原因,OpenGL ES 2.0只绘制了其中一个,尽管我将它们全部称为绘制。我看了几个小时的代码,尝试了很多不同的东西,但都没有用。所以,我的问题是:要让OpenGLES2.0绘制多个对象,我需要做什么特殊的事情? 这是我的代码(我知道它很乱。我稍后会解决:): 这是我的GLCircle课程: package com.background.gl.objects;
package com.background.gl.objects;
import static android.opengl.GLES20.GL_TRIANGLE_FAN;
import static android.opengl.GLES20.glDrawArrays;
import static android.opengl.Matrix.multiplyMM;
import static android.opengl.Matrix.setIdentityM;
import static android.opengl.Matrix.translateM;
import static com.background.gl.glcirclebackgroundanimation.Constants.BYTES_PER_FLOAT;
import java.util.Random;
import android.opengl.Matrix;
import com.background.gl.data.VertexArray;
import com.background.gl.helper.TextureShaderProgram;
public class GLCircle {
private static final int POSITION_COMPONENT_COUNT = 2;
private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
private static final int STRIDE = (POSITION_COMPONENT_COUNT
+ TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT;
public float x;
public float y;
protected float[] wallBounds;
protected boolean positiveX, positiveY;
public boolean nullify;
protected float xCounter = 0f;
protected float yCounter = 0f;
public float[] bounds;
protected Random ran;
private float[] VERTEX_DATA = {
// Order of coordinates: X, Y, S, T
// Triangle Fan
0f, 0f, 0.5f, 0.5f,
-0.25f, -0.25f, 0f, 0.9f,
0.25f, -0.25f, 1f, 0.9f,
0.25f, 0.25f, 1f, 0.1f,
-0.25f, 0.25f, 0f, 0.1f,
-0.25f, -0.25f, 0f, 0.9f };
private final VertexArray vertexArray;
public GLCircle(float x, float y) {
vertexArray = new VertexArray(VERTEX_DATA);
ran = new Random();
wallBounds = new float[4];
nullify = false;
this.x = x;
this.y = y;
}
public void bindData(TextureShaderProgram textureProgram) {
//Bind the position data to the shader attribute
vertexArray.setVertexAttribPointer(
0,
textureProgram.getPositionAttributeLocation(),
POSITION_COMPONENT_COUNT,
STRIDE);
//Bind the texture coordinate data to the shader attribute
vertexArray.setVertexAttribPointer(
POSITION_COMPONENT_COUNT,
textureProgram.getTextureCoordinatesAttributeLocation(),
TEXTURE_COORDINATES_COMPONENT_COUNT,
STRIDE);
}
public void drawCircle() {
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
}
public float getX() {
return this.x;
}
public float getY() {
return this.y;
}
public boolean isPositiveX() {
return positiveX;
}
public boolean isPositiveY() {
return positiveY;
}
public float[] getBounds(float ranX, float ranY) {
if(!positiveX) {
/*if(ranX >= 0f) {
wallBounds[0] = 1.05f + ranX;
} else {*/
this.wallBounds[0] = 1.05f + ranX;
//}
} else {
/*
if(ranX >= 0f) {
wallBounds[0] = 1.05f - ranX;
} else {*/
this.wallBounds[1] = 1.05f - ranX;
//}
}
if(!positiveY) {
this.wallBounds[2] = 1.75f + ranY;
} else {
this.wallBounds[3] = 1.75f - ranY;
}
return this.wallBounds;
}
public void setPos(float[] modelMatrix,
float[] projectionMatrix, TextureShaderProgram textureProgram,
int texture, float x, float y) {
setIdentityM(modelMatrix, 0);
translateM(modelMatrix, 0, 0f, 0.01f, 0f);
final float[] temp = new float[16];
multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0);
System.arraycopy(temp, 0, projectionMatrix, 0, temp.length);
textureProgram.useProgram();
textureProgram.setUniforms(projectionMatrix, texture);
bindData(textureProgram);
drawCircle();
}
public void scaleCircle(float[] modelMatrix, float x, float y, float z) {
Matrix.scaleM(modelMatrix, 0, x, y, z);
}
public void translateCircle(float x, float[] modelMatrix) {
translateM(modelMatrix, 0, /*-0.001f*/ x, -3f, -2f);
}
}
因此,在我的renderer类中,这就是我在onSurfaceCreated()中实例化这些对象所做的(我知道我调用GeneratorFloat两次,这样做会浪费内存):
现在,问题是我只看到我的一个圆圈被翻译到屏幕上,而我应该有三个。据我所知,我应该能够在屏幕上画多个物体。那么为什么只画一个呢?我还尝试添加一个单独的名为circle2的GLCircle对象,并对这个对象进行了相同的操作,但它也没有显示出来。要在屏幕上绘制多个对象,我必须做什么?非常感谢您的帮助,如果您需要查看更多代码,请告诉我。:)
编辑:整个渲染器类
package com.background.gl.glcirclebackgroundanimation;
import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glViewport;
import static android.opengl.Matrix.multiplyMM;
import static android.opengl.Matrix.setIdentityM;
import static android.opengl.Matrix.translateM;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
import com.background.gl.helper.TextureShaderProgram;
import com.background.gl.objects.GLCircle;
import com.background.gl.objects.Mallet;
import com.background.gl.objects.Table;
import com.background.gl.util.MatrixHelper;
import com.background.gl.util.TextureHelper;
public class CircleDynamicBackgroundRenderer implements Renderer {
private final Context context;
private final float[] projectionMatrix = new float[16];
private final float[] modelMatrix = new float[16];
protected static float ranX, ranY, ranSignX, ranSignY, ranSignVeloX, ranSignVeloY;
private Table table;
private Mallet mallet;
private List<GLCircle> circles;
private GLCircle circle2;
float xPos, yPos;
int x = 1;
float[] bounds;
Random ran;
private TextureShaderProgram textureProgram;
private int texture;
public CircleDynamicBackgroundRenderer(Context context) {
this.context = context;
circles = new ArrayList<GLCircle>();
xPos = 0.1f;
ran = new Random();
}
@Override
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
glViewport(0, 0, width, height);
Log.w("Width and height", Integer.toString(width) + ", " + Integer.toString(height));
MatrixHelper.perspectiveM(projectionMatrix, 90, (float) width
/ (float) height, 1f, 10f);
setIdentityM(modelMatrix, 0);
for(int i = 0; i < circles.size(); i++) {
circles.get(i).translateCircle(circles.get(i).x, modelMatrix);
}
circle2.translateCircle(circle2.x, modelMatrix);
final float[] temp = new float[16];
multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0);
System.arraycopy(temp, 0, projectionMatrix, 0, temp.length);
}
@Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
table = new Table();
mallet = new Mallet();
textureProgram = new TextureShaderProgram(context);
texture = TextureHelper.loadTexture(context, R.drawable.air_hockey_surface);
//texture2 = TextureHelper.loadTexture(context, R.drawable.air_hockey_surface_2);
for(int i = 0; i < 3; i++) {
GLCircle circle = new GLCircle(generateRanFloats()[0], generateRanFloats()[1]);
circles.add(circle);
/*circle[i].x = circle[i].getX();
circle[i].y = circle[i].getY();
circle[i].bounds = circle[i].getBounds();*/
}
circle2 = new GLCircle(generateRanFloats()[0], generateRanFloats()[1]);
Log.d("Circles size", Integer.toString(circles.size()));
Log.d("circles", Float.toString(circles.get(1).getX()) + " " + Float.toString(circles.get(2).getX()));
}
@Override
public void onDrawFrame(GL10 glUnused) {
//Clear the rendering surface
glClear(GL_COLOR_BUFFER_BIT);
for(int i = 0; i < circles.size(); i++) {
circles.get(i).setPos(modelMatrix, projectionMatrix, textureProgram, texture, circles.get(i).x, circles.get(i).y);
Log.d("Circles", Float.toString(circles.get(i).x));
}
circle2.setPos(modelMatrix, projectionMatrix, textureProgram, texture, ranSignVeloX, circle2.x);
Log.d("Circle2", Float.toString(circle2.x));
//Pass data into our shaders(u_matrix) and enable/bind the texture
//textureProgram.setUniforms2(projectionMatrix, texture, texture2);
//Bind our [vertex array] data to our shaders(attribute data)
//Draw it
/*
// Draw the mallets.
colorProgram.useProgram();
colorProgram.setUniforms(projectionMatrix);
mallet.bindData(colorProgram);
mallet.draw();*/
}
public float[] generateRanFloats() {
ranSignX = ran.nextFloat();
ranSignY = ran.nextFloat();
ranSignX = ranSignX > 0.5f? -1:1;
ranSignY = ranSignY > 0.5f? -1:1;
ranSignVeloX = ran.nextFloat();
ranSignVeloY = ran.nextFloat();
ranX = ran.nextFloat() * 1.05f;
ranY = ran.nextFloat() * 1.75f;
ranX = ranSignX > 0.5? -ranX:ranX;
ranY = ranSignY > 0.5? -ranY:ranY;
Log.d("Generated", Float.toString(ranX));
return new float[] {ranX, ranY};
}
}
package com.background.gl.glcirclebackgroundanimation;
导入静态android.opengl.GLES20.GL\u颜色\u缓冲\u位;
导入静态android.opengl.GLES20.glClear;
导入静态android.opengl.GLES20.glClearColor;
导入静态android.opengl.GLES20.glViewport;
导入静态android.opengl.Matrix.multiplyMM;
导入静态android.opengl.Matrix.setIdentityM;
导入静态android.opengl.Matrix.translateM;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
导入javax.microedition.khronos.egl.EGLConfig;
导入javax.microedition.khronos.opengles.GL10;
导入android.content.Context;
导入android.opengl.GLSurfaceView.Renderer;
导入android.util.Log;
导入com.background.gl.helper.TextureShader程序;
导入com.background.gl.objects.GLCircle;
导入com.background.gl.objects.Mallet;
导入com.background.gl.objects.Table;
导入com.background.gl.util.MatrixHelper;
导入com.background.gl.util.TextureHelper;
公共类CircleDynamicBackgroundRenderer实现渲染器{
私人最终语境;
私有最终浮动[]projectionMatrix=新浮动[16];
私有最终浮动[]模型矩阵=新浮动[16];
受保护静态浮子ranX、ranY、ranSignX、ranSignY、ranSignVeloX、ranSignVeloY;
私人餐桌;
私人木槌;
私人名单圈;
私人圆圈2;
浮动XPO、YPO;
int x=1;
浮动[]边界;
随机运行;
私有TextureShader程序textureProgram;
私有int纹理;
公共CircleDynamicBackgroundRenderer(上下文){
this.context=上下文;
圆圈=新的ArrayList();
xPos=0.1f;
ran=新随机数();
}
@凌驾
表面上的公共空隙已更改(GL10已涂胶、整型宽度、整型高度){
glViewport(0,0,宽度,高度);
Log.w(“宽度和高度”,Integer.toString(宽度)+”,“Integer.toString(高度));
矩阵帮助透视图(投影矩阵,90,(浮动)宽度
/(浮子)高度,1f,10f);
setIdentityM(modelMatrix,0);
对于(int i=0;i0.5f?-1:1;
ranSignY=ranSignY>0.5f?-1:1;
ranSignVeloX=r
for(int i = 0; i < 3; i++) {
GLCircle circle = new GLCircle(generateRanFloats()[0], generateRanFloats()[1]);
circles.add(circle);
}
for(int i = 0; i < circles.size(); i++) {
circles.get(i).translateCircle(circles.get(i).x, modelMatrix);
}
circles.get(i).setPos(modelMatrix, projectionMatrix, textureProgram, texture, circles.get(i).x, circles.get(i).y);
package com.background.gl.glcirclebackgroundanimation;
import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glViewport;
import static android.opengl.Matrix.multiplyMM;
import static android.opengl.Matrix.setIdentityM;
import static android.opengl.Matrix.translateM;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
import com.background.gl.helper.TextureShaderProgram;
import com.background.gl.objects.GLCircle;
import com.background.gl.objects.Mallet;
import com.background.gl.objects.Table;
import com.background.gl.util.MatrixHelper;
import com.background.gl.util.TextureHelper;
public class CircleDynamicBackgroundRenderer implements Renderer {
private final Context context;
private final float[] projectionMatrix = new float[16];
private final float[] modelMatrix = new float[16];
protected static float ranX, ranY, ranSignX, ranSignY, ranSignVeloX, ranSignVeloY;
private Table table;
private Mallet mallet;
private List<GLCircle> circles;
private GLCircle circle2;
float xPos, yPos;
int x = 1;
float[] bounds;
Random ran;
private TextureShaderProgram textureProgram;
private int texture;
public CircleDynamicBackgroundRenderer(Context context) {
this.context = context;
circles = new ArrayList<GLCircle>();
xPos = 0.1f;
ran = new Random();
}
@Override
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
glViewport(0, 0, width, height);
Log.w("Width and height", Integer.toString(width) + ", " + Integer.toString(height));
MatrixHelper.perspectiveM(projectionMatrix, 90, (float) width
/ (float) height, 1f, 10f);
setIdentityM(modelMatrix, 0);
for(int i = 0; i < circles.size(); i++) {
circles.get(i).translateCircle(circles.get(i).x, modelMatrix);
}
circle2.translateCircle(circle2.x, modelMatrix);
final float[] temp = new float[16];
multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0);
System.arraycopy(temp, 0, projectionMatrix, 0, temp.length);
}
@Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
table = new Table();
mallet = new Mallet();
textureProgram = new TextureShaderProgram(context);
texture = TextureHelper.loadTexture(context, R.drawable.air_hockey_surface);
//texture2 = TextureHelper.loadTexture(context, R.drawable.air_hockey_surface_2);
for(int i = 0; i < 3; i++) {
GLCircle circle = new GLCircle(generateRanFloats()[0], generateRanFloats()[1]);
circles.add(circle);
/*circle[i].x = circle[i].getX();
circle[i].y = circle[i].getY();
circle[i].bounds = circle[i].getBounds();*/
}
circle2 = new GLCircle(generateRanFloats()[0], generateRanFloats()[1]);
Log.d("Circles size", Integer.toString(circles.size()));
Log.d("circles", Float.toString(circles.get(1).getX()) + " " + Float.toString(circles.get(2).getX()));
}
@Override
public void onDrawFrame(GL10 glUnused) {
//Clear the rendering surface
glClear(GL_COLOR_BUFFER_BIT);
for(int i = 0; i < circles.size(); i++) {
circles.get(i).setPos(modelMatrix, projectionMatrix, textureProgram, texture, circles.get(i).x, circles.get(i).y);
Log.d("Circles", Float.toString(circles.get(i).x));
}
circle2.setPos(modelMatrix, projectionMatrix, textureProgram, texture, ranSignVeloX, circle2.x);
Log.d("Circle2", Float.toString(circle2.x));
//Pass data into our shaders(u_matrix) and enable/bind the texture
//textureProgram.setUniforms2(projectionMatrix, texture, texture2);
//Bind our [vertex array] data to our shaders(attribute data)
//Draw it
/*
// Draw the mallets.
colorProgram.useProgram();
colorProgram.setUniforms(projectionMatrix);
mallet.bindData(colorProgram);
mallet.draw();*/
}
public float[] generateRanFloats() {
ranSignX = ran.nextFloat();
ranSignY = ran.nextFloat();
ranSignX = ranSignX > 0.5f? -1:1;
ranSignY = ranSignY > 0.5f? -1:1;
ranSignVeloX = ran.nextFloat();
ranSignVeloY = ran.nextFloat();
ranX = ran.nextFloat() * 1.05f;
ranY = ran.nextFloat() * 1.75f;
ranX = ranSignX > 0.5? -ranX:ranX;
ranY = ranSignY > 0.5? -ranY:ranY;
Log.d("Generated", Float.toString(ranX));
return new float[] {ranX, ranY};
}
}
setIdentityM(modelMatrix, 0);
translateM(modelMatrix, 0, 0f, 0.01f, 0f);
final float[] temp = new float[16];
multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0);
...
translateM(modelMatrix, 0, 0f, 0.01f, 0f);
translateM(modelMatrix, 0, x, 0.01f, 0f);