[UPDATED] Solid2Layer Material (two tcoords and lighting)

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

[UPDATED] Solid2Layer Material (two tcoords and lighting)

Post by Foaly »

Hello!

Is it possible to use the material type EMT_SOLID_2_LAYER with S3DVertex2TCoords?
Because it looks to me as if it just uses the first texture coordinates.

I think this is really important, because it allows to use two similar textures (eg. wall textures, one with ivy, one without) and make it look more realistic.
And it's also sad that it's not available in OpenGL (is it so hard to implement?).
Last edited by Foaly on Thu Sep 04, 2014 2:37 pm, edited 2 times in total.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Solid2Layer Material: Using two texture coordinates

Post by hendu »

You should use a shader. It allows all the flexibility you need in GL.
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: Solid2Layer Material: Using two texture coordinates

Post by Foaly »

Well, I know very little about shader programming. I don't think that I would get everything in Irrlicht, like fog and lighting right.

I think it should be included as a built in material, because it's something very basic.

If I would write some code for Irrlicht to support two texture coordinates for solid2layer, would it be included in Irrlicht trunk? (Because I don't think it is hard to do)
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: Solid2Layer Material: Using two texture coordinates

Post by Foaly »

Hello again.
What I found out now is that solid2layer works as expected in OpenGL, and it does use both texture coordinates (but the documentation says it doesn't work), but the DirectX material only uses the first texture coordinates.
By changing the code a little bit:
line 209 in CD3DMaterialRenderer.h:
pID3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
to
pID3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);

This makes the D3D material look like in OpenGL.
I think this should be changed to make the behaviour consistent.
CuteAlien
Admin
Posts: 9733
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [SOLVED] Solid2Layer Material: Using two texture coordin

Post by CuteAlien »

Changing materials is always risky (you break everyone's code who depends on the old behavior!). Thought I must admit in this case I thought it would use the second uv set. Anyway - not my area - but I'll move it to bugreports.
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
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: Solid2Layer Material: second uv coordinates (D3D9 and do

Post by Foaly »

Hm...
Until now I worked with disabled lightning and today I set the lighting flag to true and then it ignores blending! (in OpenGL and (modified) Direct3D)
This is very odd!
Solid2Layer seems to ignore lighting always (it looks like regular solid material with disabled lighting if lighting is enabled), but it doesn't blend if lighting is enabled. With enabled lighting it only displays the second texture.

I don't have much time to check the whole configuration of my program (maybe it's my mistake) because I'll be on vacation for a week from now.
When I'm back, I'll try to find out what's wrong with that material type (or with me :) ) I will also show some screenshots.
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: [UPDATED] Solid2Layer Material (two tcoords and lighting

Post by Foaly »

Hello!
I tested everything again, and found out that I had a really bad mistake which caused the blending not to work with enabled lighting:
The model loader set the color material to ECM_NONE, so it somehow ignored the vertex alpha with enabled lighting. After setting it to ECM_DIFFUSE the blending worked again in D3D and OpenGL.

But I noticed that it only lights the first texture and ignores lighting on the the second texture in both OpenGL and direct 3D. After trying around a lot (I never really did anything with d3d directly) I managed to apply lighting to both textures:

(in CD3D9MaterialRenderer.h in class CD3D9MaterialRenderer_SOLID_2_LAYER)

Code: Select all

 
virtual void OnSetMaterial(const SMaterial& material, const SMaterial& lastMaterial,
        bool resetAllRenderstates, IMaterialRendererServices* services) _IRR_OVERRIDE_
    {
        services->setBasicRenderStates(material, lastMaterial, resetAllRenderstates);
 
        if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
        {
            setTextureColorStage(pID3DDevice, 0, D3DTA_TEXTURE);
            pID3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
 
            pID3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);  //changed last parameter from 0 to 1
            pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_BLENDDIFFUSEALPHA);
            pID3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);  //this and next line are maybe unnecessary
            pID3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
            pID3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
            pID3DDevice->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
 
            setTextureColorStage(pID3DDevice, 2,
                D3DTA_CURRENT, D3DTOP_MODULATE, D3DTA_DIFFUSE);
        }
    }
 
It works with and without lighting, but I didn't check if all SetTextureStageState calls are really needed. Maybe some can be removed.

