Does Iphone support shadow

Discussion of issues specific to mobile platforms such as iOS, Android, Symbian and Meego.
pengliu1980
Gnoblar
Posts: 2
Joined: Wed Mar 16, 2011 2:49 pm

Does Iphone support shadow

Post by pengliu1980 »

Hi Guys!

I use Ogre 1.7.2 with IOS 4.
When i run my test app on Iphone Simulator , i got a warning,
WARNING: Stencil shadows were requested, but this device does not have a hardware stencil. Shadows disabled.

And there's no shadow be displayed .

Does iphone/iphone simulator supports shadow? or How can i enable shadow ?

Thank you
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

It's true, the iPhones do not support a stencil buffer. Shadows are supported but only shader based and in the 1.8 branch.
pengliu1980
Gnoblar
Posts: 2
Joined: Wed Mar 16, 2011 2:49 pm

Re: Does Iphone support shadow

Post by pengliu1980 »

Thank you masterfalcon .

Let me try Ogre 1.8 :)
fanlansen
Gnoblar
Posts: 18
Joined: Tue Jun 29, 2010 9:28 am

Re: Does Iphone support shadow

Post by fanlansen »

Does Shadow need OpenGLES 2 ?
I used ogre1.8 + iOS 4.1 + iTouch 2nd + OpenGLES 1.x ,
Ogre.cfg setting: FASS=2
All demo has no shadow ,and black instead .
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

Yes, that's correct. Only texture shadows with GL ES 2.0
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

But apple suggests to use shadow volumes as the primary shadowing technique on iOS devices. There is a video and slides with explanation how to do it on GL ES 2.0 at WWDC2010, https://developer.apple.com/devcenter/d ... dering.pdf.
There is a desceription how to do it.

I performed shadowmapping on the iphone but got very low perfonmance. I followed that article

http://www.ogre3d.org/tikiwiki/Depth+Sh ... e=Cookbook

just ported the shaders to GLSL ES and integrated that shader to my lightmapping shader.Here is my initialization code:

Code: Select all

        
        Ogre::Light* spotLight = sceneManager->createLight("spotLight");
        spotLight->setType(Ogre::Light::LT_SPOTLIGHT);
        spotLight->setDiffuseColour(1.0, 1.0, 1.0);
        spotLight->setSpecularColour(0, 0, 1.0);
        
        spotLight->setDirection(-1, -1, -1);
        spotLight->setPosition(Ogre::Vector3(600, 600, 400));
        
        spotLight->setSpotlightRange(Ogre::Degree(35), Ogre::Degree(90));
        
        sceneManager->setShadowTextureSelfShadow(false);
        // Set the caster material which uses the shaders defined above
        sceneManager->setShadowTextureCasterMaterial("Ogre/DepthShadowmap/Caster/Float");
        sceneManager->setShadowTextureSize(1024);
        // Set the pixel format to floating point
        sceneManager->setShadowTexturePixelFormat(Ogre::PF_FLOAT32_R);
        // You can switch this on or off, I suggest you try both and see which works best for you
        sceneManager->setShadowCasterRenderBackFaces(false);
        sceneManager->setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_ADDITIVE_INTEGRATED);
Shadows were too pixeladed even if i used 2048 texture for a shadowmap.

And the last question: does ogre use the gl_oes_depth_texture for shadow mapping? If no is it very hard to add?
Do anybody has similar problems?
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

There could be a couple things at play here.

1. You might need to optimize your shaders.
2. PF_FLOAT32_R gets you the format GL_LUMINANCE. Therefore the format is not DEPTH_COMPONENT and the data type is UNSIGNED_BYTE instead of UNSIGNED_INT or SHORT. I don't know what would be the best way to accomplish this. There have been multiple discussions about this before and the result is to just use float textures because they are handled in a more consistent way across implementations. But there may be driver optimizations made for the depth formats.

I can work on adding these float formats since they've been supported since at least iOS 4.3(probably earlier, I haven't looked).
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

is it very hard to add the stencil shadows for iphone port? i read that iphone has the stencil buffer and apple suggests to use that technique? i can try to add it myself, just want a hint where to start.
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

The support is already there if the hardware supports it. In addition, stencil buffer attachments for FBO's are supported. If I recall, some hardware does not have a stencil buffer.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

I tried to apply simple shadowing example from Tutorial2:

mSceneMgr->setAmbientLight(Ogre::ColourValue(0, 0, 0));
mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);

got "Stencil shadows are not supported on this hardware" on ipod4, ios4.3.

I'm using the revision of ogre1.8 from April, 2011.

Redards, Victor
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

Well, there ya go. No hardware stencil buffer. I didn't think any of the hardware supported it. So you'll have to use texture shadows.

I'll have to re-watch that session to see what they're talking about again.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

