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(); } }
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(); }
public override void Render(ref SSRenderConfig renderConfig) { if (renderConfig.drawingShadowMap) { return; } base.Render(ref renderConfig); SSShaderProgram.DeactivateAll(); GL.Color4(Color.Red); GL.Disable(EnableCap.Texture2D); GL.Disable(EnableCap.Lighting); GL.LineWidth(1.0f); GL.Color4(Color.Red); GL.MatrixMode(MatrixMode.Modelview); ibo.Bind(); vbo.DrawBind(); this.renderCells(bvh.rootBVH, ref bvh.rootBVH.box, 0); vbo.DrawUnbind(); ibo.Unbind(); }
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); } }
public override void Render(ref SSRenderConfig renderConfig) { base.Render(ref renderConfig); // mode setup SSShaderProgram.DeactivateAll(); // disable GLSL GL.Disable(EnableCap.CullFace); GL.Disable(EnableCap.Blend); GL.Disable(EnableCap.Lighting); GL.ActiveTexture(TextureUnit.Texture0); GL.Enable(EnableCap.Texture2D); GL.BindTexture(TextureTarget.Texture2D, GLu_textureID); // GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Nearest); // GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest); // draw quad... GL.Begin(PrimitiveType.Triangles); GL.Color3(1.0f, 1.0f, 1.0f); float w = 500; float h = 500; // 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(); }
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); }
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(); }
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 }
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); if (hasAlpha) { GL.Enable(EnableCap.AlphaTest); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); } else { GL.Disable(EnableCap.AlphaTest); GL.Disable(EnableCap.Blend); } // 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); 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(); }
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); } }
public void Deactivate() { SSShaderProgram.DeactivateAll(); }
public override void renderMesh(SSRenderConfig renderConfig) { SSShaderProgram.DeactivateAll(); base.renderMesh(renderConfig); }
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); }
private void _renderSetupGLSL(ref SSRenderConfig renderConfig, SSMeshOBJSubsetData subset) { // Step 1: setup GL rendering modes... GL.Enable(EnableCap.CullFace); // 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... GL.Color3(System.Drawing.Color.White); // clear the vertex color to white.. SSMainShaderProgram shaderPgm = renderConfig.BaseShader; if (renderConfig.drawingShadowMap) { // assume SSObject.Render has setup our materials properly for the shadowmap Pass // TODO: find a better way to do this! return; } if (shaderPgm == null) { SSShaderProgram.DeactivateAll(); GL.Disable(EnableCap.CullFace); // fixed function single-texture GL.Disable(EnableCap.Texture2D); if (subset.diffuseTexture != null) { GL.ActiveTexture(TextureUnit.Texture0); GL.Enable(EnableCap.Texture2D); GL.BindTexture(TextureTarget.Texture2D, subset.diffuseTexture.TextureID); } } else { // activate GLSL shader shaderPgm.Activate(); // bind our texture-images to GL texture-units // http://adriangame.blogspot.com/2010/05/glsl-multitexture-checklist.html // these texture-unit assignments are hard-coded in the shader setup GL.ActiveTexture(TextureUnit.Texture0); if (subset.diffuseTexture != null) { GL.BindTexture(TextureTarget.Texture2D, subset.diffuseTexture.TextureID); shaderPgm.u_DiffTexEnabled = true; } else { GL.BindTexture(TextureTarget.Texture2D, 0); shaderPgm.u_DiffTexEnabled = false; } GL.ActiveTexture(TextureUnit.Texture1); if (subset.specularTexture != null) { GL.BindTexture(TextureTarget.Texture2D, subset.specularTexture.TextureID); shaderPgm.u_SpecTexEnabled = true; } else { GL.BindTexture(TextureTarget.Texture2D, 0); shaderPgm.u_SpecTexEnabled = false; } GL.ActiveTexture(TextureUnit.Texture2); if (subset.ambientTexture != null) { GL.BindTexture(TextureTarget.Texture2D, subset.ambientTexture.TextureID); shaderPgm.u_AmbTexEnabled = true; } else { GL.BindTexture(TextureTarget.Texture2D, 0); shaderPgm.u_AmbTexEnabled = false; } GL.ActiveTexture(TextureUnit.Texture3); if (subset.bumpTexture != null) { GL.BindTexture(TextureTarget.Texture2D, subset.bumpTexture.TextureID); shaderPgm.u_BumpTexEnabled = true; } else { GL.BindTexture(TextureTarget.Texture2D, 0); shaderPgm.u_BumpTexEnabled = false; } // reset to texture-unit 0 to be friendly.. GL.ActiveTexture(TextureUnit.Texture0); } }