I will now try to fix the OpenGL material render, but it looks a bit more complicated.
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: [UPDATED] Solid2Layer Material (two tcoords and lighting

Post by Foaly »

I think I've fixed the OpenGL MaterialRenderer for Solid2Layer materials now! It works now with and without lighting.
But I'm not sure whether the OnUnsetMaterial() is still correct. Because I've worked on texture stage 0, I've added an

Code: Select all

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
after the line

Code: Select all

Driver->getBridgeCalls()->setActiveTexture(GL_TEXTURE0_ARB);
Here is the new code:
(in COpenGLMaterialRenderer.h in class COpenGLMaterialRenderer_SOLID_2_LAYER in OnSetMaterial,
replaces all code inside the brackets after the line "if (Driver->queryFeature(EVDF_MULTITEXTURE))")

Code: Select all

 
#ifdef GL_ARB_texture_env_combine
                Driver->getBridgeCalls()->setActiveTexture(GL_TEXTURE0_ARB);            //first stage:
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
                glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PRIMARY_COLOR_ARB);
                glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE);          //interpolate
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE0_ARB);         //texture 0
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE1_ARB);         //and texture 1
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_PRIMARY_COLOR_ARB);    //using the vertex color's
                glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_ONE_MINUS_SRC_ALPHA); //inverse alpha channel
 
                Driver->getBridgeCalls()->setActiveTexture(GL_TEXTURE1_ARB);            //second stage:
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
                glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);             //multiply
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PREVIOUS_ARB);         //result of first stage
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PRIMARY_COLOR_ARB);    //with lighting
#else
                Driver->getBridgeCalls()->setActiveTexture(GL_TEXTURE0_ARB);            //first stage:
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
                glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_EXT, GL_REPLACE);
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_EXT, GL_PRIMARY_COLOR_EXT);
                glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_INTERPOLATE);          //interpolate
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_EXT, GL_TEXTURE0_ARB);         //texture 0
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_EXT, GL_TEXTURE1_ARB);         //and texture 1
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE2_RGB_EXT, GL_PRIMARY_COLOR_EXT);    //using the vertex color's
                glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND2_RGB_EXT, GL_ONE_MINUS_SRC_ALPHA); //inverse alpha channel
 
                Driver->getBridgeCalls()->setActiveTexture(GL_TEXTURE1_ARB);            //second stage:
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
                glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_MODULATE);             //multiply
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_EXT, GL_PREVIOUS_EXT);         //result of first stage
                glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_EXT, GL_PRIMARY_COLOR_EXT);    //with lighting
#endif
 
I'm not sure if I did all that stuff with the EXT or ARB suffixes right. I just replaced ARB by EXT in the else part, where possible.
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: [UPDATED] Solid2Layer Material (two tcoords and lighting

Post by Foaly »

Added a bug on sourceforge.net:
http://sourceforge.net/p/irrlicht/bugs/432/
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: [UPDATED] Solid2Layer Material (two tcoords and lighting

Post by Foaly »

Hello!

I'm not sure, why these fixes (the one for d3d9 and the other for gl) weren't added to irrlicht. I tested them and they work. Is it too hard to understand where to replace the code or did everybody just forget about this?
Again, the problem with the current implementation is, that they ignore lighting for the second texture. (And d3d ignored the second texture coords)

I hope this bug can be closed soon.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: [UPDATED] Solid2Layer Material (two tcoords and lighting

Post by hendu »

Basically there are two semiactive devs, and both are busy doing their other things. Two weeks is nothing, there are six years old patches not applied :P
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: [UPDATED] Solid2Layer Material (two tcoords and lighting

Post by kklouzal »

hendu wrote:...Two weeks is nothing, there are six years old patches not applied :P
That's sad. Definitely not something to laugh about.
Dream Big Or Go Home.
Help Me Help You.
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: [UPDATED] Solid2Layer Material (two tcoords and lighting

Post by Nadro »

This patch requires verification by someone with good fixed pipeline experience (my skill in this case is not enough) that's why it's still unimplemented in trunk.

In the last time I try to apply patches existing on a tracker, but lots of them has some major or minor problems thats why there are still a lot of unimplemented patches. Of course as hendu said there is a lack of human resources, we need a more developers to increase the speed of irr development.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: [UPDATED] Solid2Layer Material (two tcoords and lighting

Post by Foaly »

Thanks for your answers!
I don't have any fixed pipeline experience, too. I just tried around until it seemed to work :)
Post Reply