drawPixel.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (C) 2008-2012 Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. using namespace irr;
  5. using namespace core;
  6. using namespace scene;
  7. using namespace video;
  8. using namespace io;
  9. using namespace gui;
  10. //! Tests IVideoDriver::drawPixel().
  11. /** Expect to see two diagonal lines overlaying a wall texture cube.
  12. One line should run from green at the top left to red at the bottom right.
  13. The other should run from cyan 100% transparent at the bottom left to
  14. cyan 100% opaque at the top right. */
  15. static bool lineRender(E_DRIVER_TYPE driverType)
  16. {
  17. IrrlichtDevice *device = createDevice( driverType, dimension2d<u32>(160, 120), 32);
  18. if (!device)
  19. return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
  20. IVideoDriver* driver = device->getVideoDriver();
  21. ISceneManager * smgr = device->getSceneManager();
  22. stabilizeScreenBackground(driver);
  23. logTestString("Testing driver %ls\n", driver->getName());
  24. // Draw a cube background so that we can check that the pixels' alpha is working.
  25. ISceneNode * cube = smgr->addCubeSceneNode(50.f, 0, -1, vector3df(0, 0, 60));
  26. cube->setMaterialTexture(0, driver->getTexture("../media/wall.bmp"));
  27. cube->setMaterialFlag(video::EMF_LIGHTING, false);
  28. (void)smgr->addCameraSceneNode();
  29. driver->beginScene(true, true, SColor(255,100,101,140));
  30. smgr->drawAll();
  31. // Test for benign handling of offscreen pixel values as well as onscreen ones.
  32. for(s32 x = -10; x < 170; ++x)
  33. {
  34. s32 y = 120 * x / 160;
  35. driver->drawPixel((u32)x, (u32)y, SColor(255, 255 * x / 640, 255 * (640 - x) / 640, 0));
  36. y = 120 - y;
  37. driver->drawPixel((u32)x, (u32)y, SColor(255 * x / 640, 0, 255, 255));
  38. }
  39. driver->endScene();
  40. bool result = takeScreenshotAndCompareAgainstReference(driver, "-drawPixel.png", 98.81f);
  41. device->closeDevice();
  42. device->run();
  43. device->drop();
  44. return result;
  45. }
  46. // this test draws alternating white and black borders with
  47. // increasing thickness. Intended use of this test is to ensure
  48. // the corect pixel alignment within the render window.
  49. static bool pixelAccuracy(E_DRIVER_TYPE driverType)
  50. {
  51. IrrlichtDevice *device = createDevice( driverType, dimension2d<u32>(160, 120), 32);
  52. if (!device)
  53. return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
  54. IVideoDriver* driver = device->getVideoDriver();
  55. stabilizeScreenBackground(driver);
  56. logTestString("Testing driver %ls\n", driver->getName());
  57. device->getSceneManager()->addCameraSceneNode();
  58. driver->beginScene(true, true, SColor(255,100,101,140));
  59. u32 start=0;
  60. for (u32 count=1; count<10; ++count)
  61. {
  62. for (u32 j=0; j<count; ++j)
  63. {
  64. for (u32 x=0; x<100-start; ++x)
  65. {
  66. driver->drawPixel(start+x, start, (count%2==1)?0xffffffff:0xff000000);
  67. }
  68. ++start;
  69. }
  70. }
  71. start=0;
  72. for (u32 count=1; count<10; ++count)
  73. {
  74. for (u32 j=0; j<count; ++j)
  75. {
  76. for (u32 x=0; x<100-start; ++x)
  77. {
  78. driver->drawPixel(start, start+x, (count%2==1)?0xffffffff:0xff000000);
  79. }
  80. ++start;
  81. }
  82. }
  83. for (u32 x=0; x<100; ++x)
  84. {
  85. driver->drawPixel(x, x, 0xffff0000);
  86. }
  87. driver->endScene();
  88. bool result = takeScreenshotAndCompareAgainstReference(driver, "-pixelAccuracy.png");
  89. device->closeDevice();
  90. device->run();
  91. device->drop();
  92. return result;
  93. }
  94. // this test draws lines of different lengths and compares
  95. // them with pixel placement
  96. // grey pixels denote start and end of the white drawn lines
  97. // black pixels only make those grey points better visible
  98. // yellow and magenta lines should start and end next toa black pixel,
  99. // yellow one right to the last black pixel down, magenta below the last
  100. // black pixel to the right
  101. // white lines are always double drawn, lines back and forth.
  102. static bool drawLine(E_DRIVER_TYPE driverType)
  103. {
  104. IrrlichtDevice *device = createDevice( driverType, dimension2d<u32>(160, 120), 32);
  105. if (!device)
  106. return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
  107. IVideoDriver* driver = device->getVideoDriver();
  108. stabilizeScreenBackground(driver);
  109. logTestString("Testing driver %ls\n", driver->getName());
  110. device->getSceneManager()->addCameraSceneNode();
  111. driver->beginScene(true, true, SColor(255,100,101,140));
  112. // horizontal lines
  113. for (u32 i=0; i<20; ++i)
  114. {
  115. driver->draw2DLine(core::vector2di(10,10+3*i), core::vector2di(10+2*i,10+3*i));
  116. // mark start point
  117. driver->drawPixel(9,10+3*i+1, video::SColor(0xff000000));
  118. driver->drawPixel(10,10+3*i+1, video::SColor(0xff888888));
  119. driver->drawPixel(11,10+3*i+1, video::SColor(0xff000000));
  120. // mark end point
  121. driver->drawPixel(9+2*i,10+3*i+1, video::SColor(0xff000000));
  122. driver->drawPixel(10+2*i,10+3*i+1, video::SColor(0xff888888));
  123. driver->drawPixel(11+2*i,10+3*i+1, video::SColor(0xff000000));
  124. driver->draw2DLine(core::vector2di(10+2*i,10+3*i+2), core::vector2di(10,10+3*i+2));
  125. }
  126. // vertical lines
  127. for (u32 i=0; i<20; ++i)
  128. {
  129. driver->draw2DLine(core::vector2di(11+3*i,10), core::vector2di(11+3*i,10+2*i));
  130. // mark start point
  131. driver->drawPixel(11+3*i+1,9, video::SColor(0xff000000));
  132. driver->drawPixel(11+3*i+1,10, video::SColor(0xff888888));
  133. driver->drawPixel(11+3*i+1,11, video::SColor(0xff000000));
  134. // mark end point
  135. driver->drawPixel(11+3*i+1,9+2*i, video::SColor(0xff000000));
  136. driver->drawPixel(11+3*i+1,10+2*i, video::SColor(0xff888888));
  137. driver->drawPixel(11+3*i+1,11+2*i, video::SColor(0xff000000));
  138. driver->draw2DLine(core::vector2di(11+3*i+2,10+2*i), core::vector2di(11+3*i+2, 10));
  139. }
  140. // diagonal lines
  141. driver->draw2DLine(core::vector2di(14,14),core::vector2di(50,68), video::SColor(0xffffff00));
  142. driver->draw2DLine(core::vector2di(15,14),core::vector2di(69,50), video::SColor(0xffff00ff));
  143. driver->endScene();
  144. bool result = takeScreenshotAndCompareAgainstReference(driver, "-drawLine.png");
  145. device->closeDevice();
  146. device->run();
  147. device->drop();
  148. return result;
  149. }
  150. bool drawPixel(void)
  151. {
  152. bool result = true;
  153. TestWithAllDrivers(lineRender);
  154. TestWithAllDrivers(pixelAccuracy);
  155. TestWithAllDrivers(drawLine);
  156. return result;
  157. }