コード例 #1
0
 public void Activate()
 {
     if (s_activeProgram != this) {
         s_activeProgram = this;
         GL.UseProgram (m_programID);
     }
 }
コード例 #2
0
            public void renderFaceAveragedVertexNormals()
            {
                Vector3[] perVertexNormals = new Vector3[_runtimeMesh.numVertices];

                for (int i = 0; i < _runtimeMesh.numTriangles; i++)
                {
                    int     baseIdx = i * 3;
                    Vector3 p0      = _runtimeMesh.computeVertexPosFromTriIndex(baseIdx);
                    Vector3 p1      = _runtimeMesh.computeVertexPosFromTriIndex(baseIdx + 1);
                    Vector3 p2      = _runtimeMesh.computeVertexPosFromTriIndex(baseIdx + 2);

                    Vector3 face_normal = Vector3.Cross(p1 - p0, p2 - p0).Normalized();

                    int v0 = _runtimeMesh.indices[baseIdx];
                    int v1 = _runtimeMesh.indices[baseIdx + 1];
                    int v2 = _runtimeMesh.indices[baseIdx + 2];

                    perVertexNormals[v0] += face_normal;
                    perVertexNormals[v1] += face_normal;
                    perVertexNormals[v2] += face_normal;
                }

                // render face averaged vertex normals

                SSShaderProgram.DeactivateAll();
                GL.Color4(Color4.Yellow);
                for (int v = 0; v < perVertexNormals.Length; v++)
                {
                    GL.Begin(PrimitiveType.Lines);
                    GL.Vertex3(_runtimeMesh.computeVertexPos(v));
                    GL.Vertex3(_runtimeMesh.computeVertexPos(v) + perVertexNormals[v].Normalized() * 0.5f);
                    GL.End();
                }
            }
コード例 #3
0
            public override void renderMesh(SSRenderConfig renderConfig)
            {
                base.renderMesh(renderConfig);

                // debugging vertex normals...
                                #if false
                {
                    // do not change the order!!
                    renderFaceNormals();                           // these are correct..
                    renderFaceAveragedVertexNormals();             // these are correct..
                    // renderBindPoseVertexNormals ();
                    renderAnimatedVertexNormals();                 // these are currently WRONG
                }
                                #endif

                                #if false
                SSShaderProgram.DeactivateAll();
                // bounding box debugging
                GL.PolygonMode(MaterialFace.Front, PolygonMode.Line);
                GL.Disable(EnableCap.Texture2D);
                GL.Translate(aabb.Center());
                GL.Scale(aabb.Diff());
                GL.Color4(1f, 0f, 0f, 0.1f);
                SSTexturedCube.Instance.DrawArrays(ref renderConfig, PrimitiveType.Triangles);
                GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
                                #endif
            }
コード例 #4
0
        public override void Render(SSRenderConfig renderConfig)
        {
            base.Render(renderConfig);

            // mode setup
            SSShaderProgram.DeactivateAll();             // disable GLSL

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.Enable(EnableCap.Texture2D);
            GL.BindTexture(TextureTarget.Texture2D, 0);              // reset first
            GL.BindTexture(TextureTarget.Texture2D, GLu_textureID);  // now bind the shadowmap texture id

            // GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Nearest);
            // GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest);

            // draw quad...
            GL.Begin(PrimitiveType.Triangles);

            float w = 500;
            float h = 500;

            // upper-left
            GL.TexCoord2(0.0, 0.0); GL.Vertex3(0.0, 0.0, 0.0);
            GL.TexCoord2(0.0, 1.0); GL.Vertex3(0.0, h, 0.0);
            GL.TexCoord2(1.0, 0.0); GL.Vertex3(w, 0.0, 0.0);

            // lower-right
            GL.TexCoord2(0.0, 1.0); GL.Vertex3(0.0, h, 0.0);
            GL.TexCoord2(1.0, 1.0); GL.Vertex3(w, h, 0.0);
            GL.TexCoord2(1.0, 0.0); GL.Vertex3(w, 0.0, 0.0);

            GL.End();
        }
