~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to tests/textureRenderStates.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2008-2011 Christian Stehno, Colin MacDonald
 
2
// No rights reserved: this software is in the public domain.
 
3
 
 
4
#include "testUtils.h"
 
5
 
 
6
using namespace irr;
 
7
using namespace core;
 
8
 
 
9
static bool manyTextures(video::E_DRIVER_TYPE driverType)
 
10
{
 
11
        IrrlichtDevice *device = createDevice(driverType, dimension2d<u32>(160, 120), 32);
 
12
        if (!device)
 
13
                return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
 
14
 
 
15
        video::IVideoDriver* driver = device->getVideoDriver();
 
16
        scene::ISceneManager * smgr = device->getSceneManager();
 
17
 
 
18
        logTestString("Testing driver %ls\n", driver->getName());
 
19
 
 
20
        (void)smgr->addCameraSceneNode();
 
21
 
 
22
        scene::SMeshBufferLightMap* mesh = new scene::SMeshBufferLightMap;
 
23
 
 
24
        mesh->Vertices.reallocate(4);
 
25
        mesh->Indices.reallocate(6);
 
26
 
 
27
        mesh->Vertices.push_back(video::S3DVertex2TCoords(-50,50,80,irr::video::SColor(255,100,100,100),0,1,0,1));
 
28
        mesh->Vertices.push_back(video::S3DVertex2TCoords(50,50,80,irr::video::SColor(255,100,100,100),1,1,1,1));
 
29
        mesh->Vertices.push_back(video::S3DVertex2TCoords(50,-50,80,irr::video::SColor(255,100,100,100),1,0,1,0));
 
30
        mesh->Vertices.push_back(video::S3DVertex2TCoords(-50,-50,80,irr::video::SColor(255,100,100,100),0,0,0,0));
 
31
 
 
32
        mesh->Indices.push_back(0);
 
33
        mesh->Indices.push_back(1);
 
34
        mesh->Indices.push_back(2);
 
35
        mesh->Indices.push_back(2);
 
36
        mesh->Indices.push_back(3);
 
37
        mesh->Indices.push_back(0);
 
38
 
 
39
        video::SMaterial& mat = mesh->getMaterial();
 
40
        mat.Lighting = false;
 
41
        mat.setTexture(0,driver->getTexture("../media/fire.bmp"));
 
42
        mat.setTexture(1,driver->getTexture("../media/fire.bmp"));
 
43
        mat.setTexture(2,driver->getTexture("../media/fire.bmp"));
 
44
        mat.setTexture(3,driver->getTexture("../media/fire.bmp"));
 
45
 
 
46
        mesh->setDirty();
 
47
 
 
48
        driver->beginScene(true, true, video::SColor(255,100,101,140));
 
49
        // set camera
 
50
        smgr->drawAll();
 
51
        // draw meshbuffer
 
52
        driver->setMaterial(mat);
 
53
        driver->drawMeshBuffer(mesh); 
 
54
        driver->endScene();
 
55
        mesh->drop();
 
56
 
 
57
        bool result = takeScreenshotAndCompareAgainstReference(driver, "-multiTexture.png", 99.31f);
 
58
 
 
59
        device->closeDevice();
 
60
        device->run();
 
61
        device->drop();
 
62
 
 
63
        return result;
 
64
}
 
65
 
 
66
//! Tests interleaved loading and rendering of textures
 
67
/** The test loads a texture, renders it using draw2dimage, loads another
 
68
        texture and renders the first one again. Due to the texture cache this
 
69
        can lead to rendering of the second texture in second place. */
 
70
static bool renderAndLoad(video::E_DRIVER_TYPE driverType)
 
71
{
 
72
        IrrlichtDevice *device = createDevice( driverType, dimension2d<u32>(160, 120), 32);
 
73
        if (!device)
 
74
                return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
 
75
 
 
76
        video::IVideoDriver* driver = device->getVideoDriver();
 
77
        scene::ISceneManager * smgr = device->getSceneManager();
 
78
 
 
79
        logTestString("Testing driver %ls\n", driver->getName());
 
80
 
 
81
        video::ITexture* tex1 = driver->getTexture("../media/wall.bmp");
 
82
 
 
83
        (void)smgr->addCameraSceneNode();
 
84
 
 
85
        driver->beginScene(true, true, video::SColor(255,100,101,140));
 
86
        driver->draw2DImage(tex1, position2di(0,0));
 
87
        driver->endScene();
 
88
 
 
89
        driver->getTexture("../media/tools.png");
 
90
 
 
91
        driver->beginScene(true, true, video::SColor(255,100,101,140));
 
92
        driver->draw2DImage(tex1, position2di(0,0));
 
93
        driver->endScene();
 
94
 
 
95
        bool result = takeScreenshotAndCompareAgainstReference(driver, "-textureRenderStates.png", 99.85f);
 
96
 
 
97
        device->closeDevice();
 
98
        device->run();
 
99
        device->drop();
 
100
 
 
101
        return result;
 
102
}
 
