It is currently Sat Aug 22, 2020 3:31 pm


All times are UTC




Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Rendering different Materials
PostPosted: Fri Oct 14, 2011 6:32 pm 
User avatar

Joined: Tue Feb 22, 2011 8:04 am
Posts: 101
I'm feeling really noobish and stupid, but I can't seem to find any information on rendering different materials.

Currently I extract the entire mesh and render it one way. Which is working nicely.

How do I extract different materials in order to render them differently?

_________________
--Real Programmers use a magnetized needle and a steady hand.
--xkcd--


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Rendering different Materials
PostPosted: Fri Oct 14, 2011 8:52 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
Firstly, do you have material information in the volume? That is, your voxel type needs to have a material component, such as Material8 or MaterialDensityPair44.

Assuming you do have this then the material ID is stored in the vertices of the generated mesh. So if you are using the CubicSurfaceExtractor then the mesh contains vertices of type PositionMaterial, wihch looks like this:

Code:
class PositionMaterial
{
public:   
   PositionMaterial();
   PositionMaterial(Vector3DFloat positionToSet, float materialToSet);

   float getMaterial(void) const;
   const Vector3DFloat& getPosition(void) const;

   void setMaterial(float materialToSet);
   void setPosition(const Vector3DFloat& positionToSet);
public:      
   //Nicely fits into four floats.
   Vector3DFloat position;
   float material;
};


So you can access the material which is associated with each vertex. Now exactly how you use this is (you guessed it...) outside the scope of PolyVox, but there are a couple of options.

You may choose to break your mesh down into smaller meshes, each of which has only one material. There was some code to do this in PolyVox (you can still find the commented out code if you search for 'extractSubset') which might shw you where to start. The you can render each mesh one at a time with one material. Quite straightforward, but you end up rendering a lot of meshes.

Another approach is to pass the material ID to the GPU as part of the vertex data. This requires some shader skills but isn't too hard. Then it's up to you how you use it... for example you could convert it into a colour or use it as the index into a texture atlas. I guess the real question here is what visual look are you trying to achieve?


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Rendering different Materials
PostPosted: Fri Oct 14, 2011 9:07 pm 
User avatar

Joined: Tue Feb 22, 2011 8:04 am
Posts: 101
Ok the vertices contain more than positional data.

I am using the smooth surface extractor.

I was thinking about trying a cel-shaded look.

I won't know if I like it until I try it.

I think that answers my question, but if you have any more advice I'd love to hear it.

Thanks for the help.

_________________
--Real Programmers use a magnetized needle and a steady hand.
--xkcd--


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Rendering different Materials
PostPosted: Sat Oct 15, 2011 9:46 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
GM_Riscvul wrote:
I think that answers my question, but if you have any more advice I'd love to hear it.


I'll let you see what you can come up with, but you can just ask if there's something you can't work out.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Rendering different Materials
PostPosted: Sat Oct 15, 2011 6:07 pm 
User avatar

Joined: Tue Feb 22, 2011 8:04 am
Posts: 101
Quote:
You may choose to break your mesh down into smaller meshes, each of which has only one material. There was some code to do this in PolyVox (you can still find the commented out code if you search for 'extractSubset') which might shw you where to start. The you can render each mesh one at a time with one material. Quite straightforward, but you end up rendering a lot of meshes.


Yes this is the method I vaguely remembered, I borrowed some code originally which used this method to produce each mesh, but I couldn't find it again. It sounds though, like using a shader to do the work might be a better solution.

Quote:
Another approach is to pass the material ID to the GPU as part of the vertex data. This requires some shader skills but isn't too hard.


How would I go about passing the material id into a shader?

Right now I am using the positional data for one texture's coordinates. Could I provide multiple textures with coordinates based on material ID?

_________________
--Real Programmers use a magnetized needle and a steady hand.
--xkcd--


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Rendering different Materials
PostPosted: Sat Oct 15, 2011 7:23 pm 

Joined: Sat Sep 18, 2010 9:45 pm
Posts: 189
In my setup I'm currently using the first texture set to store material id. My material id is an integer [0, 255]. I have to normalize this integer into [0, 1] ( ID / 255) because texture coordinates only takes a number in that range. In the vertex shader I expand it into a number in [0, 255] again. I then use this to compute x,y index into the texture atlas.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Rendering different Materials
PostPosted: Sun Oct 16, 2011 7:56 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
GM_Riscvul wrote:
How would I go about passing the material id into a shader?

Right now I am using the positional data for one texture's coordinates. Could I provide multiple textures with coordinates based on material ID?


Yes, you can provide multiple sets of texture coordinates for each vertex, and you can use one of these to store the material ID. Although the GPU will think it's a texture coordinate you will know otherwise and can use it however you like in the shader.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Rendering different Materials
PostPosted: Sun Oct 16, 2011 9:42 pm 
User avatar

Joined: Tue Feb 22, 2011 8:04 am
Posts: 101
So I am using a manual object in Ogre.

Would the idea be to call:
Code:
manual->textureCoord(materialID / 255.0,0.0)


for every vertex, and then in my ogre material add another default param:

Code:
param_named_auto material whatever_stores_texture_coords_in_ogre


That seems simple enough if I am understanding it correctly.

Then I would change which texture I passed out a coordinate for depending upon what the material ID was.

_________________
--Real Programmers use a magnetized needle and a steady hand.
--xkcd--


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Rendering different Materials
PostPosted: Wed Jan 25, 2012 4:43 pm 

Joined: Wed Jan 25, 2012 12:29 am
Posts: 18
Sorry to grave dig this thread but I think it's the best place for the question:

I want to make a quick and dirty solution to this problem and I hit upon the idea of using different volumes for different meshes. So the ground (I hesitate to use the word terrain) is one volume then overlays (e.g. to be filled or to be removed voxels, vegitation, characters) exist in a different but overlapping volume.

This way you can render each volume/mesh easily in a different colour without messing with shaders or subsurfaces or any difficult stuff.

The question is really, is this a terrible idea? and why?

Cheers,
Charlie


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Rendering different Materials
PostPosted: Thu Jan 26, 2012 9:41 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
Firstly I should point out that things like characters and vegitation aren't likely to be stored as voxels (basically because they are animated).

For storing the environment your suggestion would be possible but has some drawbacks. Let's imgine we have two different materials next to each other... for example a large mass of rock next to a large mass of clay. Now to render this we need a mesh representing the surface where the clay meets the air (this will have a clay texture on it), and also one for where the rock meets the air (this will have a rock texture), but we don't need one where the rock meets the clay. This is because the user can never see the interface between clay and rock as it is inside these two solid structures. That is, assuming the camera can only ever be in the air and that no surfaces are transparent.

If you have two seperate volumes, one with clay voxels and one with rock voxels then this becomes a problem. When generating the clay surface you don't know that there is also rock in the scene, and so you create a much bigger surface than needed. Does that make sense?

So in general I don't think this is a good idea. That said, I have pondered the idea of combining a smooth mesh (for terrain) with a cubic mesh (for buildings). This might be implemented as you describe but I haven't thought about it in detail.


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net