/demos/pointLight/LightingView.j

http://github.com/tartiflop/ojgl · Unknown · 104 lines · 71 code · 33 blank · 0 comment · 0 complexity · 97f20786e84402a5067339f6add03821 MD5 · raw file

  1. @import "../../OJGL/GLView.j"
  2. @import "../../OJGL/GLU.j"
  3. @import "../../OJGL/GLLight.j"
  4. @import "../../primitives/Sphere.j"
  5. @import "../../materials/ColorMaterial.j"
  6. @import "../../renderers/GenericRenderer.j"
  7. @implementation LightingView : GLView {
  8. GLContext _glContext;
  9. GLLight _light1;
  10. GLLight _light2;
  11. GLLight _light3;
  12. float _angle;
  13. BOOL _ready;
  14. }
  15. - (id)initWithFrame:(CGRect)aFrame {
  16. self = [super initWithFrame:aFrame];
  17. if (self) {
  18. _ready = NO;
  19. // Get the OpenGL Context
  20. _glContext = [self glContext];
  21. // Initialise the renderer manager
  22. [[RendererManager alloc] initWithContext:_glContext];
  23. // build the scene
  24. [self initScene];
  25. _angle = 0;
  26. }
  27. return self;
  28. }
  29. - (void)initScene {
  30. // Prepare (initialise) context
  31. [_glContext prepare:[0.0, 0.0, 0.0, 1] clearDepth:1.0];
  32. [_glContext enableBackfaceCulling];
  33. // Create sphere with Color material
  34. var colorMaterial = [[ColorMaterial alloc] initWithHexColors:"BBBBBB" diffuse:"FFFFFF" specular:"FFFFFF" shininess:0.7 precise:YES];
  35. var _sphere = [[Sphere alloc] initWithGeometry:colorMaterial radius:4 longs:100 lats:100];
  36. [_sphere prepareGL:_glContext];
  37. [[RendererManager getInstance] addPrimitive:_sphere];
  38. _light1 = [[GLLight alloc] initWithHexColor:"0000FF" specularColor:"FFFFFF" attenuation:0.02];
  39. [[RendererManager getInstance] addLight:_light1];
  40. _light2 = [[GLLight alloc] initWithHexColor:"FF0000" specularColor:"FFFFFF" attenuation:0.02];
  41. [[RendererManager getInstance] addLight:_light2];
  42. _light3 = [[GLLight alloc] initWithHexColor:"00FF00" specularColor:"FFFFFF" attenuation:0.02];
  43. [[RendererManager getInstance] addLight:_light3];
  44. // Set the scene ambient color
  45. [[RendererManager getInstance] setSceneAmbient:"222222"];
  46. // Initialise view and projection matrices
  47. var lookat = [GLU lookAt:0 eyey:0 eyez:15 centerx:0 centery:0 centerz:0 upx:0 upy:1 upz:0];
  48. [[RendererManager getInstance] setViewMatrix:lookat];
  49. var perspective = [GLU perspective:60 aspect:[self width]/[self height] near:1 far:10000];
  50. [[RendererManager getInstance] setProjectionMatrix:perspective];
  51. // reshape
  52. [_glContext reshape:[self width] height:[self height]];
  53. _ready = YES;
  54. }
  55. - (void)drawRect:(CPRect)dirtyRect {
  56. // Only render the scene if ready
  57. if (!_ready) {
  58. return;
  59. }
  60. // Clear context
  61. [_glContext clearBuffer];
  62. [_light1 setPosition:[2 * Math.cos(_angle * Math.PI / 90), 10 * Math.sin(_angle * Math.PI / 90), 5]];
  63. [_light2 setPosition:[10 * Math.sin(_angle * Math.PI / 90), 2 * Math.cos(_angle * Math.PI / 30), 5]];
  64. [_light3 setPosition:[5 * Math.cos(_angle * Math.PI / 60), 5 * Math.cos(_angle * Math.PI / 120), 5]];
  65. // Render the scene
  66. [[RendererManager getInstance] render];
  67. _angle = _angle + 1 % 360;
  68. // flush
  69. [_glContext flush];
  70. }
  71. @end