PolySceneImage.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolySceneImage.h"
  20. #include "PolyMesh.h"
  21. #include "PolyTexture.h"
  22. #include "PolyVertex.h"
  23. using namespace Polycode;
  24. SceneImage* SceneImage::SceneImageWithImage(Image *image) {
  25. return new SceneImage(image);
  26. }
  27. SceneImage* SceneImage::SceneImageWithTexture(Texture *texture) {
  28. return new SceneImage(texture);
  29. }
  30. SceneImage::SceneImage(const String& fileName) : ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 1, 1) {
  31. loadTexture(fileName);
  32. imageWidth = texture->getWidth();
  33. imageHeight = texture->getHeight();
  34. setWidth(texture->getWidth());
  35. setHeight(texture->getHeight());
  36. setPrimitiveOptions(ScenePrimitive::TYPE_VPLANE, getWidth(), getHeight());
  37. }
  38. SceneImage::SceneImage(Image *image) : ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 1, 1) {
  39. loadTextureFromImage(image);
  40. imageWidth = texture->getWidth();
  41. imageHeight = texture->getHeight();
  42. setWidth(texture->getWidth());
  43. setHeight(texture->getHeight());
  44. setPrimitiveOptions(ScenePrimitive::TYPE_VPLANE, getWidth(), getHeight());
  45. }
  46. SceneImage::SceneImage(Texture *texture) : ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 1, 1) {
  47. setTexture(texture);
  48. imageWidth = texture->getWidth();
  49. imageHeight = texture->getHeight();
  50. setWidth(texture->getWidth());
  51. setHeight(texture->getHeight());
  52. setPrimitiveOptions(ScenePrimitive::TYPE_VPLANE, getWidth(), getHeight());
  53. }
  54. SceneImage::~SceneImage() {
  55. }
  56. Entity *SceneImage::Clone(bool deepClone, bool ignoreEditorOnly) const {
  57. SceneImage *newImage = new SceneImage(getTexture()->getResourcePath());
  58. applyClone(newImage, deepClone, ignoreEditorOnly);
  59. return newImage;
  60. }
  61. void SceneImage::applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const {
  62. ScenePrimitive::applyClone(clone, deepClone, ignoreEditorOnly);
  63. }
  64. void SceneImage::setImageCoordinates(Number x, Number y, Number width, Number height, Number realWidth, Number realHeight) {
  65. Number pixelSizeX = 1/imageWidth;
  66. Number pixelSizeY = 1/imageHeight;
  67. if(realWidth == -1)
  68. realWidth = width;
  69. if(realHeight == -1)
  70. realHeight = height;
  71. setWidth(realWidth);
  72. setHeight(realHeight);
  73. setPrimitiveOptions(ScenePrimitive::TYPE_VPLANE, getWidth(), getHeight());
  74. Number xFloat = x * pixelSizeX;
  75. Number yFloat = 1 - (y * pixelSizeY);
  76. Number wFloat = width * pixelSizeX;
  77. Number hFloat = height * pixelSizeY;
  78. mesh->getVertex(0)->setTexCoord(xFloat, yFloat - hFloat);
  79. mesh->getVertex(1)->setTexCoord(xFloat + wFloat, yFloat - hFloat);
  80. mesh->getVertex(2)->setTexCoord(xFloat + wFloat, yFloat);
  81. mesh->getVertex(3)->setTexCoord(xFloat, yFloat);
  82. mesh->arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  83. mesh->arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
  84. rebuildTransformMatrix();
  85. matrixDirty = true;
  86. }
  87. Number SceneImage::getImageWidth() const {
  88. return imageWidth;
  89. }
  90. Number SceneImage::getImageHeight() const {
  91. return imageHeight;
  92. }