I'm not that great writing C++ programs, so the code is just an altered example.
It just renders an image into a render target and then from the render target onto the screen.
Code: Select all
#include <irrlicht.h>
#include <iostream>
#include "driverChoice.h"
#include "exampleHelper.h"
using namespace irr;
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
IrrlichtDevice* device = 0;
class MyShaderCallBack : public video::IShaderConstantSetCallBack
{
public:
MyShaderCallBack() : WorldViewProjID(-1), FirstUpdate(true)
{
}
virtual void OnSetConstants(video::IMaterialRendererServices* services,
s32 userData)
{
video::IVideoDriver* driver = services->getVideoDriver();
// get shader constants id.
if (FirstUpdate)
{
WorldViewProjID = services->getVertexShaderConstantID("cameraTransformMatrix");
// Textures ID are important only for OpenGL interface.
if(driver->getDriverType() == video::EDT_OPENGL)
TextureID = services->getVertexShaderConstantID("TexA");
FirstUpdate = false;
}
// set clip matrix
core::matrix4 worldViewProj;
worldViewProj = driver->getTransform(video::ETS_PROJECTION);
worldViewProj *= driver->getTransform(video::ETS_VIEW);
worldViewProj *= driver->getTransform(video::ETS_WORLD);
services->setVertexShaderConstant(WorldViewProjID, worldViewProj.pointer(), 16);
// set texture, for textures you can use both an int and a float setPixelShaderConstant interfaces (You need it only for an OpenGL driver).
s32 TextureLayerID = 0;
services->setPixelShaderConstant(TextureID, &TextureLayerID, 1);
}
private:
s32 WorldViewProjID;
s32 TextureID;
bool FirstUpdate;
};
/*
The next few lines start up the engine just like in most other tutorials
before. But in addition, we ask the user if he wants to use high level shaders
in this example, if he selected a driver which is capable of doing so.
*/
int main()
{
// ask user for driver
video::E_DRIVER_TYPE driverType=driverChoiceConsole();
if (driverType==video::EDT_COUNT)
return 1;
// create device
device = createDevice(driverType, core::dimension2d<u32>(640, 480));
if (device == 0)
return 1; // could not create selected driver.
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* gui = device->getGUIEnvironment();
const io::path mediaPath = getExampleMediaPath();
char* vsShaderProgram;
char* psShaderProgram;
switch(driverType)
{
case video::EDT_DIRECT3D9:
vsShaderProgram =
"struct VS_INPUT\n"
"{\n"
" float4 position : POSITION;\n"
" float2 tcoord0 : TEXCOORD0;\n"
"};\n"
""
"struct VS_OUTPUT\n"
"{\n"
" float4 position : POSITION;\n"
" float2 tcoord0 : TEXCOORD0;\n"
"};\n"
""
"float4x4 cameraTransformMatrix;\n"
""
"VS_OUTPUT vertexMain(VS_INPUT input)\n"
"{\n"
" VS_OUTPUT output;\n"
" output.position = mul(input.position, cameraTransformMatrix);\n"
" output.tcoord0 = input.tcoord0;\n"
" return output;\n"
"}\n";
psShaderProgram =
"struct PS_OUTPUT\n"
"{\n"
" float4 color : COLOR;\n"
"};\n"
""
"struct PS_INPUT\n"
"{\n"
" float4 position : POSITION;\n"
" float2 tcoord0 : TEXCOORD0;\n"
"};\n"
""
"sampler2D texA;\n"
""
"PS_OUTPUT pixelMain(PS_INPUT input)\n"
"{\n"
" PS_OUTPUT output;\n"
" float4 color = tex2D(texA, input.tcoord0);\n"
" output.color = color;\n"
" return output;\n"
"}\n";
break;
case video::EDT_OPENGL:
vsShaderProgram =
"uniform mat4 cameraTransformMatrix;\n"
""
"void main(void)\n"
"{"
" gl_Position = cameraTransformMatrix * gl_Vertex;\n"
" gl_TexCoord[0] = gl_MultiTexCoord0;\n"
"}\n";
psShaderProgram =
"uniform sampler2D texA;\n"
""
"void main(void)\n"
"{\n"
" vec4 color = texture(texA, gl_TexCoord[0]);\n"
" gl_FragColor = color;\n"
"}\n";
break;
}
video::IGPUProgrammingServices* gpu = driver->getGPUProgrammingServices();
s32 newMaterialType = 0;
if (gpu)
{
MyShaderCallBack* mc = new MyShaderCallBack();
const video::E_GPU_SHADING_LANGUAGE shadingLanguage = video::EGSL_DEFAULT;
newMaterialType = gpu->addHighLevelShaderMaterial(
vsShaderProgram, "vertexMain", video::EVST_VS_2_0,
psShaderProgram, "pixelMain", video::EPST_PS_3_0,
mc, video::EMT_SOLID, 0, shadingLanguage);
mc->drop();
}
//simple material with any image on it.
video::SMaterial material1 = video::SMaterial();
material1.setTexture(0, driver->getTexture(mediaPath + "irrlichtlogo.bmp"));
material1.setFlag(video::EMF_LIGHTING, false);
material1.setFlag(video::EMF_BACK_FACE_CULLING, false);
material1.MaterialType = ((video::E_MATERIAL_TYPE)newMaterialType);
video::ITexture* targetTex1 = driver->addRenderTargetTexture(core::dimension2du(1280, 720), "target1");
//second material has the render target as a texture
video::SMaterial material2 = video::SMaterial(material1);
material2.setTexture(0, targetTex1);
float zValue = -0.5f;
video::SColor defaultColor = video::SColor(255, 255, 255, 255);
// 0 - 1
// 2 - 3
video::S3DVertex quadVerts[4] =
{
video::S3DVertex(core::vector3df(-.5f,+.5f,zValue), core::vector3df(0), defaultColor, core::vector2df(0,0)),
video::S3DVertex(core::vector3df(+.5f,+.5f,zValue), core::vector3df(0), defaultColor, core::vector2df(1,0)),
video::S3DVertex(core::vector3df(-.5f,-.5f,zValue), core::vector3df(0), defaultColor, core::vector2df(0,1)),
video::S3DVertex(core::vector3df(+.5f,-.5f,zValue), core::vector3df(0), defaultColor, core::vector2df(1,1))
};
u16 quadIndices[6] =
{
0, 1, 2,
1, 3, 2
};
core::matrix4 projectMatrix = core::matrix4();
core::matrix4 worldMatrix = core::matrix4();
core::matrix4 cameraMatrix = core::matrix4();
cameraMatrix = cameraMatrix.buildProjectionMatrixOrthoRH(1.0f, 1.0f, 0.01f, 100.0f);
int lastFPS = -1;
while(device->run())
if (device->isWindowActive())
{
driver->beginScene(video::ECBF_ALL, video::SColor(255,0,0,0));
driver->setTransform(video::ETS_PROJECTION, projectMatrix);
driver->setTransform(video::ETS_WORLD, worldMatrix);
driver->setTransform(video::ETS_VIEW, cameraMatrix);
//render on the first render target
driver->setRenderTarget(targetTex1, video::ECBF_ALL);
driver->setMaterial(material1);
driver->drawVertexPrimitiveList(quadVerts, 4, quadIndices, 2);
//render on the second render target
driver->setRenderTarget(nullptr);
driver->setMaterial(material2);
driver->drawVertexPrimitiveList(quadVerts, 4, quadIndices, 2);
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"Irrlicht Engine - Y flipping bug [";
str += driver->getName();
str += "] FPS:";
str += fps;
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
device->drop();
return 0;
}
OpenGL:
Code: Select all
//Vertex Shader:
uniform mat4 cameraTransformMatrix;
void main(void)
{
gl_Position = cameraTransformMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
}
//Pixel Shader:
uniform sampler2D texA;
void main(void)
{
vec4 color = texture(texA, gl_TexCoord[0]);
gl_FragColor = color;
}
Code: Select all
struct VS_INPUT
{
float4 position : POSITION;
float2 tcoord0 : TEXCOORD0;
};
struct VS_OUTPUT
{
float4 position : POSITION;
float2 tcoord0 : TEXCOORD0;
};
float4x4 cameraTransformMatrix;
VS_OUTPUT vertexMain(VS_INPUT input)
{
VS_OUTPUT output;
output.position = mul(input.position, cameraTransformMatrix);
output.tcoord0 = input.tcoord0;
return output;
}
struct PS_OUTPUT
{
float4 color : COLOR;
};
struct PS_INPUT
{
float4 position : POSITION;
float2 tcoord0 : TEXCOORD0;
};
sampler2D texA;
PS_OUTPUT pixelMain(PS_INPUT input)
{
PS_OUTPUT output;
float4 color = tex2D(texA, input.tcoord0);
output.color = color;
return output;
}