Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Dice.m
- // DiceRoller
- #import "Dice.h"
- #import "d4.h"
- #import "d6.h"
- #import "d8.h"
- #import "d10.h"
- #import "d12.h"
- #import "d20.h"
- @interface Dice () {
- GLuint _vertexArray;
- GLuint _vertexBuffer;
- GLuint _indexBuffer;
- GLuint _colorBuffer;
- GLuint _normalBuffer;
- GLfloat *vertices;
- GLubyte *indices;
- GLfloat *normals;
- GLfloat *colors;
- GLubyte numVertices;
- GLubyte numIndices;
- GLubyte numNormals;
- GLubyte numColors;
- NSMutableArray *surfaceNormals;
- bool k;
- }
- - (void) rotateXInDegrees: (float) rot;
- - (void) rotateYInDegrees: (float) rot;
- - (void) makeSurfaceNormals;
- @end
- @implementation Dice
- @synthesize preD = _preD;
- @synthesize postD = _postD;
- @synthesize plusD = _plusD;
- @synthesize success = _success;
- @synthesize up = _up;
- @synthesize front = _front;
- @synthesize right = _right;
- @synthesize position =_position;
- @synthesize direction = _direction;
- @synthesize modelViewMatrix = _modelViewMatrix;
- - (id) initWithParams: (NSUInteger)preD: (NSUInteger)postD: (NSUInteger)plusD: (NSUInteger)success{
- if(self = [super init]){
- self.preD = preD;
- self.postD = postD;
- self.plusD = plusD;
- self.success = success;
- size = 75;
- friction = 0.95f;
- surfaceNormals = [[NSMutableArray alloc] init];
- return self;
- }
- return nil;
- }
- - (void) Context: (EAGLContext *) c Effect: (GLKBaseEffect *) e {
- NSLog(@"before context\n");
- self.right = GLKVector3Make(1, 0, 0);
- self.up = GLKVector3Make(0, 1, 0);
- self.front = GLKVector3Make(0, 0, 1);
- context = c;
- effect = e;
- k = true;
- NSLog(@"after context\n");
- [self setup];
- }
- - (void) setup{
- NSLog(@"before setup\n");
- //too lazy to swap to a switch statement
- if(self.postD == 4){
- vertices = d4vertices;
- numVertices = sizeof(d4vertices);
- indices = d4indices;
- numIndices = sizeof(d4indices);
- normals = d4normals;
- numNormals = sizeof(d4normals);
- colors = d4colors;
- numColors = sizeof(d4colors);
- }
- else if(self.postD == 6){
- vertices = d6vertices;
- numVertices = sizeof(d6vertices);
- indices = d6indices;
- numIndices = sizeof(d6indices);
- normals = d6normals;
- numNormals = sizeof(d6normals);
- colors = d6colors;
- numColors = sizeof(d6colors);
- }
- else if(self.postD == 8){
- vertices = d8vertices;
- numVertices = sizeof(d8vertices);
- indices = d8indices;
- numIndices = sizeof(d8indices);
- normals = d8normals;
- numNormals = sizeof(d8normals);
- colors = d8colors;
- numColors = sizeof(d8colors);
- }
- else if(self.postD == 10){
- vertices = d10vertices;
- numVertices = sizeof(d10vertices);
- indices = d10indices;
- numIndices = sizeof(d10indices);
- normals = d10normals;
- numNormals = sizeof(d10normals);
- colors = d10colors;
- numColors = sizeof(d10colors);
- }
- else if(self.postD == 12){
- vertices = d12vertices;
- numVertices = sizeof(d12vertices);
- indices = d12indices;
- numIndices = sizeof(d12indices);
- normals = d12normals;
- numNormals = sizeof(d12normals);
- colors = d12colors;
- numColors = sizeof(d12colors);
- }
- else {
- vertices = d20vertices;
- numVertices = sizeof(d20vertices);
- indices = d20indices;
- numIndices = sizeof(d20indices);
- normals = d20normals;
- numNormals = sizeof(d20normals);
- colors = d20colors;
- numColors = sizeof(d20colors);
- }
- NSLog(@"after postD\n");
- glGenVertexArraysOES(1, &_vertexArray);
- glBindVertexArrayOES(_vertexArray);
- glGenBuffers(1, &_indexBuffer);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices, indices, GL_STATIC_DRAW);
- glGenBuffers(1, &_vertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, numVertices, vertices, GL_STATIC_DRAW);
- glEnableVertexAttribArray(GLKVertexAttribPosition);
- glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glGenBuffers(1, &_colorBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, _colorBuffer);
- glBufferData(GL_ARRAY_BUFFER, numColors, colors, GL_STATIC_DRAW);
- glEnableVertexAttribArray(GLKVertexAttribColor);
- glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, 0);
- glGenBuffers(1, &_normalBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, _normalBuffer);
- glBufferData(GL_ARRAY_BUFFER, numNormals, normals, GL_STATIC_DRAW);
- glEnableVertexAttribArray(GLKVertexAttribNormal);
- glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glBindVertexArrayOES(0);
- NSLog(@"after setup\n");
- }
- - (void) tearDown {
- glDeleteBuffers(1, &_vertexBuffer);
- glDeleteBuffers(1, &_indexBuffer);
- glDeleteBuffers(1, &_colorBuffer);
- glDeleteBuffers(1, &_normalBuffer);
- }
- - (void) makeSurfaceNormals {
- int numElements = sizeof(normals)/sizeof(GLfloat);
- GLKVector3 down = GLKVector3Make(0, 0, -1);
- GLKVector3 currentClosestToDown;
- float closestDistance = 9999;
- float rollAmount = 1;
- for(int i = 0; i < numElements; i++){
- int index0 = indices[i*3];
- int index1 = indices[i*3+1];
- int index2 = indices[i*3+2];
- GLKVector3 vertNormal0 = GLKVector3Make(normals[index0], normals[index0+1], normals[index0+2]);
- GLKVector3 vertNormal1 = GLKVector3Make(normals[index1], normals[index1+1], normals[index1+2]);
- GLKVector3 vertNormal2 = GLKVector3Make(normals[index2], normals[index2+1], normals[index2+2]);
- GLKVector3 surfNormal = GLKMatrix4MultiplyVector3(mvm, GLKVector3Add(GLKVector3Add(vertNormal0, vertNormal1), vertNormal2));
- float distanceToDown = GLKVector3Length(GLKVector3Subtract(surfNormal, down));
- if(distanceToDown < closestDistance){
- closestDistance = distanceToDown;
- currentClosestToDown = surfNormal;
- }
- }
- GLKVector3 rollDirection;
- //rollAmount = (closestDistance*closestDistance);
- damp = logf(rollAmount+1);
- rollDirection = GLKVector3Subtract(currentClosestToDown, down);
- rollDirection = GLKVector3MultiplyScalar(GLKVector3Normalize(GLKVector3Make(rollDirection.x, rollDirection.y, 0)), 5/rollAmount);
- [self applyForceInDirection: GLKVector3Make(rollDirection.x*damp, rollDirection.y*damp, rollDirection.z*damp)];
- }
- - (void) update: (float) timeSinceLastUpdate{
- tslu = timeSinceLastUpdate;
- self.direction = GLKVector3MultiplyScalar(self.direction, friction);
- [self checkIfCollidedWithWalls];
- [self moveInDirection:self.direction];
- mvm = GLKMatrix4Make(self.right.x, self.right.y, self.right.z, 0,
- self.up.x, self.up.y, self.up.z, 0,
- self.front.x, self.front.y, self.front.z, 0,
- self.position.x, self.position.y, self.position.z, 1);
- [self makeSurfaceNormals];
- }
- - (void) draw {
- effect.transform.modelviewMatrix = GLKMatrix4Multiply(self.modelViewMatrix, mvm);
- if(k){
- NSLog(@"before effect\n");
- }
- [effect prepareToDraw];
- if(k){
- NSLog(@"after effect\n");
- k = false;
- }
- glBindVertexArrayOES(_vertexArray);
- glDrawElements(GL_TRIANGLES, numIndices/sizeof(GLubyte), GL_UNSIGNED_BYTE, 0);
- }
- - (void) rotateXInDegrees:(float)rot{
- GLKMatrix3 temp = GLKMatrix3MakeRotation(GLKMathDegreesToRadians(rot), 1, 0, 0);
- self.right = GLKMatrix3MultiplyVector3(temp, self.right);
- self.up = GLKMatrix3MultiplyVector3(temp, self.up);
- self.front = GLKMatrix3MultiplyVector3(temp, self.front);
- }
- - (void) rotateYInDegrees:(float)rot{
- GLKMatrix3 temp = GLKMatrix3MakeRotation(GLKMathDegreesToRadians(rot), 0, 1, 0);
- self.right = GLKMatrix3MultiplyVector3(temp, self.right);
- self.up = GLKMatrix3MultiplyVector3(temp, self.up);
- self.front = GLKMatrix3MultiplyVector3(temp, self.front);
- }
- - (void) applyForceInDirection: (GLKVector3) dir {
- self.direction = GLKVector3Add(self.direction, dir);
- }
- - (void) moveInDirection: (GLKVector3) dir {
- previousPosition = self.position;
- self.position = GLKVector3Make(self.position.x + dir.x, self.position.y + dir.y, self.position.z + dir.z);
- [self rotateXInDegrees:-dir.y/3.14f];
- [self rotateYInDegrees:dir.x/3.14f];
- }
- - (BOOL) collidedWith: (Dice *) dice{
- float distance = GLKVector3Distance(self.position, dice.position);
- if(distance < size + dice->size){
- GLKVector3 newDirection = GLKVector3Normalize(GLKVector3Subtract(self.position, dice.position));
- float newSpeed = (GLKVector3Length(self.direction) + GLKVector3Length(dice.direction)) / 2;
- self.direction = GLKVector3MultiplyScalar(GLKVector3Make(newDirection.x, newDirection.y, newDirection.z), newSpeed * 0.75f);
- dice.direction = GLKVector3MultiplyScalar(GLKVector3Make(-newDirection.x, -newDirection.y, -newDirection.z), newSpeed * 0.75f);
- self.position = previousPosition;
- return true;
- }
- return false;
- }
- - (BOOL) checkIfCollidedWithWalls{
- BOOL didCollide = false;
- if(self.position.x - size < -500){
- self.position = GLKVector3Make(-500 + size, self.position.y, self.position.z);
- self.direction = GLKVector3Make(-self.direction.x, self.direction.y, self.direction.z);
- didCollide = true;
- }
- if (self.position.x + size > 500){
- self.position = GLKVector3Make(500 - size, self.position.y, self.position.z);
- self.direction = GLKVector3Make(-self.direction.x, self.direction.y, self.direction.z);
- didCollide = true;
- }
- if(self.position.y - size < -625){
- self.position = GLKVector3Make(self.position.x, -625 + size, self.position.z);
- self.direction = GLKVector3Make(self.direction.x, -self.direction.y, self.direction.z);
- didCollide = true;
- }
- if (self.position.y + size > 625){
- self.position = GLKVector3Make(self.position.x, 625 - size, self.position.z);
- self.direction = GLKVector3Make(self.direction.x, -self.direction.y, self.direction.z);
- didCollide = true;
- }
- return didCollide;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement