Texture mapping with shader pipeline

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
stefany
Posts: 58
Joined: Wed Dec 07, 2011 10:50 am

Texture mapping with shader pipeline

Post by stefany »

Hi, friends, i'm playing with the shader pipeline, and i have two questions.

Why i don't get the correct vertex coordinates with this:
Where i define my vertex descriptor for a S3DVertex

Code: Select all

 
IVertexDescriptor* ShaderFloor::getVertexDescriptor(IVideoDriver* driver)
{
  IVertexDescriptor* descriptor = driver->getVertexDescriptor(SHADER_FLOOR_VERTEX_FORMAT_NAME);
  if(descriptor==nullptr) 
  {
    descriptor = driver->addVertexDescriptor(SHADER_FLOOR_VERTEX_FORMAT_NAME);
    descriptor->addAttribute("inPosition",3,video::EVAS_POSITION,video::EVAT_FLOAT,0);
    descriptor->addAttribute("inNormal",3,video::EVAS_NORMAL,video::EVAT_FLOAT,0);
    descriptor->addAttribute("inColor",1,video::EVAS_COLOR,video::EVAT_UINT,0);
    descriptor->addAttribute("inTexCoord",2,video::EVAS_TEXCOORD0,video::EVAT_FLOAT,0);
  }
  
  return descriptor;
}
 
Then in some part of my code i set the descriptor

Code: Select all

 
void Plane::setVertexDescriptor(IVertexDescriptor* descriptor)
{
  mMeshBuffer->setVertexDescriptor(descriptor);
}

my vertexShader

Code: Select all

 
#version 120
uniform mat4 uWorldViewProj;
attribute vec3 inPosition;
attribute vec3 inNormal;
attribute int inColor;
attribute vec2 inTexCoord;
varying vec2 vTexCoord;
void main ()
{
  gl_Position = uWorldViewProj * vec4(inPosition,1.0f);
  vTexCoord=inTexCoord;
}
 
fragment shader

Code: Select all

 
#version 120
uniform sampler2D uTex0;
varying vec2 vTexCoord;
void main ()
{
  gl_FragColor = texture2D(uTex0,vTexCoord);
}
 
I tested it and the mesh is drawn, but the color is just a plain color. The texture is also correct.

In the shader pipeline is this function correct? CMeshBuffer.h

Code: Select all

        virtual bool setVertexDescriptor(video::IVertexDescriptor* vertexDescriptor)
        {
            if (vertexDescriptor && vertexDescriptor != VertexDescriptor)
            {
                if (VertexDescriptor)
                    VertexDescriptor->drop();
 
                VertexDescriptor = vertexDescriptor;
                VertexDescriptor->grab();
 
                bool vertexBufferCompatible = true;
 
                for (u32 i = 0; i < VertexBuffer.size(); ++i)
                {
                    if (VertexDescriptor->getVertexSize(i) != VertexBuffer[i]->getVertexSize())
                    {
                        vertexBufferCompatible = false;
                        break;
                    }
                }
 
                VertexBufferCompatible = vertexBufferCompatible;
 
                return true; /// <-- HERE
            }
 
            return false;
        }
Where is the comment "HERE", I think it should be "return VertexBufferCompatible;" is that right?
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Texture mapping with shader pipeline

Post by Nadro »

Hi brkpnt,

If you need a vertex descriptor for S3DVertex you can get them by:

Code: Select all

VideoDriver->getVertexDescriptor(0);
Can you check if your mesh works fine with that vertex descriptor?

As I see this is the only difference between your vertex descriptor and standard vertex descriptor:

Code: Select all

descriptor->addAttribute("inColor",1,video::EVAS_COLOR,video::EVAT_UINT,0); // your descriptor
vs

Code: Select all

descriptor->addAttribute("inColor", 4, EVAS_COLOR, EVAT_UBYTE, 0); // standard descriptor for S3DVertex
Yes, you're right about "HERE", it should return "VertexBufferCompatible" instead of "true".
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
stefany
Posts: 58
Joined: Wed Dec 07, 2011 10:50 am

Re: Texture mapping with shader pipeline

Post by stefany »

Semi-solved... that means i solved it but i don't undestand why what i did wrong...

I changed this...

Code: Select all

descriptor->addAttribute("inTexCoord",2,video::EVAS_TEXCOORD0,video::EVAT_FLOAT,0);
to this

Code: Select all

descriptor->addAttribute("inTexCoord",2,video::EVAS_CUSTOM,video::EVAT_FLOAT,0);
I changed the semantic. When i use TEXCOORD0, the coordinates doesn't change, are the same for every vertex.
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Texture mapping with shader pipeline

Post by Nadro »

It looks like a bug. I'll check that when I'll back to shader-pipeline. At now I focus at ogl-es branch and trunk -> tasks related to v1.9 release.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
Post Reply