Guest User

Untitled

a guest
Dec 17th, 2012
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)startRenderLoop:(GLView *)MeshGLView
  2. {
  3.     if(displayLink == nil)
  4.     {
  5.         displayLink = [self.view.window.screen displayLinkWithTarget:MeshGLView selector:@selector(drawView)];
  6.         [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  7.         NSLog(@"Starting Render Loop");
  8.     }
  9. }
  10.  
  11.  
  12. - (void)drawView:(GLView *)theView {
  13.     NSLog(@"drawView");
  14.     if (theView.context != nil)
  15.     {
  16.         [EAGLContext setCurrentContext:theView.context];
  17.        
  18.         if (![theView getFrameBuffer])
  19.             [theView createFramebuffer];
  20.     }
  21.    
  22.     if (redraw) {
  23.         NSLog(@"drawMesh");
  24.         [self drawMesh:theView];
  25.         redraw = NO;
  26.     }
  27. }
  28.  
  29. - (void)drawMesh:(GLView *)MeshView {
  30.    
  31.     if (MeshView.context != nil)
  32.     {
  33.         //make it the current context for rendering
  34.         [EAGLContext setCurrentContext:MeshView.context];
  35.        
  36.         //if our framebuffers have not been created yet, do that now!
  37.         if (![MeshView getFrameBuffer])
  38.             [MeshView createFramebuffer];
  39.     }
  40.    
  41.     glMatrixMode(GL_MODELVIEW);
  42.  
  43.     Mesh mesh = _sculpture->getMesh();
  44.     int numberOfTriangles = mesh.getNumberOfTriangles();
  45.     std::vector<float> vertices = mesh.getVertices();
  46.     std::vector<float> normals = mesh.getNormals();
  47.    
  48.     //Draw mesh
  49.     {
  50.     glColor4f(1,0,1,1);
  51.    
  52.     glEnable(GL_COLOR_MATERIAL);
  53.     glShadeModel(GL_FLAT);
  54.    
  55.     glEnableClientState(GL_VERTEX_ARRAY);
  56.     glEnableClientState(GL_NORMAL_ARRAY);
  57.        
  58.         static double theta = 0;
  59.         theta += 0.03;
  60.         glRotatef(theta, 1, 1, 0);
  61.        
  62.     glVertexPointer(3, GL_FLOAT, 0, vertices.data());
  63.     glNormalPointer(GL_FLOAT, 0, normals.data());
  64.  
  65.     glDrawArrays(GL_TRIANGLES, 0, numberOfTriangles);
  66.  
  67.    
  68.     glDisableClientState(GL_NORMAL_ARRAY);
  69.     glDisableClientState(GL_VERTEX_ARRAY);
  70.    
  71.    
  72.     glShadeModel(GL_SMOOTH);
  73.     glDisable(GL_COLOR_MATERIAL);
  74.     glColor4f(1, 1, 1, 1);
  75.     }
  76.    
  77.     //Draw Axes
  78.     {
  79.         const GLfloat lineX[] = {
  80.             FrustumData.left * FrustumData.ratio, (FrustumData.top + FrustumData.bottom) / 2 * FrustumData.ratio, -FrustumData.zFar, //point A
  81.             FrustumData.right * FrustumData.ratio, (FrustumData.top + FrustumData.bottom) / 2 * FrustumData.ratio, -FrustumData.zFar //point B
  82.         };
  83.         const GLfloat lineY[] = {
  84.             (FrustumData.left + FrustumData.right) / 2 * FrustumData.ratio , FrustumData.bottom * FrustumData.ratio, -FrustumData.zFar, //point A
  85.             (FrustumData.left + FrustumData.right) / 2 * FrustumData.ratio , FrustumData.top * FrustumData.ratio, -FrustumData.zFar //point B
  86.         };
  87.         const GLfloat lineZ[] = {
  88.             (FrustumData.left + FrustumData.right) / 2 * FrustumData.ratio, (FrustumData.top + FrustumData.bottom) / 2 * FrustumData.ratio, -FrustumData.zFar, //point A
  89.             (FrustumData.left + FrustumData.right) / 2 * FrustumData.ratio, (FrustumData.top + FrustumData.bottom) / 2 * FrustumData.ratio, -FrustumData.zNear //point B
  90.         };
  91.        
  92.         glEnableClientState(GL_VERTEX_ARRAY);
  93.        
  94.         glColor4f(1.0f, 0.0f, 0.0f, 1.0f); // opaque red
  95.         glVertexPointer(3, GL_FLOAT, 0, lineX);
  96.         glDrawArrays(GL_LINES, 0, 2);
  97.        
  98.         glColor4f(0.0f, 1.0f, 0.0f, 1.0f); // opaque green
  99.         glVertexPointer(3, GL_FLOAT, 0, lineY);
  100.         glDrawArrays(GL_LINES, 0, 2);
  101.        
  102.         glColor4f(0.0f, 0.0f, 1.0f, 1.0f); // opaque blue
  103.         glVertexPointer(3, GL_FLOAT, 0, lineZ);
  104.         glDrawArrays(GL_LINES, 0, 2);
  105.        
  106.         glDisableClientState(GL_VERTEX_ARRAY);
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment