How to use glsl in ogre?

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
User avatar
haibo19981985
Halfling
Posts: 79
Joined: Tue Jan 20, 2009 9:31 am

How to use glsl in ogre?

Post by haibo19981985 »

I create a material base on ogre manual,as follows:
http://www.ogre3d.org/docs/manual/manual_21.html#SEC117
But I don't see the effect in scene.Just the mesh is grey.
If I don't use glsl,it is normal.
If I use hlsl,cg,it is also normal.
Anybody can solve the problem.
By the way,I use ogre1.70,NVIDIA GeForce 9500GT,Intel Pentium Dual CPU,windows xp .
I set up DX9,but don't set up openGL.

my code:

Code: Select all

#include "ExampleApplication.h"
using namespace Ogre;
class MyListener : public ExampleFrameListener
{
public:
	SceneManager* mSceneMgr;
public:
	MyListener(SceneManager *sceneMgr, RenderWindow* win, Camera* cam)
		: ExampleFrameListener(win, cam)
		, mSceneMgr(sceneMgr)
	{

	}
	bool frameStarted(const FrameEvent& evt)
	{
		if (!ExampleFrameListener::frameStarted(evt))
			return false;
		return true;
	}
	virtual bool processUnbufferedKeyInput(const FrameEvent& evt)
	{
		return ExampleFrameListener::processUnbufferedKeyInput(evt);
	}
};
class testEffect : public ExampleApplication
{
public:
	testEffect ()
	{	
	}
	~testEffect () 
	{
	}
	void createScene(void)
	{
		// Ambient
		mSceneMgr->setAmbientLight( ColourValue( 0.1, 0.1, 0.1) );
		mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");
		// camera
		mCamera->setPosition(Vector3(0,200,200));
		mCamera->lookAt(Vector3(0,0,0));
		// light
		Vector3 lightdir(0, -1, -1);
		lightdir.normalise();
		Light* l = mSceneMgr->createLight("tstLight");
		l->setType(Light::LT_DIRECTIONAL);
		l->setDirection(lightdir);
		l->setDiffuseColour(ColourValue::White);
		l->setSpecularColour(ColourValue(0.1, 0.1, 0.1));
		// ground
		MeshManager::getSingleton().createPlane("ground", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
			Plane(Vector3::UNIT_Y, 0), 1000, 1000, 20, 20, true, 1, 6, 6, Vector3::UNIT_Z);
		Entity* ground = mSceneMgr->createEntity("Ground", "ground"); 
		ground->setMaterialName("Examples/Rockwall");
		ground->setCastShadows(false);
		mSceneMgr->getRootSceneNode()->attachObject(ground);
                                // robot
		Entity* robot = mSceneMgr->createEntity("robot", "robot.mesh");
		SceneNode* robotNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("robotnode",Vector3(0, 0, 0));
		robotNode->attachObject(robot);
		robot->setMaterialName("test_robot");
	}
	void createFrameListener(void)
	{
		mFrameListener = new MyListener(mSceneMgr, mWindow, mCamera);
		mRoot->addFrameListener(mFrameListener);
	}
public:
};

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"

INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
#else
int main(int argc, char *argv[])
#endif
{
	try{
		testEffect  app;
		app.go();
	}catch (Ogre::Exception& e){
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
		MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_ICONERROR | MB_TASKMODAL);
#else
		std::cerr << "An exception has occurred: " << e.getFullDescription().c_str() << std::endl;
#endif
	}

	return 0;
}
my test_robot.material:

Code: Select all

fragment_program myFragmentShader glsl
{
  source example.frag
}
material test_robot
{
  technique
  {
    pass
    {
     fragment_program_ref myFragmentShader
     {
       param_named diffuseMap int 0
     }
     texture_unit
     {
       texture r2skin.jpg 2d
     }
    }
  }
}
my example.frag:

Code: Select all

varying vec2 UV;
uniform sampler2D diffuseMap;
void main(void)
{
	gl_FragColor = texture2D(diffuseMap, UV);
}
User avatar
wacom
Gnome
Posts: 350
Joined: Sun Feb 10, 2008 2:07 pm

Re: How to use glsl in ogre?

Post by wacom »

In order to see anything GLSL produces, you have to use OpenGL, as GLSL is the shader language of OpenGL.

In any case, check your Ogre.log.
rcpfuchs
Gnoblar
Posts: 1
Joined: Tue Jan 14, 2014 11:07 am

Re: How to use glsl in ogre?

Post by rcpfuchs »

The post seems to be pretty old, but it is number one at google so it deserves the correct answer.
Your Code works for me if I exchange UV with gl_TexCoord.
Like this

Code: Select all

varying vec2 UV;
uniform sampler2D diffuseMap;
void main(void)
{
   gl_FragColor = texture2D(diffuseMap, gl_TexCoord[0].st);
}
Also a bmp file I exported using blender resulted in a black color. I exported it as tga file then it worked.

Have a nice day!
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: How to use glsl in ogre?

Post by c6burns »

Oh pagerank ... you have failed humanity hehe. Also there's nothing wrong with his fragment program except that it expects a varying from the vertex program, but he didn't attach a vertex program :shock: Myself, I would at least move up to version 120 and try to do away with FFP. This way it is easier to adapt to newer versions of GLSL down the road, or to GLSLES:

.material file

Code: Select all

fragment_program SimpleFp_GLSL glsl
{
	source SimpleFp.glsl
}

vertex_program SimpleVp_GLSL glsl
{
	source SimpleVp.glsl
}

material test_robot
{
	technique
	{
		pass
		{
			vertex_program_ref SimpleVp_GLSL
			{
				param_named_auto worldViewProj worldviewproj_matrix
			}
			
			fragment_program_ref SimpleFp_GLSL
			{
				param_named diffuseMap int 0
			}
			
			texture_unit
			{
				texture r2skin.jpg 2d
			}
		}
	}
}
vertex program (SimpleVp.glsl)

Code: Select all

#version 120

attribute vec4 position;
attribute vec2 uv0;

uniform mat4 worldViewProj;

varying vec2 oUV0;

void main()
{
	gl_Position = worldViewProj * position;
	oUV0 = uv0;
}
fragment program (SimpleFp.glsl)

Code: Select all

#version 120

uniform sampler2D diffuseMap;

varying vec2 oUV0;

void main ()
{
	gl_FragColor = texture2D(diffuseMap, oUV0);
}
Post Reply