0

I realised that models can be loaded in many different ways. Some might have no textures and just use vertex colours, while others have materials with textures. Now, I have one shader that is used to draw 3d models with lighting but how would I go about loading models that are required to be rendered differently and have them rendered correctly in the scene.

One method I thought of is having hard defined attribute pointers in the vertex shader and that if the model doesn't require binding an attribute, it doesn't have to. I would imagine that if an attribute isn't binded to and is used in calculations, it won't contribute or offset any values (especially if you do the calculations separately and sum them in the end (for example, calculating the colour of the pixel for the vertex colour at that fragment and summing that for the colour of the pixel for the texture)

You can imagine the vertex shader looking like:

#version 330 core
layout (location = 0) in vec3 aPos; // Vertex positions
layout (location = 1) in vec3 aNormal; // Normals
layout (location = 2) in vec2 aTexCoords; // Optional Texture coordinates 
layout (location = 3) in vec3 aColors; // Optional vertex colours

out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoords;
out vec3 Colors;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    FragPos = vec3(model * vec4(aPos, 1.0));
    Normal = mat3(transpose(inverse(model))) * aNormal;  
    TexCoords = aTexCoords;
    Colors = aColors;

    gl_Position = projection * view * vec4(FragPos, 1.0);
}

An issue I can see with this is I'm not sure how I could tell whether to use texCoords or colours or both. Perhaps using a uniform as a flag and keeping that data in the model object?

Another method would be by defining shaders for different types of lighting techniques and then determining which shader is called for which model. This is my least favourite as it doesn't allow for dynamic approaches (i.e both vertex colours and textures at the same time)

What would be the approach done in a professional setting so that any model can be loaded and drawn into a scene with lighting in the same vain as game engines manage? (Using forward rendering right now so don't need any deffered shading concepts as that's above my paygrade at the moment). I'm not seeking specific solutions, more of an experts knowledge of how this issue is approached.

Also, I'm using OpenGL 3.3

  • 1
    This does not answer your opengl question, but the more explicitly you want to do things, the more I recommend to at least find out what "Vulkan" does. It aims at allowing to do things more explicitly and in the end faster - not however to do things easier... – Yunnosch Jan 13 '20 at 17:38
  • 3
    "What would be the approach done in a professional setting so that any model can be loaded and drawn into a scene with lighting in the same vain as game engines manage?" Game engines don't do that - nobody can. Game engines are closely tied to their respective asset pipelines, and you can't import just _any_ model, especially _any_ material. The material parameters only make sense in for specific lighting models, and without any knowledge of that information (usually not stored in the files), it is just impossible to render it "correctly". – derhass Jan 13 '20 at 17:45
  • @derhass could you elaborate what you mean by asset pipeline specifically? As this could mean different things in different contexts. Also, it's moreso the fact that I can import a model that uses vertex colours and a model that uses diffuse, ambient, specular maps and I guess that Unity or Unreal can pick up on that. I was wondering how they pick up on that and send the right data over to their shaders – Samuel Innocent-Primus Mungy Jan 13 '20 at 18:19
  • 1
    Well, with "asset pipeline" I just mean all the tools and the actual _workflows_ which are used by the various artists to create content/assets for use with these engines. "model that uses diffuse, ambient, specular maps" that assumes a lighting model that has these three components - these aren't used any more. The trend is physically based shading (also to increase interchangablity between different engines / asset pipelines), where you typically have some albedo, roughness and "metalness" parameters. But the actual models and hence meaning of the parameters varies. – derhass Jan 13 '20 at 18:26
  • In the end, it boils down to something like this: the data stored in those model files are just _parameters_ / _inputs_ in some formula. And with programmable shading, this formula is not baked in to your GPUs any more (we had that two decades ago), and also not written down in some definitive spec, but each and everyone can - and will - write their own formulas. Without knowing the formulas, the parameters don't have any _meaning_ on their own. – derhass Jan 13 '20 at 18:36
  • @derhass Alright, I understand. But I'm still left wondering, so I've loaded a model using assimp. I have vertex colours and some textures. Should I just then not render any lighting on the model until an end-user has set that up using specific in-engine tools? I mean, Assimp even allows you to pick up diffuse and ambient textures from the model. Can't that be used in the lighting model? Abd what of the shader that renders the model? Should it be like the example above whereby I have predetermined vertex attribute pointers for all models so that I don't have to write a shader for every model – Samuel Innocent-Primus Mungy Jan 13 '20 at 18:37
  • and by that I mean for every model, location 2 refers to texture coords and location 3 refers to vertex colours, etc I just want a model to be loaded without having a custom shader written to draw that model with its textures and colours – Samuel Innocent-Primus Mungy Jan 13 '20 at 18:39
  • @SamuelInnocent-PrimusMungy If you just want "some lighting", then treat whatever colour information you have as diffuse. – user253751 Jan 14 '20 at 10:20

1 Answers1

1

What would be the approach done in a professional setting so that any model can be loaded and drawn into a scene with lighting in the same vain as game engines manage?

Either this:

defining shaders for different types of lighting techniques and then determining which shader is called for which model

or this:

having hard defined attribute pointers in the vertex shader and that if the model doesn't require binding an attribute, it doesn't have to

Pick one. In the second approach, you don't need flags - you can just bind dummy values, like a plain white texture if the model has no texture, and a white vertex colour if the model has no vertex colours.

user253751
  • 57,427
  • 7
  • 48
  • 90