コード例 #5
0
        private void _RenderLines_ICO(ref SSRenderConfig renderConfig)
        {
            // mode setup
            SSShaderProgram.DeactivateAll();             // disable GLSL
            GL.Disable(EnableCap.CullFace);
            GL.Disable(EnableCap.Texture2D);
            GL.Disable(EnableCap.Blend);
            GL.Disable(EnableCap.Lighting);

            if (icoSphereVertices == null)
            {
                var icoSphereCreator  = new IcoSphereCreator();
                var icoSphereGeometry = icoSphereCreator.Create(3);
                icoSphereVertices = icoSphereGeometry.Positions.ToArray();
                icoSphereFaces    = icoSphereGeometry.Faces.ToArray();
            }

            GL.Begin(PrimitiveType.Lines);
            GL.LineWidth(1.0f);
            GL.Color3(1.0f, 1.0f, 1.0f);

            foreach (var face in icoSphereFaces)
            {
                var v1 = icoSphereVertices[face.v1];
                var v2 = icoSphereVertices[face.v2];
                var v3 = icoSphereVertices[face.v3];

                GL.Vertex3(v1);  GL.Vertex3(v2);
                GL.Vertex3(v2);  GL.Vertex3(v3);
                GL.Vertex3(v1);  GL.Vertex3(v3);
            }
            GL.End();
        }
コード例 #6
0
ファイル: SSMesh_wfOBJ.cs プロジェクト: wumohai/SimpleScene
 private void _renderSetupWireframe()
 {
     SSShaderProgram.DeactivateAll();
     GL.Disable(EnableCap.Texture2D);
     GL.Disable(EnableCap.Blend);
     GL.Disable(EnableCap.Lighting);
 }
コード例 #7
0
 public static void DeactivateAll()
 {
     if (s_activeProgram != null)
     {
         s_activeProgram = null;
         GL.UseProgram(0);
     }
 }
コード例 #8
0
 public void Activate()
 {
     if (s_activeProgram != this)
     {
         s_activeProgram = this;
         GL.UseProgram(m_programID);
     }
 }
コード例 #9
0
        public virtual void Render(SSRenderConfig renderConfig)
        {
            // compute and set the modelView matrix, by combining the cameraViewMat
            // with the object's world matrix
            //    ... http://www.songho.ca/opengl/gl_transform.html
            //    ... http://stackoverflow.com/questions/5798226/3d-graphics-processing-how-to-calculate-modelview-matrix
            renderBoundingSphereMesh(renderConfig);

            Matrix4 modelViewMat = this.worldMat * renderConfig.invCameraViewMatrix;

            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelViewMat);

            resetTexturingState();

            if (renderConfig.drawingShadowMap)
            {
                if (renderConfig.drawingPssm && renderConfig.pssmShader != null)
                {
                    renderConfig.pssmShader.Activate();
                    renderConfig.pssmShader.UniObjectWorldTransform = this.worldMat;
                }
                else
                {
                    SSShaderProgram.DeactivateAll();
                }
            }
            else
            {
                if (renderConfig.mainShader != null)
                {
                    setDefaultShaderState(renderConfig.mainShader);
                }
                setMaterialState(renderConfig.mainShader);

                if (alphaBlendingEnabled)
                {
                    GL.Enable(EnableCap.Blend);
                    GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                }
                else
                {
                    GL.Disable(EnableCap.Blend);
                }

                if (this.renderState.lighted)
                {
                    GL.Enable(EnableCap.Lighting);
                    GL.ShadeModel(ShadingModel.Flat);
                }
                else
                {
                    GL.Disable(EnableCap.Lighting);
                }
            }
        }
コード例 #10
0
        public override void Render(ref SSRenderConfig renderConfig)
        {
            UpdateTexture();

            base.Render(ref renderConfig);

            SSShaderProgram.DeactivateAll();             // disable GLSL

            // mode setup
            // GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);

            // Step 2: setup our material mode and paramaters...
            GL.Disable(EnableCap.CullFace);

            GL.Disable(EnableCap.Lighting);
            // enable alpha blending
            {
                GL.Enable(EnableCap.AlphaTest);
                GL.Enable(EnableCap.Blend);
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            }


            // fixed function single-texture
            GL.ActiveTexture(TextureUnit.Texture0);
            GL.Enable(EnableCap.Texture2D);
            GL.BindTexture(TextureTarget.Texture2D, textureSurface.TextureID);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest);

            // draw text rectangle...
            GL.Begin(BeginMode.Triangles);
            GL.Color3(Color.White);  // clear the vertex color to white..

            float w = gdiSize.Width;
            float h = gdiSize.Height;

            if (gdiSize != textureSize)
            {
                // adjust texture coordinates
                throw new Exception("not implemented");
            }

            // upper-left
            GL.TexCoord2(0.0, 0.0); GL.Vertex3(0.0, 0.0, 0.0);
            GL.TexCoord2(1.0, 0.0); GL.Vertex3(w, 0.0, 0.0);
            GL.TexCoord2(0.0, 1.0); GL.Vertex3(0.0, h, 0.0);

            // lower-right
            GL.TexCoord2(0.0, 1.0); GL.Vertex3(0.0, h, 0.0);
            GL.TexCoord2(1.0, 0.0); GL.Vertex3(w, 0.0, 0.0);
            GL.TexCoord2(1.0, 1.0); GL.Vertex3(w, h, 0.0);

            GL.End();
        }
