使用OpenGL ES 2.0绘制2D图像。

时间:2022-09-10 16:36:22

I've been struggling to draw a 2D image from jpg/png files using openGL ES 2.0 for Android. Everywhere I look the tutorials are for texturing 3D images so its been rough figuring out how to draw a regular 2D Sprite. I got a square to draw and rotate but once it came to texturing I must have messed up somewhere because I keep getting an error saying DrawElements isn't bound to any data but if I comment out any code to do with texturing it works fine.

我一直在努力从jpg/png文件中使用openGL ES 2.0为Android绘制2D图像。在我看来,所有的教程都是为了给3D图像贴图,所以它一直在琢磨如何绘制一个普通的2D精灵。我有一个正方形来画和旋转,但是一旦它变成了纹理,我一定会把它弄乱,因为我一直有一个错误,说DrawElements没有绑定到任何数据,但是如果我注释掉任何与纹理贴图相关的代码就可以了。

Any help would be greatly appreciated.

非常感谢您的帮助。

Here is my Code for my Sprite class and Renderer Class:

下面是我的Sprite类和Renderer类的代码:

public class Sprite
{
//Reference to Activity Context
private final Context mActivityContext;

//Added for Textures
private final FloatBuffer mCubeTextureCoordinates;
private int mTextureUniformHandle;
private int mTextureCoordinateHandle;
private final int mTextureCoordinateDataSize = 2;
private int mTextureDataHandle;

private final String vertexShaderCode =
//Test
"attribute vec2 a_TexCoordinate;" +
"varying vec2 v_TexCoordinate;" +
//End Test
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main() {" +
"  gl_Position = vPosition * uMVPMatrix;" +
    //Test
    "v_TexCoordinate = a_TexCoordinate" +
    //End Test
"}";

private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
//Test
"uniform sampler2D u_Texture;" +
"varying vec2 v_TexCoordinate;" +
//End Test
"void main() {" +
//"gl_FragColor = vColor;" +
"gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));" +
"}";

private final int shaderProgram;    
private final FloatBuffer vertexBuffer;
private final ShortBuffer drawListBuffer;
private int mPositionHandle;
private int mColorHandle;
private int mMVPMatrixHandle;

// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 2;
static float spriteCoords[] = { -0.5f,  0.5f,   // top left
                                -0.5f, -0.5f,   // bottom left
                                 0.5f, -0.5f,   // bottom right
                                 0.5f,  0.5f }; //top right

private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; //Order to draw vertices
private final int vertexStride = COORDS_PER_VERTEX * 4; //Bytes per vertex

// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

public Sprite(final Context activityContext)
{
    mActivityContext = activityContext;

    //Initialize Vertex Byte Buffer for Shape Coordinates / # of coordinate values * 4 bytes per float
    ByteBuffer bb = ByteBuffer.allocateDirect(spriteCoords.length * 4); 
    //Use the Device'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(spriteCoords);
    //Set the Buffer to Read the first coordinate
    vertexBuffer.position(0);

    // S, T (or X, Y)
    // Texture coordinate data.
    // Because images have a Y axis pointing downward (values increase as you move down the image) while
    // OpenGL has a Y axis pointing upward, we adjust for that here by flipping the Y axis.
    // What's more is that the texture coordinates are the same for every face.
    final float[] cubeTextureCoordinateData =
    {                                               
            //Front face
            /*0.0f, 0.0f,               
            0.0f, 1.0f,
            1.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 1.0f,
            1.0f, 0.0f*/

            -0.5f,  0.5f,
            -0.5f, -0.5f,
             0.5f, -0.5f,
             0.5f,  0.5f
    };

    mCubeTextureCoordinates = ByteBuffer.allocateDirect(cubeTextureCoordinateData.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    mCubeTextureCoordinates.put(cubeTextureCoordinateData).position(0);

    //Initialize byte buffer for the draw list
    ByteBuffer dlb = ByteBuffer.allocateDirect(spriteCoords.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    int vertexShader = MyGL20Renderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = MyGL20Renderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    shaderProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(shaderProgram, vertexShader);
    GLES20.glAttachShader(shaderProgram, fragmentShader);

    //Texture Code
    GLES20.glBindAttribLocation(shaderProgram, 0, "a_TexCoordinate");

    GLES20.glLinkProgram(shaderProgram);

    //Load the texture
    mTextureDataHandle = loadTexture(mActivityContext, R.drawable.brick);
}

public void Draw(float[] mvpMatrix)
{
    //Add program to OpenGL ES Environment
    GLES20.glUseProgram(shaderProgram);

    //Get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetAttribLocation(shaderProgram, "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(shaderProgram, "vColor");

    //Set the Color for drawing the triangle
    GLES20.glUniform4fv(mColorHandle, 1, color, 0);

    //Set Texture Handles and bind Texture
    mTextureUniformHandle = GLES20.glGetAttribLocation(shaderProgram, "u_Texture");
    mTextureCoordinateHandle = GLES20.glGetAttribLocation(shaderProgram, "a_TexCoordinate");

    //Set the active texture unit to texture unit 0.
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

    //Bind the texture to this unit.
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);

    //Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
    GLES20.glUniform1i(mTextureUniformHandle, 0); 

    //Pass in the texture coordinate information
    mCubeTextureCoordinates.position(0);
    GLES20.glVertexAttribPointer(mTextureCoordinateHandle, mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0, mCubeTextureCoordinates);
    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);

    //Get Handle to Shape's Transformation Matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(shaderProgram, "uMVPMatrix");

    //Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

    //Draw the triangle
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    //Disable Vertex Array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
}

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

My Renderer Class:

我的渲染器类:

public class MyGL20Renderer implements GLSurfaceView.Renderer
{
private final Context mActivityContext;

//Matrix Initializations
private final float[] mMVPMatrix = new float[16];
private final float[] mProjMatrix = new float[16];
private final float[] mVMatrix = new float[16];
private float[] mRotationMatrix = new float[16];

//Declare as volatile because we are updating it from another thread
public volatile float mAngle;

//private Triangle triangle;
private Sprite sprite;

public MyGL20Renderer(final Context activityContext)
{
    mActivityContext = activityContext;
}

public void onSurfaceCreated(GL10 unused, EGLConfig config)
{
    //Set the background frame color
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    //Initialize Shapes
    //triangle = new Triangle();
    sprite = new Sprite(mActivityContext);
}

public void onDrawFrame(GL10 unused)
{
    //Redraw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    //Set the camera position (View Matrix)
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    //Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

    //Create a rotation transformation for the triangle
    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);

    //Combine the rotation matrix with the projection and camera view
    Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);

    //Draw Shape
    //triangle.Draw(mMVPMatrix);
    sprite.Draw(mMVPMatrix);
}

public void onSurfaceChanged(GL10 unused, 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(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}

public static int loadShader(int type, String shaderCode)
{
    //Create a Vertex Shader Type Or a Fragment Shader Type (GLES20.GL_VERTEX_SHADER OR GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type);

    //Add The Source Code and Compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    return shader;
}
}

5 个解决方案

#1


20  

"v_TexCoordinate = a_TexCoordinate" +

“v_TexCoordinate = a_TexCoordinate”+

should have been

应该是

"v_TexCoordinate = a_TexCoordinate;" +

“v_TexCoordinate = a_TexCoordinate;”+

Apparently I forgot a semi-colon, now I realize just how much I rely on my IDE's to tell me when I mess up stupid things haha.

显然我忘记了分号,现在我意识到我是多么依赖我的IDE告诉我什么时候我把愚蠢的事情搞砸了哈哈。

#2


9  

There is a mistake with variable vColor naming (or using) in fragmentShaderCode. Here your variable has name vColor:

在fragmentShaderCode中有一个变量vColor命名(或使用)的错误。这里你的变量的名字是vColor:

uniform vec4 vColor;

and in this line it has name v_Color

在这一行中,它的名字是v_Color。

gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));

