/// <summary> /// Converts a raw text file, saved as a resource, into an OpenGL ES shader. /// </summary> /// <param name="tag"></param> /// <param name="context"></param> /// <param name="type">The type of shader we will be creating.</param> /// <param name="resId">The resource ID of the raw text file about to be turned into a shader.</param> /// <returns>The shader object handler.</returns> public static int LoadGLShader(string tag, Context context, int type, int resId) { string code = ReadRawTextFile(context, resId); int shader = GLES20.GlCreateShader(type); GLES20.GlShaderSource(shader, code); GLES20.GlCompileShader(shader); // Get the compilation status. int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus[0] == 0) { Log.Error(tag, "Error compiling shader: " + GLES20.GlGetShaderInfoLog(shader)); GLES20.GlDeleteShader(shader); shader = 0; } if (shader == 0) { throw new RuntimeException("Error creating shader."); } return(shader); }
public static int LoadShader(int shaderType, string source) { try { int shader = GLES20.GlCreateShader(shaderType); if (shader != 0) { GLES20.GlShaderSource(shader, source); GLES20.GlCompileShader(shader); int[] compiled = new int[1]; GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, compiled, 0); if (compiled[0] == 0) { string info = GLES20.GlGetShaderInfoLog(shader); GLES20.GlDeleteShader(shader); throw new RuntimeException("Could not compile shader " + shaderType + ":" + info); } } return(shader); } catch (Exception e) { Console.WriteLine(e); return(0); } }
public static int LoadGLShader(string tag, Context context, int type, int resId) { var code = ReadRawTextFile(context, resId); var shader = GLES20.GlCreateShader(type); GLES20.GlShaderSource(shader, code); GLES20.GlCompileShader(shader); var compileStatus = new int[1]; GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, compileStatus, 0); if (compileStatus[0] == 0) { GLES20.GlDeleteShader(shader); shader = 0; } if (shader == 0) { throw new Exception("Error creating shader"); } return(shader); }
/** * Converts a raw text file, saved as a resource, into an OpenGL ES shader. */ int loadGLShader(int type, int resId) { var code = ReadRawTextFile(resId); var shader = GLES20.GlCreateShader(type); GLES20.GlShaderSource(shader, code); GLES20.GlCompileShader(shader); // Get the compilation status. var compileStatus = new int [1]; GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus [0] == 0) { Android.Util.Log.Error(TAG, "Error compiling shader: " + GLES20.GlGetShaderInfoLog(shader)); GLES20.GlDeleteShader(shader); shader = 0; } if (shader == 0) { throw new Exception("Error creating shader."); } return(shader); }
public static int LoadShader(int type, string shaderCode) { // create a vertex shader type (GLES20.GL_VERTEX_SHADER) // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER) int shader = GLES20.GlCreateShader(type); // add the source code to the shader and compile it GLES20.GlShaderSource(shader, shaderCode); GLES20.GlCompileShader(shader); return(shader); }
// Compile & attach a |type| shader specified by |source| to |program|. private static void addShaderTo(int type, string source, int program) { int[] result = new int[] { GLES20.GlFalse }; int shader = GLES20.GlCreateShader(type); GLES20.GlShaderSource(shader, source); GLES20.GlCompileShader(shader); GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, result, 0); abortUnless(result[0] == GLES20.GlTrue, GLES20.GlGetShaderInfoLog(shader) + ", source: " + source); GLES20.GlAttachShader(program, shader); GLES20.GlDeleteShader(shader); checkNoGLES2Error(); }
public void compileShader() { int vShader = GLES20.GlCreateShader(GLES20.GlVertexShader); GLES20.GlShaderSource(vShader, shaderSource); GLES20.GlCompileShader(vShader); int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(vShader, GLES20.GlCompileStatus, compileStatus, 0); if (compileStatus[0] != GLES20.GlTrue) { result += "vShader: \n"; result += GLES20.GlGetShaderInfoLog(vShader) + "\n"; ShowMessage.ShowCrash(result); } // ---------------------------------------------------------------------- int fShader = GLES20.GlCreateShader(GLES20.GlFragmentShader); GLES20.GlShaderSource(fShader, fragmentSource); GLES20.GlCompileShader(fShader); GLES20.GlGetShaderiv(fShader, GLES20.GlCompileStatus, compileStatus, 0); if (compileStatus[0] != GLES20.GlTrue) { result += "fShader: \n"; result += GLES20.GlGetShaderInfoLog(fShader) + "\n"; ShowMessage.ShowCrash(result); } // ---------------------------------------------------------------------- program = GLES20.GlCreateProgram(); GLES20.GlAttachShader(program, vShader); GLES20.GlAttachShader(program, fShader); GLES20.GlLinkProgram(program); int[] linkStatus = new int[1]; GLES20.GlGetProgramiv(program, GLES20.GlLinkStatus, linkStatus, 0); if (linkStatus[0] != GLES20.GlTrue) { result += "Could not link program: \n"; result += GLES20.GlGetProgramInfoLog(program) + "\n"; ShowMessage.ShowCrash(result); } }
private int loadShader(int shaderType, String source) { int shader = GLES20.GlCreateShader(shaderType); checkGlError("glCreateShader type=" + shaderType); GLES20.GlShaderSource(shader, source); GLES20.GlCompileShader(shader); int[] compiled = new int[1]; GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, compiled, 0); if (compiled[0] == 0) { Log.Error(TAG, "Could not compile shader " + shaderType + ":"); Log.Error(TAG, " " + GLES20.GlGetShaderInfoLog(shader)); GLES20.GlDeleteShader(shader); shader = 0; } return shader; }
private int LoadShader(int shaderType, string source) { int shader = GLES20.GlCreateShader(shaderType); if (shader != 0) { GLES20.GlShaderSource(shader, source); GLES20.GlCompileShader(shader); int[] compiled = new int[1]; GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, compiled, 0); if (compiled[0] == 0) { GLES20.GlDeleteShader(shader); shader = 0; } } return(shader); }
/** * \brief Compile shader code. * @param shaderType type of shader (either GLES20.GL_VERTEX_SHADER or GLES20.GL_FRAGMENT_SHADER). * @param source source code of the shader. * @return gl shader id. */ private static int initShader(int shaderType, string source) { int shader = GLES20.GlCreateShader(shaderType); if (shader != 0) { GLES20.GlShaderSource(shader, source); GLES20.GlCompileShader(shader); int[] glStatusVar = { GLES20.GlFalse }; GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, glStatusVar, 0); if (glStatusVar[0] == GLES20.GlFalse) { //Log.e("RenderUtils", "initShader could NOT compile shader " + shaderType + " : " + GLES20.GlGetShaderInfoLog(shader)); GLES20.GlDeleteShader(shader); shader = 0; } } return(shader); }
/** * Helper method for compiling a shader. * * @param shaderType * Type of shader to compile * @param source * String presentation for shader * @return id for compiled shader */ private int LoadShader(int shaderType, string source) { var shader = GLES20.GlCreateShader(shaderType); if (shader != 0) { GLES20.GlShaderSource(shader, source); GLES20.GlCompileShader(shader); int[] compiled = new int[1]; GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, compiled, 0); if (compiled[0] == 0) { var error = GLES20.GlGetShaderInfoLog(shader); GLES20.GlDeleteShader(shader); throw new Exception(error); } } return(shader); }
private static int LoadShader(int shaderType, string source) { int shader = GLES20.GlCreateShader(shaderType); if (0 != shader) { GLES20.GlShaderSource(shader, source); GLES20.GlCompileShader(shader); int[] compiled = new int[1]; GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, compiled, 0); if (compiled[0] == 0) { Log.Debug(TAG, "glError: Could not compile shader " + shaderType); Log.Debug(TAG, "GLES20 Error: " + GLES20.GlGetShaderInfoLog(shader)); GLES20.GlDeleteShader(shader); shader = 0; } } return(shader); }
private int loadShader(int shaderType, String source) { int shader = GLES20.GlCreateShader(shaderType); if (shader != 0) { GLES20.GlShaderSource(shader, source); GLES20.GlCompileShader(shader); int[] compiled = new int[1]; GLES20.GlGetShaderiv(shader, 35713, compiled, 0); if (compiled[0] == 0) { Log.Error("DistortionRenderer", "Could not compile shader " + shaderType + ":"); Log.Error("DistortionRenderer", GLES20.GlGetShaderInfoLog(shader)); GLES20.GlDeleteShader(shader); shader = 0; } } return(shader); }
public static int LoadShader(int shaderType, string source) { int shader = GLES20.GlCreateShader(shaderType); if (shader != 0) { GLES20.GlShaderSource(shader, source); GLES20.GlCompileShader(shader); var compiled = new int[1]; GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, compiled, 0); if (compiled [0] == 0) { string info = GLES20.GlGetShaderInfoLog(shader); GLES20.GlDeleteShader(shader); throw new InvalidOperationException("Could not compile shader " + shaderType + ":" + info); } } return(shader); }
private int loadShader(int shaderType, String source) { int shader = GLES20.GlCreateShader(shaderType); if (shader != 0) { GLES20.GlShaderSource(shader, source); GLES20.GlCompileShader(shader); int[] compiled = new int[1]; GLES20.GlGetShaderiv(shader, GLES20.GlCompileStatus, compiled, 0); if (compiled[0] == 0) { Log.Error(TAG, "Could not compile shader " + shaderType + ":"); Log.Error(TAG, GLES20.GlGetShaderInfoLog(shader)); Log.Error(TAG, "message ends."); GLES20.GlDeleteShader(shader); shader = 0; } } return(shader); }
private static int loadShader(String vss, String fss) { int vshader = GLES20.GlCreateShader(GLES20.GlVertexShader); GLES20.GlShaderSource(vshader, vss); GLES20.GlCompileShader(vshader); int[] compiled = new int[1]; GLES20.GlGetShaderiv(vshader, GLES20.GlCompileStatus, compiled, 0); if (compiled[0] == 0) { //Log.e("Shader", "Could not compile vshader"); //Log.v("Shader", "Could not compile vshader:" + GLES20.glGetShaderInfoLog(vshader)); GLES20.GlDeleteShader(vshader); vshader = 0; } int fshader = GLES20.GlCreateShader(GLES20.GlFragmentShader); GLES20.GlShaderSource(fshader, fss); GLES20.GlCompileShader(fshader); GLES20.GlGetShaderiv(fshader, GLES20.GlCompileStatus, compiled, 0); if (compiled[0] == 0) { /*Log.e("Shader", "Could not compile fshader"); * Log.v("Shader", "Could not compile fshader:" + GLES20.glGetShaderInfoLog(fshader));*/ GLES20.GlDeleteShader(fshader); fshader = 0; } int program = GLES20.GlCreateProgram(); GLES20.GlAttachShader(program, vshader); GLES20.GlAttachShader(program, fshader); GLES20.GlLinkProgram(program); return(program); }
private int[] VBOBuffers = new int[2]; //2 buffers for vertices and colors public void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config) { const float edge = 1.0f; // X, Y, Z, float[] triangleVerticesData = { -1.5f, -0.25f, 0.0f, 0.5f, -0.25f, 0.0f, 0.0f, 0.559016994f, 0.0f }; FloatBuffer mTriangleVertices = ByteBuffer.AllocateDirect(triangleVerticesData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer(); mTriangleVertices.Put(triangleVerticesData).Flip(); // R, G, B, A float[] triangleColorsData = { 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f }; FloatBuffer mTriangleColors = ByteBuffer.AllocateDirect(triangleColorsData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer(); mTriangleColors.Put(triangleColorsData).Flip(); //Use VBO GLES20.GlGenBuffers(2, VBOBuffers, 0); //2 buffers for vertices and colors GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[0]); GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleVertices.Capacity() * mBytesPerFloat, mTriangleVertices, GLES20.GlStaticDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[1]); GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleColors.Capacity() * mBytesPerFloat, mTriangleColors, GLES20.GlStaticDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0); GLES20.GlClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Position the eye behind the origin. float eyeX = 0.0f; float eyeY = 0.0f; float eyeZ = 4.5f; // We are looking toward the distance float lookX = 0.0f; float lookY = 0.0f; float lookZ = -5.0f; // Set our up vector. This is where our head would be pointing were we holding the camera. float upX = 0.0f; float upY = 1.0f; 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); string vertexShader = "uniform mat4 u_MVPMatrix; \n" // A constant representing the combined model/view/projection matrix. + "attribute vec4 a_Position; \n" // Per-vertex position information we will pass in. + "attribute vec4 a_Color; \n" // Per-vertex color information we will pass in. + "varying vec4 v_Color; \n" // This will be passed into the fragment shader. + "void main() \n" // The entry point for our vertex shader. + "{ \n" + " v_Color = a_Color; \n" // Pass the color through to the fragment shader. It will be interpolated across the triangle. + " gl_Position = u_MVPMatrix \n" // gl_Position is a special variable used to store the final position. + " * a_Position; \n" // Multiply the vertex by the matrix to get the final point in normalized screen coordinates. + "} \n"; string fragmentShader = "precision mediump float; \n" // Set the default precision to medium. We don't need as high of a // precision in the fragment shader. + "varying vec4 v_Color; \n" // This is the color from the vertex shader interpolated across the triangle per fragment. + "void main() \n" // The entry point for our fragment shader. + "{ \n" + " gl_FragColor = v_Color; \n" // Pass the color directly through the pipeline. + "} \n"; int vertexShaderHandle = GLES20.GlCreateShader(GLES20.GlVertexShader); if (vertexShaderHandle != 0) { // Pass in the shader source. GLES20.GlShaderSource(vertexShaderHandle, vertexShader); // Compile the shader. GLES20.GlCompileShader(vertexShaderHandle); // Get the compilation status. int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(vertexShaderHandle, GLES20.GlCompileStatus, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus[0] == 0) { GLES20.GlDeleteShader(vertexShaderHandle); vertexShaderHandle = 0; } } if (vertexShaderHandle == 0) { throw new Exception("Error creating vertex shader."); } // Load in the fragment shader shader. int fragmentShaderHandle = GLES20.GlCreateShader(GLES20.GlFragmentShader); if (fragmentShaderHandle != 0) { // Pass in the shader source. GLES20.GlShaderSource(fragmentShaderHandle, fragmentShader); // Compile the shader. GLES20.GlCompileShader(fragmentShaderHandle); // Get the compilation status. int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(fragmentShaderHandle, GLES20.GlCompileStatus, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus[0] == 0) { GLES20.GlDeleteShader(fragmentShaderHandle); fragmentShaderHandle = 0; } } if (fragmentShaderHandle == 0) { throw new Exception("Error creating fragment shader."); } // Create a program object and store the handle to it. int programHandle = GLES20.GlCreateProgram(); if (programHandle != 0) { // Bind the vertex shader to the program. GLES20.GlAttachShader(programHandle, vertexShaderHandle); // Bind the fragment shader to the program. GLES20.GlAttachShader(programHandle, fragmentShaderHandle); // Bind attributes GLES20.GlBindAttribLocation(programHandle, 0, "a_Position"); GLES20.GlBindAttribLocation(programHandle, 1, "a_Color"); // Link the two shaders together into a program. GLES20.GlLinkProgram(programHandle); // Get the link status. int[] linkStatus = new int[1]; GLES20.GlGetProgramiv(programHandle, GLES20.GlLinkStatus, linkStatus, 0); // If the link failed, delete the program. if (linkStatus[0] == 0) { GLES20.GlDeleteProgram(programHandle); programHandle = 0; } } if (programHandle == 0) { throw new Exception("Error creating program."); } // Set program handles. These will later be used to pass in values to the program. mMVPMatrixHandle = GLES20.GlGetUniformLocation(programHandle, "u_MVPMatrix"); mPositionHandle = GLES20.GlGetAttribLocation(programHandle, "a_Position"); mColorHandle = GLES20.GlGetAttribLocation(programHandle, "a_Color"); // Tell OpenGL to use this program when rendering. GLES20.GlUseProgram(programHandle); }
public Shader(int type) { handle = GLES20.GlCreateShader(type); }
public void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config) { const float coord = 1.0f; ObjParser model3D = new ObjParser(); List <byte[]> test1 = model3D.ParsedObject(context, "buggy"); float[] vertexArray = new float[test1[0].Length / 4]; System.Buffer.BlockCopy(test1[0], 0, vertexArray, 0, (int)test1[0].Length); modelVerticesData = vertexArray; FloatBuffer mTriangleVertices = ByteBuffer.AllocateDirect(modelVerticesData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer(); mTriangleVertices.Put(modelVerticesData).Flip(); // Cube colors // R, G, B, A float[] modelColorsData = { 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f }; FloatBuffer mTriangleColors = ByteBuffer.AllocateDirect(modelColorsData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer(); mTriangleColors.Put(modelColorsData).Flip(); float[] textureUVMapArray = new float[test1[1].Length / 4]; System.Buffer.BlockCopy(test1[1], 0, textureUVMapArray, 0, (int)test1[1].Length); modelTextureUVMapData = textureUVMapArray; FloatBuffer mTriangleTextureUVMap = ByteBuffer.AllocateDirect(modelTextureUVMapData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer(); mTriangleTextureUVMap.Put(modelTextureUVMapData).Flip(); //Data buffers to VBO GLES20.GlGenBuffers(3, VBOBuffers, 0); //2 buffers for vertices, texture and colors GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[0]); GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleVertices.Capacity() * mBytesPerFloat, mTriangleVertices, GLES20.GlStaticDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[1]); GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleColors.Capacity() * mBytesPerFloat, mTriangleColors, GLES20.GlStaticDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[2]); GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleTextureUVMap.Capacity() * mBytesPerFloat, mTriangleTextureUVMap, GLES20.GlStaticDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0); //Load and setup texture GLES20.GlGenTextures(1, textureHandle, 0); //init 1 texture storage handle if (textureHandle[0] != 0) { //Android.Graphics cose class Matrix exists at both Android.Graphics and Android.OpenGL and this is only sample of using Android.Graphics.BitmapFactory.Options options = new Android.Graphics.BitmapFactory.Options(); options.InScaled = false; // No pre-scaling Android.Graphics.Bitmap bitmap = Android.Graphics.BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.iam, options); GLES20.GlBindTexture(GLES20.GlTexture2d, textureHandle[0]); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlNearest); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlNearest); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bitmap, 0); bitmap.Recycle(); } //Ask android to run RAM garbage cleaner System.GC.Collect(); //Setup OpenGL ES GLES20.GlClearColor(coord, coord, coord, coord); // GLES20.GlEnable(GLES20.GlDepthTest); //uncoment if needs enabled dpeth test GLES20.GlEnable(2884); // GlCullFace == 2884 see OpenGL documentation to this constant value GLES20.GlCullFace(GLES20.GlBack); // Position the eye behind the origin. float eyeX = 0.0f; float eyeY = 0.0f; float eyeZ = 4.5f; // We are looking toward the distance float lookX = 0.0f; float lookY = 0.0f; float lookZ = -5.0f; // Set our up vector. This is where our head would be pointing were we holding the camera. float upX = 0.0f; float upY = coord; 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); string vertexShader = "uniform mat4 u_MVPMatrix; \n" // A constant representing the combined model/view/projection matrix. + "attribute vec4 a_Position; \n" // Per-vertex position information we will pass in. + "attribute vec4 a_Color; \n" // Per-vertex color information we will pass in. + "varying vec4 v_Color; \n" // This will be passed into the fragment shader. + "attribute vec2 a_TextureCoord; \n" + "varying vec2 v_TextureCoord; \n" + "void main() \n" // The entry point for our vertex shader. + "{ \n" + " v_TextureCoord = a_TextureCoord; \n" // Pass the color through to the fragment shader. It will be interpolated across the triangle. + " v_Color = a_Color; \n" // Pass the color through to the fragment shader. It will be interpolated across the triangle. + " gl_Position = u_MVPMatrix \n" // gl_Position is a special variable used to store the final position. + " * a_Position; \n" // Multiply the vertex by the matrix to get the final point in normalized screen coordinates. + "} \n"; string fragmentShader = "precision mediump float; \n" // Set the default precision to medium. We don't need as high of a // precision in the fragment shader. + "varying vec4 v_Color; \n" // This is the color from the vertex shader interpolated across the triangle per fragment. + "varying vec2 v_TextureCoord; \n" + "uniform sampler2D u_Texture; \n" + "void main() \n" // The entry point for our fragment shader. + "{ \n" + " gl_FragColor = texture2D(u_Texture, v_TextureCoord); \n" // Pass the color directly through the pipeline. + "} \n"; vertexShader = string.Empty; fragmentShader = string.Empty; int resourceId = context.Resources.GetIdentifier("vertexshadervladimir1", "raw", context.PackageName); Stream fileStream = context.Resources.OpenRawResource(resourceId); StreamReader streamReader = new StreamReader(fileStream); string line = string.Empty; while ((line = streamReader.ReadLine()) != null) { vertexShader += line + "\n"; } resourceId = context.Resources.GetIdentifier("fragmentshadervladimir1", "raw", context.PackageName); fileStream = context.Resources.OpenRawResource(resourceId); streamReader = new StreamReader(fileStream); while ((line = streamReader.ReadLine()) != null) { fragmentShader += line + "\n"; } int vertexShaderHandle = GLES20.GlCreateShader(GLES20.GlVertexShader); if (vertexShaderHandle != 0) { // Pass in the shader source. GLES20.GlShaderSource(vertexShaderHandle, vertexShader); // Compile the shader. GLES20.GlCompileShader(vertexShaderHandle); // Get the compilation status. int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(vertexShaderHandle, GLES20.GlCompileStatus, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus[0] == 0) { GLES20.GlDeleteShader(vertexShaderHandle); vertexShaderHandle = 0; } } if (vertexShaderHandle == 0) { throw new Exception("Error creating vertex shader."); } // Load in the fragment shader shader. int fragmentShaderHandle = GLES20.GlCreateShader(GLES20.GlFragmentShader); if (fragmentShaderHandle != 0) { // Pass in the shader source. GLES20.GlShaderSource(fragmentShaderHandle, fragmentShader); // Compile the shader. GLES20.GlCompileShader(fragmentShaderHandle); // Get the compilation status. int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(fragmentShaderHandle, GLES20.GlCompileStatus, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus[0] == 0) { GLES20.GlDeleteShader(fragmentShaderHandle); fragmentShaderHandle = 0; } } if (fragmentShaderHandle == 0) { throw new Exception("Error creating fragment shader."); } // Create a program object and store the handle to it. int programHandle = GLES20.GlCreateProgram(); if (programHandle != 0) { // Bind the vertex shader to the program. GLES20.GlAttachShader(programHandle, vertexShaderHandle); // Bind the fragment shader to the program. GLES20.GlAttachShader(programHandle, fragmentShaderHandle); // Bind attributes GLES20.GlBindAttribLocation(programHandle, 0, "a_Position"); GLES20.GlBindAttribLocation(programHandle, 1, "a_Color"); GLES20.GlBindAttribLocation(programHandle, 2, "a_TextureCoord"); // Link the two shaders together into a program. GLES20.GlLinkProgram(programHandle); // Get the link status. int[] linkStatus = new int[1]; GLES20.GlGetProgramiv(programHandle, GLES20.GlLinkStatus, linkStatus, 0); // If the link failed, delete the program. if (linkStatus[0] == 0) { GLES20.GlDeleteProgram(programHandle); programHandle = 0; } } if (programHandle == 0) { throw new Exception("Error creating program."); } // Set program handles. These will later be used to pass in values to the program. mMVPMatrixHandle = GLES20.GlGetUniformLocation(programHandle, "u_MVPMatrix"); mPositionHandle = GLES20.GlGetAttribLocation(programHandle, "a_Position"); mColorHandle = GLES20.GlGetAttribLocation(programHandle, "a_Color"); mTextureCoordHandle = GLES20.GlGetAttribLocation(programHandle, "a_TextureCoord"); mTextureHandle = GLES20.GlGetUniformLocation(programHandle, "u_Texture"); // Tell OpenGL to use this program when rendering. GLES20.GlUseProgram(programHandle); }
public string Compile() { string result = string.Empty; string vertexShader = string.Empty; string fragmentShader = string.Empty; int resourceId = context.Resources.GetIdentifier(vertexShaderFile, "raw", context.PackageName); Stream fileStream = context.Resources.OpenRawResource(resourceId); StreamReader streamReader = new StreamReader(fileStream); string line = string.Empty; while ((line = streamReader.ReadLine()) != null) { vertexShader += line + "\n"; } resourceId = context.Resources.GetIdentifier(fragmentShaderFile, "raw", context.PackageName); fileStream = context.Resources.OpenRawResource(resourceId); streamReader = new StreamReader(fileStream); while ((line = streamReader.ReadLine()) != null) { fragmentShader += line + "\n"; } int vertexShaderHandle = GLES20.GlCreateShader(GLES20.GlVertexShader); if (vertexShaderHandle != 0) { // Pass in the shader source. GLES20.GlShaderSource(vertexShaderHandle, vertexShader); // Compile the shader. GLES20.GlCompileShader(vertexShaderHandle); // Get the compilation status. int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(vertexShaderHandle, GLES20.GlCompileStatus, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus[0] == 0) { result += "vertex shader error"; result += GLES20.GlGetProgramInfoLog(vertexShaderHandle); GLES20.GlDeleteShader(vertexShaderHandle); vertexShaderHandle = 0; } } // Load in the fragment shader shader. int fragmentShaderHandle = GLES20.GlCreateShader(GLES20.GlFragmentShader); if (fragmentShaderHandle != 0) { // Pass in the shader source. GLES20.GlShaderSource(fragmentShaderHandle, fragmentShader); // Compile the shader. GLES20.GlCompileShader(fragmentShaderHandle); // Get the compilation status. int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(fragmentShaderHandle, GLES20.GlCompileStatus, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus[0] == 0) { result += "fragment shader error"; result += GLES20.GlGetProgramInfoLog(fragmentShaderHandle); GLES20.GlDeleteShader(fragmentShaderHandle); fragmentShaderHandle = 0; return(result); } } // Create a program object and store the handle to it. programHandle = GLES20.GlCreateProgram(); if (programHandle != 0) { // Bind the vertex shader to the program. GLES20.GlAttachShader(programHandle, vertexShaderHandle); // Bind the fragment shader to the program. GLES20.GlAttachShader(programHandle, fragmentShaderHandle); // Bind attributes GLES20.GlBindAttribLocation(programHandle, 0, "a_Position"); GLES20.GlBindAttribLocation(programHandle, 1, "a_Color"); GLES20.GlBindAttribLocation(programHandle, 2, "a_TextureCoord"); GLES20.GlBindAttribLocation(programHandle, 3, "a_Normal"); // Link the two shaders together into a program. GLES20.GlLinkProgram(programHandle); // Get the link status. int[] linkStatus = new int[1]; GLES20.GlGetProgramiv(programHandle, GLES20.GlLinkStatus, linkStatus, 0); // If the link failed, delete the program. if (linkStatus[0] == 0) { result += "shader link error"; result += GLES20.GlGetProgramInfoLog(programHandle); GLES20.GlDeleteProgram(programHandle); programHandle = 0; return(result); } } // Set program handles. These will later be used to pass in values to the program. mMVPMatrixHandle = GLES20.GlGetUniformLocation(programHandle, "u_MVPMatrix"); mLightPos = GLES20.GlGetUniformLocation(programHandle, "u_LightPos"); mPositionHandle = GLES20.GlGetAttribLocation(programHandle, "a_Position"); mColorHandle = GLES20.GlGetAttribLocation(programHandle, "a_Color"); mTextureCoordHandle = GLES20.GlGetAttribLocation(programHandle, "a_TextureCoord"); mNormalHandle = GLES20.GlGetAttribLocation(programHandle, "a_Normal"); mTextureHandle = GLES20.GlGetUniformLocation(programHandle, "u_Texture"); return(result); }
public void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config) { const float coord = 1.0f; // Cube coords // X, Y, Z = 1 vertex * 3 = 1 face * 12 = 1 cube float[] triangleVerticesData = { -coord, -coord, -coord, -coord, -coord, coord, -coord, coord, coord, coord, coord, -coord, -coord, -coord, -coord, -coord, coord, -coord, coord, -coord, coord, -coord, -coord, -coord, coord, -coord, -coord, coord, coord, -coord, coord, -coord, -coord, -coord, -coord, -coord, -coord, -coord, -coord, -coord, coord, coord, -coord, coord, -coord, coord, -coord, coord, -coord, -coord, coord, -coord, -coord, -coord, -coord, coord, coord, -coord, -coord, coord, coord, -coord, coord, coord, coord, coord, coord, -coord, -coord, coord, coord, -coord, coord, -coord, -coord, coord, coord, coord, coord, -coord, coord, coord, coord, coord, coord, coord, -coord, -coord, coord, -coord, coord, coord, coord, -coord, coord, -coord, -coord, coord, coord, coord, coord, coord, -coord, coord, coord, coord, -coord, coord }; FloatBuffer mTriangleVertices = ByteBuffer.AllocateDirect(triangleVerticesData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer(); mTriangleVertices.Put(triangleVerticesData).Flip(); // Cube colors // R, G, B, A float[] triangleColorsData = { 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f }; FloatBuffer mTriangleColors = ByteBuffer.AllocateDirect(triangleColorsData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer(); mTriangleColors.Put(triangleColorsData).Flip(); //Cube texture UV Map float[] triangleTextureUVMapData = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f }; FloatBuffer mTriangleTextureUVMap = ByteBuffer.AllocateDirect(triangleTextureUVMapData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer(); mTriangleTextureUVMap.Put(triangleTextureUVMapData).Flip(); //triagles normals //This normal array is not right, it is spacialy DO FOR demonstrate how normals work with faces when light is calculated at shader program float[] triangleNormalData = { // Front face 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Right face 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Back face 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, // Left face -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // Top face 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // Bottom face 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f }; FloatBuffer mTriangleNormal = ByteBuffer.AllocateDirect(triangleNormalData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer(); mTriangleNormal.Put(triangleNormalData).Flip(); //Data buffers to VBO GLES20.GlGenBuffers(4, VBOBuffers, 0); //2 buffers for vertices, texture and colors GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[0]); GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleVertices.Capacity() * mBytesPerFloat, mTriangleVertices, GLES20.GlStaticDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[1]); GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleColors.Capacity() * mBytesPerFloat, mTriangleColors, GLES20.GlStaticDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[2]); GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleTextureUVMap.Capacity() * mBytesPerFloat, mTriangleTextureUVMap, GLES20.GlStaticDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[3]); GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleNormal.Capacity() * mBytesPerFloat, mTriangleNormal, GLES20.GlStaticDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0); //Load and setup texture GLES20.GlGenTextures(1, textureHandle, 0); //init 1 texture storage handle if (textureHandle[0] != 0) { //Android.Graphics cose class Matrix exists at both Android.Graphics and Android.OpenGL and this is only sample of using Android.Graphics.BitmapFactory.Options options = new Android.Graphics.BitmapFactory.Options(); options.InScaled = false; // No pre-scaling Android.Graphics.Bitmap bitmap = Android.Graphics.BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.texture1, options); GLES20.GlBindTexture(GLES20.GlTexture2d, textureHandle[0]); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlNearest); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlNearest); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bitmap, 0); bitmap.Recycle(); } //Ask android to run RAM garbage cleaner System.GC.Collect(); //Setup OpenGL ES GLES20.GlClearColor(0.0f, 0.0f, 0.0f, 0.0f); // GLES20.GlEnable(GLES20.GlDepthTest); //uncoment if needs enabled dpeth test GLES20.GlEnable(2884); // GlCullFace == 2884 see OpenGL documentation to this constant value GLES20.GlCullFace(GLES20.GlBack); // Position the eye behind the origin. float eyeX = 0.0f; float eyeY = 0.0f; float eyeZ = 4.5f; // We are looking toward the distance float lookX = 0.0f; float lookY = 0.0f; float lookZ = -5.0f; // Set our up vector. This is where our head would be pointing were we holding the camera. float upX = 0.0f; float upY = coord; 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); //all "attribute" variables is "triagles" VBO (arrays) items representation //a_Possition[0] <=> a_Color[0] <=> a_TextureCoord[0] <=> a_Normal[0] //a_Possition[1] <=> a_Color[1] <=> a_TextureCoord[1] <=> a_Normal[1] //... //a_Possition[n] <=> a_Color[n] <=> a_TextureCoord[n] <=> a_Normal[n] -- where "n" is object buffers length //-> HOW MANY faces in your object (model) in VBO -> how many times the vertex shader will be called by OpenGL string vertexShader = "uniform mat4 u_MVPMatrix; \n" // A constant representing the combined model/view/projection matrix. + "uniform vec3 u_LightPos; \n" // A constant representing the light source position + "attribute vec4 a_Position; \n" // Per-vertex position information we will pass in. (it means vec4[x,y,z,w] but we put only x,y,z at this sample + "attribute vec4 a_Color; \n" // Per-vertex color information we will pass in. + "varying vec4 v_Color; \n" // This will be passed into the fragment shader. + "attribute vec2 a_TextureCoord; \n" // Per-vertex texture UVMap information we will pass in. + "varying vec2 v_TextureCoord; \n" // This will be passed into the fragment shader. + "attribute vec3 a_Normal; \n" // Per-vertex normals information we will pass in. + "void main() \n" // The entry point for our vertex shader. + "{ \n" //light calculation section for fragment shader + " vec3 modelViewVertex = vec3(u_MVPMatrix * a_Position);\n" + " vec3 modelViewNormal = vec3(u_MVPMatrix * vec4(a_Normal, 0.0));\n" + " float distance = length(u_LightPos - modelViewVertex);\n" + " vec3 lightVector = normalize(u_LightPos - modelViewVertex);\n" + " float diffuse = max(dot(modelViewNormal, lightVector), 0.1);\n" + " diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));\n" + " v_Color = a_Color * vec4(diffuse);\n" //Pass the color with light aspect to fragment shader + " v_TextureCoord = a_TextureCoord; \n" // Pass the texture coordinate through to the fragment shader. It will be interpolated across the triangle. + " gl_Position = u_MVPMatrix \n" // gl_Position is a special variable used to store the final position. + " * a_Position; \n" // Multiply the vertex by the matrix to get the final point in normalized screen coordinates. + "} \n"; string fragmentShader = "precision mediump float; \n" // Set the default precision to medium. We don't need as high of a // precision in the fragment shader. + "varying vec4 v_Color; \n" // This is the color from the vertex shader interpolated across the triangle per fragment. + "varying vec2 v_TextureCoord; \n" // This is the texture coordinate from the vertex shader interpolated across the triangle per fragment. + "uniform sampler2D u_Texture; \n" // This is the texture image handler + "void main() \n" // The entry point for our fragment shader. + "{ \n" + " gl_FragColor = texture2D(u_Texture, v_TextureCoord) * v_Color; \n" // Pass the color directly through the pipeline. + "} \n"; int vertexShaderHandle = GLES20.GlCreateShader(GLES20.GlVertexShader); if (vertexShaderHandle != 0) { // Pass in the shader source. GLES20.GlShaderSource(vertexShaderHandle, vertexShader); // Compile the shader. GLES20.GlCompileShader(vertexShaderHandle); // Get the compilation status. int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(vertexShaderHandle, GLES20.GlCompileStatus, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus[0] == 0) { GLES20.GlDeleteShader(vertexShaderHandle); vertexShaderHandle = 0; } } if (vertexShaderHandle == 0) { throw new Exception("Error creating vertex shader."); } // Load in the fragment shader shader. int fragmentShaderHandle = GLES20.GlCreateShader(GLES20.GlFragmentShader); if (fragmentShaderHandle != 0) { // Pass in the shader source. GLES20.GlShaderSource(fragmentShaderHandle, fragmentShader); // Compile the shader. GLES20.GlCompileShader(fragmentShaderHandle); // Get the compilation status. int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(fragmentShaderHandle, GLES20.GlCompileStatus, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus[0] == 0) { GLES20.GlDeleteShader(fragmentShaderHandle); fragmentShaderHandle = 0; } } if (fragmentShaderHandle == 0) { throw new Exception("Error creating fragment shader."); } // Create a program object and store the handle to it. int programHandle = GLES20.GlCreateProgram(); if (programHandle != 0) { // Bind the vertex shader to the program. GLES20.GlAttachShader(programHandle, vertexShaderHandle); // Bind the fragment shader to the program. GLES20.GlAttachShader(programHandle, fragmentShaderHandle); // Bind attributes GLES20.GlBindAttribLocation(programHandle, 0, "a_Position"); GLES20.GlBindAttribLocation(programHandle, 1, "a_Color"); GLES20.GlBindAttribLocation(programHandle, 2, "a_TextureCoord"); GLES20.GlBindAttribLocation(programHandle, 3, "a_Normal"); // Link the two shaders together into a program. GLES20.GlLinkProgram(programHandle); // Get the link status. int[] linkStatus = new int[1]; GLES20.GlGetProgramiv(programHandle, GLES20.GlLinkStatus, linkStatus, 0); // If the link failed, delete the program. if (linkStatus[0] == 0) { GLES20.GlDeleteProgram(programHandle); programHandle = 0; } } if (programHandle == 0) { throw new Exception("Error creating program."); } // Set program handles. These will later be used to pass in values to the program. mMVPMatrixHandle = GLES20.GlGetUniformLocation(programHandle, "u_MVPMatrix"); mLightPos = GLES20.GlGetUniformLocation(programHandle, "u_LightPos"); mPositionHandle = GLES20.GlGetAttribLocation(programHandle, "a_Position"); mColorHandle = GLES20.GlGetAttribLocation(programHandle, "a_Color"); mTextureCoordHandle = GLES20.GlGetAttribLocation(programHandle, "a_TextureCoord"); mNormalHandle = GLES20.GlGetAttribLocation(programHandle, "a_Normal"); mTextureHandle = GLES20.GlGetUniformLocation(programHandle, "u_Texture"); // Tell OpenGL to use this program when rendering. GLES20.GlUseProgram(programHandle); }
void GLSurfaceView.IRenderer.OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config) { //Set the background clear color to gray GLES20.GlClearColor(0.5f, 0.5f, 0.5f, 0.5f); //Position the eye behind the origin. float eyeX = 0.0f; float eyeY = 0.0f; float eyeZ = 0.0f; //We are looking toward the distance. float lookX = 0.0f; float lookY = 0.0f; float lookZ = -5.0f; //Set our up vector,This is where our head wold be pointing were we holding the camera float upX = 0.0f; float upY = 1.0f; float upZ = 0.0f; Matrix.SetLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); string vertexShader = "uniform mat4 u_MVPMatrix; \n" + "attribute vec4 a_Position;\n" + "attribute vec4 a_Color;\n" + "varying vec4 v_Color; \n" + "void main() \n" + "{ \n" + "v_Color = a_Color; \n" + "gl_Position = u_MVPMatrix \n" + "*a_Position; \n" + "} \n"; string fragmentShader = "precision mediump float; \n" + "varying vec4 v_Color; \n" + "void main() \n" + "{ \n" + "gl_FragColor=v_Color; \n" + "}"; int vertexShaderHandle = GLES20.GlCreateShader(GLES20.GlVertexShader); if (vertexShaderHandle != 0) { //Pass in the shader source. GLES20.GlShaderSource(vertexShaderHandle, vertexShader); //Compile the shader GLES20.GlCompileShader(vertexShaderHandle); int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(vertexShaderHandle, GLES20.GlCompileStatus, compileStatus, 0); //If the compilation failed,delete the shader if (compileStatus[0] == 0) { GLES20.GlDeleteShader(vertexShaderHandle); vertexShaderHandle = 0; } } if (vertexShaderHandle == 0) { throw new ArgumentException("Error creating vertex shader."); } //Load in the fragment shader shader. int fragmentShaderHandle = GLES20.GlCreateShader(GLES20.GlFragmentShader); if (fragmentShaderHandle != 0) { //Pass in the shader source. GLES20.GlShaderSource(fragmentShaderHandle, fragmentShader); //Compile the shader. GLES20.GlCompileShader(fragmentShaderHandle); int[] compileStatus = new int[1]; GLES20.GlGetShaderiv(fragmentShaderHandle, GLES20.GlCompileStatus, compileStatus, 0); if (compileStatus[0] == 0) { GLES20.GlDeleteShader(fragmentShaderHandle); fragmentShaderHandle = 0; } } if (fragmentShaderHandle == 0) { throw new ArgumentException("Error createing fragment shader"); } //Create a program object and store the handle to it. int programHandle = GLES20.GlCreateProgram(); if (programHandle != 0) { //Bind the vertex shader to the program. GLES20.GlAttachShader(programHandle, vertexShaderHandle); //Bind the fragment shader to the program. GLES20.GlAttachShader(programHandle, fragmentShaderHandle); //Bind attributes. GLES20.GlBindAttribLocation(programHandle, 0, "a_Position"); GLES20.GlBindAttribLocation(programHandle, 1, "a_Color"); //Link the two shaders together into a program. GLES20.GlLinkProgram(programHandle); //Get the link status. int[] linkStatus = new int[1]; GLES20.GlGetProgramiv(programHandle, GLES20.GlLinkStatus, linkStatus, 0); //If the link failed ,delete the program if (linkStatus[0] == 0) { GLES20.GlDeleteProgram(programHandle); programHandle = 0; } } if (programHandle == 0) { throw new ArgumentException("Error creating program."); } //Set program handles. These will later be used to pass in values mMVPMatrixHandle = GLES20.GlGetUniformLocation(programHandle, "u_MVPMatrix"); mPositionHandle = GLES20.GlGetAttribLocation(programHandle, "a_Position"); mColorHandle = GLES20.GlGetAttribLocation(programHandle, "a_Color"); //Tell OpenGL to use this program when rendering. GLES20.GlUseProgram(programHandle); }