I can help with integrating
warmi
Gnoll
Posts: 674
Joined: Sun May 27, 2007 3:56 am
Location: USA

Re: Does Iphone support shadow

Post by warmi »

masterfalcon wrote:Well, there ya go. No hardware stencil buffer. I didn't think any of the hardware supported it. So you'll have to use texture shadows.

I'll have to re-watch that session to see what they're talking about again.
It does. You just have to use GL_DEPTH24_STENCIL8_OES - i.e packed together with depth buffer.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

should i write my own plugin?
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

No, the packed depth/stencil format is already supported.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

Just created the test app and enabled shadowing

Code: Select all

OgreFramework::getSingletonPtr()->m_pSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox");
    
	OgreFramework::getSingletonPtr()->m_pSceneMgr->createLight("Light")->setPosition(75,75,75);
    OgreFramework::getSingletonPtr()->m_pSceneMgr->setAmbientLight(Ogre::ColourValue(0, 0, 0));
    OgreFramework::getSingletonPtr()->m_pSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
	m_pCubeEntity = OgreFramework::getSingletonPtr()->m_pSceneMgr->createEntity("Cube", "ogrehead.mesh");
	m_pCubeNode = OgreFramework::getSingletonPtr()->m_pSceneMgr->getRootSceneNode()->createChildSceneNode("CubeNode");
	m_pCubeNode->attachObject(m_pCubeEntity);

Code: Select all

WARNING: Stencil shadows were requested, but this device does not have a hardware stencil. Shadows disabled.
What type of shadows should i use?
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

Just run the shadows sample. the GUI says Technique Stencil, Lighting:Additive, but when i put a breakpoint on Shadows.h,

Code: Select all

if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_HWSTENCIL))
        {
            mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE); //bp
            mCurrentShadowTechnique = SHADOWTYPE_STENCIL_ADDITIVE;
        }
        else
        {
            mSceneMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_MODULATIVE);//bp
            mCurrentShadowTechnique = SHADOWTYPE_TEXTURE_MODULATIVE;
        }
i trapped to the second.
So could anybody explain me is the setcil shadows mean the approach used in Gamasutra's acticle about shadows on iphone, or hardware packed depth/stencil buffer is not yet supported there?
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

The method mentioned in the Gamasutra article was never committed because I never received a patch.

The type of stencil shadows that are used in Ogre currently depend on a separate hardware stencil buffer, which is different that a stencil render buffer attachment. I haven't checked specifically if the packed formats are working on iOS for shadows, but they are supposed to be supported.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

i have created a simple app to test the ipod4 stencil buffer. I created an app from xcode gl template and draw this picture. Quad is drawn first and triangle is drawn after is with less depth. Here is a code:

Code: Select all

- (void)createFramebuffer
{
    if (context && !defaultFramebuffer) {
        [EAGLContext setCurrentContext:context];
        
        // Create default framebuffer object.
        glGenFramebuffers(1, &defaultFramebuffer);
        glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
        
        // Create color render buffer and allocate backing store.
        glGenRenderbuffers(1, &colorRenderbuffer);
                
        glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
        [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
        
        GLint stencil1;
        glGetIntegerv(GL_DEPTH_BITS, &stencil1);
        
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
        
        glGenRenderbuffers(1, &depthStencilRenderBuffer);
        glGenRenderbuffers(1, &stencilRenderBuffer);
        glBindRenderbuffer(GL_RENDERBUFFER, depthStencilRenderBuffer);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, framebufferWidth, framebufferHeight);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthStencilRenderBuffer);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthStencilRenderBuffer);
        
        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
            NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
        GLint stencil;
        glGetIntegerv(GL_STENCIL_BITS, &stencil);
    }
}

Code: Select all

void drawTriangle()
{
    static const GLfloat vertices[] = {
        -1.0f, -1.0f,0.1,
        1.0f, -1.0f,0.2,
        0.0f,  1.0f,0.4
    };
    
    static const GLubyte colors[] = {
        255, 0,   0, 255,
        255,   0, 0, 255,
        255,     0,   0,   255
    };
    
    glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, 0, 0, vertices);
    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, 1, 0, colors);
    glEnableVertexAttribArray(ATTRIB_COLOR);
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

