Effects.fx in Monogame

Started by
1 comment, last by JustJim 9 years, 7 months ago

Hey!

As I am programming my little space shooter and gone through many changes containing the planets (changing them from Models to Vertexbased Icospheres) I came across a problem I could not solve yet.

I want to have lighting on my Planets but as basicEffect.lightingenabled = true won't work (I guess it's due to the fact it's no model),

I need to make an own shader to make pixel-based-lighting.

As I am programming with Monogame which is like XNA I tried to read through tutorials which gives solutions to that.

I came across Riemers tutorials who uses a effects.fx-file.

According to the tutorial, I added the file to the Game1ContentContent (yes in Monogame there will be 2 Content Projects of which the latter one is the one to use for the files) and loaded it via Effect effect = Content.Load<Effect>("effects")

But this gives me huge problems.

First I encountered the problem that VS2013 told me that this is not a valid mgfx file.

I stumbled through the net and found that I need to change the version to ps_4_0 and vs_4_0 as well as POSITION and POSITION0 to SV_POSITION.

After that I got an error, telling me that I only could use version 3.X or earlier.

Changing them to ps/vs_3_0 it looked like it was working: Window popped up and seemed to load but after a couple of seconds it

crashes, telling me "Could not load effects asset as a non-content file!", which suprises me, as it is in the content folder.

What can I do about this?

Advertisement


I want to have lighting on my Planets but as basicEffect.lightingenabled = true won't work (I guess it's due to the fact it's no model),

As long as you're using the effect to draw, it doesn't matter if it's a "Model" class or not. Just make sure your icosphere vertices have the necessary information - specifically, they need a normal component in order to have 3d lighting work.

Well okay, I guess, I am not that into it.

I'll post my code, so you can see if it has the necessary information.


protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            basicEffect = new BasicEffect(GraphicsDevice);
            List<VertexPositionColor> vertices = new List<VertexPositionColor>();

            float t = (float)(1.0 + Math.Sqrt(5.0f)) / 2.0f;
            
            vertices = AddNormalizedVertex(vertices, new Vector3(-1, 0, t), Color.Gray);
            vertices = AddNormalizedVertex(vertices, new Vector3(1, 0, t), Color.Gray);
            vertices = AddNormalizedVertex(vertices, new Vector3(-1, 0, -t), Color.Gray);
            vertices = AddNormalizedVertex(vertices, new Vector3(1, 0, -t), Color.Gray);


            vertices = AddNormalizedVertex(vertices, new Vector3(0, t, 1), Color.Gray);
            vertices = AddNormalizedVertex(vertices, new Vector3(0, t, -1), Color.Gray);
            vertices = AddNormalizedVertex(vertices, new Vector3(0, -t, 1), Color.Gray);
            vertices = AddNormalizedVertex(vertices, new Vector3(0, -t, -1), Color.Gray);


            vertices = AddNormalizedVertex(vertices, new Vector3(t, 1, 0), Color.Gray);
            vertices = AddNormalizedVertex(vertices, new Vector3(-t, 1, 0), Color.Gray);
            vertices = AddNormalizedVertex(vertices, new Vector3(t, -1, 0), Color.Gray);
            vertices = AddNormalizedVertex(vertices, new Vector3(-t, -1, 0), Color.Gray);

            List<int> indices = new List<int>();

            indices.Add(0); indices.Add(6); indices.Add(1);
            indices.Add(0); indices.Add(11); indices.Add(6);
            indices.Add(1); indices.Add(4); indices.Add(0);
            indices.Add(1); indices.Add(8); indices.Add(4);
            indices.Add(1); indices.Add(10); indices.Add(8);

            indices.Add(2); indices.Add(5); indices.Add(3);
            indices.Add(2); indices.Add(9); indices.Add(5);
            indices.Add(2); indices.Add(11); indices.Add(9);
            indices.Add(3); indices.Add(7); indices.Add(2);
            indices.Add(3); indices.Add(10); indices.Add(7);

            indices.Add(4); indices.Add(8); indices.Add(5);
            indices.Add(4); indices.Add(9); indices.Add(0);
            indices.Add(5); indices.Add(8); indices.Add(3);
            indices.Add(5); indices.Add(9); indices.Add(4);
            indices.Add(6); indices.Add(10); indices.Add(1);

            indices.Add(6); indices.Add(11); indices.Add(7);
            indices.Add(7); indices.Add(10); indices.Add(6);
            indices.Add(7); indices.Add(11); indices.Add(2);
            indices.Add(8); indices.Add(10); indices.Add(3);
            indices.Add(9); indices.Add(11); indices.Add(0);

            List<int> indices2 = new List<int>();
            for (int j = 0; j <4 ; j++)
            {
                
                for (int i = 0; i < indices.Count; i++)
                {
                    Vector3 p1 = vertices[indices[i]].Position;
                    int p1I = (int)i++;
                    Vector3 p2 = vertices[indices[i]].Position;
                    int p2I = (int)i++;
                    Vector3 p3 = vertices[indices[i]].Position;
                    int p3I = (int)i;

                    int a = (int)vertices.Count;
                    vertices = AddNormalizedVertex(vertices, new Vector3(((p1.X + p2.X) / 2), (p1.Y + p2.Y) / 2, (p1.Z + p2.Z) / 2), Color.Green);

                    int b = (int)vertices.Count;
                    vertices = AddNormalizedVertex(vertices, new Vector3((p2.X + p3.X) / 2, (p2.Y + p3.Y) / 2, (p2.Z + p3.Z) / 2), Color.Yellow);

                    int c = (int)vertices.Count;
                    vertices = AddNormalizedVertex(vertices, new Vector3((p3.X + p1.X) / 2, (p3.Y + p1.Y) / 2, (p3.Z + p1.Z) / 2), Color.Red);

                    indices2.Add(indices[p1I]); indices2.Add(c); indices2.Add(a);
                    indices2.Add(indices[p2I]); indices2.Add(a); indices2.Add(b);
                    indices2.Add(indices[p3I]); indices2.Add(b); indices2.Add(c);
                    indices2.Add(b); indices2.Add(a); indices2.Add(c);

                }
                indices.Clear();
                foreach (int index in indices2)
                {
                    indices.Add(index);
                }
            }
            vertexList = new VertexPositionColor[vertices.Count];
            for (int i = 0; i < vertices.Count; i++)
            {
                vertexList[i] = vertices[i];
            }

            indexList = new int[indices.Count];
            for (int i = 0; i < indices.Count; i++)
            {
                indexList[i] = indices[i];
            }

            vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), vertexList.Length, BufferUsage.WriteOnly);
            vertexBuffer.SetData<VertexPositionColor>(vertexList);
            indexBuffer = new IndexBuffer(graphics.GraphicsDevice, typeof(int), indexList.Length, BufferUsage.WriteOnly);
            indexBuffer.SetData(indexList);


            base.Initialize();
        }

Thats the Code for the Icosphere (building a Icosahedron and sudivide the vertices).


protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.DarkSlateBlue);

            DrawPrimitive(vertexList);

            base.Draw(gameTime);
        }
        protected void DrawPrimitive(VertexPositionColor[] vertices)
        {
            basicEffect.VertexColorEnabled = true;
            basicEffect.World = world;
            basicEffect.View = view;
            basicEffect.Projection = projection;
           
            GraphicsDevice.SetVertexBuffer(vertexBuffer);
            GraphicsDevice.Indices = indexBuffer;

            RasterizerState rasterizerState = new RasterizerState();
            rasterizerState.CullMode = CullMode.None;
            GraphicsDevice.RasterizerState = rasterizerState;
           
            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                
                GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexList.Length, 0, indexList.Length / 3);
            }
        }

And thats the draw Methodes. if I type in basicEffect.EnableLighting = true; It will crash.

Another thing just came to my mind, if I Enable Culling, there are some weird effects in the icosphere as some triangles won't be drawn correctly.

But either way, even with only 0 iteration (pure Icosahedron without subdividing) there is this crash :/

The error it gives me is this:

"SharpDX.SharpDXException" in SharpDX.dll

HRESULT: [0x80070057], Modules: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: Invalide Parameter.

This topic is closed to new replies.

Advertisement