Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/include/misc.hpp
00001 /*
00002  * misc.hpp - Kukatz 3D
00003  * Copyright (c) 2010, 2011 - TÖRÖK Attila (torokati44@gmail.com)
00004  *
00005  * This software is provided 'as-is', without any express or implied
00006  * warranty. In no event will the authors be held liable for any damages
00007  * arising from the use of this software.
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  *       1. The origin of this software must not be misrepresented; you must not
00014  *                      claim that you wrote the original software. If you use this software
00015  *                      in a product, an acknowledgment in the product documentation would be
00016  *                      appreciated but is not required.
00017  *
00018  *       2. Altered source versions must be plainly marked as such, and must not be
00019  *                      misrepresented as being the original software.
00020  *
00021  *       3. This notice may not be removed or altered from any source
00022  *                      distribution.
00023  */
00024 
00025 #ifndef MISC_HPP_INCLUDED
00026 #define MISC_HPP_INCLUDED
00027 
00028 #include <vector>
00029 #include <cmath>
00030 #include <cstdlib>
00031 
00032 #include "opengl.hpp"
00033 
00034 #include <SFML/System/Vector2.hpp>
00035 #include <SFML/System/Vector3.hpp>
00036 #include <SFML/Graphics/Color.hpp>
00037 #include <SFML/Window/Event.hpp>
00038 #include <SFML/System/Unicode.hpp>
00039 
00040 inline sf::Vector3f itof3(const sf::Vector3i& i)
00041 {
00042         return sf::Vector3f(i.x, i.y, i.z);
00043 }
00044 
00045 inline sf::Vector3i ftoi3(const sf::Vector3f& f)
00046 {
00047         return sf::Vector3i(f.x + 0.5f, f.y + 0.5f, f.z + 0.5f); // for correct rounding
00048 }
00049 
00050 inline sf::Vector2i ftoi2(const sf::Vector2f& f)
00051 {
00052         return sf::Vector2i(f.x + 0.5f, f.y + 0.5f); // for correct rounding
00053 }
00054 
00055 inline sf::Vector3i crossprod(const sf::Vector3i& v1, const sf::Vector3i& v2)
00056 {       // xyzzy
00057         return sf::Vector3i(
00058                 v1.y * v2.z - v1.z * v2.y,
00059                 v1.z * v2.x - v1.x * v2.z,
00060                 v1.x * v2.y - v1.y * v2.x
00061         );
00062 }
00063 
00064 inline sf::Vector3f crossprod(const sf::Vector3f& v1, const sf::Vector3f& v2)
00065 {       // xyzzy
00066         return sf::Vector3f(
00067                 v1.y * v2.z - v1.z * v2.y,
00068                 v1.z * v2.x - v1.x * v2.z,
00069                 v1.x * v2.y - v1.y * v2.x
00070         );
00071 }
00072 
00073 inline sf::Vector3f crossprod(
00074         float x1, float y1, float z1,
00075         float x2, float y2, float z2)
00076 {       // xyzzy
00077         return sf::Vector3f(
00078                 y1 * z2 - z1 * y2,
00079                 z1 * x2 - x1 * z2,
00080                 x1 * y2 - y1 * x2
00081         );
00082 }
00083 
00084 
00085 inline float dotprod(const sf::Vector3f& v1, const sf::Vector3f& v2)
00086 {
00087         return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
00088 }
00089 
00090 
00091 inline sf::Vector3f transform(const sf::Vector3f& v, float m[16])
00092 {
00093         return sf::Vector3f(
00094                         v.x * m[0] + v.y * m[4] + v.z * m[8] + m[12],
00095                         v.x * m[1] + v.y * m[5] + v.z * m[9] + m[13],
00096                         v.x * m[2] + v.y * m[6] + v.z * m[10] + m[14]
00097                 );
00098 }
00099 
00100 inline sf::Vector3f transform(const sf::Vector3f& v, double m[16])
00101 {
00102         return sf::Vector3f(
00103                         v.x * m[0] + v.y * m[4] + v.z * m[8] + m[12],
00104                         v.x * m[1] + v.y * m[5] + v.z * m[9] + m[13],
00105                         v.x * m[2] + v.y * m[6] + v.z * m[10] + m[14]
00106                 );
00107 }
00108 
00109 inline float length(const sf::Vector3f& v)
00110 {
00111         return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
00112 }
00113 
00114 inline void clamp_to_distance(const sf::Vector3f& o, float d, sf::Vector3f& v)
00115 {
00116         v -= o;
00117         v *= (d / length(v));
00118         v += o;
00119 }
00120 
00121 inline unsigned int step_dist(const sf::Vector3i& v1, const sf::Vector3i& v2)
00122 {
00123         return abs(v1.x - v2.x) + abs(v1.y - v2.y) + abs(v1.z - v2.z);
00124 }
00125 
00126 inline sf::Vector3f lerp(const sf::Vector3f& v1, const sf::Vector3f& v2,
00127         float ratio)
00128 {
00129         if (ratio == 0.0) return v1;
00130         if (ratio == 1.0) return v2;
00131         
00132         return v1 + (v2 - v1) * ratio; // faster than v1*(1.0-ratio) + v2*ratio
00133 }
00134 
00135 inline sf::Vector3f normalize(const sf::Vector3f& v)
00136 {
00137         return v / sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
00138 }
00139 
00140 inline void reset_material()
00141 {
00142         glColor4ub(255, 255, 255, 255);
00143 }
00144 
00145 inline void set_material(const sf::Color& col)
00146 {
00147         glColor4ub(col.r, col.g, col.b, col.a);
00148 }
00149 
00150 inline sf::Unicode::Text get_key_name(sf::Key::Code c)
00151 {
00152         switch((unsigned int)c)
00153         {
00154                 case 0: return L"NINCS";
00155                 case sf::Key::A: return L"A";
00156                 case sf::Key::B: return L"B";
00157                 case sf::Key::C: return L"C";
00158                 case sf::Key::D: return L"D";
00159                 case sf::Key::E: return L"E";
00160                 case sf::Key::F: return L"F";
00161                 case sf::Key::G: return L"G";
00162                 case sf::Key::H: return L"H";
00163                 case sf::Key::I: return L"I";
00164                 case sf::Key::J: return L"J";
00165                 case sf::Key::K: return L"K";
00166                 case sf::Key::L: return L"L";
00167                 case sf::Key::M: return L"M";
00168                 case sf::Key::N: return L"N";
00169                 case sf::Key::O: return L"O";
00170                 case sf::Key::P: return L"P";
00171                 case sf::Key::Q: return L"Q";
00172                 case sf::Key::R: return L"R";
00173                 case sf::Key::S: return L"S";
00174                 case sf::Key::T: return L"T";
00175                 case sf::Key::U: return L"U";
00176                 case sf::Key::V: return L"V";
00177                 case sf::Key::W: return L"W";
00178                 case sf::Key::X: return L"X";
00179                 case sf::Key::Y: return L"Y";
00180                 case sf::Key::Z: return L"Z";
00181                 case sf::Key::Num0: return L"0";
00182                 case sf::Key::Num1: return L"1";
00183                 case sf::Key::Num2: return L"2";
00184                 case sf::Key::Num3: return L"3";
00185                 case sf::Key::Num4: return L"4";
00186                 case sf::Key::Num5: return L"5";
00187                 case sf::Key::Num6: return L"6";
00188                 case sf::Key::Num7: return L"7";
00189                 case sf::Key::Num8: return L"8";
00190                 case sf::Key::Num9: return L"9";
00191                 case sf::Key::LControl: return L"BAL CONTROL";
00192                 case sf::Key::LShift: return L"BAL SHIFT";
00193                 case sf::Key::LAlt: return L"BAL ALT";
00194                 case sf::Key::LSystem: return L"BAL RENDSZER";
00195                 case sf::Key::RControl: return L"JOBB CONTROL";
00196                 case sf::Key::RShift: return L"JOBB SHIFT";
00197                 case sf::Key::RAlt: return L"JOBB ALT";
00198                 case sf::Key::RSystem: return L"JOBB RENDSZER";
00199                 case sf::Key::LBracket: return L"[";
00200                 case sf::Key::RBracket: return L"]";
00201                 case sf::Key::SemiColon: return L";";
00202                 case sf::Key::Comma: return L",";
00203                 case sf::Key::Period: return L".";
00204                 case sf::Key::Quote: return L"'";
00205                 case sf::Key::Slash: return L"/";
00206                 case sf::Key::BackSlash: return L"\\";
00207                 case sf::Key::Tilde: return L"~";
00208                 case sf::Key::Equal: return L"=";
00209                 case sf::Key::Dash: return L"-";
00210                 case sf::Key::Space: return L"SZÓKÖZ";
00211                 case sf::Key::Return: return L"ENTER";
00212                 case sf::Key::Back: return L"VISSZA";
00213                 case sf::Key::Tab: return L"TAB";
00214                 case sf::Key::PageUp: return L"PAGE UP";
00215                 case sf::Key::PageDown: return L"PAGE DOWN";
00216                 case sf::Key::End: return L"END";
00217                 case sf::Key::Home: return L"HOME";
00218                 case sf::Key::Insert: return L"INSERT";
00219                 case sf::Key::Delete: return L"DELETE";
00220                 case sf::Key::Add: return L"+";
00221                 case sf::Key::Subtract: return L"-";
00222                 case sf::Key::Multiply: return L"*";
00223                 case sf::Key::Divide: return L"/";
00224                 case sf::Key::Left: return L"BALRA";
00225                 case sf::Key::Right: return L"JOBBRA";
00226                 case sf::Key::Up: return L"FEL";
00227                 case sf::Key::Down: return L"LE";
00228                 case sf::Key::Numpad0: return L"NUMPAD 0";
00229                 case sf::Key::Numpad1: return L"NUMPAD 1";
00230                 case sf::Key::Numpad2: return L"NUMPAD 2";
00231                 case sf::Key::Numpad3: return L"NUMPAD 3";
00232                 case sf::Key::Numpad4: return L"NUMPAD 4";
00233                 case sf::Key::Numpad5: return L"NUMPAD 5";
00234                 case sf::Key::Numpad6: return L"NUMPAD 6";
00235                 case sf::Key::Numpad7: return L"NUMPAD 7";
00236                 case sf::Key::Numpad8: return L"NUMPAD 8";
00237                 case sf::Key::Numpad9: return L"NUMPAD 9";
00238                 case sf::Key::F1: return L"F1";
00239                 case sf::Key::F2: return L"F2";
00240                 case sf::Key::F3: return L"F3";
00241                 case sf::Key::F4: return L"F4";
00242                 case sf::Key::F5: return L"F5";
00243                 case sf::Key::F6: return L"F6";
00244                 case sf::Key::F7: return L"F7";
00245                 case sf::Key::F8: return L"F8";
00246                 case sf::Key::F9: return L"F9";
00247                 case sf::Key::F10: return L"F10";
00248                 case sf::Key::F11: return L"F11";
00249                 case sf::Key::F12: return L"F12";
00250                 case sf::Key::F13: return L"F13";
00251                 case sf::Key::F14: return L"F14";
00252                 case sf::Key::F15: return L"F15";
00253                 default: return L"ISMERETLEN";
00254         }
00255 }
00256 
00257 #endif // MISC_HPP_INCLUDED
 Összes Osztályok