- (void)drawFrame
{
    [(EAGLView *)self.view setFramebuffer];
    
    // Replace the implementation of this method to do your own custom drawing.
       
    static GLfloat scaleMatrix4f[] = {
        0.0f,0.0f,0.0f,0.0f,
        0.0f,0.0f,0.0f,0.0f,
        0.0f,0.0f,0.0f,0.0f,
        0.0f,0.0f,0.0f,1.0f
    };
    
    static float transY = 0.0f;
    
    glClearColor(0.5f, 1.0f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    
    if ([context API] == kEAGLRenderingAPIOpenGLES2)
    {
        // Use shader program.
        glUseProgram(program);
        
        // Update uniform value.
        glUniform1f(uniforms[UNIFORM_TRANSLATE], (GLfloat)transY);
        buildScaleMatrix(0.2, .2, 1, scaleMatrix4f);
        glUniformMatrix4fv(uniforms[UNIFORM_SCALEMATRIX], 16, GL_FALSE, scaleMatrix4f);
        //transY += 0.075f;	
        
        // Update attribute values.
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LESS);
        glEnable(GL_STENCIL_TEST);
        glStencilFunc(GL_EQUAL, 0, 0xFF);
        glStencilOp(GL_ZERO, GL_ZERO, GL_INCR);
        
        drawQuad();
        buildScaleMatrix(0.1, .4, 1, scaleMatrix4f);
        glUniformMatrix4fv(uniforms[UNIFORM_SCALEMATRIX], 16, GL_FALSE, scaleMatrix4f);
        glStencilFunc(GL_EQUAL, 1, 0xFF);
        glStencilOp(GL_ZERO, GL_ZERO, GL_INCR);
        glDepthFunc(GL_GREATER);
        drawTriangle();
        // Validate program before drawing. This is a good check, but only really necessary in a debug build.
        // DEBUG macro must be defined in your debug configurations if that's not already the case.
#if defined(DEBUG)
        if (![self validateProgram:program]) {
            NSLog(@"Failed to validate program: %d", program);
            return;
        }
#endif
    }
IMG_0633.PNG
Last edited by blackmonster on Mon Sep 12, 2011 5:55 pm, edited 1 time in total.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

i also found that ogre detects a stencil buffer support in OgreGLeS2renderSystem in such a way:

Code: Select all

        glGetIntegerv(GL_STENCIL_BITS, &stencil);
        GL_CHECK_ERROR;

        if(stencil)
        {
            rsc->setCapability(RSC_HWSTENCIL);
			rsc->setCapability(RSC_TWO_SIDED_STENCIL);
            rsc->setStencilBufferBitDepth(stencil);
        }

I tried to do the same in my test app and here is a result:

When i call glGetIntegerv(GL_STENCIL_BITS, &stencil); BEFORE the FBO is created and stencil(depthbuffer) is attached, it returns zero in stencil variable.
And when i call this routine AFTER the FBO init code (as shpwn above) i get stencil == 8 as should be because iphone 8-bit stencil buffer

The next step i tried was to comment that code in OgreGLES2RenderSystem.cpp and write

Code: Select all

  // Check for hardware stencil support and set bit depth
        GLint stencil=8;
I got a lot of ruun-time errors most caused my abscence of fragment program.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

Just added a stencil buffer to OgreEagles2Context:

Code: Select all

#endif
        {
            glGenRenderbuffers(1, &mDepthRenderbuffer);
            GL_CHECK_ERROR
            glBindRenderbuffer(GL_RENDERBUFFER, mDepthRenderbuffer);
            GL_CHECK_ERROR
            glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, mBackingWidth, mBackingHeight);
            GL_CHECK_ERROR
            glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthRenderbuffer);
            GL_CHECK_ERROR
            glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mDepthRenderbuffer);
            GL_CHECK_ERROR
        }
the situation didn't change. sample browser crashes at

Code: Select all

GLSLESLinkProgram::GLSLESLinkProgram(GLSLESGpuProgram* vertexProgram, GLSLESGpuProgram* fragmentProgram)
        : mVertexProgram(vertexProgram)
		, mFragmentProgram(fragmentProgram)
		, mUniformRefsBuilt(false)
        , mLinked(false)
		, mTriedToLinkAndFailed(false)
	{
		// init CustomAttributesIndexs
		for(size_t i = 0 ; i < VES_COUNT; i++)
			for(size_t j = 0 ; j < OGRE_MAX_TEXTURE_COORD_SETS; j++)
		{
			mCustomAttributesIndexes[i][j] = NULL_CUSTOM_ATTRIBUTES_INDEX;
		}
        
        if (!mVertexProgram || !mFragmentProgram)
        {
            OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
                        "Attempted to create a shader program without both a vertex and fragment program.",
                        "GLSLESLinkProgram::GLSLESLinkProgram");
        }
	}
Last edited by blackmonster on Mon Sep 19, 2011 11:27 pm, edited 1 time in total.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

i got shadows working!!!!!!
User avatar
Wolfmanfx
OGRE Team Member
OGRE Team Member
Posts: 1525
Joined: Fri Feb 03, 2006 10:37 pm
Location: Austria - Leoben
x 99
Contact:

Re: Does Iphone support shadow

Post by Wolfmanfx »

You mean stencil shadows with the gles1 rs?
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

i mean stencil shadows with ogre 1.8 GLES 2.0 on iphone 3gs+
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

but should also work with 1.7.3 on gles 1.1
Post Reply