Java 是否可以使用OpenGL ES 2.0绘制多个对象

Java 是否可以使用OpenGL ES 2.0绘制多个对象,java,android,object,opengl-es,opengl-es-2.0,Java,Android,Object,Opengl Es,Opengl Es 2.0,这是对我最近提出的一个问题的更新,但在我尝试了一些东西之后,出现了新的问题。我想要的是在屏幕上画多个圆圈。我为圆形对象创建了一个类。在我的renderer类中,我创建了这些圆的数组列表,每个圆具有不同的位置。当调用draw时,它只绘制其中一个。我记录了正在发生的事情,每个对象都被调用来绘制,但似乎只有一个对象出现在屏幕上。这不仅仅是我的圈子。在整个个人测试过程中,OpenGL ES 2.0只会在屏幕上绘制一个对象。我不知道如何解决这个问题。在OpenGLES2.0中有什么特别的事情需要我去做吗?

这是对我最近提出的一个问题的更新,但在我尝试了一些东西之后,出现了新的问题。我想要的是在屏幕上画多个圆圈。我为圆形对象创建了一个类。在我的renderer类中,我创建了这些圆的数组列表,每个圆具有不同的位置。当调用draw时,它只绘制其中一个。我记录了正在发生的事情,每个对象都被调用来绘制,但似乎只有一个对象出现在屏幕上。这不仅仅是我的圈子。在整个个人测试过程中,OpenGL ES 2.0只会在屏幕上绘制一个对象。我不知道如何解决这个问题。在OpenGLES2.0中有什么特别的事情需要我去做吗?这是我的密码。现在就忽略那些令人难以置信的草率和低效的代码。我知道这件事,以后会解决的。这是我的circle对象类:

GLCircle:

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 android.util.Log;

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, String log) {
        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();
        Log.d("Drawing", "Drawing " + log);
    }

    public void scaleCircle(float[] modelMatrix, float x, float y, float z) {
        Matrix.scaleM(modelMatrix, 0, x, y, z);
    }

    public void storeResults(float[] results) {
        this.x = results[0];
        this.y = results[1];
    }

    public void translateCircle(float x, float[] modelMatrix, float[] projectionMatrix) {
        setIdentityM(modelMatrix, 0);
        translateM(modelMatrix, 0, /*-0.001f*/ x, -3f, -2f);
        final float[] temp = new float[16];
        multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0);
        System.arraycopy(temp, 0, projectionMatrix, 0, temp.length);
    }
}
同样,我知道我做的大部分事情都不正确,但目前我只需要弄清楚为什么我不能在屏幕上绘制多个对象。以下是我的渲染器代码:

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;
    public boolean logNums;
    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();
        logNums = true;
    }

    @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);

        for(int i = 0; i < circles.size(); i++) {
            circles.get(i).translateCircle(circles.get(i).x, modelMatrix, projectionMatrix);
        }
//      /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, "1");         if(logNums) {
            Log.d("Circle1, c2", Float.toString(circles.get(i).x) + ", " + Float.toString(circles.get(i).x));
            logNums = false;
        }
        //Log.d("Circles", Float.toString(circles.get(i).x));
        }
    }

    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};
    }

}

现在已经两天了,我无法以我的生命找出什么是错误的,以及如何解决这个问题。我真的需要想办法解决这个问题。任何帮助都将不胜感激。如果您需要查看更多代码,请告诉我。

根据上一次讨论-您是否处理透视矩阵?我建议首先从正交投影开始,让代码正常工作,然后转到透视投影。您可以使用以下选项作为正交投影的起点

mat4.正交-1.0,1.0,-1,1.0,-10.0,100.0,投影矩阵

还要删除代码中的所有z翻译,使代码在xy上工作,然后添加z翻译


PS:继续使用与相同的线程不是更好吗?

我不知道该怎么办。我可以删除这个并返回到那个。实际上我甚至不需要使用透视投影。我不知道我为什么要从它开始。我呆在2d,所以我应该从一开始就用ortho。我可能只是用正交投影重新写一遍,然后看看问题是否仍然存在。我仍然可以使用正交投影进行翻译吗?我只知道如何使用投影矩阵进行转换。请尝试将MatrixHelper.perspectiveM调用替换为MatrixHelper.orthoMprojectionMatrix,0,-1.0,1.0,-1,1.0,-10.0,100.0