コード例 #11
0
 private void renderBindPoseVertexNormals()
 {
     SSShaderProgram.DeactivateAll();
     GL.Color4(Color4.White);
     for (int v = 0; v < _vertices.Length; ++v)
     {
         GL.Begin(PrimitiveType.Lines);
         GL.Vertex3(_vertices [v].Position);
         GL.Vertex3(_vertices [v].Position + _runtimeMesh.bindPoseNormal(v) * 0.3f);
         GL.End();
     }
 }
コード例 #12
0
 private void renderAnimatedVertexNormals()
 {
     SSShaderProgram.DeactivateAll();
     GL.Color4(Color4.Magenta);
     for (int v = 0; v < _vertices.Length; ++v)
     {
         GL.Begin(PrimitiveType.Lines);
         GL.Vertex3(_vertices[v].Position);
         GL.Vertex3(_vertices[v].Position + _vertices[v].Normal * 0.2f);
         GL.End();
     }
 }
コード例 #13
0
        public override void renderMesh(SSRenderConfig renderConfig)
        {
            SSShaderProgram.DeactivateAll();

            GL.Disable(EnableCap.Lighting);
            GL.Disable(EnableCap.ColorMaterial);
            GL.Enable(EnableCap.AlphaTest);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            base.renderMesh(renderConfig);
        }
コード例 #14
0
        public override void Render(SSRenderConfig renderConfig)
        {
            UpdateTexture();

            base.Render(renderConfig);

            SSShaderProgram.DeactivateAll();              // disable GLSL

            // mode setup
            // GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);

            // Step 2: setup our material mode and paramaters...

            GL.Disable(EnableCap.Lighting);

            // setup our texture source
            if (textureSurface != null)
            {
                GL.ActiveTexture(TextureUnit.Texture0);
                GL.Enable(EnableCap.Texture2D);
                GL.BindTexture(TextureTarget.Texture2D, textureSurface.TextureID);
            }

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest);

            // draw text rectangle...
            GL.Begin(PrimitiveType.Triangles);

            float w = gdiSize.Width;
            float h = gdiSize.Height;

            if (gdiSize != textureSize)
            {
                // adjust texture coordinates
                throw new Exception("not implemented");
            }

            // upper-left
            GL.TexCoord2(0.0, 0.0); GL.Vertex3(0.0, 0.0, 0.0);
            GL.TexCoord2(0.0, 1.0); GL.Vertex3(0.0, h, 0.0);
            GL.TexCoord2(1.0, 0.0); GL.Vertex3(w, 0.0, 0.0);

            // lower-right
            GL.TexCoord2(0.0, 1.0); GL.Vertex3(0.0, h, 0.0);
            GL.TexCoord2(1.0, 1.0); GL.Vertex3(w, h, 0.0);
            GL.TexCoord2(1.0, 0.0); GL.Vertex3(w, 0.0, 0.0);

            GL.End();
        }
コード例 #15
0
ファイル: SSObjectRay.cs プロジェクト: wumohai/SimpleScene
        public override void Render(SSRenderConfig renderConfig)
        {
            base.Render(renderConfig);

            // mode setup
            SSShaderProgram.DeactivateAll();             // disable GLSL
            GL.Disable(EnableCap.Texture2D);

            GL.LineWidth(5.0f);

            GL.Begin(PrimitiveType.Lines);
            GL.Color3(1.0f, 1f, 1f);   GL.Vertex3(0, 0, 0);
            GL.Color3(1.0f, 0.5f, 0.5f);   GL.Vertex3(this.ray.dir * 10.0f);
            GL.End();
        }
