hi everyone,
i’m asking myself how to best manage the data i need to draw a simple scene, with lighting (phong shaded)
i’m currently using:
struct Vertex {
vec3 Position;
vec3 Normal;
vec2 TexCoord;
unsigned int MaterialIndex;
};
struct Material {
vec4 Diffuse;
vec4 Specular;
float Shininess, Alpha;
int map_DiffuseIndex, map_SpecularIndex;
// padding etc
};
first i collect all the models meshes (triangularized faces) and Materials, then i put ALL vertices of all meshes into 1 vertexbuffer used by 1 vertexarray, and ALL materials into 1 materialbuffer, but that materialbuffer is NOT part of the vertexarray
vertexshader looks like that:
in layout (location = 0) vec3 in_position;
in layout (location = 1) vec3 in_normal;
in layout (location = 2) vec2 in_texcoord;
in layout (location = 3) uint in_materialindex;
flat out uint materialindex; // to fragmentshader
fragmentshader looks like that:
struct Material {
vec4 Diffuse;
vec4 Specular;
float Shininess, Alpha;
int map_DiffuseIndex, map_SpecularIndex;
};
layout (std140, binding = 1) uniform MaterialBlock {
Material materials[256];
};
in the fragmentshader i access the material array backed by an uniform buffer, using the passed “uint materialindex”
that works perfectly, but im asking myself how to best use meshes with different (!!!) materials
lets say i want to render earth, and the moon, i have 1 mesh (sphere), but i want to apply 2 different materials (earth texture and later the moon texture), the problem is that using that vertex layout, i have to put 2 times the same mesh data but with 2 different “materialIndex” attributes
whats a practical solution for that problem ? should i separate the “uint materialindex” into a separate buffer ? should i put all the material attributes as “vertex attributes” (and avoid completely the uniform block for materials) ? how would you approach that task ? (rendering a sphere with [many] different materials) ??
thanks for all your ideas!