[3524] | 1 | |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
| 12 | |
---|
| 13 | ### File Specific |
---|
| 14 | main-programmer: Patrick Boenzli |
---|
| 15 | co-programmer: |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | #include "primitive.h" |
---|
[3608] | 20 | |
---|
[3524] | 21 | #include "stdincl.h" |
---|
| 22 | #include "world_entity.h" |
---|
| 23 | #include "objModel.h" |
---|
[3608] | 24 | #include "vector.h" |
---|
[3524] | 25 | |
---|
| 26 | using namespace std; |
---|
| 27 | |
---|
| 28 | |
---|
[3586] | 29 | |
---|
| 30 | /** |
---|
| 31 | \brief standard constructor |
---|
| 32 | */ |
---|
[3612] | 33 | Primitive::Primitive (pForm form) : WorldEntity() |
---|
[3524] | 34 | { |
---|
[3612] | 35 | this->form = form; |
---|
[3615] | 36 | this->size = 0.2f; |
---|
| 37 | this->segments = 6; |
---|
[3586] | 38 | this->object = gluNewQuadric(); |
---|
[3589] | 39 | |
---|
[3612] | 40 | gluQuadricNormals(this->object, GLU_SMOOTH); |
---|
[3586] | 41 | gluQuadricTexture(this->object, GL_TRUE); |
---|
[3612] | 42 | |
---|
[3587] | 43 | this->material = new Material("Sphere"); |
---|
[3586] | 44 | this->material->setIllum(3); |
---|
[3607] | 45 | this->material->setAmbient(1, .5, 1); |
---|
[3524] | 46 | } |
---|
| 47 | |
---|
| 48 | |
---|
[3586] | 49 | /** |
---|
| 50 | \brief standard deconstructor |
---|
| 51 | */ |
---|
[3524] | 52 | Primitive::~Primitive () |
---|
| 53 | { |
---|
[3586] | 54 | delete this->material; |
---|
| 55 | free(this->object); |
---|
[3524] | 56 | } |
---|
[3616] | 57 | |
---|
[3524] | 58 | |
---|
[3586] | 59 | /** |
---|
[3614] | 60 | \brief sets the form of the primitive body |
---|
| 61 | \param form (enumeration) from pForm |
---|
| 62 | */ |
---|
| 63 | void Primitive::setForm(pForm form) |
---|
| 64 | { |
---|
| 65 | this->form = form; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | \brief sets the size of the body |
---|
| 71 | \param size from [0..1] |
---|
| 72 | */ |
---|
| 73 | void Primitive::setSize(float size) |
---|
| 74 | { |
---|
| 75 | if( size < 0 || size > 1) |
---|
| 76 | { |
---|
| 77 | PRINTF(1)("size must be value between 0 and 1 [0..1]. resetting it to 1\n"); |
---|
| 78 | size = 1.0; |
---|
| 79 | } |
---|
| 80 | this->size = size; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | /** |
---|
[3615] | 85 | \brief set number of segments |
---|
| 86 | \param segment count |
---|
| 87 | |
---|
| 88 | the more segments the body has, the smoother it seems. This function makes only |
---|
| 89 | sense with speres and cylinders, cubes have constant number of segments :) |
---|
| 90 | */ |
---|
| 91 | void Primitive::setSegments(int segments) |
---|
| 92 | { |
---|
| 93 | if( segments <= 0) |
---|
| 94 | { |
---|
| 95 | PRINTF(1)("number of segments must be more than 0! not setting segment number\n"); |
---|
| 96 | } |
---|
| 97 | this->segments = segments; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /** |
---|
[3614] | 101 | \brief stets the material |
---|
| 102 | \param material to be set |
---|
| 103 | */ |
---|
| 104 | void Primitive::setMaterial(Material* material) |
---|
| 105 | { |
---|
| 106 | if( material == NULL) |
---|
| 107 | { |
---|
| 108 | PRINTF(1)("material reference is NULL, returning without modifications\n"); |
---|
| 109 | return; |
---|
| 110 | } |
---|
| 111 | this->material = material; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | \brief sets the texture name |
---|
| 117 | \param path to the texture |
---|
| 118 | |
---|
| 119 | eg: name could have following value: "../data/pictures/load_screen.jpg" |
---|
| 120 | */ |
---|
| 121 | void Primitive::setTexture(char* name) |
---|
| 122 | { |
---|
| 123 | if( name == NULL ) |
---|
| 124 | { |
---|
| 125 | PRINTF(1)("texture char reference is NULL, returning without modifications\n"); |
---|
| 126 | return; |
---|
| 127 | } |
---|
| 128 | this->textureName = name; |
---|
| 129 | this->material->setDiffuseMap( name); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | /** |
---|
[3586] | 134 | \brief called, when the object gets hit |
---|
| 135 | \param other object, that hits it |
---|
| 136 | \param location of impact |
---|
| 137 | */ |
---|
| 138 | void Primitive::hit (WorldEntity* weapon, Vector* loc) {} |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | \brief object collides |
---|
| 143 | */ |
---|
| 144 | void Primitive::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) {} |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | \brief tick singal put everything here, that is timedependent |
---|
| 149 | \param time in sec |
---|
| 150 | */ |
---|
[3524] | 151 | void Primitive::tick (float time) |
---|
| 152 | { |
---|
[3607] | 153 | // Vector v(0.0, 0.0, 1.0); |
---|
| 154 | // Quaternion q(10.0, v); |
---|
| 155 | // this->setRelDir(&(this->relDirection * q)); |
---|
[3524] | 156 | } |
---|
| 157 | |
---|
[3586] | 158 | /** |
---|
| 159 | \brief drawing function of the object |
---|
| 160 | */ |
---|
[3524] | 161 | void Primitive::draw () |
---|
| 162 | { |
---|
| 163 | glMatrixMode(GL_MODELVIEW); |
---|
[3526] | 164 | glPushMatrix(); |
---|
[3586] | 165 | |
---|
[3524] | 166 | float matrix[4][4]; |
---|
| 167 | glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); |
---|
| 168 | //rotate |
---|
[3586] | 169 | //this->getAbsDir().matrix (matrix); |
---|
| 170 | //glMultMatrixf((float*)matrix); |
---|
[3589] | 171 | this->material->select(); |
---|
[3612] | 172 | |
---|
| 173 | switch( this->form) |
---|
| 174 | { |
---|
| 175 | case P_SPHERE: |
---|
[3615] | 176 | gluSphere(this->object, this->size, this->segments, this->segments); |
---|
[3612] | 177 | break; |
---|
| 178 | case P_CUBE: |
---|
| 179 | this->glDrawCube(); |
---|
| 180 | break; |
---|
| 181 | case P_CYLINDER: |
---|
[3615] | 182 | gluCylinder(this->object, this->size, this->size, this->size * 2, this->segments, this->segments); |
---|
[3612] | 183 | break; |
---|
| 184 | case P_DISK: |
---|
[3615] | 185 | gluDisk(this->object, this->size, 2, this->segments, this->segments); |
---|
[3612] | 186 | break; |
---|
| 187 | default: |
---|
| 188 | break; |
---|
| 189 | } |
---|
[3586] | 190 | |
---|
[3526] | 191 | glPopMatrix(); |
---|
[3524] | 192 | } |
---|
| 193 | |
---|
[3612] | 194 | |
---|
| 195 | void Primitive::glDrawCube() |
---|
| 196 | { |
---|
| 197 | glBegin(GL_QUADS); |
---|
| 198 | glNormal3f( 0.0f, 0.0f, 1.0f); |
---|
| 199 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); |
---|
| 200 | glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); |
---|
| 201 | glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); |
---|
| 202 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); |
---|
| 203 | |
---|
| 204 | glNormal3f( 0.0f, 0.0f,-1.0f); |
---|
| 205 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); |
---|
| 206 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); |
---|
| 207 | glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); |
---|
| 208 | glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); |
---|
| 209 | |
---|
| 210 | glNormal3f( 0.0f, 1.0f, 0.0f); |
---|
| 211 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); |
---|
| 212 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); |
---|
| 213 | glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); |
---|
| 214 | glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); |
---|
| 215 | |
---|
| 216 | glNormal3f( 0.0f,-1.0f, 0.0f); |
---|
| 217 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); |
---|
| 218 | glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); |
---|
| 219 | glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); |
---|
| 220 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); |
---|
| 221 | |
---|
| 222 | glNormal3f( 1.0f, 0.0f, 0.0f); |
---|
| 223 | glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); |
---|
| 224 | glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); |
---|
| 225 | glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); |
---|
| 226 | glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); |
---|
| 227 | |
---|
| 228 | glNormal3f(-1.0f, 0.0f, 0.0f); |
---|
| 229 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); |
---|
| 230 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); |
---|
| 231 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); |
---|
| 232 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); |
---|
| 233 | glEnd(); |
---|
| 234 | } |
---|