コード例 #16
0
                public override void Render(SSRenderConfig renderConfig)
                {
                    this.Pos = _missile.position;
                    base.Render(renderConfig);
                    SSShaderProgram.DeactivateAll();
                    GL.LineWidth(3f);
                    GL.Begin(PrimitiveType.Lines);
                    GL.Color4(Color4.LightCyan);
                    GL.Vertex3(Vector3.Zero);
                    GL.Vertex3(_missile._lataxDebug);
                    GL.Color4(Color4.Magenta);
                    GL.Vertex3(Vector3.Zero);
                    GL.Vertex3(_missile._hitTimeCorrAccDebug);
                    GL.End();

                    viewProjMat = renderConfig.invCameraViewMatrix * renderConfig.projectionMatrix;
                }
コード例 #17
0
        public override void Render(SSRenderConfig renderConfig)
        {
            base.Render(renderConfig);

            // mode setup
            SSShaderProgram.DeactivateAll();             // disable GLSL
            GL.Disable(EnableCap.Texture2D);

            // triangle draw...
            GL.Begin(PrimitiveType.Triangles);

            GL.Color3(1.0f, 1.0f, 0.0f); GL.Vertex3(-1.0f, -1.0f, 0.0f);
            GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(1.0f, -1.0f, 0.0f);
            GL.Color3(0.2f, 0.9f, 1.0f); GL.Vertex3(0.0f, 1.0f, 0.0f);

            GL.End();
        }
コード例 #18
0
        private void _RenderSolid(ref SSRenderConfig renderConfig)
        {
            // mode setup
            SSShaderProgram.DeactivateAll();             // disable GLSL
            GL.Disable(EnableCap.CullFace);
            GL.Disable(EnableCap.Texture2D);
            GL.Disable(EnableCap.Blend);
            GL.Enable(EnableCap.Lighting);

            // should generate these from screen-space size approximation
            float rings    = 10.0f;
            float segments = 10.0f;

            // Establish constants used in sphere generation
            float fDeltaRingAngle = ((float)Math.PI / rings);
            float fDeltaSegAngle  = (2.0f * (float)Math.PI / segments);

            float r0;
            float y0;
            float x0;
            float z0;

            GL.Begin(PrimitiveType.Triangles);

            GL.LineWidth(1.0f);
            GL.Color3(1.0f, 1.0f, 1.0f);

            for (int ring = 0; ring < rings + 1; ring++)
            {
                r0 = (float)Math.Sin(ring * fDeltaRingAngle);
                y0 = (float)Math.Cos(ring * fDeltaRingAngle);

                for (int seg = 0; seg < segments + 1; seg++)
                {
                    x0 = r0 * (float)Math.Sin(seg * fDeltaSegAngle);
                    z0 = r0 * (float)Math.Cos(seg * fDeltaSegAngle);

                    // add first vertex
                    var pos = new Vector3(x0 * radius, y0 * radius, z0 * radius);
                    GL.Normal3(pos);
                    GL.Vertex3(pos);
                }
            }
            GL.End();
        }
コード例 #19
0
ファイル: ssBVH_SSObject.cs プロジェクト: wumohai/SimpleScene
        public override void Render(SSRenderConfig renderConfig)
        {
            if (renderConfig.drawingShadowMap)
            {
                return;
            }
            base.Render(renderConfig);
            SSShaderProgram.DeactivateAll();
            GL.Disable(EnableCap.Texture2D);
            GL.LineWidth(1.0f);

            GL.MatrixMode(MatrixMode.Modelview);
            ibo.Bind();
            vbo.DrawBind(renderConfig);
            this.renderCells(renderConfig, bvh.rootBVH, ref bvh.rootBVH.box, 0);
            vbo.DrawUnbind();
            ibo.Unbind();
        }
