/// <summary>
        /// Allocates and initializes OpenGL resources needed by the plane renderer.  Must be
        /// called on the OpenGL thread, typically in
        /// {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}.
        /// </summary>
        public void CreateOnGlThread(Context context)
        {
            //ShaderHelper.CheckGLError(TAG, "before create");

            var buffers = new int[1];

            GLES20.GlGenBuffers(1, buffers, 0);
            mVbo = buffers[0];
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVbo);

            mVboSize = INITIAL_BUFFER_POINTS * BYTES_PER_POINT;
            GLES20.GlBufferData(GLES20.GlArrayBuffer, mVboSize, null, GLES20.GlDynamicDraw);
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);

            var vertexShader = ShaderHelper.Load(TAG, context,
                                                 GLES20.GlVertexShader, Resource.Raw.point_cloud_vertex);
            var passthroughShader = ShaderHelper.Load(TAG, context,
                                                      GLES20.GlFragmentShader, Resource.Raw.passthrough_fragment);

            mProgramName = GLES20.GlCreateProgram();
            GLES20.GlAttachShader(mProgramName, vertexShader);
            GLES20.GlAttachShader(mProgramName, passthroughShader);
            GLES20.GlLinkProgram(mProgramName);
            GLES20.GlUseProgram(mProgramName);


            mPositionAttribute          = GLES20.GlGetAttribLocation(mProgramName, "a_Position");
            mColorUniform               = GLES20.GlGetUniformLocation(mProgramName, "u_Color");
            mModelViewProjectionUniform = GLES20.GlGetUniformLocation(
                mProgramName, "u_ModelViewProjection");
            mPointSizeUniform = GLES20.GlGetUniformLocation(mProgramName, "u_PointSize");
        }
        /// <summary>
        ///  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.
        /// </summary>
        public void CreateOnGlThread(Context context)
        {
            // Generate the background texture.
            var textures = new int[1];

            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);

            var 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();

            var vertexShader   = ShaderHelper.Load(TAG, context, GLES20.GlVertexShader, Resource.Raw.screenquad_vertex);
            var fragmentShader = ShaderHelper.Load(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);

            ShaderHelper.CheckError(TAG, "Program creation");

            mQuadPositionParam = GLES20.GlGetAttribLocation(mQuadProgram, "a_Position");
            mQuadTexCoordParam = GLES20.GlGetAttribLocation(mQuadProgram, "a_TexCoord");

            ShaderHelper.CheckError(TAG, "Program parameters");
        }
        /// <summary>
        /// Allocates and initializes OpenGL resources needed by the plane renderer.  Must be
        /// called on the OpenGL thread, typically in
        /// {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}.
        /// @param context Needed to access shader source and texture PNG.
        /// @param gridDistanceTextureName  Name of the PNG file containing the grid texture.
        /// </summary>
        public void CreateOnGlThread(Context context, String gridDistanceTextureName)
        {
            int vertexShader = ShaderHelper.Load(TAG, context,
                                                 GLES20.GlVertexShader, Resource.Raw.plane_vertex);
            int passthroughShader = ShaderHelper.Load(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);

            // 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);

            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");
        }
