PPaste!

Ugly OpenGl ES

Home - All the pastes - Authored by Thooms

Raw version

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
private final String vertexShaderCode =
                    "uniform mat4 uMVPMatrix;" +
                    "uniform mat4 uMVMatrix;" +
                    "uniform vec3 vLightPos;" +
                    "attribute vec4 vPosition;" +
                    "attribute vec4 vColor;" +
                    "attribute vec3 vNormal;" +
                    "varying vec4 finalColor;"
                    + "void main()                    \n"     // The entry point for our vertex shader.
                    + "{                              \n"
                    // Transform the vertex into eye space.
                    + "   vec3 modelViewVertex = vec3(uMVPMatrix * vPosition);              \n"
                    // Transform the normal's orientation into eye space.
                    + "   vec3 modelViewNormal = vec3(uMVMatrix * vec4(vNormal, 0.0));     \n"
                    // Will be used for attenuation.
                    + "   float distance = length(vLightPos - modelViewVertex);             \n"
                    // Get a lighting direction vector from the light to the vertex.
                    + "   vec3 lightVector = normalize(vLightPos - modelViewVertex);        \n"
                    // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are
                    // pointing in the same direction then it will get max illumination.
                    + "   float diffuse = max(dot(modelViewNormal, lightVector), 0.1);       \n"
                    // Attenuate the light based on distance.
                    + "   diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));  \n"
                    // Multiply the color by the illumination level. It will be interpolated across the triangle.
                    + "   finalColor = vColor * diffuse;                                       \n"
                    // gl_Position is a special variable used to store the final position.
                    // Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
                    + "   gl_Position = uMVPMatrix * vPosition;                            \n"
                    + "}";

    private final String fragmentShaderCode =
            "precision mediump float;" +
                    "varying vec4 finalColor;" +
                    "void main() {" +
                    "  gl_FragColor = finalColor;" +
                    "}";

    // Define a simple shader program for our point.
    final String pointVertexShaderCode =
            "uniform mat4 vMVPMatrix;      \n"
                    + "attribute vec4 vPosition;     \n"
                    + "void main()                    \n"
                    + "{                              \n"
                    + "   gl_Position = vMVPMatrix   \n"
                    + "               * vPosition;   \n"
                    + "   gl_PointSize = 5.0;         \n"
                    + "}                              \n";

    final String pointFragmentShaderCode =
            "precision mediump float;       \n"
                    + "void main()                    \n"
                    + "{                              \n"
                    + "   gl_FragColor = vec4(1.0,    \n"
                    + "   1.0, 1.0, 1.0);             \n"
                    + "}                              \n";

    private final int mProgram;

    private int mPositionHandle;
    private int mColorHandle;
    // Use to access and set the view transformation
    private int mMVPMatrixHandle;


    private final int vertexCount = drawOrder.length;
    private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex


    public Cube() {
        // initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(
                // (# of coordinate values * 4 bytes per float)
                squareCoords.length * 4);
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(squareCoords);
        vertexBuffer.position(0);

        // initialize byte buffer for the draw list
        ByteBuffer dlb = ByteBuffer.allocateDirect(
                // (# of coordinate values * 2 bytes per short)
                drawOrder.length * 2);
        dlb.order(ByteOrder.nativeOrder());
        drawListBuffer = dlb.asShortBuffer();
        drawListBuffer.put(drawOrder);
        drawListBuffer.position(0);

        int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
                vertexShaderCode);
        int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
                fragmentShaderCode);
        int pointVertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, pointVertexShaderCode);
        int pointFragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, pointFragmentShaderCode);

        // create empty OpenGL ES Program
        mProgram = GLES20.glCreateProgram();

        // add the vertex shader to program
        GLES20.glAttachShader(mProgram, vertexShader);

        // add the fragment shader to program
        GLES20.glAttachShader(mProgram, fragmentShader);

        GLES20.glAttachShader(mProgram, pointVertexShader);
        GLES20.glAttachShader(mProgram, pointFragmentShader);


        GLES20.glBindAttribLocation(mProgram, 0, "vPosition");
        GLES20.glBindAttribLocation(mProgram, 0, "finalColor");

        // creates OpenGL ES program executables
        GLES20.glLinkProgram(mProgram);
    }