コード例 #20
0
ファイル: SSShadowMap.cs プロジェクト: wpwen/SimpleScene
        public void PrepareForRender(SSRenderConfig renderConfig,
                                     List <SSObject> objects,
                                     Util3d.FrustumCuller frustum,
                                     SSCamera camera)
        {
            GL.Ext.BindFramebuffer(FramebufferTarget.Framebuffer, m_frameBufferID);
            GL.Viewport(0, 0, c_texWidth, c_texHeight);

            float   width, height, nearZ, farZ;
            Vector3 viewEye, viewTarget, viewUp;

            if (true)
            {
                // dynamically compute light frustum
                Util3d.Projections.SimpleShadowmapProjection(
                    objects, m_light, frustum, camera,
                    out width, out height, out nearZ, out farZ,
                    out viewEye, out viewTarget, out viewUp);
            }
            else
            {
                // hard-coded "whole scene"...
                width      = 2500;
                height     = 1500;
                nearZ      = 1;
                farZ       = 15000;
                viewEye    = new Vector3(0, 0, -1500f);
                viewTarget = new Vector3(0, 0, 1f);
                viewUp     = new Vector3(0, 1f, 0);
            }

            m_projMatrix = Matrix4.CreateOrthographic(width, height, nearZ, farZ);
            m_viewMatrix = Matrix4.LookAt(viewEye, viewTarget, viewUp);

            renderConfig.projectionMatrix = m_projMatrix;
            renderConfig.invCameraViewMat = m_viewMatrix;
            renderConfig.drawingShadowMap = true;
            SSShaderProgram.DeactivateAll();

            GL.DrawBuffer(DrawBufferMode.None);
            GL.Clear(ClearBufferMask.DepthBufferBit);

            assertFramebufferOK();
        }
コード例 #21
0
        public override void Render(SSRenderConfig renderConfig)
        {
            base.Render(renderConfig);
            SSShaderProgram.DeactivateAll();

            GL.Disable(EnableCap.Texture2D);
            GL.Disable(EnableCap.Lighting);

            GL.Enable(EnableCap.PointSmooth);

            GL.PointSize(1.5f);
            GL.Begin(BeginMode.Points);
            for (int i = 0; i < this.numstars; i++)
            {
                GL.Color3(Color.FromArgb(vertices[i].DiffuseColor));
                GL.Normal3(vertices[i].Normal);
                GL.Vertex3(vertices[i].Position);
            }
            GL.End();
        }
コード例 #22
0
            private void renderFaceNormals()
            {
                SSShaderProgram.DeactivateAll();
                GL.Color4(Color4.Green);
                for (int i = 0; i < _runtimeMesh.numTriangles; i++)
                {
                    int     baseIdx = i * 3;
                    Vector3 p0      = _runtimeMesh.computeVertexPosFromTriIndex(baseIdx);
                    Vector3 p1      = _runtimeMesh.computeVertexPosFromTriIndex(baseIdx + 1);
                    Vector3 p2      = _runtimeMesh.computeVertexPosFromTriIndex(baseIdx + 2);

                    Vector3 face_center = (p0 + p1 + p2) / 3.0f;
                    Vector3 face_normal = Vector3.Cross(p1 - p0, p2 - p0).Normalized();

                    GL.Begin(PrimitiveType.Lines);
                    GL.Vertex3(face_center);
                    GL.Vertex3(face_center + face_normal * 0.2f);
                    GL.End();
                }
            }
コード例 #23
0
        public override void PrepareForRender(
            SSRenderConfig renderConfig,
            List <SSObject> objects,
            float fov, float aspect, float nearZ, float farZ)
        {
            base.PrepareForRenderBase(renderConfig, objects);

            ComputeProjections(objects, m_light,
                               renderConfig.invCameraViewMatrix, renderConfig.projectionMatrix);

            // update info for the regular draw pass later
            Matrix4[] vp = { m_shadowViewMatrix *m_shadowProjMatrix *c_biasMatrix };
            configureDrawShader(ref renderConfig, renderConfig.mainShader, vp);
            configureDrawShader(ref renderConfig, renderConfig.instanceShader, vp);

            // setup for render shadowmap pass
            renderConfig.projectionMatrix    = m_shadowProjMatrix;
            renderConfig.invCameraViewMatrix = m_shadowViewMatrix;
            SSShaderProgram.DeactivateAll();
        }
コード例 #24
0
ファイル: SSMesh_wfOBJ.cs プロジェクト: wumohai/SimpleScene
        private void _renderSetupGLSL(SSRenderConfig renderConfig, SSMainShaderProgram shaderPgm, SSMeshOBJSubsetData subset)
        {
            // Step 1: setup GL rendering modes...

            // GL.Enable(EnableCap.Lighting);

            // GL.Enable(EnableCap.Blend);
            // GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            // GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);

            // Step 2: setup our material mode and paramaters...

            if (renderConfig.drawingShadowMap)
            {
                // assume SSObject.Render has setup our materials properly for the shadowmap Pass
                // TODO: find a better way to do this!
                return;
            }

            SSColorMaterial.applyColorMaterial(subset.colorMaterial);
            if (shaderPgm == null)
            {
                SSShaderProgram.DeactivateAll();

                // fixed function single-texture
                if (subset.textureMaterial.diffuseTex != null)
                {
                    GL.ActiveTexture(TextureUnit.Texture0);
                    GL.Enable(EnableCap.Texture2D);
                    GL.BindTexture(TextureTarget.Texture2D, subset.textureMaterial.diffuseTex.TextureID);
                }
            }
            else
            {
                shaderPgm.Activate();
                shaderPgm.SetupTextures(subset.textureMaterial);
            }
        }
コード例 #25
0
        private void _RenderLines_ICO(SSRenderConfig renderConfig)
        {
            // mode setup
            SSShaderProgram.DeactivateAll();             // disable GLSL
            GL.Disable(EnableCap.Texture2D);
            GL.Disable(EnableCap.Blend);
            GL.Disable(EnableCap.Lighting);

            GL.Begin(PrimitiveType.Lines);
            GL.LineWidth(1.0f);

            foreach (var face in icoSphereFaces)
            {
                var v1 = icoSphereVertices[face.v1] * radius;
                var v2 = icoSphereVertices[face.v2] * radius;
                var v3 = icoSphereVertices[face.v3] * radius;

                GL.Vertex3(v1);  GL.Vertex3(v2);
                GL.Vertex3(v2);  GL.Vertex3(v3);
                GL.Vertex3(v1);  GL.Vertex3(v3);
            }
            GL.End();
        }
コード例 #26
0
        public override void renderMesh(SSRenderConfig renderConfig)
        {
            SSShaderProgram.DeactivateAll();

            GL.Disable(EnableCap.Texture2D);
            GL.Disable(EnableCap.Lighting);

            GL.Enable(EnableCap.PointSmooth);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            GL.PointSize(1.5f);
            GL.Begin(BeginMode.Points);
            for (int i = 0; i < this.numstars; i++)
            {
                GL.Color3(Color.FromArgb(vertices[i].DiffuseColor));
                GL.Normal3(vertices[i].Normal);
                GL.Vertex3(vertices[i].Position);
            }
            GL.End();

            GL.Disable(EnableCap.Blend);
        }
コード例 #27
0
        public override void Render(SSRenderConfig renderConfig)
        {
            var beam = _laser.beam(_beamId);

            if (beam == null)
            {
                return;
            }

            base.Render(renderConfig);

            // step: setup render settings
            SSShaderProgram.DeactivateAll();
            GL.ActiveTexture(TextureUnit.Texture0);
            GL.Enable(EnableCap.Texture2D);

            var laserParams = _laser.parameters;

            var startView  = Vector3.Transform(beam.startPosWorld, renderConfig.invCameraViewMatrix);
            var endView    = Vector3.Transform(beam.endPosWorld, renderConfig.invCameraViewMatrix);
            var middleView = (startView + endView) / 2f;

            // step: draw middle section:
            Vector3 diff                = endView - startView;
            float   diff_xy             = diff.Xy.Length;
            float   phi                 = -(float)Math.Atan2(diff.Z, diff_xy);
            float   theta               = (float)Math.Atan2(diff.Y, diff.X);
            Matrix4 backgroundOrientMat = Matrix4.CreateRotationY(phi) * Matrix4.CreateRotationZ(theta);
            Matrix4 middlePlacementMat  = backgroundOrientMat * Matrix4.CreateTranslation(middleView);
            //Matrix4 startPlacementMat = Matrix4.CreateTranslation (startView);

            float laserLength = diff.Length;
            float middleWidth = laserParams.middleBackgroundWidth * _laser.envelopeIntensity;

            Vector3 cameraDir = Vector3.Transform(
                -Vector3.UnitZ, _cameraScene.renderConfig.invCameraViewMatrix).Normalized();
            float dot = Vector3.Dot(cameraDir, beam.directionWorld());

            dot = Math.Max(dot, 0f);
            float interferenceWidth = middleWidth * laserParams.middleInterferenceScale;

            GL.Color4(1f, 1f, 1f, beam.periodicIntensity * beam.periodicIntensity);

            _updateMiddleMesh(laserLength, middleWidth);

                        #if true
            // stretched middle background sprite
            if (middleBackgroundSprite != null)
            {
                GL.Material(MaterialFace.Front, MaterialParameter.Emission, laserParams.backgroundColor);
                GL.BindTexture(TextureTarget.Texture2D, middleBackgroundSprite.TextureID);

                foreach (var oriX in xOrientationPresets)
                {
                    Matrix4 rotated = oriX * middlePlacementMat;
                    GL.LoadMatrix(ref rotated);
                    _middleMesh.renderMesh(renderConfig);
                }
            }
                        #endif
                        #if true
            // stretched middle overlay sprite
            if (middleOverlayTexture != null)
            {
                GL.Material(MaterialFace.Front, MaterialParameter.Emission, laserParams.overlayColor);
                GL.BindTexture(TextureTarget.Texture2D, middleOverlayTexture.TextureID);

                _middleMesh.renderMesh(renderConfig);

                foreach (var oriX in xOrientationPresets)
                {
                    Matrix4 rotated = oriX * middlePlacementMat;
                    GL.LoadMatrix(ref rotated);
                    _middleMesh.renderMesh(renderConfig);
                }
            }
                        #endif
                        #if true
            // interference sprite with a moving U-coordinate offset
            if (laserParams.middleInterferenceScale > 0f && interferenceTexture != null)
            {
                _updateInterfernenceVertices(laserLength, interferenceWidth);

                GL.Material(MaterialFace.Front, MaterialParameter.Emission, laserParams.middleInterferenceColor);
                //GL.BindTexture(TextureTarget.Texture2D, interferenceSprite.TextureID);
                GL.BindTexture(TextureTarget.Texture2D, interferenceTexture.TextureID);
                var scaleMat = Matrix4.CreateScale(laserLength + middleWidth / 2f, interferenceWidth, 1f);

                foreach (var oriX in xOrientationPresets)
                {
                    Matrix4 rotated = scaleMat * oriX * middlePlacementMat;
                    GL.LoadMatrix(ref rotated);
                    _interferenceMesh.renderMesh(renderConfig);
                }
            }
                        #endif
        }
