public static int loadTexture(Context context, int resourceId) { if (resourceId == -1) { return(resourceId); } System.GC.Collect(); int[] textureHandle = new int[1]; GLES20.GlGenTextures(1, textureHandle, 0); if (textureHandle[0] != 0) { BitmapFactory.Options options = new BitmapFactory.Options(); options.InScaled = false; // No pre-scaling Bitmap bitmap = BitmapFactory.DecodeResource(context.Resources, resourceId, 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(); } if (textureHandle[0] == 0) { throw new RuntimeException("Error loading texture."); } return(textureHandle[0]); }
/// <summary> /// Create the shader program for label display in the openGL thread. /// This method will be called when WorldRenderManager's OnSurfaceCreated. /// </summary> /// <param name="labelBitmaps">View data indicating the plane type.</param> public void Init(List <Bitmap> labelBitmaps) { ShaderUtil.CheckGlError(TAG, "Init start."); if (labelBitmaps.Count == 0) { Log.Debug(TAG, "No bitmap."); } CreateProgram(); int idx = 0; GLES20.GlGenTextures(textures.Length, textures, 0); foreach (Bitmap labelBitmap in labelBitmaps) { // for semantic label plane GLES20.GlActiveTexture(GLES20.GlTexture0 + idx); GLES20.GlBindTexture(GLES20.GlTexture2d, textures[idx]); GLES20.GlTexParameteri( GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinearMipmapLinear); GLES20.GlTexParameteri( GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLUtils.TexImage2D(GLES20.GlTexture2d, 0, labelBitmap, 0); GLES20.GlGenerateMipmap(GLES20.GlTexture2d); GLES20.GlBindTexture(GLES20.GlTexture2d, 0); idx++; ShaderUtil.CheckGlError(TAG, "Texture loading"); } ShaderUtil.CheckGlError(TAG, "Init end."); }
/** * Initializes GL state. Call this after the EGL surface has been created and made current. */ public void surfaceCreated() { mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER); if (mProgram == 0) { throw new RuntimeException("failed creating program"); } maPositionHandle = GLES20.GlGetAttribLocation(mProgram, "aPosition"); checkLocation(maPositionHandle, "aPosition"); maTextureHandle = GLES20.GlGetAttribLocation(mProgram, "aTextureCoord"); checkLocation(maTextureHandle, "aTextureCoord"); muMVPMatrixHandle = GLES20.GlGetUniformLocation(mProgram, "uMVPMatrix"); checkLocation(muMVPMatrixHandle, "uMVPMatrix"); muSTMatrixHandle = GLES20.GlGetUniformLocation(mProgram, "uSTMatrix"); checkLocation(muSTMatrixHandle, "uSTMatrix"); int[] textures = new int[1]; GLES20.GlGenTextures(1, textures, 0); mTextureID = textures[0]; GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, mTextureID); checkGlError("glBindTexture mTextureID"); GLES20.GlTexParameterf(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureMinFilter, GLES20.GlNearest); GLES20.GlTexParameterf(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLES20.GlTexParameteri(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); checkGlError("glTexParameter"); }
public void LoadTextures() { try { // Generate textures GLES20.GlGenTextures(2, MTextures, 0); // Load input bitmap if (MSourceBitmap != null) { MImageWidth = MSourceBitmap.Width; MImageHeight = MSourceBitmap.Height; MTexRenderer.UpdateTextureSize(MImageWidth, MImageHeight); // Upload to texture GLES20.GlBindTexture(GLES20.GlTexture2d, MTextures[0]); GLUtils.TexImage2D(GLES20.GlTexture2d, 0, MSourceBitmap, 0); // Set texture parameters GlToolbox.InitTexParams(); } } catch (Exception e) { Console.WriteLine(e); } }
private static TexInfo CreateTexInfoFromBitmap(Bitmap bitmap) { TexInfo ret = null; if (bitmap != null) { ret = new TexInfo(); ret.has_alpha = bitmap.HasAlpha; ret.needs_alpha_test = false; // ad-hock GLES20.GlPixelStorei(GLES20.GlUnpackAlignment, 1); int[] tex = new int[1]; GLES20.GlGenTextures(1, tex, 0); ret.tex = tex [0]; GLES20.GlBindTexture(GLES20.GlTexture2d, ret.tex); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear); GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bitmap, 0); bitmap.Recycle(); } return(ret); }
public Texture(Context context, string fileName) { if (string.IsNullOrEmpty(fileName)) { return; } 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 int id = context.Resources.GetIdentifier(fileName, "drawable", context.PackageName); Android.Graphics.Bitmap bitmap = Android.Graphics.BitmapFactory.DecodeResource(context.Resources, id, 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(); handle = textureHandle[0]; } }
/** * Allocates and initializes OpenGL resources needed by the background renderer. Must be * called on the OpenGL thread, typically in * {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}. * * @param context Needed to access shader source. */ public void CreateOnGlThread(Context context) { // Generate the background texture. var textures = new int[1]; //GLES20 = OpenGL ES 2.0 //Gibt eine bestimmte Anzahl an freien Texturnamen zurück. GLES20.GlGenTextures(1, textures, 0); TextureId = textures[0]; GLES20.GlBindTexture(mTextureTarget, TextureId); GLES20.GlTexParameteri(mTextureTarget, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(mTextureTarget, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); GLES20.GlTexParameteri(mTextureTarget, GLES20.GlTextureMinFilter, GLES20.GlNearest); GLES20.GlTexParameteri(mTextureTarget, GLES20.GlTextureMagFilter, GLES20.GlNearest); int numVertices = 4; if (numVertices != QUAD_COORDS.Length / COORDS_PER_VERTEX) { throw new Exception("Unexpected number of vertices in BackgroundRenderer."); } var bbVertices = ByteBuffer.AllocateDirect(QUAD_COORDS.Length * FLOAT_SIZE); bbVertices.Order(ByteOrder.NativeOrder()); mQuadVertices = bbVertices.AsFloatBuffer(); mQuadVertices.Put(QUAD_COORDS); mQuadVertices.Position(0); var bbTexCoords = ByteBuffer.AllocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); bbTexCoords.Order(ByteOrder.NativeOrder()); mQuadTexCoord = bbTexCoords.AsFloatBuffer(); mQuadTexCoord.Put(QUAD_TEXCOORDS); mQuadTexCoord.Position(0); var bbTexCoordsTransformed = ByteBuffer.AllocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); bbTexCoordsTransformed.Order(ByteOrder.NativeOrder()); mQuadTexCoordTransformed = bbTexCoordsTransformed.AsFloatBuffer(); int vertexShader = ShaderUtil.LoadGLShader(TAG, context, GLES20.GlVertexShader, Resource.Raw.screenquad_vertex); int fragmentShader = ShaderUtil.LoadGLShader(TAG, context, GLES20.GlFragmentShader, Resource.Raw.screenquad_fragment_oes); mQuadProgram = GLES20.GlCreateProgram(); GLES20.GlAttachShader(mQuadProgram, vertexShader); GLES20.GlAttachShader(mQuadProgram, fragmentShader); GLES20.GlLinkProgram(mQuadProgram); GLES20.GlUseProgram(mQuadProgram); ShaderUtil.CheckGLError(TAG, "Program creation"); mQuadPositionParam = GLES20.GlGetAttribLocation(mQuadProgram, "a_Position"); mQuadTexCoordParam = GLES20.GlGetAttribLocation(mQuadProgram, "a_TexCoord"); ShaderUtil.CheckGLError(TAG, "Program parameters"); }
/// <summary> /// This method is called when Android.Opengl.GLSurfaceView.IRenderer's OnSurfaceCreated /// to initialize the texture ID and create the OpenGL ES shader program. /// </summary> public void Init() { int[] textures = new int[1]; GLES20.GlGenTextures(1, textures, 0); mExternalTextureId = textures[0]; GenerateExternalTexture(); CreateProgram(); }
public Texture() { var ids = new int[1]; GLES20.GlGenTextures(1, ids, 0); Id = ids[0]; Bind(); }
/** * \brief Create an external texture(GL_TEXTURE_EXTERNAL_OES) for video rendering. * @return gl texture id. */ public static int createVideoTexture() { int[] gl_textureID = new int[1]; GLES20.GlGenTextures(1, gl_textureID, 0); GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, gl_textureID[0]); GLES20.GlTexParameterf(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureMinFilter, GLES20.GlLinear); GLES20.GlTexParameterf(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, 0); return(gl_textureID[0]); }
private void initTex() { hTex = new int[1]; GLES20.GlGenTextures(1, hTex, 0); GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, hTex[0]); GLES20.GlTexParameteri(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureMinFilter, GLES20.GlNearest); GLES20.GlTexParameteri(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureMagFilter, GLES20.GlNearest); }
private int CreateTargetTexture() { int texture; int[] textures = new int[1]; GLES20.GlGenTextures(1, textures, 0); texture = textures[0]; updateTargetTexture(32, 32); return(texture); }
public void OnSurfaceCreated(Javax.Microedition.Khronos.Opengles.IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config) { _glProgram = CreateProgram(mVertexShader, mFragmentShader); var extensions = " " + GLES20.GlGetString(GLES20.GlExtensions) + " "; var ok = extensions.IndexOf("GL_OES_framebuffer_object") >= 0; var ab = extensions; if (_glProgram == 0) { return; } GLES20.GlUseProgram(0); GLES20.GlUseProgram(_glProgram); _aPositionHandle = ActOnError(GLES20.GlGetAttribLocation(_glProgram, "aPosition"), "aPosition"); _aTextureCoord = ActOnError(GLES20.GlGetAttribLocation(_glProgram, "aTextureCoord"), "aTextureCoord"); _uMVPMatrixHandle = ActOnError(GLES20.GlGetUniformLocation(_glProgram, "uMVPMatrix"), "uMVPMatrix"); _uSTMatrixHandle = ActOnError(GLES20.GlGetUniformLocation(_glProgram, "uSTMatrix"), "uSTMatrix"); _OESTextureUniform = ActOnError(GLES20.GlGetUniformLocation(_glProgram, textureName), textureName); _otherTextureUniform = ActOnError(GLES20.GlGetUniformLocation(_glProgram, "overlay"), "overlay"); int[] textures = new int[1]; GLES20.GlGenTextures(1, textures, 0); _OESTextureId = textures[0]; GLES20.GlUseProgram(0); GLES20.GlUseProgram(_glProgram); GLES20.GlActiveTexture(GLES20.GlTexture1); GLES20.GlBindTexture(GL_TEXTURE_EXTERNAL_OES, _OESTextureId); GLES20.GlUniform1i(_OESTextureUniform, 1); GLES20.GlUseProgram(0); GLES20.GlUseProgram(_glProgram); GLES20.GlTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GlTextureMinFilter, GLES20.GlNearest); GLES20.GlTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GlTextureMagFilter, GLES20.GlLinear); _surfaceTexture = new SurfaceTexture(_OESTextureId); _surfaceTexture.FrameAvailable += _surfaceTexture_FrameAvailable; Surface surface = new Surface(_surfaceTexture); _mediaPlayer.SetSurface(surface); surface.Release(); _otherTextureId = CreateTargetTexture(); GLES20.GlActiveTexture(GLES20.GlTexture2); GLES20.GlBindTexture(GLES20.GlTexture2d, _otherTextureId); GLES20.GlUniform1i(_otherTextureUniform, 2); _updateSurface = false; _mediaPlayer.Start(); }
public override void initShader() { base.initShader(); //make texture ---------------------------------------------------- //draw texture if (LevelCanvas.b != null) { LevelCanvas.b.Recycle(); } int width = steps * textureStep; int height = steps * textureStep; Bitmap bitmap = Bitmap.CreateBitmap(width, height, Config.Argb8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.Color = Android.Graphics.Color.Green; paint.SetStyle(Paint.Style.Fill); canvas.DrawPaint(paint); paint.Color = Android.Graphics.Color.White; paint.AntiAlias = true; paint.TextSize = textureStep; paint.TextAlign = Paint.Align.Center; for (int i = 0; i < steps; i++) { paint.Color = Android.Graphics.Color.Rgb(i * 0xF, i * 0xF, i * 0xF); canvas.DrawText((steps - i).ToString(), textureStep + textureStep / 2, i * textureStep + textureStep - textureStep / 6, paint); } //load texture int[] textureHandle = new int[1]; GLES20.GlGenTextures(1, textureHandle, 0); if (textureHandle[0] != 0) { 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(); LevelCanvas.b = bitmap; } textureResID = textureHandle[0]; TextureManager.addTextureHandle(textureResID, textureHandle[0]); }
/// <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."); }
/** * Initializes GL state. Call this after the EGL surface has been created and made current. */ public SurfaceTexture createTexture() { mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER); if (mProgram == 0) { throw new Java.Lang.RuntimeException("failed creating program"); } maPositionHandle = GLES20.GlGetAttribLocation(mProgram, "aPosition"); checkGlError("glGetAttribLocation aPosition"); if (maPositionHandle == -1) { throw new Java.Lang.RuntimeException("Could not get attrib location for aPosition"); } maTextureHandle = GLES20.GlGetAttribLocation(mProgram, "aTextureCoord"); checkGlError("glGetAttribLocation aTextureCoord"); if (maTextureHandle == -1) { throw new Java.Lang.RuntimeException("Could not get attrib location for aTextureCoord"); } muMVPMatrixHandle = GLES20.GlGetUniformLocation(mProgram, "uMVPMatrix"); checkGlError("glGetUniformLocation uMVPMatrix"); if (muMVPMatrixHandle == -1) { throw new Java.Lang.RuntimeException("Could not get attrib location for uMVPMatrix"); } muSTMatrixHandle = GLES20.GlGetUniformLocation(mProgram, "uSTMatrix"); checkGlError("glGetUniformLocation uSTMatrix"); if (muSTMatrixHandle == -1) { throw new Java.Lang.RuntimeException("Could not get attrib location for uSTMatrix"); } int[] textures = new int[1]; GLES20.GlGenTextures(1, textures, 0); mTextureID = textures[0]; GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, mTextureID); checkGlError("glBindTexture mTextureID"); GLES20.GlTexParameterf(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureMinFilter, GLES20.GlNearest); GLES20.GlTexParameterf(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLES20.GlTexParameteri(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES11Ext.GlTextureExternalOes, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); checkGlError("glTexParameter"); mSurfaceTexture = new SurfaceTexture(mTextureID); return mSurfaceTexture; }
/// <summary> /// Allocates and initializes OpenGL resources needed by the plane renderer. Must be /// called on the OpenGL thread, typically in /// <see cref="GLSurfaceView.IRenderer.OnSurfaceCreated(IGL10, Javax.Microedition.Khronos.Egl.EGLConfig)"/> /// </summary> /// <param name="context">Needed to access shader source and texture PNG.</param> /// <param name="gridDistanceTextureName">Name of the PNG file containing the grid texture.</param> public void CreateOnGlThread(Context context, String gridDistanceTextureName) { int vertexShader = ShaderUtil.LoadGLShader(TAG, context, GLES20.GlVertexShader, Resource.Raw.plane_vertex); int passthroughShader = ShaderUtil.LoadGLShader(TAG, context, GLES20.GlFragmentShader, Resource.Raw.plane_fragment); mPlaneProgram = GLES20.GlCreateProgram(); GLES20.GlAttachShader(mPlaneProgram, vertexShader); GLES20.GlAttachShader(mPlaneProgram, passthroughShader); GLES20.GlLinkProgram(mPlaneProgram); GLES20.GlUseProgram(mPlaneProgram); ShaderUtil.CheckGLError(TAG, "Program creation"); // Read the texture. var textureBitmap = Android.Graphics.BitmapFactory.DecodeStream( context.Assets.Open(gridDistanceTextureName)); GLES20.GlActiveTexture(GLES20.GlTexture0); GLES20.GlGenTextures(mTextures.Length, mTextures, 0); GLES20.GlBindTexture(GLES20.GlTexture2d, mTextures[0]); 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, "Texture loading"); mPlaneXZPositionAlphaAttribute = GLES20.GlGetAttribLocation(mPlaneProgram, "a_XZPositionAlpha"); mPlaneModelUniform = GLES20.GlGetUniformLocation(mPlaneProgram, "u_Model"); mPlaneModelViewProjectionUniform = GLES20.GlGetUniformLocation(mPlaneProgram, "u_ModelViewProjection"); mTextureUniform = GLES20.GlGetUniformLocation(mPlaneProgram, "u_Texture"); mLineColorUniform = GLES20.GlGetUniformLocation(mPlaneProgram, "u_lineColor"); mDotColorUniform = GLES20.GlGetUniformLocation(mPlaneProgram, "u_dotColor"); mGridControlUniform = GLES20.GlGetUniformLocation(mPlaneProgram, "u_gridControl"); mPlaneUvMatrixUniform = GLES20.GlGetUniformLocation(mPlaneProgram, "u_PlaneUvMatrix"); ShaderUtil.CheckGLError(TAG, "Program parameters"); }
/// <summary> /// Create a shader program to read the data of the virtual object. /// This method is called when WorldRenderManager's OnSurfaceCreated. /// </summary> /// <param name="context">Context.</param> public void Init(Context context) { ShaderUtil.CheckGlError(TAG, "Init start."); CreateProgram(); // Coordinate and index. int[] buffers = new int[2]; GLES20.GlGenBuffers(2, buffers, 0); mVertexBufferId = buffers[0]; mIndexBufferId = buffers[1]; GLES20.GlActiveTexture(GLES20.GlTexture0); GLES20.GlGenTextures(mTextures.Length, mTextures, 0); GLES20.GlBindTexture(GLES20.GlTexture2d, mTextures[0]); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinearMipmapLinear); InitGlTextureData(context); InitializeGlObjectData(context); ShaderUtil.CheckGlError(TAG, "Init end."); }
private int createTexture(int width, int height) { int[] textureIds = new int[1]; GLES20.GlGenTextures(1, textureIds, 0); GLES20.GlBindTexture(3553, textureIds[0]); GLES20.GlTexParameteri(3553, 10242, 33071); GLES20.GlTexParameteri(3553, 10243, 33071); GLES20.GlTexParameteri(3553, 10240, 9729); GLES20.GlTexParameteri(3553, 10241, 9729); GLES20.GlTexImage2D(3553, 0, 6407, width, height, 0, 6407, 33635, null); return(textureIds[0]); }
private void SetupImage() { // Create our UV coordinates. uvs = new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f }; // The texture buffer ByteBuffer byteBuffer = ByteBuffer.AllocateDirect(uvs.Length * 4); byteBuffer.Order(ByteOrder.NativeOrder()); uvBuffer = byteBuffer.AsFloatBuffer(); uvBuffer.Put(uvs); uvBuffer.Position(0); // Generate Textures, if more needed, alter these numbers. int[] textureIds = new int[1]; GLES20.GlGenTextures(1, textureIds, 0); // Retrieve our image from resources. Bitmap bitmap = BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.plant07); // Bind texture to texturename GLES20.GlActiveTexture(GLES20.GlTexture0); GLES20.GlBindTexture(GLES20.GlTexture2d, textureIds[0]); // Set filtering GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear); // Set wrapping mode GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); // Load the bitmap into the bound texture. GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bitmap, 0); // We are done using the bitmap so we should recycle it. bitmap.Recycle(); bitmap.Dispose(); }
public static int loadTextureFromByteBuffer(ByteBuffer data, int width, int height) { try { int[] gl_textureID = new int[1]; GLES20.GlGenTextures(1, gl_textureID, 0); GLES20.GlBindTexture(GLES20.GlTexture2d, gl_textureID[0]); GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear); GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLES20.GlTexImage2D(GLES20.GlTexture2d, 0, GLES20.GlRgba, width, height, 0, GLES20.GlRgba, GLES20.GlUnsignedByte, data); return(gl_textureID[0]); } catch (Exception e) { //Log.e("RenderUtils", "loadTextureFromApk failed to load texture '" + fileName + "' from APK with error " + e.getMessage()); return(-1); } }
void LoadTextures() { // Generate textures GLES20.GlGenTextures(2, _textures, 0); // Load input bitmap var bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Lenna); _imageWidth = bitmap.Width; _imageHeight = bitmap.Height; Renderer.UpdateTextureSize(_imageWidth, _imageHeight); // Upload to texture GLES20.GlBindTexture(GLES20.GlTexture2d, _textures [0]); GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bitmap, 0); // Set texture parameters GLToolbox.InitTexParams(); }
/** * Initializes FBO with given parameters. Width and height are used to * generate textures out of which all are sized same to this FBO. If you * give genRenderBuffer a value 'true', depth buffer will be generated also. * * @param width * FBO width in pixels * @param height * FBO height in pixels * @param textureCount * Number of textures to generate * @param genDepthBuffer * If true, depth buffer is allocated for this FBO @ param * genStencilBuffer If true, stencil buffer is allocated for this * FBO */ public void init(int width, int height, int textureCount, bool textureExternalOES) { // Just in case. Reset(); // Store FBO size. _width = width; _height = height; // Genereta FBO. var handle = new int[] { 0 }; GLES20.GlGenFramebuffers(1, handle, 0); _frameBufferHandle = handle[0]; GLES20.GlBindFramebuffer(GLES20.GlFramebuffer, _frameBufferHandle); // Generate textures. _textureHandles = new int[textureCount]; GLES20.GlGenTextures(textureCount, _textureHandles, 0); var target = textureExternalOES ? GLES11Ext.GlTextureExternalOes : GLES20.GlTexture2d; foreach (var texture in _textureHandles) { GLES20.GlBindTexture(target, texture); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(target, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); GLES20.GlTexParameteri(target, GLES20.GlTextureMinFilter, GLES20.GlNearest); GLES20.GlTexParameteri(target, GLES20.GlTextureMagFilter, GLES20.GlLinear); if (target == GLES20.GlTexture2d) { GLES20.GlTexImage2D(GLES20.GlTexture2d, 0, GLES20.GlRgba, _width, _height, 0, GLES20.GlRgba, GLES20.GlUnsignedByte, null); } } }
/// <summary> /// Inform this View of the dimensions of frames coming from |stream|. </summary> public virtual void setSize(Endpoint stream, int width, int height) { // Generate 3 texture ids for Y/U/V and place them into |textures|, // allocating enough storage for |width|x|height| pixels. int[] textures = yuvTextures[stream == Endpoint.LOCAL ? 0 : 1]; GLES20.GlGenTextures(3, textures, 0); for (int i = 0; i < 3; ++i) { int w = i == 0 ? width : width / 2; int h = i == 0 ? height : height / 2; GLES20.GlActiveTexture(GLES20.GlTexture0 + i); GLES20.GlBindTexture(GLES20.GlTexture2d, textures[i]); GLES20.GlTexImage2D(GLES20.GlTexture2d, 0, GLES20.GlLuminance, w, h, 0, GLES20.GlLuminance, GLES20.GlUnsignedByte, null); GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear); GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); } checkNoGLES2Error(); }
private void SetCamera() { if (isOpenCameraOutside && mCamera == null) { Log.Debug(TAG, "new Camera"); DisplayMetrics dm = new DisplayMetrics(); mCamera = new CameraHelper(this); mCamera.SetupCamera(dm.WidthPixels, dm.HeightPixels); } // Check whether setCamera is called for the first time. if (isOpenCameraOutside) { if (textureId != -1) { mArSession.SetCameraTextureName(textureId); InitSurface(); } else { int[] textureIds = new int[1]; GLES20.GlGenTextures(1, textureIds, 0); textureId = textureIds[0]; mArSession.SetCameraTextureName(textureId); InitSurface(); } SurfaceTexture surfaceTexture = new SurfaceTexture(textureId); mCamera.setPreviewTexture(surfaceTexture); mCamera.setPreViewSurface(mPreViewSurface); mCamera.setVgaSurface(mVgaSurface); mCamera.setDepthSurface(mDepthSurface); if (!mCamera.OpenCamera()) { string showMessage = "Open camera filed!"; Log.Debug(TAG, showMessage); Toast.MakeText(this, showMessage, ToastLength.Long).Show(); Finish(); } } }
public void StartCapture(int width, int height, int fps) { this.width = width; this.height = height; textureHelper.SetTextureSize(width, height); int[] textures = new int[1]; GLES20.GlGenTextures(1, textures, 0); textureId = textures[0]; GLES20.GlBindTexture(GLES20.GlTexture2d, textureId); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlNearest); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlNearest); //var matrix = new Android.Graphics.Matrix(); //matrix.PreTranslate(0.5f, 0.5f); //matrix.PreScale(-1f, -1f); //matrix.PreTranslate(-0.5f, -0.5f); //YuvConverter yuvConverter = new YuvConverter(); //buffer = new TextureBufferImpl( // width, // height, // VideoFrame.TextureBufferType.Oes, // 1, // matrix, // textureHelper.Handler, // yuvConverter, // null); //capturerObserver.OnCapturerStarted(true); startTime = DateTime.Now; //imageSource.ImageAvailable += ImageSource_ImageAvailable; imageSource.VideoFrameAvailable += ImageSource_VideoFrameAvailable; //imageSource.Texture2dAvailable += ImageSource_Texture2dAvailable; imageSource.Start(); }
public static int LoadGlTexture(Resources res, int resId) { var texture = new int[1]; GLES20.GlGenTextures(1, texture, 0); if (texture [0] == 0) { throw new InvalidOperationException("Can't create texture"); } var options = new Android.Graphics.BitmapFactory.Options { InScaled = false }; var bmp = Android.Graphics.BitmapFactory.DecodeResource(res, resId, options); GLES20.GlBindTexture(GLES20.GlTexture2d, texture [0]); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlNearest); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlNearest); GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bmp, 0); bmp.Recycle(); return(texture [0]); }
private void create(int width, int height) { // FBO int[] ret = new int[1]; // frame buffer GLES20.GlGenFramebuffers(1, ret, 0); FBO = ret[0]; GLES20.GlBindFramebuffer(GLES20.GlFramebuffer, FBO); // depth buffer GLES20.GlGenRenderbuffers(1, ret, 0); RBOD = ret[0]; GLES20.GlBindRenderbuffer(GLES20.GlFramebuffer, RBOD); GLES20.GlRenderbufferStorage(GLES20.GlRenderbuffer, GLES20.GlDepthComponent16, width, height); GLES20.GlFramebufferRenderbuffer(GLES20.GlFramebuffer, GLES20.GlDepthAttachment, GLES20.GlRenderbuffer, RBOD); // color buffer (is texture) GLES20.GlPixelStorei(GLES20.GlUnpackAlignment, 1); GLES20.GlGenTextures(1, ret, 0); RBOC = ret[0]; GLES20.GlBindTexture(GLES20.GlTexture2d, RBOC); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear); GLES20.GlTexImage2D(GLES20.GlTexture2d, 0, GLES20.GlRgba, width, height, 0, GLES20.GlRgba, GLES20.GlUnsignedByte, null); GLES20.GlFramebufferTexture2D(GLES20.GlFramebuffer, GLES20.GlColorAttachment0, GLES20.GlTexture2d, RBOC, 0); if (GLES20.GlCheckFramebufferStatus(GLES20.GlFramebuffer) != GLES20.GlFramebufferComplete) { Log.Debug(TAG, "Fail to create FBO."); FBO = 0; RBOD = 0; RBOC = 0; } }
/** * \brief Load a texture from a data buffer and create related OpenGL structures. * @param data texture data buffer as int array. * @param width texture width. * @param height texture height * @return gl texture id. */ private static int loadTextureFromIntBuffer(int[] data, int width, int height) { // convert from int array to byte RGBA array int numPixels = width * height; byte[] dataBytes = new byte[numPixels * 4]; for (int p = 0; p < numPixels; ++p) { int colour = data[p]; dataBytes[p * 4] = (byte)(colour >> 16); // R dataBytes[p * 4 + 1] = (byte)(colour >> 8); // G dataBytes[p * 4 + 2] = (byte)colour; // B dataBytes[p * 4 + 3] = (byte)(colour >> 24); // A } // put data into a direct memory byte buffer ByteBuffer bb_data = ByteBuffer.AllocateDirect(dataBytes.Length).Order(ByteOrder.NativeOrder()); int rowSize = width * 4; for (int r = 0; r < height; r++) { bb_data.Put(dataBytes, rowSize * (height - 1 - r), rowSize); } bb_data.Rewind(); // cleans variables dataBytes = null; data = null; // create gl texture and upload data int[] gl_textureID = new int[1]; GLES20.GlGenTextures(1, gl_textureID, 0); GLES20.GlBindTexture(GLES20.GlTexture2d, gl_textureID[0]); GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear); GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear); GLES20.GlTexImage2D(GLES20.GlTexture2d, 0, GLES20.GlRgba, width, height, 0, GLES20.GlRgba, GLES20.GlUnsignedByte, bb_data); return(gl_textureID[0]); }
/** * Creates and initializes OpenGL resources needed for rendering the model. * * @param context Context for loading the shader and below-named model and texture assets. * @param objAssetName Name of the OBJ file containing the model geometry. * @param diffuseTextureAssetName Name of the PNG file containing the diffuse texture map. */ public void CreateOnGlThread(Context context, string objAssetName, string diffuseTextureAssetName) { // Read the texture. var textureBitmap = BitmapFactory.DecodeStream(context.Assets.Open(diffuseTextureAssetName)); GLES20.GlActiveTexture(GLES20.GlTexture0); GLES20.GlGenTextures(mTextures.Length, mTextures, 0); GLES20.GlBindTexture(GLES20.GlTexture2d, mTextures[0]); 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); textureBitmap.Recycle(); ShaderUtil.CheckGLError(TAG, "Texture loading"); // Read the obj file. var objInputStream = context.Assets.Open(objAssetName); var obj = ObjReader.Read(objInputStream); // Prepare the Obj so that its structure is suitable for // rendering with OpenGL: // 1. Triangulate it // 2. Make sure that texture coordinates are not ambiguous // 3. Make sure that normals are not ambiguous // 4. Convert it to single-indexed data obj = ObjUtils.ConvertToRenderable(obj); // OpenGL does not use Java arrays. ByteBuffers are used instead to provide data in a format // that OpenGL understands. // Obtain the data from the OBJ, as direct buffers: IntBuffer wideIndices = ObjData.GetFaceVertexIndices(obj, 3); FloatBuffer vertices = ObjData.GetVertices(obj); FloatBuffer texCoords = ObjData.GetTexCoords(obj, 2); FloatBuffer normals = ObjData.GetNormals(obj); // Convert int indices to shorts for GL ES 2.0 compatibility ShortBuffer indices = ByteBuffer.AllocateDirect(2 * wideIndices.Limit()) .Order(ByteOrder.NativeOrder()).AsShortBuffer(); while (wideIndices.HasRemaining) { indices.Put((short)wideIndices.Get()); } indices.Rewind(); var buffers = new int[2]; GLES20.GlGenBuffers(2, buffers, 0); mVertexBufferId = buffers[0]; mIndexBufferId = buffers[1]; // Load vertex buffer mVerticesBaseAddress = 0; mTexCoordsBaseAddress = mVerticesBaseAddress + 4 * vertices.Limit(); mNormalsBaseAddress = mTexCoordsBaseAddress + 4 * texCoords.Limit(); int totalBytes = mNormalsBaseAddress + 4 * normals.Limit(); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVertexBufferId); GLES20.GlBufferData(GLES20.GlArrayBuffer, totalBytes, null, GLES20.GlStaticDraw); GLES20.GlBufferSubData( GLES20.GlArrayBuffer, mVerticesBaseAddress, 4 * vertices.Limit(), vertices); GLES20.GlBufferSubData( GLES20.GlArrayBuffer, mTexCoordsBaseAddress, 4 * texCoords.Limit(), texCoords); GLES20.GlBufferSubData( GLES20.GlArrayBuffer, mNormalsBaseAddress, 4 * normals.Limit(), normals); GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0); // Load index buffer GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, mIndexBufferId); mIndexCount = indices.Limit(); GLES20.GlBufferData( GLES20.GlElementArrayBuffer, 2 * mIndexCount, indices, GLES20.GlStaticDraw); GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, 0); ShaderUtil.CheckGLError(TAG, "OBJ buffer load"); int vertexShader = ShaderUtil.LoadGLShader(TAG, context, GLES20.GlVertexShader, Resource.Raw.object_vertex); int fragmentShader = ShaderUtil.LoadGLShader(TAG, context, GLES20.GlFragmentShader, Resource.Raw.object_fragment); mProgram = GLES20.GlCreateProgram(); GLES20.GlAttachShader(mProgram, vertexShader); GLES20.GlAttachShader(mProgram, fragmentShader); GLES20.GlLinkProgram(mProgram); GLES20.GlUseProgram(mProgram); ShaderUtil.CheckGLError(TAG, "Program creation"); mModelViewUniform = GLES20.GlGetUniformLocation(mProgram, "u_ModelView"); mModelViewProjectionUniform = GLES20.GlGetUniformLocation(mProgram, "u_ModelViewProjection"); mPositionAttribute = GLES20.GlGetAttribLocation(mProgram, "a_Position"); mNormalAttribute = GLES20.GlGetAttribLocation(mProgram, "a_Normal"); mTexCoordAttribute = GLES20.GlGetAttribLocation(mProgram, "a_TexCoord"); mTextureUniform = GLES20.GlGetUniformLocation(mProgram, "u_Texture"); mLightingParametersUniform = GLES20.GlGetUniformLocation(mProgram, "u_LightingParameters"); mMaterialParametersUniform = GLES20.GlGetUniformLocation(mProgram, "u_MaterialParameters"); ShaderUtil.CheckGLError(TAG, "Program parameters"); Android.Opengl.Matrix.SetIdentityM(mModelMatrix, 0); }