shaotine_wu 2015-08-15 02:14 采纳率: 16.7%
浏览 2554
已结题

android 使用 opengl 2.0 显示一张静态背景图片,效果显示出来的很奇怪

背景原图是:
图片说明

显示的效果却是:
图片说明

我不知道我的代码错在哪了。请有OpenGL开发经验的兄弟帮忙下。我的代码如下:

 public class BackgroundGLRnder implements GLSurfaceView.Renderer
{

    // Our matrices
    private final float[] mtrxProjection = new float[16];
    private final float[] mtrxView = new float[16];
    private final float[] mtrxProjectionAndView = new float[16];

    // Geometric variables
    private  float vertices[];
    private static float  uvs[] = new float[] {
            0.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 1.0f,
            1.0f, 0.0f
    };
    private static short indices[] = new short[]{ 0,  1,  2,  0,  2,  3};


    private FloatBuffer vertexBuffer;
    private ShortBuffer drawListBuffer;
    private FloatBuffer uvBuffer;

    // Our screenresolution
    private float   mScreenWidth = 0;
    private float   mScreenHeight = 0;

    // Misc
    private Context mContext;
    private int mProgramHandle;
    private int mTextureDataHandle0;

    /** This will be used to pass in the transformation matrix. */
    private int mMVPMatrixHandle;

    /** This will be used to pass in the texture. */
    private int mTextureUniformHandle0;

    /** This will be used to pass in model position information. */
    private int mPositionHandle;

    /** This will be used to pass in model color information. */
    // private int mColorHandle;

    /** This will be used to pass in model texture coordinate information. */
    private int mTextureCoordinateHandle;

    public BackgroundGLRnder(Context c)
    {
        mContext = c;
    }

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


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

        final String vertexShader = getVertexShader();
        final String fragmentShader = getFragmentShader();

        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_TexCoordinate" });

        // Set our per-vertex lighting program.
        GLES20.glUseProgram(mProgramHandle);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
        mScreenWidth = width;
        mScreenHeight = height;

        // Redo the Viewport, making it fullscreen.
        GLES20.glViewport(0, 0, (int) mScreenWidth, (int) mScreenHeight);

        // Clear our matrices
        for(int i=0;i<16;i++)
        {
            mtrxProjection[i] = 0.0f;
            mtrxView[i] = 0.0f;
            mtrxProjectionAndView[i] = 0.0f;
        }

        // Setup our screen width and height for normal sprite translation.
        Matrix.orthoM(mtrxProjection, 0, 0f, mScreenWidth, 0.0f, mScreenHeight, -1, 1);

    }

    @Override
    public void onDrawFrame(GL10 gl)
    {

        // Set program handles for cube drawing.
        mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle,
                "u_MVPMatrix");
        mTextureUniformHandle0 = GLES20.glGetUniformLocation(mProgramHandle,
                "u_Texture");
        mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
        mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle,
                "a_TexCoordinate");

        // clear Screen and Depth Buffer,
        // we have set the clear color as black.
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

        GLES20.glEnableVertexAttribArray(mPositionHandle);
        GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);

        GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
        GLES20.glVertexAttribPointer(mTextureCoordinateHandle, 3, GLES20.GL_FLOAT, false, 0,
                uvBuffer);


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

        /**
         * First texture map
         */
        // Set the active texture0 unit to texture unit 0.
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

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


        // Set the sampler texture unit to 0, where we have saved the texture.
        GLES20.glUniform1i(mTextureUniformHandle0, 0);

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

        // Disable vertex array
        GLES20.glDisableVertexAttribArray(mPositionHandle);
        GLES20.glDisableVertexAttribArray(mTextureCoordinateHandle);
    }

    private String getVertexShader() {
        return RawResourceReader.readTextFileFromRawResource(mContext, R.raw
                ._vertex_shader);
    }

    private String getFragmentShader() {
        return RawResourceReader.readTextFileFromRawResource(mContext, R.raw._fragment_shader);
    }

    private void constructVertex(){
        float with = getScreenWidth();
        float heigh = getScreenHeight();

        vertices = new float[]{
           0.0f,0.0f,-1.0f,
           with,0.0f,-1.0f,
           with,heigh,-1.0f,
           0.0f,heigh,-1.0f
        };
    }


    public void SetupImage()
    {

        constructVertex();
        // The vertex buffer.
        ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);

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

        // The texture buffer
        ByteBuffer uvsbb = ByteBuffer.allocateDirect(uvs.length * 4);
        uvsbb.order(ByteOrder.nativeOrder());
        uvBuffer = uvsbb.asFloatBuffer();
        uvBuffer.put(uvs);
        uvBuffer.position(0);

        // Load the texture
        mTextureDataHandle0 = TextureHelper.loadTexture(mContext, R.raw.bg_fine_day);
//        mTextureDataHandle0 = TextureHelper.loadTexture(mContext.getResources().openRawResource
//                (R.raw.bg_fine_day));x
    }




    private  int getScreenWidth() {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        @SuppressWarnings("deprecation")
        int width = wm.getDefaultDisplay().getWidth();// 屏幕宽度
        return width;
    }

    private  int getScreenHeight() {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        @SuppressWarnings("deprecation")
        int height = wm.getDefaultDisplay().getHeight();// 屏幕高度
        return height;
    }

展开全部

  • 写回答

0条回答 默认 最新

      编辑
      预览

      报告相同问题?

      悬赏问题

      • ¥15 编写Linux测试服务启动失败
      • ¥20 matlab绘冲床平面连杆机构图和仿真,求帮助
      • ¥15 为什么树莓派5b显示禁止连接
      • ¥15 请专家处理报错!基于深度学习的车型分类问题:数据集为包含 10 种车型的图像数据集,分为训练集、验证集和测试集。
      • ¥20 流量太费!寻找便宜的app音视频SDK或平替方案。
      • ¥15 kubeasz部署遇到问题
      • ¥15 GUIDE to App Designer Migration Tool for MATLAB
      • ¥50 第三代非支配排序遗传算法(NSGA-Ⅲ)和多目标粒子群优化算法(MOPSO)的实现
      • ¥20 plant simulation与python com接口实时数据交互
      • ¥15 有关汽车的MC9S12XS128单片机实验
      手机看
      程序员都在用的中文IT技术交流社区

      程序员都在用的中文IT技术交流社区

      专业的中文 IT 技术社区,与千万技术人共成长

      专业的中文 IT 技术社区,与千万技术人共成长

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      客服 返回
      顶部