#3


1  

Try with following Texture coordinates:

尝试以下纹理坐标:

final float[] cubeTextureCoordinateData = {
0.5,-0.5, 0.5,0.5, -0.5,0.5, -0.5,-0.5 };

最终浮点[]cubeTextureCoordinateData = {0.5,-0.5, 0.5,0.5, -0.5,0.5, -0.5,-0.5};

Its working. Thank you very much.

它的工作。非常感谢。

#4


1  

I would also change this in the shader

我也会在着色器中改变这个。

  gl_Position = vPosition * uMVPMatrix;

to this

这个

  gl_Position = uMVPMatrix * vPosition;

it will make a difference when trying to translate the position of the image.

当试图翻译图像的位置时,它会产生不同的效果。

#5


0  

The solution could be as simple as Enabling mTextureCoord... before assigning the VertexAttribPointer;

解决方案可以像启用mTextureCoord一样简单……在分配VertexAttribPointer之前;

#1


20  

"v_TexCoordinate = a_TexCoordinate" +

“v_TexCoordinate = a_TexCoordinate”+

should have been

应该是

"v_TexCoordinate = a_TexCoordinate;" +

“v_TexCoordinate = a_TexCoordinate;”+

Apparently I forgot a semi-colon, now I realize just how much I rely on my IDE's to tell me when I mess up stupid things haha.

显然我忘记了分号,现在我意识到我是多么依赖我的IDE告诉我什么时候我把愚蠢的事情搞砸了哈哈。

#2


9  

There is a mistake with variable vColor naming (or using) in fragmentShaderCode. Here your variable has name vColor:

在fragmentShaderCode中有一个变量vColor命名(或使用)的错误。这里你的变量的名字是vColor:

uniform vec4 vColor;

and in this line it has name v_Color

在这一行中,它的名字是v_Color。

gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));

#3


1  

Try with following Texture coordinates:

尝试以下纹理坐标:

final float[] cubeTextureCoordinateData = {
0.5,-0.5, 0.5,0.5, -0.5,0.5, -0.5,-0.5 };

最终浮点[]cubeTextureCoordinateData = {0.5,-0.5, 0.5,0.5, -0.5,0.5, -0.5,-0.5};

Its working. Thank you very much.

它的工作。非常感谢。

#4


1  

I would also change this in the shader

我也会在着色器中改变这个。

  gl_Position = vPosition * uMVPMatrix;

to this

这个

  gl_Position = uMVPMatrix * vPosition;

it will make a difference when trying to translate the position of the image.

当试图翻译图像的位置时,它会产生不同的效果。

#5


0  

The solution could be as simple as Enabling mTextureCoord... before assigning the VertexAttribPointer;

解决方案可以像启用mTextureCoord一样简单……在分配VertexAttribPointer之前;