| 1 | /* | 
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 | Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 | This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | it under the terms of the GNU General Public License as published by | 
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 | any later version. | 
|---|
| 10 |  | 
|---|
| 11 | ### File Specific: | 
|---|
| 12 | main-programmer: Patrick Boenzli | 
|---|
| 13 | co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON | 
|---|
| 17 |  | 
|---|
| 18 | #include "image_entity.h" | 
|---|
| 19 |  | 
|---|
| 20 | #include "load_param.h" | 
|---|
| 21 | #include "factory.h" | 
|---|
| 22 |  | 
|---|
| 23 | #include "graphics_engine.h" | 
|---|
| 24 | #include "material.h" | 
|---|
| 25 | #include "glincl.h" | 
|---|
| 26 | #include "state.h" | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | using namespace std; | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | CREATE_FACTORY(ImageEntity, CL_IMAGE_ENTITY); | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | /** | 
|---|
| 36 | * standart constructor | 
|---|
| 37 | */ | 
|---|
| 38 | ImageEntity::ImageEntity (const TiXmlElement* root) | 
|---|
| 39 | { | 
|---|
| 40 | this->init(); | 
|---|
| 41 | if( root) | 
|---|
| 42 | this->loadParams(root); | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | /** | 
|---|
| 47 | * destroys a ImageEntity | 
|---|
| 48 | */ | 
|---|
| 49 | ImageEntity::~ImageEntity () | 
|---|
| 50 | { | 
|---|
| 51 | if (this->material) | 
|---|
| 52 | delete this->material; | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 |  | 
|---|
| 56 | /** | 
|---|
| 57 | * initializes the ImageEntity | 
|---|
| 58 | */ | 
|---|
| 59 | void ImageEntity::init() | 
|---|
| 60 | { | 
|---|
| 61 | this->setClassID(CL_IMAGE_ENTITY, "ImageEntity"); | 
|---|
| 62 | this->setName("ImageEntity"); | 
|---|
| 63 |  | 
|---|
| 64 | this->setLayer(E2D_LAYER_TOP); | 
|---|
| 65 | this->setRotationSpeed(5); | 
|---|
| 66 | this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0, GraphicsEngine::getInstance()->getResolutionY()/10.0); | 
|---|
| 67 |  | 
|---|
| 68 | this->setBindNode(this); | 
|---|
| 69 | this->material = new Material; | 
|---|
| 70 | this->setTexture("pictures/error_texture.png"); | 
|---|
| 71 | this->bBillboarding = false; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 | void ImageEntity::loadParams(const TiXmlElement* root) | 
|---|
| 76 | { | 
|---|
| 77 | PNode::loadParams(root); | 
|---|
| 78 |  | 
|---|
| 79 | LoadParam(root, "texture", this, ImageEntity, setTexture) | 
|---|
| 80 | .describe("the texture-file to load onto the ImageEntity"); | 
|---|
| 81 |  | 
|---|
| 82 | LoadParam(root, "size", this, ImageEntity, setSize) | 
|---|
| 83 | .describe("the size of the ImageEntity in Pixels"); | 
|---|
| 84 |  | 
|---|
| 85 | LoadParam(root, "rotation-speed", this, ImageEntity, setRotationSpeed) | 
|---|
| 86 | .describe("the Speed with which the ImageEntity should rotate"); | 
|---|
| 87 |  | 
|---|
| 88 | LoadParam(root, "billboarding", this, ImageEntity, toggleBillboarding) | 
|---|
| 89 | .describe("sets the Billboard to always look in the direction of the Player"); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 |  | 
|---|
| 93 | /** | 
|---|
| 94 | * sets the size of the ImageEntity. | 
|---|
| 95 | * @param size the size in pixels | 
|---|
| 96 | */ | 
|---|
| 97 | void ImageEntity::setSize(float sizeX, float sizeY) | 
|---|
| 98 | { | 
|---|
| 99 | this->setSize2D(sizeX, sizeY); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 | /** | 
|---|
| 104 | * sets the material to load | 
|---|
| 105 | * @param textureFile The texture-file to load onto the crosshair | 
|---|
| 106 | */ | 
|---|
| 107 | void ImageEntity::setTexture(const char* textureFile) | 
|---|
| 108 | { | 
|---|
| 109 | this->material->setDiffuseMap(textureFile); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 |  | 
|---|
| 113 | /** this turns on/off the billboarding of this WorldEntity | 
|---|
| 114 | * | 
|---|
| 115 | * This means that the image will always look in the direction of the Player | 
|---|
| 116 | */ | 
|---|
| 117 | void ImageEntity::toggleBillboarding() | 
|---|
| 118 | { | 
|---|
| 119 | this->bBillboarding = !this->bBillboarding; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 |  | 
|---|
| 123 | /** | 
|---|
| 124 | * ticks the ImageEntity | 
|---|
| 125 | * @param dt the time to ticks | 
|---|
| 126 | */ | 
|---|
| 127 | void ImageEntity::tick(float dt) | 
|---|
| 128 | { | 
|---|
| 129 | // let the crosshair rotate | 
|---|
| 130 | //this->shiftDir2D(dt * rotationSpeed); | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 | float z = 0.0f; | 
|---|
| 134 | glReadPixels ((int)this->getAbsCoor2D().x, | 
|---|
| 135 | GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, | 
|---|
| 136 | 1, | 
|---|
| 137 | 1, | 
|---|
| 138 | GL_DEPTH_COMPONENT, | 
|---|
| 139 | GL_FLOAT, | 
|---|
| 140 | &z); | 
|---|
| 141 |  | 
|---|
| 142 |  | 
|---|
| 143 | GLdouble objX=.0, objY=.0, objZ=.0; | 
|---|
| 144 | gluUnProject(this->getAbsCoor2D().x, | 
|---|
| 145 | GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, | 
|---|
| 146 | .99,  // z | 
|---|
| 147 | GraphicsEngine::modMat, | 
|---|
| 148 | GraphicsEngine::projMat, | 
|---|
| 149 | GraphicsEngine::viewPort, | 
|---|
| 150 | &objX, | 
|---|
| 151 | &objY, | 
|---|
| 152 | &objZ ); | 
|---|
| 153 |  | 
|---|
| 154 | //this->setAbsCoor(objX, objY, objZ); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 |  | 
|---|
| 158 | /** | 
|---|
| 159 | * draws the crosshair | 
|---|
| 160 | */ | 
|---|
| 161 | void ImageEntity::draw() const | 
|---|
| 162 | { | 
|---|
| 163 | if( !this->isVisible()) | 
|---|
| 164 | return; | 
|---|
| 165 |  | 
|---|
| 166 | glPushMatrix(); | 
|---|
| 167 | glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); | 
|---|
| 168 |  | 
|---|
| 169 | //glRotatef(this->getAbsDir2D(), 0,0,1); | 
|---|
| 170 | this->material->select(); | 
|---|
| 171 | glBegin(GL_TRIANGLE_STRIP); | 
|---|
| 172 | glTexCoord2f(0, 0); | 
|---|
| 173 | glVertex2f(-this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| 174 | glTexCoord2f(1, 0); | 
|---|
| 175 | glVertex2f(this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| 176 | glTexCoord2f(0, 1); | 
|---|
| 177 | glVertex2f(-this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| 178 | glTexCoord2f(1, 1); | 
|---|
| 179 | glVertex2f(this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| 180 | glEnd(); | 
|---|
| 181 | glPopMatrix(); | 
|---|
| 182 |  | 
|---|
| 183 | } | 
|---|