[SOLVED] Eeehh where is my mesh gone?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
KP84
Posts: 44
Joined: Thu Feb 24, 2011 5:08 am

[SOLVED] Eeehh where is my mesh gone?

Post by KP84 »

Originally I wanted to ask a question about math :)) but when cleaning up my code to make it post-ready I had a real neat idea. And now a different problem :)), so I'll save the math for later.

Am trying to create some water wich will contain waves (fundamental to my app) and as it will act as big 'terrain' I thought to create it as an ITerrainSceneNode so it benefits the LOD system (<-- the real neat idea).

No need for a heightmap so I did:
- new file: copied ITerrainSceneNode
- new file: copied CTerrainSceneNode.h
- new file: copied CTerrainSceneNode.cpp
- converted those, so it calls the appropriate
- Removed all loadHeightMapRAW
- Changed the loadHeightMap().
- removed some namespace os:: functionallity too
- lost the serialization and clone for now

To instantiate I have this code:

Code: Select all

		nodeOcean = new irr::scene::cOceanSceneNode( 
			m_scenemanager->getRootSceneNode(), 
			m_scenemanager, 
			-1, 
			5, 
			irr::scene::ETPS_17, 
			irr::core::vector3df( -128.0f * 4.0f, 0.0f, -128.0f * 4.0f ), 
			irr::core::vector3df( 0.0f, 0.0f, 0.0f ), 
			irr::core::vector3df( 4.0f, 1.0f, 4.0f )
			);

		nodeOcean->loadHeightMap( 257, irr::video::SColor ( 255, 255, 255, 255 ), 0 );
		nodeOcean->setMaterialTexture( 0, m_driver->getTexture( "..\\resources\\textures\\islands\\terrain-texture.jpg" ) );
		nodeOcean->setDebugDataVisible ( irr::scene::EDS_BBOX | irr::scene::EDS_MESH_WIRE_OVERLAY | irr::scene::EDS_BBOX_BUFFERS );
This will add it to the scene manager and render it. However I do not see any mesh but the bboxes do show. So it's being rendered.

TODO: The actual dynamic wave wich will happen somewhere in the render func, don't know yet but won't be a (real) pain.

Here is the new function:

Code: Select all

	bool cOceanSceneNode::loadHeightMap( irr::s32 size, video::SColor vertexColor, s32 smoothFactor )
	{
		
		// ONLY CHANGED PARTS I POST HERE FOR READABILITY

		Mesh->MeshBuffers.clear();
		/* REMOVED
		const u32 startTime = os::Timer::getRealTime();

		ENTIRE heightMap MEMBER DECLERATION TOO
		video::IImage* heightMap = SceneManager->getVideoDriver()->createImageFromFile(file);

		if (!heightMap)
		{
			os::Printer::log("Unable to load heightmap.");
			return false;
		}
		*/

		// Set the size of the surface
		TerrainData.Size = size;// <-- heightMap->getDimension().Width;

		switch ( TerrainData.PatchSize )
		{/*NOT CHANGED*/}

		// --- Generate vertex data from heightmap ----
		// resize the vertex array for the mesh buffer one time (makes loading faster)
		scene::CDynamicMeshBuffer *mb=0;

		const u32 numVertices = TerrainData.Size * TerrainData.Size;
		if (numVertices <= 65536)
		{/*NOT CHANGED
		else
		SAME
		*/}

		mb->getVertexBuffer().set_used(numVertices);

		// Read the heightmap to get the vertex data
		// Apply positions changes, scaling changes
		for x && z
		{
			{
				
				get vertex = static_cast[ index++ ];
				vertex.Pos.X			= fx;
				// The cos and sin for the wave
				vertex.Pos.Y			= cos( PI / 180.0f * fx ) * sin( PI / 180.0f * fz );//0.0f;
				vertex.Pos.Z			= fz;

			}
		}

		smoothTerr...UNCHANGED...RenderBuffer->setDirty();

		//const u32 endTime = os::Timer::getRealTime();

		//c8 tmp[255];
		//snprintf(tmp, 255, "Generated terrain data (%dx%d) in %.4f seconds",
		//	TerrainData.Size, TerrainData.Size, (endTime - startTime) / 1000.0f );
		//os::Printer::log(tmp);

		return true;
	}

Code: Select all

	void cOceanSceneNode::render()
	{
		 /* NOTHING CHANGED AT ALL */
	}

Conclusion: With this function, changed minimal, I would think it just should work exactly the same as an CTerrainSceneNode but where did my SMesh go?

Thanks,
KP84
Last edited by KP84 on Fri May 27, 2011 9:24 pm, edited 1 time in total.

Code: Select all

// if you think real code is always interresting, read this line again
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

Instead of going all this trouble and going all hacky style on the terrain scene node, why not just create a tessellated plane and write a wave vertex shader? If you design this right you won't need any LOD system, and things will become much easier to manage (and since waves are done on the GPU you save CPU cycles too)
KP84
Posts: 44
Joined: Thu Feb 24, 2011 5:08 am

Post by KP84 »

Simply put, never heard of a wave vertex shader.

Am trying to create an ocean that will interact with ships and wind. Don't know if a shader will works for that. Is that shader thing something as the water tutorial? A flat plane won't work.

EDIT: removed typo

Code: Select all

// if you think real code is always interresting, read this line again
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

KP84 wrote:Simply put, never heard of a wave vertex shader.

