Advertisement

Model Loading Beginner.

Started by October 14, 2018 08:32 PM
1 comment, last by 1024 5 years, 11 months ago

Hello!

I'm trying to understand how to load models with Assimp. Well learning how to use this library isn't that hard, the thing is how to use the data. From what I understand so far, each model consists of several meshes which you can render individually in order to get the final result (the model). Also from what assimp says:


One mesh uses only a single material everywhere - if parts of the model use a different material, this part is moved to a separate mesh at the same node

The only thing that confuses me is how to create the shader that will use these data to draw a mesh. Lets say I have all the information about a mesh like this:


class Meshe
{
  std::vector<Texture> diffuse_textures;
  std::vector<Texture> specular_textures;

  std::vector<Vertex> vertices;
  std::vector<unsigned int> indices;
}

And lets make the simplest shaders:

 

Vertex Shader:


#version 330 core

layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;
layout(location = 2) in vec2 aTexCoord;


uniform vec3 model;
uniform vec3 view;
uniform vec3 projection;

out vec2 TextureCoordinate;
out vec3 Normals;


void main()
{
  gl_Position       = projection * view * model * vec4(aPos, 1.0f);
  TextureCoordinate = aTexCoord
  Normals           = normalize(mat3(transpose(inverse(model))) * aNormal);
}

Fragment Shader:


#version 330 core
  
out vec4 Output;
in  vec2 TextureCoordinate;
in  vec3 Normals;

uniform sampler2D diffuse;
uniform sampler2D specular;


void main()
{
	Output = texture(diffuse, TextureCoordinate);
}

 

Will this work? I mean, assimp says that each mesh has only one material that covers it, but that material how many diffuse and specular textures can it have? Does it makes sense for a material to have more than one diffuse or more that one specular textures?  If each material has only two textures, one for the diffuse and one for the specular then its easy, i'm using the specular texture on the lighting calculations and the diffuse on the actual output.

But what happens if the textures are more? How am i defining them on the fragment shader without knowing the actual number? Also how do i use them? 


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

If you have multiple textures, you need to have multiple samplers in your shader. Then in the shader you will calculate the final color by somehow combining all the samplers.

So you need to have multiple different shaders for meshes with different numbers of textures. (Although, there are some more advanced ways to avoid that, but I would say that they are too advanced to worry about them right now :) ) When you load a mesh, you remember how many textures it has, and then when it's time to draw it, you use the right shader to draw it.

This topic is closed to new replies.

Advertisement