Micropro's picture

help with Shadow2Pass simple

i have a big problem with this project
http://www.mediafire.com/download/ive7i279j5ay5r1/Shadow+2+Pass.rar

when i put texture the shadow is not draw

pleas can you help me .............


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Frassle's picture

Can you put the code somewhere easier to see like a github project or gist. Also if possible reduce it down to the smallest amount of code that still demonstrates the problem, that will make it much more likely that someone can help.

Micropro's picture
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
 * See license.txt for license info
 */
#endregion
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;
 
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL.Enums;
using OpenTK.Input;
 
namespace Examples.Tutorial
{
    /// <summary>
    /// Demonstrates the GameWindow class.
    /// </summary>
    public class SimpleWindow : GameWindow
    {
        float[] ambientLight = new float[4] { 0.2f, 0.2f, 0.2f, 1.0f };
        float[] diffuseLight = new float[4] { 0.7f, 0.7f, 0.7f, 1.0f };
        float[] noLight = new float[4] { 0.0f, 0.0f, 0.0f, 1.0f };
        float[] lightPos = new float[4] { 200.0f, 300.0f, 100.0f, 1.0f };
        float[] cameraPos = new float[4] { 100.0f, 150.0f, 200.0f, 1.0f };
 
        int shadowSize = 512;                 // set based on window size
        uint shadowTextureID;
        float[] sPlane = new float[4] { 1.0f, 0.0f, 0.0f, 0.0f };
        float[] tPlane = new float[4] { 0.0f, 1.0f, 0.0f, 0.0f };
        float[] rPlane = new float[4] { 0.0f, 0.0f, 1.0f, 0.0f };
        float[] qPlane = new float[4] { 0.0f, 0.0f, 0.0f, 1.0f };
        float factor = 5.0f;
 
        float[] lightModelview = new float[16], lightProjection = new float[16];
 
        int windowWidth = 512;                // window size
        int windowHeight = 512;
 
        IntPtr esfera, cono;
 
        float rot = 0;
 
        public SimpleWindow()
            : base(800, 600)
        {
            esfera = Glu.NewQuadric();
            cono = Glu.NewQuadric();
            Keyboard.KeyDown += new OpenTK.Input.KeyDownEvent(Keyboard_KeyDown);
        }
 
        #region Keyboard_KeyDown
 
        /// <summary>
        /// Occurs when a key is pressed.
        /// </summary>
        /// <param name="sender">The KeyboardDevice which generated this event.</param>
        /// <param name="key">The key that was pressed.</param>
        void Keyboard_KeyDown(KeyboardDevice sender, Key key)
        {
            if (sender[Key.Escape])
                this.Exit();
            if (sender[Key.Left])
                lightPos[2] += 5f;
            if (sender[Key.Right])
                lightPos[2] -= 5f;
            if (sender[Key.Up])
                lightPos[0] += 5f;
            if (sender[Key.Down])
                lightPos[0] -= 5f;
 
            if ((sender[Key.AltLeft] || sender[Key.AltRight]) && (sender[Key.Enter] || sender[Key.KeypadEnter]))
                if (this.WindowState == WindowState.Fullscreen)
                    this.WindowState = WindowState.Normal;
                else
                    this.WindowState = WindowState.Fullscreen;
        }
 
        #endregion
 
        #region DrawModels()
 