Am trying to create an ocean that will interact with ships and wind. Don't know if a shader will works for that. Is that shader thing something as the water tutorial? A flat plane won't work.
They are pretty common for water simulation, it's fast and can be pretty realistic if you design them right, you could perfectly take into account a wind vector and other environment factors
A shader can be as complex and as realistic as you want it to be, it's just a matter of finding the ideal line between performance and quality (and it can definitely look much better than irrlicht's built in water system)

Making other objects move with the water can be a bit harder, but is certainly not impossible, maybe you could write out wave data (like height and normals) to textures and pass them to the objects that need to move with the water so they can change height and rotation accordingly (haven't tried this myself though, this is just off the top of my head so it could contain flaws)

Here's a video of my water system which is based on these principles, this is done in screenspace as a post-process using a deferred context though, it would work in a forward renderer but it'd require some more setup

http://www.youtube.com/watch?v=Ahj4T5s5TIk
KP84
Posts: 44
Joined: Thu Feb 24, 2011 5:08 am

Post by KP84 »

Okay, I will dive into the shader thing. Don't know where to begin so aarrgg I am sinking ;)

Thanks for the ideas
KP84

EDIT:
btw, look neat that ocean of yours. Am seeying some stuff like ripples I would like too. Well let me take one thing at the time ;)

Code: Select all

// if you think real code is always interresting, read this line again
KP84
Posts: 44
Joined: Thu Feb 24, 2011 5:08 am

Post by KP84 »

To be hounest, have never looked at shaders yet. So this would be a good time to do so.

With a quick glance (have to shop for food, yak) this could solve the coast line parts too so it could have lower waves too, isn't it?

Code: Select all

// if you think real code is always interresting, read this line again
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

KP84 wrote:To be hounest, have never looked at shaders yet. So this would be a good time to do so.

With a quick glance (have to shop for food, yak) this could solve the coast line parts too so it could have lower waves too, isn't it?
Yes, if you could provide your shader with information about your terrain

This is why I chose for a screen-space solution, because I've stored all my geometry data in a G-Buffer I don't have to go through any other steps to provide my shaders with information about my geometry setup, allowing me to do smooth shorelines, depth-based water color falloff, etc.
drahdellort
Posts: 8
Joined: Fri May 27, 2011 10:08 am

Post by drahdellort »

Radikalizm wrote:Instead of going all this trouble and going all hacky style on the terrain scene node, why not just create a tessellated plane and write a wave vertex shader? If you design this right you won't need any LOD system, and things will become much easier to manage (and since waves are done on the GPU you save CPU cycles too)
If waves are vital to the game I would guess he needs to use the transformed vertices for physics or whatever so running a vertex shader on the card and then loading all the data back into RAM is definitely not the best way todo it!
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

drahdellort wrote:
Radikalizm wrote:Instead of going all this trouble and going all hacky style on the terrain scene node, why not just create a tessellated plane and write a wave vertex shader? If you design this right you won't need any LOD system, and things will become much easier to manage (and since waves are done on the GPU you save CPU cycles too)
If waves are vital to the game I would guess he needs to use the transformed vertices for physics or whatever so running a vertex shader on the card and then loading all the data back into RAM is definitely not the best way todo it!
I never talked about the best way to do it, I never talked about the most efficient way to do it, I just gave him an easy solution which could be used to accomplish this ;)
drahdellort
Posts: 8
Joined: Fri May 27, 2011 10:08 am

Post by drahdellort »

Radikalizm wrote:I never talked about the best way to do it, I never talked about the most efficient way to do it, I just gave him an easy solution which could be used to accomplish this ;)
You can't honestly believe writing a vertex shader is easier than just transforming the vertices in memory.
KP84
Posts: 44
Joined: Thu Feb 24, 2011 5:08 am

Post by KP84 »

Okay, so the both of you agree on shaders aren't the best solution to my problem. Anyway, think this thread will push me into shaders too.

But then again, where did my mesh go?

Code: Select all

// if you think real code is always interresting, read this line again
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

KP84 wrote:Okay, so the both of you agree on shaders aren't the best solution to my problem. Anyway, think this thread will push me into shaders too.

But then again, where did my mesh go?
Don't listen to drahdellort (reverse his name and you'll see why, also check the off-topic forum for a thread he made)

A vertex shader is made for doing vertex transformations, so for the water rendering in general it is a very good solution (the GPU is much faster with vertex transformations than the CPU)

The movement of elements on the water could be a little more tricky, maybe you should look around to see how others have handled it because I can't really provide any help for this since I never attempted it
KP84
Posts: 44
Joined: Thu Feb 24, 2011 5:08 am

Post by KP84 »

Mindblowing project the guy has, and that on the first day he joined. Somhow that name sounds familiar to me, anyway...

Are there different types of languages of shaders? Heard about GLSL, now I found HLSL and CgFX at the nvidia link you provided. They can't be compiled (at least by me atm). You've got a link that could tell me more about the basics of shaders?

For now I would like to set up my prototype game, implementing some basic stuff and change everything whenever I get around to it. So still I would like to get my first question got answered.

Code: Select all

// if you think real code is always interresting, read this line again
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

There are different languages yes, GLSL is a language for OpenGL, HLSL for directX
They differ a little syntax-wise but the core principles are the same
Cg is a cross-library shader language, but requires an additional library to be used (if you look in the projects forum, there's someone who made a Cg wrapper for irrlicht)

Normally seeing google should be able to provide you with some good tutorials, I can't really remember which ones I used back in the day so can't help you out there
Post Reply