[2.1] Hlms 2D drawing

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
cowtung
Gnoblar
Posts: 17
Joined: Wed Dec 16, 2015 10:28 pm

[2.1] Hlms 2D drawing

Post by cowtung »

I'm trying to figure out how to get a Texture rendered pixel for pixel to the screen in 2.1. I found this post: http://www.ogre3d.org/forums/viewtopic.php?f=25&t=83254. He mentions that Rectangle2D doesn't work, but that was a while ago, so does it work now? It crashes on me right now, and I'm probably making some dumb mistake. If Rectangle2D isn't supposed to work in 2.1, then what should I do?

Code: Select all

image_material->getTechnique( 0 )->getPass( 0 )->getTextureUnitState( 0 )->setTexture( img->texture() );

Ogre::v1::Rectangle2D* rect = mgr->createRectangle2D( true );
rect->setCorners( -0.2f, 0.2f, 0.2f, -0.2f );
rect->setMaterial( image_material );
rect->setRenderQueueGroup( 1 );

Ogre::SceneNode* node = mgr->getRootSceneNode()->createChildSceneNode();
node->attachObject( rect );
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5299
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1279
Contact:

Re: [2.1] Hlms 2D drawing

Post by dark_sylinc »

Callstack of the crash?
Ogre.log?
If the material doesn't have shaders, it's going to be complaining, and possibly crashing.

So far Rectangle2D has only been used for internal purposes to render compositor passes. It's possible it needs a few adjustments to render normally.

What are you trying to render in 2D?
User avatar
cowtung
Gnoblar
Posts: 17
Joined: Wed Dec 16, 2015 10:28 pm

Re: [2.1] Hlms 2D drawing

Post by cowtung »

Root::getMovableObjectFactory() fails to find "Rectangle2D" in the mMovableObjectFactoryMap.

My problem might be that 2.1 isn't finished and I'm stuck trying to learn two systems (v1 and hlms). As I am completely new to this, and new to rendering anything outside of the D3D9 Fixed Function Pipeline, trying to figure out how to do anything in 2.1 has been a nightmare. I feel like I'm trying to skip steps and am supposed to have a few months background in writing shaders or previous versions of Ogre before trying to use 2.1. I'm reliant on the Samples because the forums are full of code that doesn't work in 2.1, or incomplete snippets that leave out what I need to get this working.

I need a step-by-step walkthrough for how to render/blt a 2D texture onto the backbuffer pixel-for-pixel (no scale or rotation) while blending using the alpha channel of the texture. I'd like to do this all in C++ without relying on loading config or material files. I need to specify the coordinates to draw at in screen pixels, so camera position is ignored. I need to be able to draw 3D objects on top of the image.

Someone pointed me at the PBSMaterials sample as a way to see how hlms works. This is great for 3D stuff, but I'm confused about how to simply draw a texture to the backbuffer. Overlay won't work because I need to be able to do this on multiple screens (different stuff on each screen). It also needs to be fast because the majority of our users will only be doing 2D stuff.

Should I drop back to 2.0 or 1.9? Thank you for any help you can provide.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5299
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1279
Contact:

Re: [2.1] Hlms 2D drawing

Post by dark_sylinc »

Sounds like you want a pass_quad type of pass, which is exactly what you're looking for (drawing a 2D rectangle that covers the entire screen with a custom shader and any texture you want).

The Postprocessing uses pass_quad passes to perform postprocessing effects, but it can also be used to draw arbitrary data from arbitrary shaders with arbitrary textures.
You can set it up with scripts, or with C++ (PostprocessingGameState::createExtraEffectsFromCode shows how to create a few passes from code).
Lax
Hobgoblin
Posts: 583
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany
x 50

Re: [2.1] Hlms 2D drawing

Post by Lax »

Hi,

I also tried to render a background image on screen (later for scrolling effects etc.), but without success. I do as you described.
I created a quad pass, similiar like in the postprocessing examples:

Code: Select all

