public void OnDrawFrame(IGL10 gl) { GLES20.GlClear(GLES20.GlColorBufferBit | GLES20.GlDepthBufferBit); //Prepare model transformation matrix float[] mModelMatrix = new float[16]; Matrix.SetIdentityM(mModelMatrix, 0); Matrix.RotateM(mModelMatrix, 0, angerX, 1.0f, 0.0f, 0.0f); Matrix.RotateM(mModelMatrix, 0, angerY, 0.0f, 1.0f, 0.0f); Matrix.ScaleM(mModelMatrix, 0, scale, scale, scale); //Draw with VBO GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[0]); GLES20.GlEnableVertexAttribArray(mPositionHandle); GLES20.GlVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GlFloat, false, 0, 0); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[1]); GLES20.GlEnableVertexAttribArray(mColorHandle); GLES20.GlVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GlFloat, false, 0, 0); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[2]); GLES20.GlEnableVertexAttribArray(mTextureCoordHandle); GLES20.GlVertexAttribPointer(mTextureCoordHandle, 2, GLES20.GlFloat, false, 0, 0); GLES20.GlActiveTexture(GLES20.GlTexture0); GLES20.GlBindTexture(GLES20.GlTexture2d, textureHandle[0]); GLES20.GlUniform1i(mTextureHandle, 0); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0); //END OF Draw with VBO // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix // (which currently contains model * view). // Allocate storage for the final combined matrix. This will be passed into the shader program. */ float[] mMVPMatrix = new float[16]; Matrix.MultiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix // (which now contains model * view * projection). // THIS IS NOT WORK AT C# Matrix class -> Matrix.MultiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); float[] _mMVPMatrix = new float[16]; Matrix.MultiplyMM(_mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); GLES20.GlUniformMatrix4fv(mMVPMatrixHandle, 1, false, _mMVPMatrix, 0); GLES20.GlDrawArrays(GLES20.GlTriangles, 0, modelVerticesData.Length / 3); //Cube has 12 triagle faces each face has 3 coord }
public override void onDrawFrame(GL10 gl) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); mFrameLock.@lock(); if (mCurrentFrame != null && !mVideoDisabled) { GLES20.glUseProgram(mProgram); if (mTextureWidth != mCurrentFrame.Width || mTextureHeight != mCurrentFrame.Height) { setupTextures(mCurrentFrame); } updateTextures(mCurrentFrame); Matrix.setIdentityM(mScaleMatrix, 0); float scaleX = 1.0f, scaleY = 1.0f; float ratio = (float)mCurrentFrame.Width / mCurrentFrame.Height; float vratio = (float)mViewportWidth / mViewportHeight; if (mVideoFitEnabled) { if (ratio > vratio) { scaleY = vratio / ratio; } else { scaleX = ratio / vratio; } } else { if (ratio < vratio) { scaleY = vratio / ratio; } else { scaleX = ratio / vratio; } } Matrix.scaleM(mScaleMatrix, 0, scaleX * (mCurrentFrame.MirroredX ? -1.0f : 1.0f), scaleY, 1); int mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mScaleMatrix, 0); GLES20.glDrawElements(GLES20.GL_TRIANGLES, mVertexIndex.Length, GLES20.GL_UNSIGNED_SHORT, mDrawListBuffer); } mFrameLock.unlock(); }
public virtual void Pixels(int w, int h, byte[] pixels) { Bind(); ByteBuffer imageBuffer = ByteBuffer.AllocateDirect(w * h).Order(ByteOrder.NativeOrder()); imageBuffer.Put(pixels); imageBuffer.Position(0); GLES20.GlPixelStorei(GLES20.GlUnpackAlignment, 1); GLES20.GlTexImage2D(GLES20.GlTexture2d, 0, GLES20.GlAlpha, w, h, 0, GLES20.GlAlpha, GLES20.GlUnsignedByte, imageBuffer); }
public void OnSurfaceChanged(IGL10 gl, int width, int height) { GLES20.GlViewport(0, 0, width, height); float ratio = (float)width / height; float left = -ratio; float right = ratio; float top = 1.0f; float bottom = -1.0f; float near = 1.0f; float far = 20.0f; Matrix.FrustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); }
public void uniformMatrix4fv(__WebGLUniformLocation u, int p1, bool p2, float[] mMVPMatrix, int p3) { // see also: http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml // see also: http://developer.android.com/reference/android/opengl/GLES20.html#glUniformMatrix4fv(int, int, boolean, float[], int) //void glUniformMatrix4fv( GLint location, // GLsizei count, // GLboolean transpose, // const GLfloat * value); GLES20.glUniformMatrix4fv(u.value, p1, p2, mMVPMatrix, p3); }
/// <summary> /// Create and build a shader for the hand skeleton points on the OpenGL thread. /// this is called when HandRenderManager's OnSurfaceCreated. /// </summary> public void Init() { ShaderUtil.CheckGlError(TAG, "Init start."); int[] buffers = new int[1]; GLES20.GlGenBuffers(1, buffers, 0); mVbo = buffers[0]; GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVbo); mVboSize = INITIAL_POINTS_SIZE * BYTES_PER_POINT; GLES20.GlBufferData(GLES20.GlArrayBuffer, mVboSize, null, GLES20.GlDynamicDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0); CreateProgram(); ShaderUtil.CheckGlError(TAG, "Init end."); }
/** * Replaces the fragment shader. Pass in null to reset to default. */ public void changeFragmentShader(String fragmentShader) { if (fragmentShader == null) { fragmentShader = FRAGMENT_SHADER; } GLES20.GlDeleteProgram(mProgram); mProgram = createProgram(VERTEX_SHADER, fragmentShader); if (mProgram == 0) { throw new RuntimeException("failed creating program"); } }
// 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 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 the shader return(shader); }
public void uniformMatrix4fv(__WebGLUniformLocation location, bool transpose, float[] value) { // see also: http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml // see also: http://developer.android.com/reference/android/opengl/GLES20.html#glUniformMatrix4fv(int, int, boolean, float[], int) //void glUniformMatrix4fv( GLint location, // GLsizei count, // GLboolean transpose, // const GLfloat * value); GLES20.glUniformMatrix4fv(location.value, /* count */ 1, transpose, value, /* offset */ 0); }
public override void Draw() { if (_light) { GLES20.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOne); base.Draw(); GLES20.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha); } else { base.Draw(); } }
/// <summary> /// Initialize the OpenGL ES rendering related to face geometry, /// including creating the shader program. /// This method is called when FaceRenderManager's OnSurfaceCreated method calling. /// </summary> /// <param name="context">Context.</param> public void Init(Context context) { ShaderUtil.CheckGlError(TAG, "Init start."); int[] texNames = new int[1]; GLES20.GlActiveTexture(GLES20.GlTexture0); GLES20.GlGenTextures(1, texNames, 0); mTextureName = texNames[0]; int[] buffers = new int[BUFFER_OBJECT_NUMBER]; GLES20.GlGenBuffers(BUFFER_OBJECT_NUMBER, buffers, 0); mVerticeId = buffers[0]; mTriangleId = buffers[1]; GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVerticeId); GLES20.GlBufferData(GLES20.GlArrayBuffer, mVerticeBufferSize * BYTES_PER_POINT, null, GLES20.GlDynamicDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0); GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, mTriangleId); // Each floating-point number occupies 4 bytes. GLES20.GlBufferData(GLES20.GlElementArrayBuffer, mTriangleBufferSize * 4, null, GLES20.GlDynamicDraw); GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, 0); GLES20.GlBindTexture(GLES20.GlTexture2d, mTextureName); CreateProgram(); //Add texture to facegeometry. Bitmap textureBitmap = null; AssetManager assets = context.Assets; try { Stream sr = assets.Open("face_geometry.png"); textureBitmap = BitmapFactory.DecodeStream(sr); } catch (Exception e) { Log.Debug(TAG, " Open bitmap error!"); } GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinearMipmapLinear); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLUtils.TexImage2D(GLES20.GlTexture2d, 0, textureBitmap, 0); GLES20.GlGenerateMipmap(GLES20.GlTexture2d); GLES20.GlBindTexture(GLES20.GlTexture2d, 0); ShaderUtil.CheckGlError(TAG, "Init end."); }
private void UpdateFaceGeometryData(ARFaceGeometry faceGeometry) { ShaderUtil.CheckGlError(TAG, "Before update data."); FloatBuffer faceVertices = faceGeometry.Vertices; // Obtain the number of geometric vertices of a face. mPointsNum = faceVertices.Limit() / 3; FloatBuffer textureCoordinates = faceGeometry.TextureCoordinates; // Obtain the number of geometric texture coordinates of the // face (the texture coordinates are two-dimensional). int texNum = textureCoordinates.Limit() / 2; Log.Debug(TAG, "Update face geometry data: texture coordinates size:" + texNum); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVerticeId); if (mVerticeBufferSize < (mPointsNum + texNum) * BYTES_PER_POINT) { while (mVerticeBufferSize < (mPointsNum + texNum) * BYTES_PER_POINT) { // If the capacity of the vertex VBO buffer is insufficient, expand the capacity. mVerticeBufferSize *= 2; } GLES20.GlBufferData(GLES20.GlArrayBuffer, mVerticeBufferSize, null, GLES20.GlDynamicDraw); } GLES20.GlBufferSubData(GLES20.GlArrayBuffer, 0, mPointsNum * BYTES_PER_POINT, faceVertices); GLES20.GlBufferSubData(GLES20.GlArrayBuffer, mPointsNum * BYTES_PER_POINT, texNum * BYTES_PER_COORD, textureCoordinates); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0); mTrianglesNum = faceGeometry.TriangleCount; IntBuffer faceTriangleIndices = faceGeometry.TriangleIndices; Log.Debug(TAG, "update face geometry data: faceTriangleIndices.size: " + faceTriangleIndices.Limit()); GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, mTriangleId); if (mTriangleBufferSize < mTrianglesNum * BYTES_PER_POINT) { while (mTriangleBufferSize < mTrianglesNum * BYTES_PER_POINT) { // If the capacity of the vertex VBO buffer is insufficient, expand the capacity. mTriangleBufferSize *= 2; } GLES20.GlBufferData(GLES20.GlElementArrayBuffer, mTriangleBufferSize, null, GLES20.GlDynamicDraw); } GLES20.GlBufferSubData(GLES20.GlElementArrayBuffer, 0, mTrianglesNum * BYTES_PER_POINT, faceTriangleIndices); GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, 0); ShaderUtil.CheckGlError(TAG, "After update data."); }
/** * Checks the frame for correctness, using GL to check RGB values. * * @return true if the frame looks good */ private bool checkSurfaceFrame(int frameIndex) { ByteBuffer pixelBuf = ByteBuffer.AllocateDirect(4); // TODO - reuse this bool frameFailed = false; for (int i = 0; i < 8; i++) { // Note the coordinates are inverted on the Y-axis in GL. int x, y; if (i < 4) { x = i * (mWidth / 4) + (mWidth / 8); y = (mHeight * 3) / 4; } else { x = (7 - i) * (mWidth / 4) + (mWidth / 8); y = mHeight / 4; } GLES20.GlReadPixels(x, y, 1, 1, GL10.GlRgba, GL10.GlUnsignedByte, pixelBuf); int r = pixelBuf.Get(0) & 0xff; int g = pixelBuf.Get(1) & 0xff; int b = pixelBuf.Get(2) & 0xff; //Log.Debug(TAG, "GOT(" + frameIndex + "/" + i + "): r=" + r + " g=" + g + " b=" + b); int expR, expG, expB; if (i == frameIndex % 8) { // colored rect (green/blue swapped) expR = TEST_R1; expG = TEST_B1; expB = TEST_G1; } else { // zero background color (green/blue swapped) expR = TEST_R0; expG = TEST_B0; expB = TEST_G0; } if (!isColorClose(r, expR) || !isColorClose(g, expG) || !isColorClose(b, expB)) { Log.Warn(TAG, "Bad frame " + frameIndex + " (rect=" + i + ": rgb=" + r + "," + g + "," + b + " vs. expected " + expR + "," + expG + "," + expB + ")"); frameFailed = true; } } return(!frameFailed); }
private int CreateProgram(string vertexSource, string fragmentSource) { var vertexShader = LoadShader(GLES20.GlVertexShader, vertexSource); if (vertexShader == 0) { return(0); } var fragmentShader = LoadShader(GLES20.GlFragmentShader, fragmentSource); if (fragmentShader == 0) { return(0); } int program = GLES20.GlCreateProgram(); if (program != 0) { int[] shaderStatus = new int[1]; GLES20.GlAttachShader(program, vertexShader); program = DefaultProgramIfFail("glAttachShader", program); if (program == 0) { return(0); } GLES20.GlAttachShader(program, fragmentShader); program = DefaultProgramIfFail("glAttachShader", program); if (program == 0) { return(0); } GLES20.GlLinkProgram(program); int[] linkStatus = new int[1]; GLES20.GlGetProgramiv(program, GLES20.GlLinkStatus, linkStatus, 0); if (linkStatus[0] != GLES20.GlTrue) { var error = "Could not link program: " + GLES20.GlGetProgramInfoLog(program); GLES20.GlDeleteProgram(program); return(0); } } return(program); }
private void DrawLabel(float[] cameraViews, float[] cameraProjection) { ShaderUtil.CheckGlError(TAG, "Draw label start."); Matrix.MultiplyMM(modelViewMatrix, 0, cameraViews, 0, modelMatrix, 0); Matrix.MultiplyMM(modelViewProjectionMatrix, 0, cameraProjection, 0, modelViewMatrix, 0); float halfWidth = LABEL_WIDTH / 2.0f; float halfHeight = LABEL_HEIGHT / 2.0f; float[] vertices = { -halfWidth, -halfHeight, 1, -halfWidth, halfHeight, 1, halfWidth, halfHeight, 1, halfWidth, -halfHeight, 1, }; // The size of each floating point is 4 bits. FloatBuffer vetBuffer = ByteBuffer.AllocateDirect(4 * vertices.Length) .Order(ByteOrder.NativeOrder()).AsFloatBuffer(); vetBuffer.Rewind(); for (int i = 0; i < vertices.Length; ++i) { vetBuffer.Put(vertices[i]); } vetBuffer.Rewind(); // The size of each floating point is 4 bits. GLES20.GlVertexAttribPointer(glPositionParameter, COORDS_PER_VERTEX, GLES20.GlFloat, false, 4 * COORDS_PER_VERTEX, vetBuffer); // Set the sequence of OpenGL drawing points to generate two triangles that form a plane. short[] indices = { 0, 1, 2, 0, 2, 3 }; // Size of the allocated buffer. ShortBuffer idxBuffer = ByteBuffer.AllocateDirect(2 * indices.Length) .Order(ByteOrder.NativeOrder()).AsShortBuffer(); idxBuffer.Rewind(); for (int i = 0; i < indices.Length; ++i) { idxBuffer.Put(indices[i]); } idxBuffer.Rewind(); GLES20.GlUniformMatrix4fv(glModelViewProjectionMatrix, 1, false, modelViewProjectionMatrix, 0); GLES20.GlDrawElements(GLES20.GlTriangleStrip, idxBuffer.Limit(), GLES20.GlUnsignedShort, idxBuffer); ShaderUtil.CheckGlError(TAG, "Draw label end."); }
public void OnDrawFrame(IGL10 gl) { GLES20.GlClear(GLES20.GlColorBufferBit | GLES20.GlDepthBufferBit); if (mArSession == null) { return; } if (mDisplayRotationManager.GetDeviceRotation()) { mDisplayRotationManager.UpdateArSessionDisplayGeometry(mArSession); } try { mArSession.SetCameraTextureName(mTextureDisplay.GetExternalTextureId()); ARFrame frame = mArSession.Update(); mTextureDisplay.OnDrawFrame(frame); float fpsResult = DoFpsCalculate(); System.Collections.ICollection faces = (System.Collections.ICollection)mArSession.GetAllTrackables(Java.Lang.Class.FromType(typeof(ARFace))); if (faces.Count == 0) { mTextDisplay.OnDrawFrame(null); return; } Log.Debug(TAG, "Face number: " + faces.Count); ARCamera camera = frame.Camera; foreach (ARFace face in faces) { if (face.TrackingState == ARTrackableTrackingState.Tracking) { mFaceGeometryDisplay.OnDrawFrame(camera, face); StringBuilder sb = new StringBuilder(); UpdateMessageData(sb, fpsResult, face); mTextDisplay.OnDrawFrame(sb); } } } catch (ArDemoRuntimeException e) { Log.Debug(TAG, "Exception on the ArDemoRuntimeException!"); } catch (Throwable t) { // This prevents the app from crashing due to unhandled exceptions. Log.Debug(TAG, "Exception on the OpenGL thread", t); } }
public static void InitTexParams() { try { GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); } catch (Exception e) { Console.WriteLine(e); } }
/// <summary> /// Create and build a shader for the hand gestures on the OpenGL thread. /// This method is called when HandRenderManager's OnSurfaceCreated. /// </summary> public void Init() { ShaderUtil.CheckGlError(TAG, "Init start."); mMVPMatrix = MatrixUtil.GetOriginalMatrix(); int[] buffers = new int[1]; GLES20.GlGenBuffers(1, buffers, 0); mVbo = buffers[0]; GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVbo); CreateProgram(); GLES20.GlBufferData(GLES20.GlArrayBuffer, mVboSize, null, GLES20.GlDynamicDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0); ShaderUtil.CheckGlError(TAG, "Init end."); }
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); } }
public void InitMediaPlayerWithSurface(bool thenPlay = false) { var t = GLES20.GlGetString(GLES20.GlExtensions); //System.Diagnostics.Debug.WriteLine (t); // 1. Dispose and create _mediaPlayer.Dispose(); _mediaPlayer = new MediaPlayer(); // 2. Set surface _texture = new Surface(_surfaceTexture); _mediaPlayer.SetSurface(_texture); _texture.Release(); // 3. Set data source _mediaPlayer.SetDataSource(_filepath); // 4. Prepare (with surface) try { _mediaPlayer.Prepared += (object mediaPlayerPrepared, EventArgs mediaPlayerPreparedEvents) => { // 5. Should not update surface yet lock (syncLock) { _updateSurface = false; } // 6. Set the three event handlers _mediaPlayer.Error += (sender, e) => { System.Diagnostics.Debug.WriteLine("Error"); }; _mediaPlayer.Info += (sender, e) => { System.Diagnostics.Debug.WriteLine("Info"); }; _mediaPlayer.Completion += (object sender, EventArgs e) => { System.Diagnostics.Debug.WriteLine("Completed"); }; if (thenPlay) { _mediaPlayer.Start(); } }; _mediaPlayer.Prepare(); } catch (System.Exception) { // Probably incompatible movie return; } }
// Draw |textures| using |vertices| (X,Y coordinates). private void drawRectangle(int[] textures, FloatBuffer vertices) { for (int i = 0; i < 3; ++i) { GLES20.GlActiveTexture(GLES20.GlTexture0 + i); GLES20.GlBindTexture(GLES20.GlTexture2d, textures[i]); } GLES20.GlVertexAttribPointer(posLocation, 2, GLES20.GlFloat, false, 0, vertices); GLES20.GlEnableVertexAttribArray(posLocation); GLES20.GlDrawArrays(GLES20.GlTriangleStrip, 0, 4); checkNoGLES2Error(); }
// Upload the YUV planes from |frame| to |textures|. private void texImage2D(VideoRenderer.I420Frame frame, int[] textures) { for (int i = 0; i < 3; ++i) { ByteBuffer plane = frame.YuvPlanes[i]; GLES20.GlActiveTexture(GLES20.GlTexture0 + i); GLES20.GlBindTexture(GLES20.GlTexture2d, textures[i]); int w = i == 0 ? frame.Width : frame.Width / 2; int h = i == 0 ? frame.Height : frame.Height / 2; abortUnless(w == frame.YuvStrides[i], frame.YuvStrides[i] + "!=" + w); GLES20.GlTexImage2D(GLES20.GlTexture2d, 0, GLES20.GlLuminance, w, h, 0, GLES20.GlLuminance, GLES20.GlUnsignedByte, plane); } checkNoGLES2Error(); }
public static void InitTexParams() { try { GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); } catch (Exception e) { Methods.DisplayReportResultTrack(e); } }
/// <summary> /// Draw a virtual object at a specific location on a specified plane. /// This method is called when WorldRenderManager's OnDrawFrame. /// </summary> /// <param name="cameraView">The viewMatrix is a 4 * 4 matrix.</param> /// <param name="cameraProjection">The ProjectionMatrix is a 4 * 4 matrix.</param> /// <param name="lightIntensity">The lighting intensity.</param> /// <param name="obj">The virtual object.</param> public void OnDrawFrame(float[] cameraView, float[] cameraProjection, float lightIntensity, VirtualObject obj) { ShaderUtil.CheckGlError(TAG, "onDrawFrame start."); mModelMatrixs = obj.GetModelAnchorMatrix(); Matrix.MultiplyMM(mModelViewMatrixs, 0, cameraView, 0, mModelMatrixs, 0); Matrix.MultiplyMM(mModelViewProjectionMatrixs, 0, cameraProjection, 0, mModelViewMatrixs, 0); GLES20.GlUseProgram(mGlProgram); Matrix.MultiplyMV(mViewLightDirections, 0, mModelViewMatrixs, 0, LIGHT_DIRECTIONS, 0); MatrixUtil.NormalizeVec3(mViewLightDirections); // Light direction. GLES20.GlUniform4f(mLightingParametersUniform, mViewLightDirections[0], mViewLightDirections[1], mViewLightDirections[2], lightIntensity); float[] objColors = obj.GetColor(); GLES20.GlUniform4fv(mColorUniform, 1, objColors, 0); GLES20.GlActiveTexture(GLES20.GlTexture0); GLES20.GlBindTexture(GLES20.GlTexture2d, mTextures[0]); GLES20.GlUniform1i(mTextureUniform, 0); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVertexBufferId); // The coordinate dimension of the read virtual object is 3. GLES20.GlVertexAttribPointer( mPositionAttribute, 3, GLES20.GlFloat, false, 0, 0); // The dimension of the normal vector is 3. GLES20.GlVertexAttribPointer( mNormalAttribute, 3, GLES20.GlFloat, false, 0, mNormalsBaseAddress); // The dimension of the texture coordinate is 2. GLES20.GlVertexAttribPointer( mTexCoordAttribute, 2, GLES20.GlFloat, false, 0, mTexCoordsBaseAddress); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0); GLES20.GlUniformMatrix4fv( mModelViewUniform, 1, false, mModelViewMatrixs, 0); GLES20.GlUniformMatrix4fv( mModelViewProjectionUniform, 1, false, mModelViewProjectionMatrixs, 0); GLES20.GlEnableVertexAttribArray(mPositionAttribute); GLES20.GlEnableVertexAttribArray(mNormalAttribute); GLES20.GlEnableVertexAttribArray(mTexCoordAttribute); GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, mIndexBufferId); GLES20.GlDrawElements(GLES20.GlTriangles, mIndexCount, GLES20.GlUnsignedShort, 0); GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, 0); GLES20.GlDisableVertexAttribArray(mPositionAttribute); GLES20.GlDisableVertexAttribArray(mNormalAttribute); GLES20.GlDisableVertexAttribArray(mTexCoordAttribute); GLES20.GlBindTexture(GLES20.GlTexture2d, 0); ShaderUtil.CheckGlError(TAG, "onDrawFrame end."); }
// // FNA3D_VerifySampler // public static void FNA3D_VerifySampler(IntPtr device, int index, IntPtr texture, ref FNA3D_SamplerState sampler) { var samplerCopy = sampler; int textureId = (int)texture; var renderer = Renderer.Get(device); renderer.Send(false, () => { var state = (State)renderer.UserData; var config = state.TextureConfigs[textureId]; GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + index); GLES20.glBindTexture(config[0], textureId); if (index == renderer.TextureUnits - 1) { state.TextureOnLastUnit = textureId; } if (textureId == 0) { return; } GLES20.glTexParameteri(config[0], GLES30.GL_TEXTURE_MAX_LEVEL, config[2] - 1); GLES20.glTexParameteri(config[0], GLES30.GL_TEXTURE_BASE_LEVEL, samplerCopy.maxMipLevel); GLES20.glTexParameteri(config[0], GLES20.GL_TEXTURE_WRAP_S, TextureWrapMode[(int)samplerCopy.addressU]); GLES20.glTexParameteri(config[0], GLES20.GL_TEXTURE_WRAP_T, TextureWrapMode[(int)samplerCopy.addressV]); if (config[0] == GLES30.GL_TEXTURE_3D) { GLES20.glTexParameteri(config[0], GLES30.GL_TEXTURE_WRAP_R, TextureWrapMode[(int)samplerCopy.addressW]); } int magIndex = (int)samplerCopy.filter * 3; int minIndex = magIndex + (config[2] <= 1 ? 1 : 2); GLES20.glTexParameteri(config[0], GLES20.GL_TEXTURE_MAG_FILTER, TextureFilterMode[magIndex]); GLES20.glTexParameteri(config[0], GLES20.GL_TEXTURE_MIN_FILTER, TextureFilterMode[minIndex]); }); }
// // Send // public void Send(bool wait, Action action) { Exception exc = null; if (paused.get() == 0) { if (!waitObject.block(2000)) { // see also Present(). a timeout here means that onDrawFrame // was never called, so the surface was probably destroyed. paused.compareAndSet(0, -1); } else { var cond = wait ? new android.os.ConditionVariable() : null; surface.queueEvent(((java.lang.Runnable.Delegate)(() => { try { action(); } catch (Exception exc2) { exc = exc2; } if (checkErrors) { var error = GLES20.glGetError(); if (error != GLES20.GL_NO_ERROR) { exc = new Exception($"GL Error {error}"); } } if (cond != null) { cond.open(); } })).AsInterface()); if (cond != null) { cond.block(); } } } if (exc != null) { throw new AggregateException(exc.Message, exc); } }
public void OnDrawFrame(IGL10 gl) { // Move ShellViewModel.Animate(); mLightDir[0] = -0.5f; mLightDir[1] = -1.0f; mLightDir[2] = -0.5f; // in left-handed region Vector.normalize(mLightDir); mRT["screen"].switchTargetFrameBuffer(); GLES20.GlClear(GL10.GlColorBufferBit | GL10.GlDepthBufferBit); //////////////////////////////////////////////////////////////////// //// draw models ShellViewModel.LockWith(() => { foreach (var shell in ShellViewModel.Shells) { if (shell.Loaded) { foreach (var rs in shell.RenderSets) { mRT[rs.target].switchTargetFrameBuffer(); GLSL glsl = mGLSL[rs.shader]; GLES20.GlUseProgram(glsl.mProgram); // Projection Matrix GLES20.GlUniformMatrix4fv(glsl.muPMatrix, 1, false, ShellViewModel.ProjectionMatrix, 0); // LightPosition GLES20.GlUniform3fv(glsl.muLightDir, 1, mLightDir, 0); bindBuffer(shell.Surface, glsl); if (!rs.shader.EndsWith("alpha")) { drawNonAlpha(shell.Surface, glsl); drawAlpha(shell.Surface, glsl, false); } else { drawAlpha(shell.Surface, glsl, true); } } } } }); GLES20.GlFlush(); checkGlError(TAG); }
private void ArrayToVBO(Array source, int size) { float[] floatArray = new float[size / 4]; System.Buffer.BlockCopy(source, 0, floatArray, 0, (int)size); //temp Cut vertex int cutSize = 0; for (int i = 0; i < size / 4; i += 3) { if (floatArray[i + 1] < 20.0f) { cutSize++; } } float[] cutArray = new float[cutSize * 3]; int cutArrayIndex = 0; for (int i = 0; i < size / 4; i += 3) { if (floatArray[i + 1] < 20.0f) { cutArray[cutArrayIndex] = floatArray[i]; cutArray[cutArrayIndex + 1] = floatArray[i + 1]; cutArray[cutArrayIndex + 2] = floatArray[i + 2]; cutArrayIndex += 3; } } size = cutSize * 3 * 4; objectSize = (int)(size / 4 / 3); FloatBuffer vertexBuffer; vertexBuffer = FloatBuffer.Allocate((int)size / 4); // float array to vertexBuffer.Put(cutArray, 0, (int)size / 4); vertexBuffer.Flip(); GLES20.GlGenBuffers(1, VBOBuffers, 0); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[0]); //VBOManager.setSize(fileName, vertexBuffer.Capacity() / 4); //is size of vertex count = 1 vertex 4 float x,y,z, 1 GLES20.GlBufferData(GLES20.GlArrayBuffer, vertexBuffer.Capacity() * mBytesPerFloat, vertexBuffer, GLES20.GlStaticDraw); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0); handle = VBOBuffers[0]; floatArray = null; vertexBuffer = null; }
public override void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config) { base.OnSurfaceCreated(gl, config); //SETUP OpenGL ES GLES20.GlClearColor(0.9f, 0.9f, 0.9f, 0.9f); GLES20.GlEnable(GLES20.GlDepthTest); //uncoment if needs enabled dpeth test //ENDSETUP OpenGL ES //Loading objects glObjects.Add(new GLObject(this, "vertex_shader", "fragment_shader", "oldhouse_objvertex", "oldhouse_objnormal", "oldhouse_objtexture", "body")); //Ask android to run RAM garbage cleaner System.GC.Collect(); }