GLFunView.m
上传用户:jjjjag8
上传日期:2017-04-17
资源大小:1443k
文件大小:4k
源码类别:

iPhone

开发平台:

MultiPlatform

  1. //
  2. //  GLFunView.m
  3. //  GLFun
  4. //
  5. //  Created by Jeff LaMarche on 7/31/08.
  6. //  Copyright 2008 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "GLFunView.h"
  9. @implementation GLFunView
  10. @synthesize firstTouch;
  11. @synthesize lastTouch;
  12. @synthesize currentColor;
  13. @synthesize useRandomColor;
  14. @synthesize shapeType;
  15. @synthesize sprite;
  16. - (id)initWithCoder:(NSCoder*)coder
  17. {
  18. if((self = [super initWithCoder:coder])) {
  19. self.currentColor = [UIColor redColor];
  20. self.useRandomColor = NO;
  21. }
  22. return self;
  23. }
  24. - (void)draw 
  25. {
  26. glLoadIdentity();
  27. glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  28. glClear(GL_COLOR_BUFFER_BIT);
  29. CGColorRef color = currentColor.CGColor;
  30. const CGFloat *components = CGColorGetComponents(color);
  31. CGFloat red = components[0];
  32. CGFloat green = components[1];
  33. CGFloat blue = components[2];
  34. glColor4f(red,green, blue, 1.0);
  35. switch (shapeType) {
  36. case kLineShape: {
  37. if (sprite){
  38. [sprite release];
  39. self.sprite = nil;
  40. }
  41. GLfloat vertices[4];
  42. // Convert coordinates
  43. vertices[0] =  firstTouch.x;
  44. vertices[1] = self.frame.size.height - firstTouch.y;
  45. vertices[2] = lastTouch.x;
  46. vertices[3] = self.frame.size.height - lastTouch.y;
  47. glLineWidth(2.0);
  48. glVertexPointer (2, GL_FLOAT , 0, vertices);
  49. glDrawArrays (GL_LINES, 0, 2);
  50. break;
  51. }
  52. case kRectShape:{
  53. if (sprite){
  54. [sprite release];
  55. self.sprite = nil;
  56. }
  57. // Calculate bounding rect and store in vertices
  58. GLfloat vertices[8];
  59. GLfloat minX = (firstTouch.x > lastTouch.x) ? lastTouch.x : firstTouch.x;
  60. GLfloat minY = (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y) ? self.frame.size.height - lastTouch.y : self.frame.size.height - firstTouch.y;
  61. GLfloat maxX = (firstTouch.x > lastTouch.x) ? firstTouch.x : lastTouch.x;
  62. GLfloat maxY = (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y) ? self.frame.size.height - firstTouch.y : self.frame.size.height - lastTouch.y;
  63. vertices[0] = maxX;
  64. vertices[1] = maxY;
  65. vertices[2] = minX;
  66. vertices[3] = maxY;
  67. vertices[4] = minX;
  68. vertices[5] = minY;
  69. vertices[6] = maxX;
  70. vertices[7] = minY;
  71. glVertexPointer (2, GL_FLOAT , 0, vertices);
  72. glDrawArrays (GL_TRIANGLE_FAN, 0, 4);
  73. break;
  74. }
  75. case kEllipseShape: {
  76. if (sprite){
  77. [sprite release];
  78. self.sprite = nil;
  79. }
  80. GLfloat vertices[720];
  81. GLfloat xradius = (firstTouch.x > lastTouch.x) ? (firstTouch.x - lastTouch.x)/2 : (lastTouch.x - firstTouch.x)/2;
  82. GLfloat yradius = (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y) ? ((self.frame.size.height - firstTouch.y) - (self.frame.size.height - lastTouch.y))/2 : ((self.frame.size.height - lastTouch.y) - (self.frame.size.height - firstTouch.y))/2; 
  83. for (int i = 0; i <= 720; i+=2)
  84. {
  85. GLfloat xOffset = (firstTouch.x > lastTouch.x) ? lastTouch.x + xradius : firstTouch.x + xradius;
  86. GLfloat yOffset = (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y) ? self.frame.size.height - lastTouch.y + yradius : self.frame.size.height - firstTouch.y + yradius;
  87. vertices[i] = (cos(degreesToRadian(i))*xradius) + xOffset;
  88. vertices[i+1] = (sin(degreesToRadian(i))*yradius) + yOffset;
  89. }
  90. glVertexPointer (2, GL_FLOAT , 0, vertices);
  91. glDrawArrays (GL_TRIANGLE_FAN, 0, 360);
  92. break;
  93. }
  94. case kImageShape:
  95. if (sprite == nil) {
  96. self.sprite = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"iphone.png"]];
  97. glBindTexture(GL_TEXTURE_2D, sprite.name);
  98. }
  99. [sprite drawAtPoint:CGPointMake(lastTouch.x, self.frame.size.height - lastTouch.y)];
  100. break;
  101. default:
  102. break;
  103. }
  104. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  105. [context presentRenderbuffer:GL_RENDERBUFFER_OES];
  106. }
  107. - (void)dealloc {
  108. [currentColor release];
  109. [sprite release];
  110. [super dealloc];
  111. }
  112. // Handles the start of a touch
  113. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  114. {
  115.     UITouch* touch = [[event touchesForView:self] anyObject];
  116. firstTouch = [touch locationInView:self];
  117. lastTouch = [touch locationInView:self];
  118. [self draw];
  119. }
  120. // Handles the continuation of a touch.
  121. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  122. {  
  123. UITouch *touch = [touches anyObject];
  124. lastTouch = [touch locationInView:self];
  125. [self draw];
  126. }
  127. // Handles the end of a touch event when the touch is a tap.
  128. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  129. {
  130. UITouch *touch = [touches anyObject];
  131. lastTouch = [touch locationInView:self];
  132. [self draw];
  133. }
  134. @end