        void DrawModels()
        {
            float width = 30;
            float height = 30;
            float length = 30;
 
            width /= 2.0f;
            height /= 2.0f;
            length /= 2.0f;
 
            // Draw plane that the objects rest on
            GL.Color3(0.0f, 0.0f, 0.90f); // Blue
            GL.Normal3(0.0f, 1.0f, 0.0f);
            GL.Begin(BeginMode.Quads);
                GL.Vertex3(-100.0f, -25.0f, -100.0f);
                GL.Vertex3(-100.0f, -25.0f, 100.0f);
                GL.Vertex3(100.0f, -25.0f, 100.0f);
                GL.Vertex3(100.0f, -25.0f, -100.0f);
            GL.End();
 
            // Draw red cube
            GL.Color3(1.0f, 0.0f, 0.0f);
            GL.PushMatrix();
            GL.Translate(0.0f, 30.0f, 0.0f);
            GL.Begin(BeginMode.Quads);
                GL.Normal3(0.0f, 0.0f, -1.0f);
                GL.Vertex3(-width, height, -length); //0
                GL.Vertex3(width, height, -length);  //1
                GL.Vertex3(width, -height, -length);//2
                GL.Vertex3(-width, -height, -length); //3
                //
                GL.Normal3(0.0f, 0.0f, 1.0f);
                GL.Vertex3(-width, height, length);  //4
                GL.Vertex3(-width, -height, length); //5
                GL.Vertex3(width, -height, length);   //6
                GL.Vertex3(width, height, length);  //7
                //
                GL.Normal3(0.0f, 1.0f, 0.0f);
                GL.Vertex3(-width, height, length);  //8 (4)
                GL.Vertex3(width, height, length);   //9 (6)
                GL.Vertex3(width, height, -length); //10 (0)
                GL.Vertex3(-width, height, -length);  //11 (1)
                //
                GL.Normal3(0.0f, -1.0f, 0.0f);
                GL.Vertex3(-width, -height, length); //12 (5)
                GL.Vertex3(-width, -height, -length);//13 (2)
                GL.Vertex3(width, -height, -length);  //14 (7)
                GL.Vertex3(width, -height, length); //15 (3)
                //
                GL.Normal3(1.0f, 0.0f, 0.0f);
                GL.Vertex3(width, height, -length);  //16 (1)
                GL.Vertex3(width, height, length);   //17 (6)
                GL.Vertex3(width, -height, length); //18 (3)
                GL.Vertex3(width, -height, -length);  //19 (7)
                //
                GL.Normal3(-1.0f, 0.0f, 0.0f);
                GL.Vertex3(-width, height, -length); //20 (0)
                GL.Vertex3(-width, -height, -length);//21 (2)
                GL.Vertex3(-width, -height, length);  //22 (4)
                GL.Vertex3(-width, height, length); //23 (5)
                //
            GL.End();
            GL.PopMatrix();
 
            // Draw green sphere
            GL.Color3(0.0f, 1.0f, 0.0f);
            GL.PushMatrix();
            GL.Translate(-60.0f, 0.0f, 0.0f);
            Glu.Sphere(esfera, 25.0f, 50, 50);
            GL.PopMatrix();
 
            // Draw yellow cone
            GL.Color3(1.0f, 1.0f, 0.0f);
            GL.PushMatrix();
            GL.Rotate(-90.0f, 1.0f, 0.0f, 0.0f);
            GL.Translate(60.0f, 0.0f, -24.0f);
            Glu.Cylinder(cono, 25.0f, 0.0f, 50.0f, 50, 50);
            GL.PopMatrix();
 
            // Draw magenta torus
            GL.Color3(1.0f, 0.0f, 1.0f);
            GL.PushMatrix();
            GL.Translate(0.0f, 0.0f, 60.0f);
            //glutSolidTorus(8.0f, 16.0f, 50, 50);
            GL.PopMatrix();
 
            // Draw cyan octahedron
            GL.Color3(0.0f, 1.0f, 1.0f);
            GL.PushMatrix();
            GL.Translate(0.0f, 0.0f, -60.0f);
            GL.Scale(25.0f, 25.0f, 25.0f);
            //glutSolidOctahedron();
            GL.PopMatrix();
        }
        #endregion DrawModels()
 
        #region RegenerateShadowMap()
 
