Advertisement
Fonix

Dice.mm

Aug 30th, 2012
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //##### Dice.mm
  2.  
  3. - (void) setup{
  4.  
  5.     glGenVertexArraysOES(1, &_vertexArray);
  6.     glBindVertexArrayOES(_vertexArray);
  7.    
  8.     glGenBuffers(1, &_indexBuffer);
  9.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
  10.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices*sizeof(GLushort), indices, GL_STATIC_DRAW);
  11.    
  12.     glGenBuffers(1, &_vertexBuffer);
  13.     glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  14.     glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
  15.    
  16.     glEnableVertexAttribArray(effect->vertCoord);        
  17.     glVertexAttribPointer(effect->vertCoord, 3, GL_FLOAT, GL_FALSE, 0, 0);
  18.    
  19.     glEnableVertexAttribArray(effect->toon_vertCoord);//dont even know if you can do this twice for a bound buffer, but how else would you send vert coords to a diff shader
  20.     glVertexAttribPointer(effect->toon_vertCoord, 3, GL_FLOAT, GL_FALSE, 0, 0);
  21.    
  22.     glGenBuffers(1, &_normalBuffer);
  23.     glBindBuffer(GL_ARRAY_BUFFER, _normalBuffer);
  24.     glBufferData(GL_ARRAY_BUFFER, numNormals*sizeof(GLfloat), normals, GL_STATIC_DRAW);
  25.    
  26.     glEnableVertexAttribArray(effect->normal);
  27.     glVertexAttribPointer(effect->normal, 3, GL_FLOAT, GL_FALSE, 0, 0);
  28.  
  29.     glGenBuffers(1, &_textureBuffer);
  30.     glBindBuffer(GL_ARRAY_BUFFER, _textureBuffer);
  31.     glBufferData(GL_ARRAY_BUFFER, numTex*sizeof(GLfloat), tex, GL_STATIC_DRAW);
  32.    
  33.     glEnableVertexAttribArray(effect->texCoord);
  34.     glVertexAttribPointer(effect->texCoord, 2, GL_FLOAT, GL_FALSE, 0, 0);
  35.  
  36.     glBindVertexArrayOES(0);
  37. }
  38.  
  39. - (void) drawOutlines {
  40.    
  41.     if(selected){
  42.         [EAGLContext setCurrentContext:context];
  43.        
  44.         glBindVertexArrayOES(_vertexArray);
  45.        
  46.         effect->modelViewMatrix = mvm;
  47.        
  48.         [effect drawOutline];
  49.        
  50.         glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
  51.        
  52.     }
  53. }
  54.  
  55. - (void) draw {
  56.    
  57.     [EAGLContext setCurrentContext:context];
  58.    
  59.     glBindVertexArrayOES(_vertexArray);
  60.    
  61.     effect->modelViewMatrix = mvm;
  62.    
  63.     effect->numberColour = GLKVector4Make(numbers[colorSelected].r, numbers[colorSelected].g, numbers[colorSelected].b, 1);
  64.     effect->faceColour = GLKVector4Make(faceColors[colorSelected].r, faceColors[colorSelected].g, faceColors[colorSelected].b, 1);
  65.    
  66.     [effect prepareToDraw];
  67.          
  68.     glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
  69.    
  70.    
  71. }
  72.  
  73. //##### outside of the dice class, this is how I call the draw functions in the draw loop
  74.  
  75.  
  76. - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
  77.    
  78.     [EAGLContext setCurrentContext:self.RollView.context];
  79.    
  80.     [self update];
  81.    
  82.     glClearColor(0.9, 0.9, 0.9, 1.0);
  83.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  84.    
  85.     for(int i = 0; i < self.profile.tableOfDice.count; i++){
  86.         [[self.profile getDiceAt:i] drawOutlines];
  87.     }
  88.    
  89.     for(int i = 0; i < self.profile.tableOfDice.count; i++){
  90.         [[self.profile getDiceAt:i] draw];
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement