glCreateShader使用opengl es 2.0在Android studio中返回0

时间:2017-12-14 14:40:03

标签: opengl-es-2.0

我跟随" OpenGL ES 2 for Android_快速入门指南"并使用opengl创建了构建简单矩形表所需的一切。但是,当我运行应用程序时,我得到glCreateShader返回0.还有一个其他错误被记录,这是程序验证返回0但这取决于有一个工作着色器对象。 background / surfaceview似乎工作正常,因为当我更改glClear()的值时它会改变颜色;

这是我的代码:

openGL_hockeyMain

package earthhero.com.openglfirst;


import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class OpenGL_hockeyMain extends AppCompatActivity {
    private GLSurfaceView glSurfaceView;
    private boolean rendererSet = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        glSurfaceView = new GLSurfaceView(this);
        setContentView(glSurfaceView);

        final ActivityManager activityManager =
                (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();

        final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;

        if (supportsEs2) {
            // Request an OpenGL ES 2.0 compatible context. glSurfaceView.setEGLContextClientVersion(2);
            // Assign our renderer.
            glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
            glSurfaceView.setRenderer(new openGL_hockeyRenderer(this));
            rendererSet = true;
        }
        else {
            Toast.makeText(this, "This device does not support OpenGL ES 2.0.", Toast.LENGTH_LONG).show();
            return;
            }
    }

    @Override
    protected void onPause(){
        super.onPause();
        if (rendererSet){
            glSurfaceView.onPause();
        }
    };
    @Override
    protected void onResume(){
        super.onResume();
        if (rendererSet){
            glSurfaceView.onResume();
        }
    };

}

openGL_hockeyRenderer

package earthhero.com.openglfirst;

import android.content.Context;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static android.opengl.GLES20.GL_FLOAT;
import static android.opengl.GLES20.GL_LINES;
import static android.opengl.GLES20.GL_POINTS;
import static android.opengl.GLES20.GL_TRIANGLES;
import static android.opengl.GLES20.glClearColor;

import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glDrawArrays;
import static android.opengl.GLES20.glEnableVertexAttribArray;
import static android.opengl.GLES20.glGetAttribLocation;
import static android.opengl.GLES20.glGetUniformLocation;
import static android.opengl.GLES20.glUniform4f;
import static android.opengl.GLES20.glUseProgram;
import static android.opengl.GLES20.glVertexAttribPointer;
import static android.opengl.GLES20.glViewport;
import android.opengl.GLSurfaceView.Renderer;


import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;


/**
 * Created by Admin on 12/2/17.
 */

public class openGL_hockeyRenderer implements Renderer {
    private static final int BYTES_PER_FLOAT = 4;
    private final FloatBuffer vertexData;
    private final Context context;
    private int program;
    private static final String U_COLOR = "u_Color";
    private int uColorLocation;
    private static final String A_POSITION = "a_Position";
    private int aPositionLocation;
    private static final int POSITION_COMPONENT_COUNT=2;

        public openGL_hockeyRenderer(Context context){
            this.context = context;
            //table
            float[] tableVertices_Triangles = {
                    // Triangle 1
                    0f,  0f,
                    9f, 14f,
                    0f, 14f,
                    // Triangle 2
                    0f,  0f,
                    9f,  0f,
                    9f, 14f,
                    // Line 1
                    0f,  7f,
                    9f,  7f,
                    // Mallets
                    4.5f,  2f,
                    4.5f, 12f





//                    // Triangle 1
//                    -0.5f, -0.5f,
//                    0.5f,  0.5f,
//                    -0.5f,  0.5f,
//
//                    // Triangle 2
//                    -0.5f, -0.5f,
//                    0.5f, -0.5f,
//                    0.5f,  0.5f,
//
//                    // Line 1
//                    -0.5f, 0f,
//                    0.5f, 0f,
//
//                    // Mallets
//                    0f, -0.25f,
//                    0f, 0.25f

            };
            //ByteBuffer creates the buffer(memory) that will be used to store our vertices
            //Each Float will be 4 bytes in length because the Dalvik works with 32 bit(4 byte) floats.
            //Create a large enough buffer by calling tableVertices_Triangles.length * BYTES_PER_FLOAT
            //order those bits using the native order(little Endian or Big Endian)
            //Finally, to not deal with the bits directly we call asFloatBuffer to deal with just the floats rather than the bits
            // it represents.
            //Finally, into our buffer we enter our information.
            vertexData = ByteBuffer.allocateDirect(tableVertices_Triangles.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
            vertexData.put(tableVertices_Triangles);
        }

        @Override
        public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
            glClearColor(0f, 0f, 0f, 0f);

            String vertexShaderSource = TextResourceReader.readTextFileFromResource(context, R.raw.simple_vertex_shader);
            String fragmentShaderSource = TextResourceReader.readTextFileFromResource(context, R.raw.simple_fragment_shader);

            int vertexShader = ShaderHelper.compileVertexShader(vertexShaderSource);
            int fragmentShader = ShaderHelper.compileFragmentShader(fragmentShaderSource);

            program = ShaderHelper.linkProgram(vertexShader, fragmentShader);

            if (LoggerConfig.ON) { ShaderHelper.validateProgram(program);
            }

            glUseProgram(program);

            uColorLocation = glGetUniformLocation(program, U_COLOR);

            aPositionLocation = glGetAttribLocation(program, A_POSITION);

            vertexData.position(0);
            glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GL_FLOAT,
                    false, 0, vertexData);
            glEnableVertexAttribArray(aPositionLocation);
        }

            @Override
        public void onSurfaceChanged(GL10 glUnused, int width, int height) {
            // Set the OpenGL viewport to fill the entire surface.
            glViewport(0, 0, width, height);
        }
        @Override
        public void onDrawFrame(GL10 glUnused) {
            // Clear the rendering surface.
            glClear(GL_COLOR_BUFFER_BIT);

            //Table
            glUniform4f(uColorLocation, 1.0f, 1.0f, 1.0f, 1.0f);
            glDrawArrays(GL_TRIANGLES, 0, 6);

            //Dividing Line
            glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);
            glDrawArrays(GL_LINES, 6, 2);

            //Mallet 1
            glUniform4f(uColorLocation, 0.0f, 0.0f, 1.0f, 1.0f);
            glDrawArrays(GL_POINTS, 8, 1);

            //Mallet 2
            glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);
            glDrawArrays(GL_POINTS, 9, 1);
        }

}

ShaderHelper

package earthhero.com.openglfirst;

import android.util.Log;

import static android.opengl.GLES20.GL_COMPILE_STATUS;
import static android.opengl.GLES20.GL_FRAGMENT_SHADER;
import static android.opengl.GLES20.GL_INVALID_OPERATION;
import static android.opengl.GLES20.GL_INVALID_VALUE;
import static android.opengl.GLES20.GL_LINK_STATUS;
import static android.opengl.GLES20.GL_VALIDATE_STATUS;
import static android.opengl.GLES20.GL_VERTEX_SHADER;
import static android.opengl.GLES20.glAttachShader;
import static android.opengl.GLES20.glCompileShader;
import static android.opengl.GLES20.glCreateProgram;
import static android.opengl.GLES20.glCreateShader;
import static android.opengl.GLES20.glGetError;
import static android.opengl.GLES20.glGetProgramInfoLog;
import static android.opengl.GLES20.glGetProgramiv;
import static android.opengl.GLES20.glGetShaderInfoLog;
import static android.opengl.GLES20.glGetShaderiv;
import static android.opengl.GLES20.glLinkProgram;
import static android.opengl.GLES20.glShaderSource;
import static android.opengl.GLES20.glValidateProgram;


/**
 * Created by Admin on 12/2/17.
 */

public class ShaderHelper {

    private static final String TAG = "ShaderHelper";

    public static int compileVertexShader(String shaderCode) {
        return compileShader(GL_VERTEX_SHADER, shaderCode);
    }

    public static int compileFragmentShader(String shaderCode) {
        return compileShader(GL_FRAGMENT_SHADER, shaderCode);
    }

    private static int compileShader(int type, String shaderCode) {
        //glCreateShader creates an empty shader object and returns a non-zero reference value.
        final int shaderObjectId = glCreateShader(type);

        //If it is a zero something went wrong.
        if (shaderObjectId == 0) {
            if (LoggerConfig.ON) {
                Log.w(TAG, "Could not create new shader");
            }
            return 0;
        }
        //connect the shader source code with the shader object
        glShaderSource(shaderObjectId, shaderCode);

        int err = glGetError();


        if (LoggerConfig.ON && (err == GL_INVALID_VALUE || err == GL_INVALID_OPERATION)){
            Log.v(TAG, "Couldn't attach source to shader." +  "\n" + glGetError());
        }

        glCompileShader(shaderObjectId);


        final int[] compileStatus = new int[1];
        glGetShaderiv(shaderObjectId, GL_COMPILE_STATUS, compileStatus, 0);

        if (LoggerConfig.ON) {
            // Print the shader info log to the Android log output.
            Log.v(TAG, "Results of compiling source:" + "\n" + shaderCode + "\n:"
                    + glGetShaderInfoLog(shaderObjectId));
        }

        if (compileStatus[0] == 0) {
            // If it failed, delete the shader object. glDeleteShader(shaderObjectId);
            if (LoggerConfig.ON) {
                Log.w(TAG, "Compilation of shader failed.");
            }
            return 0;
        }

        return shaderObjectId;
    }

    public static int linkProgram(int vertexShaderId, int fragmentShaderId) {
        final int programObjectId = glCreateProgram();
        if (programObjectId == 0) { if (LoggerConfig.ON) {
            Log.w(TAG, "Could not create new program"); }
            return 0; }
        glAttachShader(programObjectId, vertexShaderId);
        glAttachShader(programObjectId, fragmentShaderId);

        glLinkProgram(programObjectId);

        final int[] linkStatus = new int[1]; glGetProgramiv(programObjectId, GL_LINK_STATUS, linkStatus, 0);
        if (LoggerConfig.ON) {
            // Print the program info log to the Android log output.
            Log.v(TAG, "Results of linking program:\n"
            + glGetProgramInfoLog(programObjectId));
        }

        if (linkStatus[0] == 0) {
        // If it failed, delete the program object. glDeleteProgram(programObjectId);
            if (LoggerConfig.ON) {
                Log.w(TAG, "Linking of program failed."); }
            return 0;
        }
        return programObjectId;

    }

    public static boolean validateProgram(int programObjectId) {
        glValidateProgram(programObjectId);
        final int[] validateStatus = new int[1];
        glGetProgramiv(programObjectId, GL_VALIDATE_STATUS, validateStatus, 0);
        Log.v(TAG, "Results of validating program: " + validateStatus[0]
                + "\nLog:" + glGetProgramInfoLog(programObjectId));
        return validateStatus[0] != 0;
    }


}

1 个答案:

答案 0 :(得分:1)

请求OpenGLES2渲染器的行最终作为评论的一部分。

// Request an OpenGL ES 2.0 compatible context. glSurfaceView.setEGLContextClientVersion(2);

应该是

 // Request an OpenGL ES 2.0 compatible context.
 glSurfaceView.setEGLContextClientVersion(2);