Esempio n. 4
0
        //Camera modelRotationCamera;
        //SatelliteRotator modelRotator;
        //private float rotateAngle;

        public FormNewNormalLine()
        {
            InitializeComponent();

            this.camera          = new Camera(CameraType.Perspecitive, this.glCanvas1.Width, this.glCanvas1.Height);
            this.camera.Position = new GLM.vec3(50, 5, 5);

            this.cameraRotator = new SatelliteRotator(this.camera);

            //this.modelRotationCamera = new Camera(CameraType.Perspecitive, this.glCanvas1.Width, this.glCanvas1.Height);
            //this.modelRotator = new SatelliteRotator(this.modelRotationCamera);
            this.modelRotator = new ArcBallRotator(this.camera);

            {
                const string           strGround = "Ground";
                IConvert2BufferPointer model     = new Ground();
                CodeShader[]           shaders   = new CodeShader[2];
                shaders[0] = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadShaderSourceCode(strGround, CodeShader.GLSLShaderType.VertexShader);
                shaders[1] = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadShaderSourceCode(strGround, CodeShader.GLSLShaderType.FragmentShader);
                PropertyNameMap propertyNameMap = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadPropertyNameMap(strGround);
                UniformNameMap  uniformNameMap  = CSharpGL.Objects.Common.ShaderLoadingHelper.LoadUniformNameMap(strGround);
                this.groundRenderer = new ModernRenderer(model, shaders, propertyNameMap, uniformNameMap);
                this.groundRenderer.Initialize();
            }

            Padding        uiPadding = new System.Windows.Forms.Padding(10, 10, 10, 10);
            Size           uiSize    = new System.Drawing.Size(50, 50);
            IUILayoutParam uiParam   = new IUILayoutParam(AnchorStyles.Left | AnchorStyles.Bottom, uiPadding, uiSize);

            uiAxis = new SimpleUIAxis(uiParam);
            uiAxis.Initialize();

            //IModel model = (new TeapotFactory()).Create(1.0f);
            {
                IConvert2BufferPointer model       = new NewNormalLineDemoModel();
                CodeShader[]           codeShaders = new CodeShader[3];
                codeShaders[0] = new CodeShader(ShaderHelper.Load("NewNormalLine.vert"), CodeShader.GLSLShaderType.VertexShader);
                codeShaders[1] = new CodeShader(ShaderHelper.Load("NewNormalLine.geom"), CodeShader.GLSLShaderType.GeometryShader);
                codeShaders[2] = new CodeShader(ShaderHelper.Load("NewNormalLine.frag"), CodeShader.GLSLShaderType.FragmentShader);
                var propertyNameMap = new PropertyNameMap();
                propertyNameMap.Add("in_Position", NewNormalLineDemoModel.strPosition);
                propertyNameMap.Add("in_Normal", NewNormalLineDemoModel.strNormal);
                var uniformNameMap = new UniformNameMap();
                uniformNameMap.Add("model matrix", "modelMatrix");
                uniformNameMap.Add("view matrix", "viewMatrix");
                uniformNameMap.Add("projection matrix", "projectionMatrix");
                uniformNameMap.Add("show model", "showModel");
                uniformNameMap.Add("show normal", "showNormal");
                uniformNameMap.Add("normal length", "normalLength");
                this.renderer = new ModernRenderer(model, codeShaders, propertyNameMap, uniformNameMap);
                this.renderer.Initialize();//不在此显式初始化也可以。
                this.renderer.SetUniformValue("show model", 1.0f);
                this.renderer.SetUniformValue("show normal", 1.0f);
                this.renderer.SetUniformValue("normal length", 1.0f);
            }

            //this.demoLifebar = new DemoLifeBar(0.2f, 0.02f, 4);
            //this.demolifebarRenderer = new NormalLineRenderer(this.demoLifebar);
            //this.demolifebarRenderer.Initialize();

            this.lifebarRenderer = new LifeBarRenderer(new LifeBar(2f, 0.2f, 4f));
            this.lifebarRenderer.Initialize();

            //StringModel stringModel = DummyStringModelFactory.GetModel("teapot");
            this.stringRenderer = new StringRenderer("teapot".GetDummyModel());
            this.stringRenderer.Initialize();

            uiParam    = new IUILayoutParam(AnchorStyles.Right | AnchorStyles.Bottom, new Padding(10, 10, 120, 10), new Size(50, 50));
            this.label = new DummyLabel(uiParam, "Hello Label!");
            this.label.Initialize();

            this.glCanvas1.MouseWheel += glCanvas1_MouseWheel;
            this.glCanvas1.KeyPress   += glCanvas1_KeyPress;
            this.glCanvas1.MouseDown  += glCanvas1_MouseDown;
            this.glCanvas1.MouseMove  += glCanvas1_MouseMove;
            this.glCanvas1.MouseUp    += glCanvas1_MouseUp;
            this.glCanvas1.OpenGLDraw += glCanvas1_OpenGLDraw;
            this.glCanvas1.Resize     += glCanvas1_Resize;

            Application.Idle += Application_Idle;
        }
Esempio n. 5
0
        /// <summary>
        /// Creates and initializes OpenGL resources needed for rendering the model.
        /// </summary>
        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();

            // ShaderHelper.CheckError(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:
            var wideIndices = ObjData.GetFaceVertexIndices(obj, 3);
            var vertices    = ObjData.GetVertices(obj);
            var texCoords   = ObjData.GetTexCoords(obj, 2);
            var normals     = ObjData.GetNormals(obj);

            // Convert int indices to shorts for GL ES 2.0 compatibility
            var 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();
            var 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);

            //ShaderHelper.CheckGLError(TAG, "OBJ buffer load");
            var vertexShader   = ShaderHelper.Load(TAG, context, GLES20.GlVertexShader, Resource.Raw.object_vertex);
            var fragmentShader = ShaderHelper.Load(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);

            //ShaderHelper.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");

            //ShaderHelper.CheckGLError(TAG, "Program parameters");

            Android.Opengl.Matrix.SetIdentityM(mModelMatrix, 0);
        }