SHOW:
|
|
- or go back to the newest paste.
| 1 | // | |
| 2 | // Dice.m | |
| 3 | // DiceRoller | |
| 4 | ||
| 5 | - | // Created by Jacques Questiaux on 2012/04/17. |
| 5 | + | |
| 6 | - | // Copyright (c) 2012 [email protected]. All rights reserved. |
| 6 | + | |
| 7 | #import "d6.h" | |
| 8 | #import "d8.h" | |
| 9 | #import "d10.h" | |
| 10 | #import "d12.h" | |
| 11 | #import "d20.h" | |
| 12 | ||
| 13 | @interface Dice () {
| |
| 14 | ||
| 15 | GLuint _vertexArray; | |
| 16 | GLuint _vertexBuffer; | |
| 17 | GLuint _indexBuffer; | |
| 18 | GLuint _colorBuffer; | |
| 19 | GLuint _normalBuffer; | |
| 20 | ||
| 21 | GLfloat *vertices; | |
| 22 | GLubyte *indices; | |
| 23 | GLfloat *normals; | |
| 24 | GLfloat *colors; | |
| 25 | ||
| 26 | GLubyte numVertices; | |
| 27 | GLubyte numIndices; | |
| 28 | GLubyte numNormals; | |
| 29 | GLubyte numColors; | |
| 30 | ||
| 31 | NSMutableArray *surfaceNormals; | |
| 32 | ||
| 33 | bool k; | |
| 34 | } | |
| 35 | ||
| 36 | ||
| 37 | - (void) rotateXInDegrees: (float) rot; | |
| 38 | - (void) rotateYInDegrees: (float) rot; | |
| 39 | - (void) makeSurfaceNormals; | |
| 40 | ||
| 41 | @end | |
| 42 | ||
| 43 | ||
| 44 | @implementation Dice | |
| 45 | ||
| 46 | @synthesize preD = _preD; | |
| 47 | @synthesize postD = _postD; | |
| 48 | @synthesize plusD = _plusD; | |
| 49 | @synthesize success = _success; | |
| 50 | ||
| 51 | @synthesize up = _up; | |
| 52 | @synthesize front = _front; | |
| 53 | @synthesize right = _right; | |
| 54 | @synthesize position =_position; | |
| 55 | ||
| 56 | @synthesize direction = _direction; | |
| 57 | ||
| 58 | @synthesize modelViewMatrix = _modelViewMatrix; | |
| 59 | ||
| 60 | - (id) initWithParams: (NSUInteger)preD: (NSUInteger)postD: (NSUInteger)plusD: (NSUInteger)success{
| |
| 61 | ||
| 62 | if(self = [super init]){
| |
| 63 | ||
| 64 | self.preD = preD; | |
| 65 | self.postD = postD; | |
| 66 | self.plusD = plusD; | |
| 67 | self.success = success; | |
| 68 | ||
| 69 | size = 75; | |
| 70 | friction = 0.95f; | |
| 71 | ||
| 72 | surfaceNormals = [[NSMutableArray alloc] init]; | |
| 73 | ||
| 74 | return self; | |
| 75 | } | |
| 76 | ||
| 77 | return nil; | |
| 78 | } | |
| 79 | ||
| 80 | - (void) Context: (EAGLContext *) c Effect: (GLKBaseEffect *) e {
| |
| 81 | ||
| 82 | NSLog(@"before context\n"); | |
| 83 | ||
| 84 | self.right = GLKVector3Make(1, 0, 0); | |
| 85 | self.up = GLKVector3Make(0, 1, 0); | |
| 86 | self.front = GLKVector3Make(0, 0, 1); | |
| 87 | ||
| 88 | context = c; | |
| 89 | effect = e; | |
| 90 | ||
| 91 | k = true; | |
| 92 | ||
| 93 | NSLog(@"after context\n"); | |
| 94 | ||
| 95 | [self setup]; | |
| 96 | } | |
| 97 | ||
| 98 | - (void) setup{
| |
| 99 | ||
| 100 | NSLog(@"before setup\n"); | |
| 101 | ||
| 102 | //too lazy to swap to a switch statement | |
| 103 | if(self.postD == 4){
| |
| 104 | vertices = d4vertices; | |
| 105 | numVertices = sizeof(d4vertices); | |
| 106 | ||
| 107 | indices = d4indices; | |
| 108 | numIndices = sizeof(d4indices); | |
| 109 | ||
| 110 | normals = d4normals; | |
| 111 | numNormals = sizeof(d4normals); | |
| 112 | ||
| 113 | colors = d4colors; | |
| 114 | numColors = sizeof(d4colors); | |
| 115 | ||
| 116 | } | |
| 117 | else if(self.postD == 6){
| |
| 118 | vertices = d6vertices; | |
| 119 | numVertices = sizeof(d6vertices); | |
| 120 | ||
| 121 | indices = d6indices; | |
| 122 | numIndices = sizeof(d6indices); | |
| 123 | ||
| 124 | normals = d6normals; | |
| 125 | numNormals = sizeof(d6normals); | |
| 126 | ||
| 127 | colors = d6colors; | |
| 128 | numColors = sizeof(d6colors); | |
| 129 | ||
| 130 | } | |
| 131 | else if(self.postD == 8){
| |
| 132 | vertices = d8vertices; | |
| 133 | numVertices = sizeof(d8vertices); | |
| 134 | ||
| 135 | indices = d8indices; | |
| 136 | numIndices = sizeof(d8indices); | |
| 137 | ||
| 138 | normals = d8normals; | |
| 139 | numNormals = sizeof(d8normals); | |
| 140 | ||
| 141 | colors = d8colors; | |
| 142 | numColors = sizeof(d8colors); | |
| 143 | ||
| 144 | } | |
| 145 | else if(self.postD == 10){
| |
| 146 | vertices = d10vertices; | |
| 147 | numVertices = sizeof(d10vertices); | |
| 148 | ||
| 149 | indices = d10indices; | |
| 150 | numIndices = sizeof(d10indices); | |
| 151 | ||
| 152 | normals = d10normals; | |
| 153 | numNormals = sizeof(d10normals); | |
| 154 | ||
| 155 | colors = d10colors; | |
| 156 | numColors = sizeof(d10colors); | |
| 157 | ||
| 158 | } | |
| 159 | else if(self.postD == 12){
| |
| 160 | vertices = d12vertices; | |
| 161 | numVertices = sizeof(d12vertices); | |
| 162 | ||
| 163 | indices = d12indices; | |
| 164 | numIndices = sizeof(d12indices); | |
| 165 | ||
| 166 | normals = d12normals; | |
| 167 | numNormals = sizeof(d12normals); | |
| 168 | ||
| 169 | colors = d12colors; | |
| 170 | numColors = sizeof(d12colors); | |
| 171 | ||
| 172 | } | |
| 173 | else {
| |
| 174 | vertices = d20vertices; | |
| 175 | numVertices = sizeof(d20vertices); | |
| 176 | ||
| 177 | indices = d20indices; | |
| 178 | numIndices = sizeof(d20indices); | |
| 179 | ||
| 180 | normals = d20normals; | |
| 181 | numNormals = sizeof(d20normals); | |
| 182 | ||
| 183 | colors = d20colors; | |
| 184 | numColors = sizeof(d20colors); | |
| 185 | ||
| 186 | } | |
| 187 | ||
| 188 | NSLog(@"after postD\n"); | |
| 189 | ||
| 190 | glGenVertexArraysOES(1, &_vertexArray); | |
| 191 | glBindVertexArrayOES(_vertexArray); | |
| 192 | ||
| 193 | glGenBuffers(1, &_indexBuffer); | |
| 194 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); | |
| 195 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices, indices, GL_STATIC_DRAW); | |
| 196 | ||
| 197 | glGenBuffers(1, &_vertexBuffer); | |
| 198 | glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); | |
| 199 | glBufferData(GL_ARRAY_BUFFER, numVertices, vertices, GL_STATIC_DRAW); | |
| 200 | ||
| 201 | glEnableVertexAttribArray(GLKVertexAttribPosition); | |
| 202 | glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
| 203 | ||
| 204 | glGenBuffers(1, &_colorBuffer); | |
| 205 | glBindBuffer(GL_ARRAY_BUFFER, _colorBuffer); | |
| 206 | glBufferData(GL_ARRAY_BUFFER, numColors, colors, GL_STATIC_DRAW); | |
| 207 | ||
| 208 | glEnableVertexAttribArray(GLKVertexAttribColor); | |
| 209 | glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, 0); | |
| 210 | ||
| 211 | glGenBuffers(1, &_normalBuffer); | |
| 212 | glBindBuffer(GL_ARRAY_BUFFER, _normalBuffer); | |
| 213 | glBufferData(GL_ARRAY_BUFFER, numNormals, normals, GL_STATIC_DRAW); | |
| 214 | ||
| 215 | glEnableVertexAttribArray(GLKVertexAttribNormal); | |
| 216 | glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
| 217 | ||
| 218 | glBindVertexArrayOES(0); | |
| 219 | ||
| 220 | NSLog(@"after setup\n"); | |
| 221 | } | |
| 222 | ||
| 223 | - (void) tearDown {
| |
| 224 | ||
| 225 | ||
| 226 | glDeleteBuffers(1, &_vertexBuffer); | |
| 227 | glDeleteBuffers(1, &_indexBuffer); | |
| 228 | glDeleteBuffers(1, &_colorBuffer); | |
| 229 | glDeleteBuffers(1, &_normalBuffer); | |
| 230 | } | |
| 231 | ||
| 232 | - (void) makeSurfaceNormals {
| |
| 233 | ||
| 234 | int numElements = sizeof(normals)/sizeof(GLfloat); | |
| 235 | ||
| 236 | GLKVector3 down = GLKVector3Make(0, 0, -1); | |
| 237 | ||
| 238 | GLKVector3 currentClosestToDown; | |
| 239 | ||
| 240 | float closestDistance = 9999; | |
| 241 | ||
| 242 | float rollAmount = 1; | |
| 243 | ||
| 244 | for(int i = 0; i < numElements; i++){
| |
| 245 | ||
| 246 | int index0 = indices[i*3]; | |
| 247 | int index1 = indices[i*3+1]; | |
| 248 | int index2 = indices[i*3+2]; | |
| 249 | ||
| 250 | GLKVector3 vertNormal0 = GLKVector3Make(normals[index0], normals[index0+1], normals[index0+2]); | |
| 251 | GLKVector3 vertNormal1 = GLKVector3Make(normals[index1], normals[index1+1], normals[index1+2]); | |
| 252 | GLKVector3 vertNormal2 = GLKVector3Make(normals[index2], normals[index2+1], normals[index2+2]); | |
| 253 | ||
| 254 | GLKVector3 surfNormal = GLKMatrix4MultiplyVector3(mvm, GLKVector3Add(GLKVector3Add(vertNormal0, vertNormal1), vertNormal2)); | |
| 255 | ||
| 256 | float distanceToDown = GLKVector3Length(GLKVector3Subtract(surfNormal, down)); | |
| 257 | ||
| 258 | if(distanceToDown < closestDistance){
| |
| 259 | ||
| 260 | closestDistance = distanceToDown; | |
| 261 | currentClosestToDown = surfNormal; | |
| 262 | } | |
| 263 | ||
| 264 | } | |
| 265 | ||
| 266 | GLKVector3 rollDirection; | |
| 267 | ||
| 268 | //rollAmount = (closestDistance*closestDistance); | |
| 269 | ||
| 270 | damp = logf(rollAmount+1); | |
| 271 | ||
| 272 | ||
| 273 | rollDirection = GLKVector3Subtract(currentClosestToDown, down); | |
| 274 | rollDirection = GLKVector3MultiplyScalar(GLKVector3Normalize(GLKVector3Make(rollDirection.x, rollDirection.y, 0)), 5/rollAmount); | |
| 275 | ||
| 276 | [self applyForceInDirection: GLKVector3Make(rollDirection.x*damp, rollDirection.y*damp, rollDirection.z*damp)]; | |
| 277 | ||
| 278 | } | |
| 279 | ||
| 280 | - (void) update: (float) timeSinceLastUpdate{
| |
| 281 | ||
| 282 | tslu = timeSinceLastUpdate; | |
| 283 | ||
| 284 | self.direction = GLKVector3MultiplyScalar(self.direction, friction); | |
| 285 | ||
| 286 | [self checkIfCollidedWithWalls]; | |
| 287 | ||
| 288 | [self moveInDirection:self.direction]; | |
| 289 | ||
| 290 | mvm = GLKMatrix4Make(self.right.x, self.right.y, self.right.z, 0, | |
| 291 | self.up.x, self.up.y, self.up.z, 0, | |
| 292 | self.front.x, self.front.y, self.front.z, 0, | |
| 293 | self.position.x, self.position.y, self.position.z, 1); | |
| 294 | ||
| 295 | [self makeSurfaceNormals]; | |
| 296 | } | |
| 297 | ||
| 298 | - (void) draw {
| |
| 299 | ||
| 300 | effect.transform.modelviewMatrix = GLKMatrix4Multiply(self.modelViewMatrix, mvm); | |
| 301 | ||
| 302 | ||
| 303 | if(k){
| |
| 304 | NSLog(@"before effect\n"); | |
| 305 | ||
| 306 | } | |
| 307 | ||
| 308 | [effect prepareToDraw]; | |
| 309 | ||
| 310 | ||
| 311 | if(k){
| |
| 312 | NSLog(@"after effect\n"); | |
| 313 | k = false; | |
| 314 | } | |
| 315 | ||
| 316 | ||
| 317 | glBindVertexArrayOES(_vertexArray); | |
| 318 | glDrawElements(GL_TRIANGLES, numIndices/sizeof(GLubyte), GL_UNSIGNED_BYTE, 0); | |
| 319 | ||
| 320 | } | |
| 321 | ||
| 322 | - (void) rotateXInDegrees:(float)rot{
| |
| 323 | ||
| 324 | GLKMatrix3 temp = GLKMatrix3MakeRotation(GLKMathDegreesToRadians(rot), 1, 0, 0); | |
| 325 | ||
| 326 | self.right = GLKMatrix3MultiplyVector3(temp, self.right); | |
| 327 | self.up = GLKMatrix3MultiplyVector3(temp, self.up); | |
| 328 | self.front = GLKMatrix3MultiplyVector3(temp, self.front); | |
| 329 | ||
| 330 | } | |
| 331 | ||
| 332 | - (void) rotateYInDegrees:(float)rot{
| |
| 333 | ||
| 334 | GLKMatrix3 temp = GLKMatrix3MakeRotation(GLKMathDegreesToRadians(rot), 0, 1, 0); | |
| 335 | self.right = GLKMatrix3MultiplyVector3(temp, self.right); | |
| 336 | self.up = GLKMatrix3MultiplyVector3(temp, self.up); | |
| 337 | self.front = GLKMatrix3MultiplyVector3(temp, self.front); | |
| 338 | ||
| 339 | } | |
| 340 | ||
| 341 | - (void) applyForceInDirection: (GLKVector3) dir {
| |
| 342 | ||
| 343 | self.direction = GLKVector3Add(self.direction, dir); | |
| 344 | } | |
| 345 | ||
| 346 | - (void) moveInDirection: (GLKVector3) dir {
| |
| 347 | ||
| 348 | previousPosition = self.position; | |
| 349 | ||
| 350 | self.position = GLKVector3Make(self.position.x + dir.x, self.position.y + dir.y, self.position.z + dir.z); | |
| 351 | ||
| 352 | [self rotateXInDegrees:-dir.y/3.14f]; | |
| 353 | [self rotateYInDegrees:dir.x/3.14f]; | |
| 354 | ||
| 355 | } | |
| 356 | ||
| 357 | - (BOOL) collidedWith: (Dice *) dice{
| |
| 358 | ||
| 359 | float distance = GLKVector3Distance(self.position, dice.position); | |
| 360 | ||
| 361 | if(distance < size + dice->size){
| |
| 362 | ||
| 363 | GLKVector3 newDirection = GLKVector3Normalize(GLKVector3Subtract(self.position, dice.position)); | |
| 364 | ||
| 365 | float newSpeed = (GLKVector3Length(self.direction) + GLKVector3Length(dice.direction)) / 2; | |
| 366 | ||
| 367 | self.direction = GLKVector3MultiplyScalar(GLKVector3Make(newDirection.x, newDirection.y, newDirection.z), newSpeed * 0.75f); | |
| 368 | ||
| 369 | dice.direction = GLKVector3MultiplyScalar(GLKVector3Make(-newDirection.x, -newDirection.y, -newDirection.z), newSpeed * 0.75f); | |
| 370 | ||
| 371 | self.position = previousPosition; | |
| 372 | ||
| 373 | return true; | |
| 374 | } | |
| 375 | ||
| 376 | ||
| 377 | return false; | |
| 378 | } | |
| 379 | ||
| 380 | - (BOOL) checkIfCollidedWithWalls{
| |
| 381 | ||
| 382 | BOOL didCollide = false; | |
| 383 | ||
| 384 | if(self.position.x - size < -500){
| |
| 385 | self.position = GLKVector3Make(-500 + size, self.position.y, self.position.z); | |
| 386 | self.direction = GLKVector3Make(-self.direction.x, self.direction.y, self.direction.z); | |
| 387 | didCollide = true; | |
| 388 | } | |
| 389 | ||
| 390 | if (self.position.x + size > 500){
| |
| 391 | self.position = GLKVector3Make(500 - size, self.position.y, self.position.z); | |
| 392 | self.direction = GLKVector3Make(-self.direction.x, self.direction.y, self.direction.z); | |
| 393 | didCollide = true; | |
| 394 | } | |
| 395 | ||
| 396 | if(self.position.y - size < -625){
| |
| 397 | self.position = GLKVector3Make(self.position.x, -625 + size, self.position.z); | |
| 398 | self.direction = GLKVector3Make(self.direction.x, -self.direction.y, self.direction.z); | |
| 399 | didCollide = true; | |
| 400 | } | |
| 401 | if (self.position.y + size > 625){
| |
| 402 | self.position = GLKVector3Make(self.position.x, 625 - size, self.position.z); | |
| 403 | self.direction = GLKVector3Make(self.direction.x, -self.direction.y, self.direction.z); | |
| 404 | didCollide = true; | |
| 405 | } | |
| 406 | ||
| 407 | ||
| 408 | return didCollide; | |
| 409 | } | |
| 410 | ||
| 411 | @end |