コード例 #28
0
ファイル: SSObject.cs プロジェクト: cyecp/SimpleScene
        public virtual void Render(SSRenderConfig renderConfig)
        {
            // compute and set the modelView matrix, by combining the cameraViewMat
            // with the object's world matrix
            //    ... http://www.songho.ca/opengl/gl_transform.html
            //    ... http://stackoverflow.com/questions/5798226/3d-graphics-processing-how-to-calculate-modelview-matrix
            renderBoundingSphereMesh(renderConfig);

            Matrix4 modelViewMat = this.worldMat * renderConfig.invCameraViewMatrix;

            if (this.renderState.matchScaleToScreenPixels)
            {
                Matrix4 scaleCompenstaion = OpenTKHelper.ScaleToScreenPxViewMat(
                    this.Pos, this.Scale.X, ref renderConfig.invCameraViewMatrix, ref renderConfig.projectionMatrix);
                modelViewMat = scaleCompenstaion * modelViewMat;
            }
            if (this.renderState.doBillboarding)
            {
                modelViewMat = OpenTKHelper.BillboardMatrix(ref modelViewMat);
            }
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelViewMat);

            resetTexturingState();

            if (renderConfig.drawingShadowMap)
            {
                if (renderConfig.drawingPssm && renderConfig.pssmShader != null)
                {
                    renderConfig.pssmShader.Activate();
                    renderConfig.pssmShader.UniObjectWorldTransform = this.worldMat;
                }
                else
                {
                    SSShaderProgram.DeactivateAll();
                }
            }
            else
            {
                if (renderState.noShader)
                {
                    SSShaderProgram.DeactivateAll();
                }
                else
                {
                    setDefaultShaderState(renderConfig.mainShader, renderConfig);
                }
                setMaterialState();

                if (this.alphaBlendingEnabled)
                {
                    GL.Enable(EnableCap.Blend);
                    GL.BlendEquation(renderState.blendEquationMode);
                    GL.BlendFunc(renderState.blendFactorSrc, renderState.blendFactorDest);
                }
                else
                {
                    GL.Disable(EnableCap.Blend);
                }

                if (this.renderState.lighted)
                {
                    GL.Enable(EnableCap.Lighting);
                    GL.ShadeModel(ShadingModel.Flat);
                }
                else
                {
                    GL.Disable(EnableCap.Lighting);
                }
            }

            if (this.renderState.alphaTest)
            {
                GL.Enable(EnableCap.AlphaTest);
            }
            else
            {
                GL.Disable(EnableCap.AlphaTest);
            }

            if (this.renderState.depthTest)
            {
                GL.Enable(EnableCap.DepthTest);
                GL.DepthFunc(renderState.depthFunc);
            }
            else
            {
                GL.Disable(EnableCap.DepthTest);
            }
            GL.DepthMask(this.renderState.depthWrite);

            GL.Disable(EnableCap.LineStipple);

            if (preRenderHook != null)
            {
                preRenderHook(this, renderConfig);
            }
        }
