Some questions about materials and meshes
-
- Posts: 57
- Joined: Sat Oct 11, 2014 11:07 pm
Some questions about materials and meshes
I am pretty new to Irrlicht and I am trying to understand how materials work when combined with meshes.
1. There doesn't seem to be a way to set the material for a mesh/node. I can get the material but I can't set it. I can only set a mesh texture, material flags, or a material type. I have an SMaterial that I want to apply to a mesh. How does one go about doing that?
2. If I wanted to apply a different textures/materials to certain faces of a mesh would I need to create my own custom mesh? For example. Lets say I have a cube and I want to set a different texture, or even a different material to each face on the cube. There doesn't seem to be a way to do that using the cube node (ISceneManager::addCubeSceneNode(....)).
3. Lets say I want to have an object that is a solid color. Would I need to create a mesh that has per vertex colors or could I accomplish the same thing using a material (so that I can use the same mesh with a texture if I really wanted too)?
4. When I assign a texture/material texture to a mesh, such as a sphere or cube, or other mesh, how do the texture indices get calculated? For example, if I have a "world" texture to apply to a sphere, does it automatically know to wrap the texture around the sphere? What if I were to apply that same texture to a cube? How does it know which vertices are "up"? Or does it apply the same texture to each face of the cube?
1. There doesn't seem to be a way to set the material for a mesh/node. I can get the material but I can't set it. I can only set a mesh texture, material flags, or a material type. I have an SMaterial that I want to apply to a mesh. How does one go about doing that?
2. If I wanted to apply a different textures/materials to certain faces of a mesh would I need to create my own custom mesh? For example. Lets say I have a cube and I want to set a different texture, or even a different material to each face on the cube. There doesn't seem to be a way to do that using the cube node (ISceneManager::addCubeSceneNode(....)).
3. Lets say I want to have an object that is a solid color. Would I need to create a mesh that has per vertex colors or could I accomplish the same thing using a material (so that I can use the same mesh with a texture if I really wanted too)?
4. When I assign a texture/material texture to a mesh, such as a sphere or cube, or other mesh, how do the texture indices get calculated? For example, if I have a "world" texture to apply to a sphere, does it automatically know to wrap the texture around the sphere? What if I were to apply that same texture to a cube? How does it know which vertices are "up"? Or does it apply the same texture to each face of the cube?
Re: Some questions about materials and meshes
1. getMaterial returns a reference - so any changes you do to that reference are set.
2. Yes. You need a cube with 6 different materials in that case. Which means it has to use 6 meshbuffers. The default-cube doesn't do that.
3. You can use a single-colored texture or use material values. It's probably best to play around a little bit with the MaterialViewer example to figure out what's going on. Especially the differences when light is enabled/disabled. (also a little easier if you use the MaterialViewer example from svn trunk as that has been simplified - old one was more confusing)
4. Good question. I think the default uv's are set in some way which makes some sense (I think for cube for example the texture will show on each side). Would have to experiment myself what happens with sphere - I only ever used that for quick tests without texture. If you want more control you will have to create your own meshes.
2. Yes. You need a cube with 6 different materials in that case. Which means it has to use 6 meshbuffers. The default-cube doesn't do that.
3. You can use a single-colored texture or use material values. It's probably best to play around a little bit with the MaterialViewer example to figure out what's going on. Especially the differences when light is enabled/disabled. (also a little easier if you use the MaterialViewer example from svn trunk as that has been simplified - old one was more confusing)
4. Good question. I think the default uv's are set in some way which makes some sense (I think for cube for example the texture will show on each side). Would have to experiment myself what happens with sphere - I only ever used that for quick tests without texture. If you want more control you will have to create your own meshes.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: Some questions about materials and meshes
CuteAlien covered the fixed side. When using shaders, 2 and 3 can be done more efficiently:
2) One big mesh with some way to detect which texture goes to which face, then map in the shader.
3) You'd set the color in the shader.
/OT So many minecraft clones.
2) One big mesh with some way to detect which texture goes to which face, then map in the shader.
3) You'd set the color in the shader.
/OT So many minecraft clones.
-
- Posts: 57
- Joined: Sat Oct 11, 2014 11:07 pm
Re: Some questions about materials and meshes
So basically I can't set the whole thing at once. Instead, I need to copy the properties from one to the other?CuteAlien wrote:1. getMaterial returns a reference - so any changes you do to that reference are set.
Thanks for your help.
Re: Some questions about materials and meshes
You can do getMaterial(0) = myMaterial to set the whole first material to myMaterial.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Posts: 57
- Joined: Sat Oct 11, 2014 11:07 pm
Re: Some questions about materials and meshes
Ok Thanks,
This is a bit off my original topic but it has been spawned by it. I need to create my own meshes in many circumstances. So I need to determine how to position the mesh properly. Does the Irrlicht engine automatically center the object on the node, or will I need to do that myself?
For example. Lets say I have a cube with a side length of n at position 0,0,0. Does Irrlicht automatically adjust it so that center of the cube is at that position or do I need to do this manually with my code so that it knows to draw the cube at -n/2, -n/2, -n/2 ?
This is a bit off my original topic but it has been spawned by it. I need to create my own meshes in many circumstances. So I need to determine how to position the mesh properly. Does the Irrlicht engine automatically center the object on the node, or will I need to do that myself?
For example. Lets say I have a cube with a side length of n at position 0,0,0. Does Irrlicht automatically adjust it so that center of the cube is at that position or do I need to do this manually with my code so that it knows to draw the cube at -n/2, -n/2, -n/2 ?
Re: Some questions about materials and meshes
Default cubes are centered around 0.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Posts: 57
- Joined: Sat Oct 11, 2014 11:07 pm
Re: Some questions about materials and meshes
Yes... but is this done manually in the code with each vertex when the cube is created? Or does Irrlicht always center the bounding box on the position? Because I need to make my own cubes etc...
Re: Some questions about materials and meshes
The engine simply uses the vertex coordinates. If one vertex coordinate is given as -0.5, -0.5, -0.5 then it's placed exactly there. And if you add a node-position that is added to those values. This stuff has nothing to do with bounding-boxes. Boundingboxes are calculated based on the vertices, not the other way round - so bounding-boxes have nothing to do with positioning.
The code is open-source so you can take a look. It's the first function in CGeometryCreator.cpp (no worries, it's not complicated). Note that in there it's done in 2 steps. First the cube is created at 0,0,0 to 1,1,1 and then moved to -0.5,-0.5,-0.5 to 0.5, 0.5, 0.5. But that's just for readablity - it could also have put the corners directly around 0 in the first step.
The code is open-source so you can take a look. It's the first function in CGeometryCreator.cpp (no worries, it's not complicated). Note that in there it's done in 2 steps. First the cube is created at 0,0,0 to 1,1,1 and then moved to -0.5,-0.5,-0.5 to 0.5, 0.5, 0.5. But that's just for readablity - it could also have put the corners directly around 0 in the first step.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Posts: 57
- Joined: Sat Oct 11, 2014 11:07 pm
Re: Some questions about materials and meshes
I guess I am not really getting through.
What I am trying to ask is that if I am building a custom scene node, do I need to center it on its position myself if I want it centered? Or is that done automatically? I am not asking about this just for a cube. I am asking in general since I will need to create my own scene nodes.
What I am trying to ask is that if I am building a custom scene node, do I need to center it on its position myself if I want it centered? Or is that done automatically? I am not asking about this just for a cube. I am asking in general since I will need to create my own scene nodes.
Re: Some questions about materials and meshes
I kinda get what you mean. And tried to answer - the problem is that you have still this conception of a mesh center in the head which does not exist. It's just vertex coordinates - x,y,z - that's all. Which are always relative to 0 for each model. When you do move a node you move all vertex coordinates (or you could think as it as moving the 0 point if you want).
There is no pivot (at least not unless you program one). If you create a new mesh in your modeling tool you put it's coordinates whereever you want - and those coordinates are numbers which tell the distance to 0,0,0 for each vertex in your model. Calculations like bounding-boxes and centers can be done if you go for example over all vertices and sum them and then find some center or borders - but this is completely independent stuff which has nothing to do with positioning. Those are higher-level concepts - Irrlicht does not care - it works with vertices. It does not care that a cube is a cube, all Irrlicht knows is there are 8 vertices with such and such coordinates. Remove one corner (or several) and all the other vertices will continue to behave the exact same way as before when you move a node around.
So if you want to position a cube so that one _corner_ is on the position of the node then you put _one corner vertex on 0,0,0_. If you want a cube where all vertices are _around_ the position of the node then _all vertices are around 0,0,0_. But you do that - there is no automatic center. All there is are vertex-coordinates.
Irrlicht's cube's vertices are around 0,0,0 - so the cube center is on the node-position in that case.
There is no pivot (at least not unless you program one). If you create a new mesh in your modeling tool you put it's coordinates whereever you want - and those coordinates are numbers which tell the distance to 0,0,0 for each vertex in your model. Calculations like bounding-boxes and centers can be done if you go for example over all vertices and sum them and then find some center or borders - but this is completely independent stuff which has nothing to do with positioning. Those are higher-level concepts - Irrlicht does not care - it works with vertices. It does not care that a cube is a cube, all Irrlicht knows is there are 8 vertices with such and such coordinates. Remove one corner (or several) and all the other vertices will continue to behave the exact same way as before when you move a node around.
So if you want to position a cube so that one _corner_ is on the position of the node then you put _one corner vertex on 0,0,0_. If you want a cube where all vertices are _around_ the position of the node then _all vertices are around 0,0,0_. But you do that - there is no automatic center. All there is are vertex-coordinates.
Irrlicht's cube's vertices are around 0,0,0 - so the cube center is on the node-position in that case.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Posts: 57
- Joined: Sat Oct 11, 2014 11:07 pm
Re: Some questions about materials and meshes
I don't really have the concept of vertex center in my head except that I thought it might be a good approach to accurately gauge the distance between objects. But I am not making that assumption. That is why I need to ask the question. The position of the mesh is relative to WHICH vertex? Lets say for example that I have a cube mesh with the following coordinates that is not automatically adjusted so that it is centered on 0,0,0.
0,0,0
1,0,0
1,1,0
0,1,0
0,0,1
1,0,1
1,1,1
0,1,1
Now lets say I add that cube to a scene and give it a parent who is at 0,0,0 and set its position at 5,5,5. What will the absolute (actual) coordinates be? In other words, which coordinate is "home"? 5,5,5 is relative to which coordinate? Is it the center? Is it 0,0,0? Another point? I suppose I should assume 0,0,0 given your answers.
0,0,0
1,0,0
1,1,0
0,1,0
0,0,1
1,0,1
1,1,1
0,1,1
Now lets say I add that cube to a scene and give it a parent who is at 0,0,0 and set its position at 5,5,5. What will the absolute (actual) coordinates be? In other words, which coordinate is "home"? 5,5,5 is relative to which coordinate? Is it the center? Is it 0,0,0? Another point? I suppose I should assume 0,0,0 given your answers.
Re: Some questions about materials and meshes
Yes, just a normal coordinate system with it's origin at 0,0,0. So you move your parent (or the object itself) to 5,5,5 then the absolute coordinates will have +5 added to x, y and z. The position of the mesh is not relative to any vertex. It's the other way round - the vertices are relative to the absolute position (and rotation) of your node. So the node transformations defines where the origin is. And if your mesh has accidentally a vertex exactly at 0,0,0 (it doesn't need to have that!) _then_ the absolute coordinates of this specific vertex will be identical to the absolute position of the node.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Posts: 57
- Joined: Sat Oct 11, 2014 11:07 pm