Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OpenGL正在尝试对多个不同的位图进行纹理处理_Android_Opengl Es_Bitmap_Textures - Fatal编程技术网

Android OpenGL正在尝试对多个不同的位图进行纹理处理

Android OpenGL正在尝试对多个不同的位图进行纹理处理,android,opengl-es,bitmap,textures,Android,Opengl Es,Bitmap,Textures,我有一些小立方体,它们组成了一个网格来制作3D立方体。在每个小立方体上,我使用位图对表面进行纹理处理,但我想使用多张图片。我可以在loadTextures中构建更多纹理,并将它们添加到final int[]textureHandle=new int[1]并返回它们。如何将它们实例化到我正在绘制的每个小立方体中 @Override public void onSurfaceCreated(GL10 glUnused, EGLConfig config) { mLastRequestedCub

我有一些小立方体,它们组成了一个网格来制作3D立方体。在每个小立方体上,我使用位图对表面进行纹理处理,但我想使用多张图片。我可以在loadTextures中构建更多纹理,并将它们添加到
final int[]textureHandle=new int[1]并返回它们。如何将它们实例化到我正在绘制的每个小立方体中

@Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
{


mLastRequestedCubeFactor = mActualCubeFactor = 3;
generateCubes(mActualCubeFactor, false, false);         

// Set the background clear color to black.
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

// Use culling to remove back faces.
GLES20.glEnable(GLES20.GL_CULL_FACE);

// Enable depth testing
GLES20.glEnable(GLES20.GL_DEPTH_TEST);                      

// Position the eye in front of the origin.
final float eyeX = 0.0f;
final float eyeY = 0.0f;
final float eyeZ = -0.5f;

// We are looking toward the distance
final float lookX = 0.0f;
final float lookY = 0.0f;
final float lookZ = -5.0f;

// Set our up vector. This is where our head would be pointing were we holding the camera.
final float upX = 0.0f;
final float upY = 1.0f;
final float upZ = 0.0f;

// Set the view matrix. This matrix can be said to represent the camera position.
// NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and
// view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose.
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);        

final String vertexShader = RawResourceReader.readTextFileFromRawResource(mLessonSevenActivity, R.raw.lesson_seven_vertex_shader);          
final String fragmentShader = RawResourceReader.readTextFileFromRawResource(mLessonSevenActivity, R.raw.lesson_seven_fragment_shader);

final int vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);       
final int fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);     

mProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle, 
        new String[] {"a_Position",  "a_Normal", "a_TexCoordinate"});                   

// Load the texture
mAndroidDataHandle = TextureHelper.loadTexture(mLessonSevenActivity, R.drawable.usb_android);       
GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);          

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mAndroidDataHandle);     
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);       

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mAndroidDataHandle);     
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);     

// Initialize the accumulated rotation matrix
Matrix.setIdentityM(mAccumulatedRotation, 0);        
}

public class TextureHelper
{
    public static int loadTexture(final Context context, final int resourceId)
    {
        final int[] textureHandle = new int[1];

        GLES20.glGenTextures(1, textureHandle, 0);

        if (textureHandle[0] != 0)
        {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inScaled = false;   // No pre-scaling

            // Read in the resource
            final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

            // Bind to the texture in OpenGL
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

            // Set filtering
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

            // Load the bitmap into the bound texture.
            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

            // Recycle the bitmap, since its data has been loaded into OpenGL.
            bitmap.recycle();                       
        }

        if (textureHandle[0] == 0)
        {
            throw new RuntimeException("Error loading texture.");
        }

        return textureHandle[0];
    }
}
如何将它们实例化到我正在绘制的每个小立方体中

简而言之,你没有。 即使在作为核心功能的桌面GL中,如果不将绘图拆分为多个绘图调用,也无法更改纹理绑定

可以使用纹理图集、阵列纹理或几何体着色器从不同(已绑定的)纹理或单个纹理的不同部分采样。或者,可以使用无绑定纹理。我提到的每一件事都需要一个比上一个更新的GL版本

在ES中执行此操作的唯一方法是使用多个绘制调用,或者使用纹理图集/将纹理绑定到多个纹理单元。但是,由于实例化不是一个核心功能,因此动态计算纹理坐标/单位是一个巨大的难题,需要复制顶点数据


底线是,你所说的实例化立方体到底是什么意思?您是试图在一个操作中绘制500个立方体,还是通过调用多维数据集类中的某个方法来分别绘制它们?根据上下文的不同,实例化有不同的含义。

您是否有我可以参考的资源或代码来实现这一点?您能否首先更好地解释一下实例化的含义?在我的回答中,我包含了一个指向GL实例化定义的链接,如果这不是您所说的,那么您可以在每次绘制时轻松地绑定不同的纹理。MandrodataHandle是我传递到glBindTexture的指针或ID。那么,我如何为不同的立方体制作不同的纹理呢?是的,我如何为每个绘制的立方体绑定不同的纹理呢?我是从这个项目中绘制的。我已经专门修改了这个案例。