Jump to content

  • Log In with Google      Sign In   
  • Create Account


[SharpDX] Unable to render texured models using BasicEffect

  • You cannot reply to this topic
6 replies to this topic

#1 Lordadmiral Drake   Members   -  Reputation: 131

Like
0Likes
Like

Posted 14 August 2014 - 04:57 PM

I finished porting my game project from XNA to SharpDX. Since the shader file I used in XNA (which was from riemers tut) doesn't work here anymore I wanted to use SharpDX's BasicEffect shader for rendering until I get myself into shader coding.

 

Unfortunatly this only works as long as I keep textures disabled.

If I set TextureEnabled = true the program crashes as soon as it tries to render a model with the following error:

 

D3D11 ERROR: ID3D11Device::CreateInputLayout: The provided input signature expects to read an element with SemanticName/Index: 'TEXCOORD'/0, but the declaration doesn't provide a matching name. [ STATE_CREATION ERROR #163: CREATEINPUTLAYOUT_MISSINGELEMENT]

 

Here's the code of my model render class:

using MetastormDX.Services;
using SharpDX;
using SharpDX.Direct3D11;
using SharpDX.Toolkit.Graphics;

namespace MetastormDX.Models
{
    public static class ModelDrawer
    {
        /// <summary>
        /// Generic model drawing method.
        /// </summary>
        /// <param name="model">Model to be drawn.</param>
        /// <param name="modelTexture">Model textures.</param>
        /// <param name="modelPos">Current position of the model in world space.</param>
        /// <param name="modelRot">Current rotation of the model.</param>
        public static void DrawModel(Model model, Texture2DBase[] modelTexture, Vector3 modelPos, Quaternion modelRot)
        {
            Matrix worldMatrix = Matrix.Scaling(0.05f, 0.05f, 0.05f) * Matrix.RotationQuaternion(modelRot) *
                Matrix.Translation(modelPos);

            Matrix[] modelTransforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(modelTransforms);

            int i = 0;
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    BasicEffect currentEffect = effect;
                    currentEffect.View = ScreenManager.ScreenManager.ViewMatrix;
                    currentEffect.Projection = ScreenManager.ScreenManager.ProjectionMatrix;
                    currentEffect.World = worldMatrix;
                    currentEffect.TextureView = modelTexture[i++];
                    currentEffect.TextureEnabled = true;
                    currentEffect.PreferPerPixelLighting = true;
                    currentEffect.EnableDefaultLighting();

                    currentEffect.CurrentTechnique.Passes[0].Apply();
                }

                mesh.Draw(GameServices.Graphics);
            }
        }
    }
}



Sponsor:

#2 lwm   Members   -  Reputation: 1383

Like
2Likes
Like

Posted 15 August 2014 - 01:33 AM

When you set TextureEnabled to true, BasicEffect will switch to a version of the shader that expects your vertices' texture coordinates to be in element "TEXCOORD0". However, according to the error message, your vertices don't provide any coordinates with that name. How are you loading the model and what vertex type are you using? If you're using a custom one, maybe you forgot to add the attribute to the struct's field that holds the texture coordinates?


current project: Roa


#3 Lordadmiral Drake   Members   -  Reputation: 131

Like
0Likes
Like

Posted 15 August 2014 - 01:47 AM

This is my model loading code:

public Model LoadModel(string assetName, Effect effect, out Texture2D[] textures)
        {
            int i = 0;
            Model newModel = GameServices.Content.Load<Model>(assetName);
            foreach (ModelMesh mesh in newModel.Meshes)
                foreach (ModelMeshPart meshPart in mesh.MeshParts)
                    i++;
            textures = new Texture2D[i];
            i = 0;
            foreach (ModelMesh mesh in newModel.Meshes)
                foreach (var effect1 in mesh.Effects)
                {
                    var currentEffect = (BasicEffect) effect1;
                    textures[i++] = currentEffect.Texture as Texture2D;
                }

            foreach (ModelMesh mesh in newModel.Meshes)
                foreach (ModelMeshPart meshPart in mesh.MeshParts)
                    meshPart.Effect = effect.Clone();

            return newModel;
        }

The models are compiled .tkb

The model as retured in newModel is stored in the respective class instance where I need it and passed to the draw code as is. 



#4 lwm   Members   -  Reputation: 1383

Like
1Likes
Like

Posted 15 August 2014 - 03:06 AM

When you inspect the loaded model in the debugger, what do the InputElements in the VertexInputLayouts look like? Do they include a "TEXCOORD" element? If they don't, are you sure that your model contains texture coordinates?

The SharpDX Toolkit automatically chooses an appropriate Vertex struct and corresponding VertexInputLayout for you. If there is no InputElement for texture coordinates, the model compiler probably wasn't able to find any texture coordinates in the source model.


Edited by lwm, 15 August 2014 - 03:07 AM.

current project: Roa


#5 Lordadmiral Drake   Members   -  Reputation: 131

Like
0Likes
Like

Posted 15 August 2014 - 04:37 PM

It shows me 2 InputElements: SV_POSITION and NORMAL

 

Gotta investigate this

 

BTW: The models were created in 3dsMax 2014, exported to .fbx and then converted with tkmodel.exe


Edited by Lordadmiral Drake, 15 August 2014 - 04:54 PM.


#6 Lordadmiral Drake   Members   -  Reputation: 131

Like
0Likes
Like

Posted 16 August 2014 - 05:21 PM

So, as a test I tried exporting a model as .obj instead of .fbx before exporting. And voila, the missing parameters are there ^^



#7 Lordadmiral Drake   Members   -  Reputation: 131

Like
0Likes
Like

Posted 17 August 2014 - 01:38 PM

So, I reexported and converted all my models. Now the render code doesn't crash anymore. But all models are plain black instead of textured. Appearantly the textures are not loaded. All texture variable values show "null"

 

EDIT: Finally, textures are working :D Had to put them in the same folder as the model, set build action to none on the textures and ToolkitModel on the model.


Edited by Lordadmiral Drake, 18 August 2014 - 12:06 PM.






PARTNERS