103
 
 
104
 
 
105
// This test would cause a crash if it does not work
 
106
// in 1.5.1 and 1.6 an illegal access in the OpenGL driver caused this
 
107
static bool renderAndRemove(video::E_DRIVER_TYPE driverType)
 
108
{
 
109
        IrrlichtDevice *device = createDevice( driverType, dimension2d<u32>(160, 120), 32);
 
110
        if (!device)
 
111
                return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
 
112
 
 
113
        video::IVideoDriver* driver = device->getVideoDriver();
 
114
        scene::ISceneManager * smgr = device->getSceneManager();
 
115
 
 
116
        logTestString("Testing driver %ls\n", driver->getName());
 
117
 
 
118
        driver->beginScene (true, true, video::SColor(255, 0, 255, 0));
 
119
        smgr->drawAll();
 
120
        driver->endScene();
 
121
 
 
122
        smgr->addCameraSceneNode();
 
123
        video::ITexture* texture = driver->getTexture ("media/tools.png");
 
124
        scene::ISceneNode * img = smgr->addCubeSceneNode();
 
125
        img->setMaterialTexture(0, texture);
 
126
 
 
127
        driver->beginScene (true, true, video::SColor (255, 0, 255, 0));
 
128
        smgr->drawAll();
 
129
        driver->endScene();
 
130
 
 
131
        smgr->clear();  // Remove anything that used the texture
 
132
        driver->removeTexture(texture);
 
133
 
 
134
        driver->beginScene(true, true, video::SColor(255,100,101,140));
 
135
        smgr->drawAll();
 
136
        driver->endScene();
 
137
 
 
138
        smgr->addCameraSceneNode();
 
139
        texture = driver->getTexture("media/tools.png");
 
140
        img = smgr->addCubeSceneNode();
 
141
        img->setMaterialTexture(0, texture);
 
142
 
 
143
        driver->beginScene (true, true, irr::video::SColor (255, 0, 255, 0));
 
144
        smgr->drawAll();
 
145
        driver->endScene();
 
146
 
 
147
        device->closeDevice();
 
148
        device->run();
 
149
        device->drop();
 
150
 
 
151
        return true;
 
152
}
 
153
 
 
154
 
 
155
static bool testTextureMatrixInMixedScenes(video::E_DRIVER_TYPE driverType)
 
156
{
 
157
        irr::IrrlichtDevice* device = createDevice(driverType, dimension2du(160,120));
 
158
        if (!device)
 
159
                return true;
 
160
 
 
161
        video::IVideoDriver* driver = device->getVideoDriver();
 
162
        scene::ISceneManager* sceneManager = device->getSceneManager();
 
163
        gui::IGUIEnvironment* gui = device->getGUIEnvironment();
 
164
 
 
165
        if (!driver->queryFeature(video::EVDF_TEXTURE_MATRIX))
 
166
        {
 
167
                device->closeDevice();
 
168
                device->run();
 
169
                device->drop();
 
170
                return true;
 
171
        }
 
172
 
 
173
        logTestString("Testing driver %ls\n", driver->getName());
 
174
 
 
175
        scene::ICameraSceneNode* camera = sceneManager->addCameraSceneNode();
 
176
        camera->setPosition(vector3df(0,10,0));
 
177
 
 
178
        gui::IGUIStaticText* stext = gui->addStaticText(L" ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 ",
 
179
                        rect<s32>(5,100,150,125),false,false,0,false);
 
180
        stext->setBackgroundColor(video::SColor(255,128,0,0));
 
181
        stext->setOverrideColor(video::SColor(255,255,255,255));
 
182
 
 
183
        gui->addEditBox(L"Test edit box", rect<s32>(5,50,150,75));
 
184
 
 
185
        gui->addCheckBox(true, rect<s32>(5, 20, 150, 45),0,-1,L"Test CheckBox");
 
186
 
 
187
        video::SMaterial mat;
 
188
        mat.MaterialType = video::EMT_SOLID;
 
189
        mat.setFlag(video::EMF_LIGHTING, false);
 
190
        // problem doesn't occur if the scale defaults: (1.f,1.f).
 
191
        mat.getTextureMatrix(0).setTextureScale(2.0,2.0);
 
192
 
 
193
        scene::IAnimatedMesh* pmesh = sceneManager->addHillPlaneMesh("testMesh",dimension2d<f32>(50,50),dimension2d<u32>(6,6),&mat);
 
194
        sceneManager->addAnimatedMeshSceneNode(pmesh);
 
195
 
 
196
        driver->beginScene(true, true, video::SColor(255,100,101,140));
 
197
        sceneManager->drawAll();
 
198
        gui->drawAll();
 
199
        driver->endScene();
 
200
 
 
201
        bool result = takeScreenshotAndCompareAgainstReference(driver, "-textureMatrixInMixedScenes.png", 99.34f);
 
202
 
 
203
        device->closeDevice();
 
204
        device->run();
 
205
        device->drop();
 
206
 
 
207
        return result;
 
208
 
209
 
 
210
// animated texture matrix test.
 
211
static bool textureMatrix(video::E_DRIVER_TYPE driverType)
 
212
{
 
213
        irr::IrrlichtDevice* device = createDevice(driverType, dimension2du(160,120));
 
214
        if (!device)
 
215
                return true;
 
216
 
 
217
        video::IVideoDriver* driver = device->getVideoDriver();
 
218
        scene::ISceneManager* sceneManager = device->getSceneManager();
 
219
 
 
220
        if (!driver->queryFeature(video::EVDF_TEXTURE_MATRIX))
 
221
        {
 
222
                device->closeDevice();
 
223
                device->run();
 
224
                device->drop();
 
225
                return true;
 
226
        }
 
227
 
 
228
        logTestString("Testing driver %ls\n", driver->getName());
 
229
 
 
230
        scene::ICameraSceneNode* camera = sceneManager->addCameraSceneNode();
 
231
        camera->setPosition(vector3df(0,0,-10));
 
232
 
 
233
        // set up plane mesh to face the camera
 
234
        scene::IMesh* mesh = sceneManager->getGeometryCreator()->createPlaneMesh(dimension2df(150,150), core::dimension2du(1,1),0,core::dimension2df(1,1));
 
235
        scene::IMeshSceneNode* node = sceneManager->addMeshSceneNode(mesh, 0, -1, vector3df(0, 0, 150), vector3df(-90, 0, 0));
 
236
        if (mesh)
 
237
                mesh->drop();
 
238
 
 
239
        // set the hillplane material texture & save a reference
 
240
        // to the texture matrix.
 
241
        video::SMaterial& material = node->getMaterial(0);
 
242
        video::ITexture* tex = driver->getTexture("../media/water.jpg");
 
243
 
 
244
        material.setTexture(0,tex);
 
245
        material.setFlag(video::EMF_LIGHTING,false);
 
246
        matrix4& textureMatrix = material.TextureLayer[0].getTextureMatrix();
 
247
 
 
248
        const vector2df rcenter, scale(1.f, 1.f);
 
249
        vector2df trans;
 
250
 
 
251
        trans.X += 0.0005f;
 
252
        textureMatrix.buildTextureTransform(0.f, rcenter, trans, scale);
 
253
 
 
254
        driver->beginScene(true, true, video::SColor(255,100,101,140));
 
255
        sceneManager->drawAll();
 
256
        driver->endScene();
 
257
 
 
258
        bool result = takeScreenshotAndCompareAgainstReference(driver, "-textureMatrix.png");
 
259
 
 
260
        trans.X += 0.45f;
 
261
        textureMatrix.buildTextureTransform(0.f, rcenter, trans, scale);
 
262
 
 
263
        driver->beginScene(true, true, video::SColor(255,100,101,140));
 
264
        sceneManager->drawAll();
 
265
        driver->endScene();
 
266
 
 
267
        result &= takeScreenshotAndCompareAgainstReference(driver, "-textureMatrix2.png");
 
268
 
 
269
        device->closeDevice();
 
270
        device->run();
 
271
        device->drop();
 
272
 
 
273
        return result;
 
274
}
 
275
 
 
276
bool textureRenderStates(void)
 
277
{
 
278
        bool result = true;
 
279
 
 
280
        TestWithAllDrivers(renderAndLoad);
 
281
        TestWithAllDrivers(renderAndRemove);
 
282
        TestWithAllDrivers(testTextureMatrixInMixedScenes);
 
283
        TestWithAllDrivers(manyTextures);
 
284
        TestWithAllDrivers(textureMatrix);
 
285
 
 
286
        return result;
 
287
}