        void RegenerateShadowMap()
        {
            float lightToSceneDistance, nearPlane, fieldOfView;
 
            // Save the depth precision for where it's useful
            lightToSceneDistance = (float)Math.Sqrt(lightPos[0] * lightPos[0] +
                                                    lightPos[1] * lightPos[1] +
                                                    lightPos[2] * lightPos[2]);
            nearPlane = lightToSceneDistance - 150.0f;
            if (nearPlane < 50.0f)
                nearPlane = 50.0f;
            // Keep the scene filling the depth texture
            fieldOfView = 17000.0f / lightToSceneDistance;
 
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            Glu.Perspective(fieldOfView, 1.0f, nearPlane, nearPlane + 300.0f);
            GL.GetFloat(GetPName.ProjectionMatrix, lightProjection);
            // Switch to light's point of view
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            Glu.LookAt(lightPos[0], lightPos[1], lightPos[2],
                        0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
            GL.GetFloat(GetPName.ModelviewMatrix, lightModelview);
            GL.Viewport(0, 0, shadowSize, shadowSize);
 
            // Clear the window with current clearing color
            GL.Clear(ClearBufferMask.DepthBufferBit);
 
            // All we care about here is resulting depth values
            GL.ShadeModel(ShadingModel.Flat);
            GL.Disable(EnableCap.Lighting);
            GL.Disable(EnableCap.ColorMaterial);
            GL.Disable(EnableCap.Normalize);
            GL.ColorMask(false, false, false, false);
 
            // Overcome imprecision
            GL.Enable(EnableCap.PolygonOffsetFill);
 
            DrawModels();
 
            // Copy depth values into depth texture
            GL.CopyTexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent,
                     0, 0, shadowSize, shadowSize, 0);
 
            // Restore normal drawing state
            GL.ShadeModel(ShadingModel.Smooth);
            GL.Enable(EnableCap.Lighting);
            GL.Enable(EnableCap.ColorMaterial);
            GL.Enable(EnableCap.Normalize);
            GL.ColorMask(true, true, true, true);
            GL.Disable(EnableCap.PolygonOffsetFill);
 
            // Set up texture matrix for shadow map projection
            GL.MatrixMode(MatrixMode.Texture);
            GL.LoadIdentity();
            GL.Translate(0.5f, 0.5f, 0.5f);
            GL.Scale(0.5f, 0.5f, 0.5f);
            GL.MultMatrix(lightProjection);
            GL.MultMatrix(lightModelview);
        }
        #endregion RegenerateShadowMap()
 
        #region OnLoad
 
        /// <summary>
        /// Setup OpenGL and load resources here.
        /// </summary>
        /// <param name="e">Not used.</param>
        public override void OnLoad(EventArgs e)
        {
            // Black background
            GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 
            // Hidden surface removal
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);
            GL.PolygonOffset(factor, 0.0f);
 
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
            //GL.LightModel(LightModelParameter.LightModelAmbient, new float[] { 0, 0, 0, 1 });
            // Set up some lighting state that never changes
            GL.ShadeModel(ShadingModel.Smooth);
            GL.Enable(EnableCap.Lighting);
            GL.Enable(EnableCap.ColorMaterial);
            GL.Enable(EnableCap.Normalize);
            GL.Enable(EnableCap.Light0);
 