コード例 #29
0
 public void Deactivate()
 {
     SSShaderProgram.DeactivateAll();
 }
コード例 #30
0
        public override void renderMesh(SSRenderConfig renderConfig)
        {
            SSShaderProgram.DeactivateAll();

            base.renderMesh(renderConfig);
        }
コード例 #31
0
 public static void DeactivateAll()
 {
     s_activeProgram = null;
     GL.UseProgram (0);
 }
コード例 #32
0
        public override void Render(SSRenderConfig renderConfig)
        {
            int queryResult = sunBillboard.OcclusionQueueryResult;

            if (queryResult <= 0)
            {
                return;
            }

            // Begin the quest to update VBO vertices
            Matrix4 viewInverted = sunScene.renderConfig.invCameraViewMatrix.Inverted();
            Vector3 viewRight    = Vector3.Transform(new Vector3(1f, 0f, 0f), viewInverted);
            Vector3 viewUp       = Vector3.Transform(new Vector3(0f, 1f, 0f), viewInverted);
            Vector3 sunRightMost = sunBillboard.Pos + viewRight.Normalized() * sunBillboard.Scale.X;
            Vector3 sunTopMost   = sunBillboard.Pos + viewUp.Normalized() * sunBillboard.Scale.Y;

            int[] viewport = new int[4];
            GL.GetInteger(GetPName.Viewport, viewport);
            Vector2 screenOrig = new Vector2(viewport [0], viewport [1]);

            clientRect       = new Vector2(viewport [2], viewport [3]) / 2f;
            screenCenter     = screenOrig + clientRect / 2f;
            sunSceneViewProj = sunScene.renderConfig.invCameraViewMatrix * sunScene.renderConfig.projectionMatrix;
            Vector2 sunScreenPos       = worldToScreen(sunBillboard.Pos);
            Vector2 sunScreenRightMost = worldToScreen(sunRightMost);
            Vector2 sunScreenTopMost   = worldToScreen(sunTopMost);
            Vector2 towardsCenter      = screenCenter - sunScreenPos;

            Vector2 tileVecBase       = new Vector2(sunScreenRightMost.X - sunScreenPos.X, sunScreenPos.Y - sunScreenTopMost.Y);
            float   sunFullEstimate   = (float)Math.PI * tileVecBase.X * tileVecBase.Y;
            float   intensityFraction = Math.Min((float)queryResult / sunFullEstimate, 1f);

            // modulate sprite size with the intensity fraction
            tileVecBase *= Math.Min(1f / (1f - intensityFraction), 1.5f);

            // allow simple scaling
            tileVecBase.X *= Scale.X;
            tileVecBase.Y *= Scale.Y;

            for (int i = 0; i < numElements; ++i)
            {
                //assign positions
                Vector2 center  = sunScreenPos + towardsCenter * 2.5f / (float)numElements * (float)i;
                Vector2 tileVec = tileVecBase * spriteScales [i];

                int baseIdx = i * 4;
                vertices [baseIdx].Position.X = center.X - tileVec.X;
                vertices [baseIdx].Position.Y = center.Y - tileVec.Y;

                vertices [baseIdx + 1].Position.X = center.X + tileVec.X;
                vertices [baseIdx + 1].Position.Y = center.Y - tileVec.Y;

                vertices [baseIdx + 2].Position.X = center.X + tileVec.X;
                vertices [baseIdx + 2].Position.Y = center.Y + tileVec.Y;

                vertices [baseIdx + 3].Position.X = center.X - tileVec.X;
                vertices [baseIdx + 3].Position.Y = center.Y + tileVec.Y;
            }
            Mesh.UpdateVertices(vertices);

            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            SSShaderProgram.DeactivateAll(); // disable shaders

            // modulate color alpha with the intensity fraction
            this.MainColor   = sunBillboard.MainColor;
            this.MainColor.A = intensityFraction;

            // now, actually draw
            base.Render(renderConfig);
        }