if (!compositorManager->hasNodeDefinition("Background"))
		{
			Ogre::CompositorNodeDef* backgroundDef = compositorManager->addNodeDefinition("Background");

			//Input channels
			backgroundDef->addTextureSourceName("rt_input", 0, Ogre::TextureDefinitionBase::TEXTURE_INPUT);
			backgroundDef->addTextureSourceName("rt_output", 1, Ogre::TextureDefinitionBase::TEXTURE_INPUT);

			backgroundDef->mCustomIdentifier = "Ogre/Postprocess";

			//Local textures
			backgroundDef->setNumLocalTextureDefinitions(1);
			{
				Ogre::TextureDefinitionBase::TextureDefinition *texDef = backgroundDef->addTextureDefinition("rt0");
				texDef->widthFactor = 0.25f;
				texDef->heightFactor = 0.25f;
				texDef->formatList.push_back(Ogre::PF_R8G8B8);
			}

			backgroundDef->setNumTargetPass(1);

			{
				Ogre::CompositorTargetDef *targetDef = backgroundDef->addTargetPass("rt0");

				{
					Ogre::CompositorPassQuadDef *passQuad;
					passQuad = static_cast<Ogre::CompositorPassQuadDef*>(targetDef->addPass(Ogre::PASS_QUAD));
					passQuad->mMaterialName = "NOWABackgroundPostprocess";
					passQuad->addQuadTextureSource(0, "rt_input", 0);
				}
			}

			//Output channels
			backgroundDef->setNumOutputChannels(2);
			backgroundDef->mapOutputChannel(0, "rt_output");
			backgroundDef->mapOutputChannel(1, "rt_input");
		}
Then, like in the post processing examples, I connect the "background" node.
The material is as follow:

Code: Select all

material NOWABackgroundPostprocess
{
	technique
	{
		pass
		{
			depth_check off			
			depth_write off

			cull_hardware none
			
			texture_unit
			{
				texture Far_Far_Mountain.png
				filtering			trilinear
				tex_address_mode	clamp
			}
		}
	}
}
The texture can be found and there is no Error in the Ogre log.

As soon as I activate the compositor, everything will be just black. When I activate e.g. dither compositor effect, that will work.

Has anybody an idea, how to get that working?

Regards
Lax

http://www.lukas-kalinowski.com/Homepage/?page_id=1631
Please support Second Earth Technic Base built of Lego bricks for Lego ideas: https://ideas.lego.com/projects/81b9bd1 ... b97b79be62

Lax
Hobgoblin
Posts: 583
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany
x 50

Re: [2.1] Hlms 2D drawing

Post by Lax »

Hi,

I got now background image working via compositor effect. But the issue is, that the background image actually is always in foreground, so no objects are visible. I orientated on the compositor effects, but they take the whole scene and change the image at runtime. But what I would like to have, is just a simple background image. Later I want to scroll the image as the player Moves like in 2.5D Jump 'n' Run games like Castlevania.

Here my code so far:

Code: Select all

compositor_node Background
{
	in 0 rt_input
	in 1 rt_output

	custom_id Ogre/Postprocess

	target rt_output
	{
		pass render_quad
		{
			// Renders a fullscreen quad with a material
			material NOWABackgroundPostprocess
		}
	}

	out 0 rt_output
	out 1 rt_input
}

Code: Select all

fragment_program NOWABackgroundPostprocess_ps_GLSL glsl
{
	source NOWABackgroundPostprocess_ps.glsl
	default_params
	{
		param_named backgroundMap int 0
	}
}

fragment_program NOWABackgroundPostprocess_ps_HLSL hlsl
{
	source NOWABackgroundPostprocess_ps.hlsl
	entry_point main
	target ps_5_0 ps_4_0 ps_4_0_level_9_1 ps_4_0_level_9_3
}

fragment_program NOWABackgroundPostprocess_ps unified
{
	delegate NOWABackgroundPostprocess_ps_HLSL
	delegate NOWABackgroundPostprocess_ps_GLSL
}

// Material definition
material NOWABackgroundPostprocess
{
	technique
	{
		pass
		{
			depth_check on
			depth_write off

			cull_hardware none

			vertex_program_ref Ogre/Compositor/Quad_vs
			{
			}

			fragment_program_ref NOWABackgroundPostprocess_ps
			{
			}

			texture_unit
			{
				texture Far_Far_Mountain.png
				filtering			trilinear
				tex_address_mode	clamp
			}
		}
	}
}
I hope somebody can help me out here.

Regards
Lax

http://www.lukas-kalinowski.com/Homepage/?page_id=1631
Please support Second Earth Technic Base built of Lego bricks for Lego ideas: https://ideas.lego.com/projects/81b9bd1 ... b97b79be62