            // Set up some texture state that never changes
            GL.GenTextures(1, out shadowTextureID);
            GL.BindTexture(TextureTarget.Texture2D, shadowTextureID);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);
            GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)(int)All.DepthTextureModeArb, (int)All.Intensity);
            //if (ambientShadowAvailable)
            GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)(int)All.TextureCompareFailValueArb, 0.5f);
            GL.TexGen(TextureCoordName.S, TextureGenParameter.TextureGenMode, (int)TextureGenMode.EyeLinear);
            GL.TexGen(TextureCoordName.T, TextureGenParameter.TextureGenMode, (int)TextureGenMode.EyeLinear);
            GL.TexGen(TextureCoordName.R, TextureGenParameter.TextureGenMode, (int)TextureGenMode.EyeLinear);
            GL.TexGen(TextureCoordName.Q, TextureGenParameter.TextureGenMode, (int)TextureGenMode.EyeLinear);
 
            RegenerateShadowMap();
        }
 
        #endregion
 
        #region OnResize
 
        /// <summary>
        /// Respond to resize events here.
        /// </summary>
        /// <param name="e">Contains information on the new GameWindow size.</param>
        /// <remarks>There is no need to call the base implementation.</remarks>
        protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
        {
            int i;
 
            windowWidth = e.Width;
            windowHeight = e.Height;
 
            // Find the largest power of two that will fit in window
            if (e.Width > e.Height)
                shadowSize = e.Height;
            else
                shadowSize = e.Width;
 
            // Try each size until we get one that's too big
            i = 0;
            while ((1 << i) <= shadowSize)
                i++;
            shadowSize = (1 << (i - 1));
        }
 
        #endregion
 
        #region OnUpdateFrame
 
        /// <summary>
        /// Add your game logic here.
        /// </summary>
        /// <param name="e">Contains timing information.</param>
        /// <remarks>There is no need to call the base implementation.</remarks>
        public override void OnUpdateFrame(UpdateFrameEventArgs e)
        {
            rot += 0.5f;
 
            RegenerateShadowMap();
        }
 
        #endregion
 
        #region OnRenderFrame
 
        /// <summary>
        /// Add your game rendering code here.
        /// </summary>
        /// <param name="e">Contains timing information.</param>
        /// <remarks>There is no need to call the base implementation.</remarks>
        public override void OnRenderFrame(RenderFrameEventArgs e)
        {
            double aspect = (double)windowWidth / (double)windowHeight;
            // Track camera angle
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            Glu.Perspective(45.0f, aspect, 1.0f, 1000.0f);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            Glu.LookAt(cameraPos[0], cameraPos[1], cameraPos[2],
                      0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
            GL.Viewport(0, 0, windowWidth, windowHeight);
 
            GL.Rotate(rot, 0, 1, 0);
 
            // Track light position
            GL.Light(LightName.Light0, LightParameter.Position, lightPos);
 
            // Clear the window with current clearing color
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
 
            // Set up shadow comparison
            GL.Enable(EnableCap.Texture2D);
            GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (float)TextureEnvMode.Modulate);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureCompareMode, (float)TextureCompareMode.CompareRToTexture);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMinFilter.Nearest);
 
            // Set up the eye plane for projecting the shadow map on the scene
            GL.Enable(EnableCap.TextureGenS);
            GL.Enable(EnableCap.TextureGenT);
            GL.Enable(EnableCap.TextureGenR);
            GL.Enable(EnableCap.TextureGenQ);
            GL.TexGen(TextureCoordName.S, TextureGenParameter.EyePlane, sPlane);
            GL.TexGen(TextureCoordName.T, TextureGenParameter.EyePlane, tPlane);
            GL.TexGen(TextureCoordName.R, TextureGenParameter.EyePlane, rPlane);
            GL.TexGen(TextureCoordName.Q, TextureGenParameter.EyePlane, qPlane);
 
            // Draw objects in the scene
            DrawModels();
 
            GL.Disable(EnableCap.AlphaTest);
            GL.Disable(EnableCap.TextureGenS);
            GL.Disable(EnableCap.TextureGenT);
            GL.Disable(EnableCap.TextureGenR);
            GL.Disable(EnableCap.TextureGenQ);
 
            this.SwapBuffers();
        }
 
        #endregion
 
        public override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
 
            Glu.DeleteQuadric(cono);
            Glu.DeleteQuadric(esfera);
        }
 
        #region public static void Main()
 
        /// <summary>
        /// Entry point of this example.
        /// </summary>
        [STAThread]
        public static void Main()
        {
            using (SimpleWindow example = new SimpleWindow())
            {
                // Get the title and category  of this example using reflection.
                example.Title = "Shadow2Pass";
                example.Run(30.0, 0.0);
            }
        }
 
        #endregion
    }
}
Micropro's picture

please can any one help me with draw shadow its very important fro me

the Fiddler's picture

Last time I rendered shadows without shaders was in 2001 or so. Is there a reason why you are using this specific approach?

Micropro's picture

i need any code to draw shadow

Frassle's picture

This honestly sounds like a 'help me with my homework' question. I really suggest you read Eric Lippert's posts on how to debug small programs and find a simpler problem. If you can't get it to work read stackoverflow's how to ask a good question and come back and ask a good question. Most of us are very willing to help but you have to put the effort in as well.