コード例 #1
0
        override protected void OnLoad(System.EventArgs e)
        {
            lastMouseState = OpenTK.Input.Mouse.GetState();

            Texture angrySquirrelTexture, angryTurtleTexture, angryDilloTexture;

            Texture[] worldTextures;
            Mesh      squirrelMesh, turtleMesh, dilloMesh;

            RectangleMesh[] worldMeshes;

            float aspectRatio = ClientSize.Width / (float)(ClientSize.Height);

            projectionMatrix.set(Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, aspectRatio, 1, 1000));

            cameraOnFrame = delegate(Matrix4f mvMatrix) {
                float   radius = 40, y = 20, timeRatio = 0.5f;
                Vector3 pos = new Vector3(
                    (float)Math.Sin(time.Value * timeRatio) * radius,
                    y,
                    (float)Math.Cos(time.Value * timeRatio) * radius);
                mvMatrix.set(Matrix4.LookAt(pos, new Vector3(0, y * 0.5f * (1.5f + (float)Math.Sin(time.Value * timeRatio)), 0), new Vector3(0, 1, 0)));
            };
            cameraOnFrame(modelviewMatrix);
            cameraOnFrame = null; // enable/disable

            ambientColor.set(0.4f, 0.4f, 0.4f);
            lightColor.set(1.0f, 1.0f, 1.0f);
            lightDirUnit.set(0.5f, 1.0f, 0.5f);
            //lightDirUnit.set( 0.1f, 5.0f, 0.1f );
            lightDirUnit.normalize();
            //texture = new Texture( "E:\\drop\\logo-dark.jpg" );
            angrySquirrelTexture = new Texture("gfx/angry-squirrel.png");
            angryTurtleTexture   = new Texture("gfx/angry-turtle.png");
            angryDilloTexture    = new Texture("gfx/angry-armadillo.png");

            worldTextures = new Texture[] {
                new Texture("gfx/drzewka-1.png"),
                new Texture("gfx/drzewka-3.png"),
                new Texture("gfx/sky.png"),
                new Texture("gfx/grass.png"),
                new Texture("gfx/drzewka-2.png"),
                new Texture("gfx/drzewka-4.png")
            };

            CreateShaders();

            squirrelMesh         = new SphereMesh(BALL_RADIUS, 20, 20, true);
            squirrelMesh.Texture = angrySquirrelTexture;

            /*
             * squirrelMesh.UpdateAction = delegate( Mesh model ) {
             *  Matrix4f transform = model.Transform;
             *  transform.setScaleAndRotation( Matrix4.CreateRotationZ( time.Value ) );
             *  //transform.setTranslationY( BALL_RADIUS + JUMP_HEIGHT * 0.5f * (1 + (float) Math.Sin( time.Value )) ); // hover
             *  //transform.setTranslationY( BALL_RADIUS + JUMP_HEIGHT * (float) Math.Abs( Math.Sin( time.Value ) ) ); // hover
             * };
             * squirrelMesh.UpdateAction( squirrelMesh );
             */
            //sm.writeOBJ();
            meshes.Add(squirrelMesh);

            turtleMesh         = new SphereMesh(BALL_RADIUS * 5, 10, 10, true);
            turtleMesh.Texture = angryTurtleTexture;

            /*
             * turtleMesh.UpdateAction = delegate( Mesh model ) {
             *  Matrix4f transform = model.Transform;
             *  transform.setScaleAndRotation( Matrix4.CreateRotationX( -time.Value ) );
             * };
             * turtleMesh.UpdateAction( turtleMesh );
             */
            meshes.Add(turtleMesh);

            dilloMesh         = new SphereMesh(BALL_RADIUS * 2, 20, 20, true);
            dilloMesh.Texture = angryDilloTexture;

            /*
             * dilloMesh.UpdateAction = delegate( Mesh model ) {
             *  Matrix4f transform = model.Transform;
             *  transform.setScaleAndRotation( Matrix4.CreateRotationX( -time.Value ) );
             * };
             * dilloMesh.UpdateAction( dilloMesh );
             */
            meshes.Add(dilloMesh);

            float boxX = 100, boxY = 50, boxZ = 100, shiftX = 0.5f * boxX, shiftY = 0.5f * boxY, shiftZ = 0.5f * boxZ;

            worldMeshes = new RectangleMesh[] {
                new RectangleMesh(Vector3f.OZ, boxX, boxY),
                new RectangleMesh(Vector3f.OZ, boxX, -boxY),
                new RectangleMesh(Vector3f.OY, boxX, boxZ),
                new RectangleMesh(Vector3f.OY, -boxX, boxZ),
                new RectangleMesh(Vector3f.OZ, -boxX, boxY),
                new RectangleMesh(Vector3f.OZ, boxX, boxY)
            };
            for (int i = worldMeshes.Length - 1; i >= 0; i--)
            {
                Mesh m = worldMeshes[i];
                m.Texture = worldTextures[i];
                meshes.Add(m);
            }
            Matrix4f trans;

            trans = worldMeshes[0].Transform;
            trans.setZero();
            trans.Data[2]  = 1;
            trans.Data[5]  = 1;
            trans.Data[8]  = 1;
            trans.Data[15] = 1;
            trans.setTranslation(shiftX, shiftY, 0);
            trans = worldMeshes[1].Transform;
            trans.setZero();
            trans.Data[2]  = -1;
            trans.Data[5]  = -1;
            trans.Data[8]  = -1;
            trans.Data[15] = 1;
            worldMeshes[1].Transform.setTranslation(-shiftX, shiftY, 0);
            worldMeshes[2].Transform.setTranslation(0, boxY, 0);
            worldMeshes[3].Transform.setTranslation(0, 0, 0);
            worldMeshes[4].Transform.setTranslation(0, shiftY, shiftZ);
            worldMeshes[5].Transform.setTranslation(0, shiftY, -shiftZ);

            foreach (Mesh m in meshes)
            {
                m.init();
            }

            //bodyManager.Gravity.setZero();
            /*SphereBody*/
            squirrelBody = new SphereBody(1, BALL_RADIUS);
            squirrelBody.Transform.setTranslation(40, 40, 40);
            squirrelBody.RotationSpeed = 1f;
            /*SphereBody*/
            turtleBody = new SphereBody(25, BALL_RADIUS * 5);
            turtleBody.Transform.setTranslation(0, 20, 0);
            //turtleBody.Velocity.Y = 5;
            turtleBody.RotationSpeed = 1f;
            /*SphereBody*/
            dilloBody = new SphereBody(25, BALL_RADIUS * 2);
            //dilloBody.Transform.setTranslation( -20, 100, -30 );
            dilloBody.Transform.setTranslation(0, 30, 0);
            //dilloBody.Velocity.Y = -5;
            dilloBody.RotationSpeed = 1f;
            Vector3f fixPoint = new Vector3f(0, 42, 9);

            /*
             * // spring - vertical harmonic oscillator
             * dilloBody.Forces.Add( delegate( Body obj ) {
             *  Vector3f disp = obj.Transform.getDisplacement( fixPoint );
             *  float k = 20.0f;
             *  disp.scale( k );
             *  obj.applyForce( disp );
             * } );
             */

            // springy pendulum - 3D harmonic oscillator
            dilloBody.Forces.Add(delegate(Body obj) {
                Vector3f disp = obj.Transform.getDisplacement(fixPoint);
                float k       = 10.0f, l = 15.0f;
                disp.scale(k * (disp.length() - l));
                obj.applyForce(disp);
            });

            planeBodies = new PlaneBody[] {
                new PlaneBody(new Plane3f(new Vector3f(0, 1, 0), 0)),
                new PlaneBody(new Plane3f(new Vector3f(0, -1, 0), boxY)),
                new PlaneBody(new Plane3f(new Vector3f(1, 0, 0), shiftX)),
                new PlaneBody(new Plane3f(new Vector3f(-1, 0, 0), shiftX)),
                new PlaneBody(new Plane3f(new Vector3f(0, 0, 1), shiftZ)),
                new PlaneBody(new Plane3f(new Vector3f(0, 0, -1), shiftZ))
            };

            foreach (PlaneBody pb in planeBodies)
            {
                pb.Friction = 0.1f;
                bodyManager.addBody(pb);
            }
            bodyManager.addBody(squirrelBody, squirrelMesh);
            bodyManager.addBody(turtleBody, turtleMesh);
            bodyManager.addBody(dilloBody, dilloMesh);

            VSync = VSyncMode.On;
        }