Lax
Hobgoblin
Posts: 583
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany
x 50

Re: [2.1] Hlms 2D drawing

Post by Lax »

Hi,

is it somehow possible to render a quad in background instead in foreground? Because all compositor effects are rendered in foreground. But I would like to accomplish a background scroll texture in background.

Has someone any idea how to accomplish that?

Thanks in advance.

Regards
Lax

http://www.lukas-kalinowski.com/Homepage/?page_id=1631
Please support Second Earth Technic Base built of Lego bricks for Lego ideas: https://ideas.lego.com/projects/81b9bd1 ... b97b79be62

Lax
Hobgoblin
Posts: 583
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany
x 50

Re: [2.1] Hlms 2D drawing

Post by Lax »

Ok, got a first version working. Maybe its useful for someone:
The trick was, that the "render_quat" pass muss directly follow after the "clear" pass in the compositor:

Code: Select all

target rt0
{
		pass clear
		{
			colour_value 0.2 0.4 0.6 1
		}
		
		pass render_quad
		{
			// quad_normals	camera_direction
			material NOWABackgroundPostprocess
		}
		
		pass render_scene
		{
			overlays	off
			shadows		NOWABackgroundShadowNode
			rq_first    0
			rq_last     5
		}
		
		pass render_scene
		{
			lod_update_list	off
			overlays	on
			rq_first	5
			rq_last		255
		}
		
	}
I orientated on the sky postprocess example with materials, so a pixel shader is required (NOWABackgroundPostprocess_ps.hlsl):

Code: Select all

Texture2D backgroundMap		: register(t0);
SamplerState samplerState	: register(s0);
float scroll;
float speed;

float4 main(float2 uv0 : TEXCOORD0) : SV_Target
{
	uv0.x += scroll * speed;
	return backgroundMap.Sample( samplerState, uv0 );
}
Then the "NOWABackgroundPostprocess" material is required, which uses the pixel shader:

Code: Select all

fragment_program NOWABackgroundPostprocess_ps_GLSL glsl
{
	source NOWABackgroundPostprocess_ps.glsl
	default_params
	{
		param_named backgroundMap int 0
	}
}

fragment_program NOWABackgroundPostprocess_ps_HLSL hlsl
{
	source NOWABackgroundPostprocess_ps.hlsl
	entry_point main
	target ps_5_0 ps_4_0 ps_4_0_level_9_1 ps_4_0_level_9_3
}

fragment_program NOWABackgroundPostprocess_ps unified
{
	delegate NOWABackgroundPostprocess_ps_HLSL
	delegate NOWABackgroundPostprocess_ps_GLSL
}

// Material definition
material NOWABackgroundPostprocess
{
	technique
	{
		pass
		{
			scene_blend alpha_blend
			//lighting off
			depth_check off
			depth_write off

			cull_hardware none

			vertex_program_ref Ogre/Compositor/Quad_vs
			{
			}

			fragment_program_ref NOWABackgroundPostprocess_ps
			{
				param_named_auto scroll time_0_x 20
				param_named speed float 0.05
			}

			texture_unit backgroundMap
			{
				texture Far_Far_Mountain.png
				alpha_op_ex modulate src_manual src_texture 0.1
				scroll_anim 0.5 0 // does not work
			}
		}
	}
}
The scrolling is done in the pixel shader. It works perfectly!

Regards
Lax

http://www.lukas-kalinowski.com/Homepage/?page_id=1631
Please support Second Earth Technic Base built of Lego bricks for Lego ideas: https://ideas.lego.com/projects/81b9bd1 ... b97b79be62

Lax
Hobgoblin
Posts: 583
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany
x 50

Re: [2.1] Hlms 2D drawing

Post by Lax »

I encountered an issue, using this technique. The movement of entities/items is jerky.

When I use the skybox workspace from the examples. The entities/items are moved smoothley.

Has somebody an idea, what can cause jerky movement?

I have just a background, that is scrolling.

Best regards
Lax

http://www.lukas-kalinowski.com/Homepage/?page_id=1631
Please support Second Earth Technic Base built of Lego bricks for Lego ideas: https://ideas.lego.com/projects/81b9bd1 ... b97b79be62

Post Reply