/demos/pointLight/LightingDemoView.j

http://github.com/tartiflop/ojgl · Unknown · 150 lines · 110 code · 40 blank · 0 comment · 0 complexity · 24f9f4344c781c59feceb187fa9cd45c 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/TextureMaterial.j"
  6. @import "../../renderers/RendererManager.j"
  7. @implementation LightingDemoView : GLView {
  8. GLContext _glContext;
  9. GLLight _blueLight;
  10. GLLight _redLight;
  11. GLLight _greenLight;
  12. GLLight _yellowLight;
  13. GLLight _cyanLight;
  14. GLLight _magentaLight;
  15. float _lightAngle;
  16. float _sphereAngle;
  17. Array _sceneObjects;
  18. BOOL _ready;
  19. }
  20. - (id)initWithFrame:(CGRect)aFrame {
  21. self = [super initWithFrame:aFrame];
  22. if (self) {
  23. _ready = NO;
  24. // Get the OpenGL Context
  25. _glContext = [self glContext];
  26. _sceneObjects = [];
  27. // Initialise the renderer manager
  28. [[RendererManager alloc] initWithContext:_glContext];
  29. // build the scene
  30. [self initScene];
  31. _lightAngle = 0;
  32. }
  33. return self;
  34. }
  35. - (void)initScene {
  36. // Prepare (initialise) context
  37. [_glContext prepare:[0.0, 0.0, 0.0, 1] clearDepth:1.0];
  38. [_glContext enableBackfaceCulling];
  39. // Create the spheres with texture materials
  40. for (var k = 0; k < 3; k++) {
  41. for (var j = 0; j < 3; j++) {
  42. for (var i = 0; i < 3; i++) {
  43. var textureMaterial = [[TextureMaterial alloc] initWithTextureFile:"Resources/images/mars.jpg" shininess:0.7 precise:YES];
  44. var primitive = [[Sphere alloc] initWithGeometry:textureMaterial radius:1.3 longs:25 lats:25];
  45. [primitive setTranslation:((i - 1) * 4) y:((j - 1) * 4) z:((k - 1) * 4)];
  46. [primitive prepareGL:_glContext];
  47. [[RendererManager getInstance] addPrimitive:primitive];
  48. _sceneObjects.push(primitive);
  49. }
  50. }
  51. }
  52. // Create the lights
  53. _blueLight = [[GLLight alloc] initWithHexColor:"0000FF" specularColor:"FFFFFF" attenuation:0.02];
  54. [[RendererManager getInstance] addLight:_blueLight];
  55. _redLight = [[GLLight alloc] initWithHexColor:"FF0000" specularColor:"FFFFFF" attenuation:0.02];
  56. [[RendererManager getInstance] addLight:_redLight];
  57. _greenLight = [[GLLight alloc] initWithHexColor:"00FF00" specularColor:"FFFFFF" attenuation:0.02];
  58. [[RendererManager getInstance] addLight:_greenLight];
  59. _yellowLight = [[GLLight alloc] initWithHexColor:"FFFF00" specularColor:"FFFFFF" attenuation:0.02];
  60. [[RendererManager getInstance] addLight:_yellowLight];
  61. _cyanLight = [[GLLight alloc] initWithHexColor:"00FFFF" specularColor:"FFFFFF" attenuation:0.02];
  62. [[RendererManager getInstance] addLight:_cyanLight];
  63. _magentaLight = [[GLLight alloc] initWithHexColor:"FF00FF" specularColor:"FFFFFF" attenuation:0.02];
  64. [[RendererManager getInstance] addLight:_magentaLight];
  65. var whiteLight = [[GLLight alloc] initWithHexColor:"FFFFFF" specularColor:"FFFFFF" attenuation:0.02];
  66. [[RendererManager getInstance] addLight:whiteLight];
  67. [whiteLight setPosition:[7, 7, 4]];
  68. // Set the scene ambient color
  69. [[RendererManager getInstance] setSceneAmbient:"333333"];
  70. // Initialise view and projection matrices
  71. var lookat = [GLU lookAt:5 eyey:10 eyez:15 centerx:0 centery:0 centerz:0 upx:0 upy:1 upz:0];
  72. [[RendererManager getInstance] setViewMatrix:lookat];
  73. var perspective = [GLU perspective:60 aspect:[self width]/[self height] near:1 far:10000];
  74. [[RendererManager getInstance] setProjectionMatrix:perspective];
  75. // reshape
  76. [_glContext reshape:[self width] height:[self height]];
  77. _ready = YES;
  78. }
  79. - (void)drawRect:(CPRect)dirtyRect {
  80. // Only render the scene if ready
  81. if (!_ready) {
  82. return;
  83. }
  84. // Clear context
  85. [_glContext clearBuffer];
  86. // Rotate the spheres
  87. for (var i = 0; i < _sceneObjects.length; i++) {
  88. [_sceneObjects[i] setRotation:_sphereAngle x:0 y:1 z:0];
  89. }
  90. // Move the lights
  91. var blueAngle = _lightAngle;
  92. var redAngle = -(blueAngle + 120 % 360);
  93. var greenAngle = redAngle + 12 % 360;
  94. var yellowAngle = _lightAngle;
  95. var cyanAngle = -(yellowAngle + 90 % 360);
  96. var magentaAngle = cyanAngle + 90 % 360;
  97. [_blueLight setPosition:[7 * Math.sin(blueAngle * Math.PI / 90), 7 * Math.cos(blueAngle * Math.PI / 90), 7 * Math.cos(blueAngle * Math.PI / 180)]];
  98. [_redLight setPosition:[7 * Math.sin(redAngle * Math.PI / 145), 7 * Math.cos(redAngle * Math.PI / 145), 7 * Math.cos(redAngle * Math.PI / 180)]];
  99. [_greenLight setPosition:[7 * Math.sin(greenAngle * Math.PI / 60), 7 * Math.cos(greenAngle * Math.PI / 60), 7 * Math.cos(greenAngle * Math.PI / 180)]];
  100. [_yellowLight setPosition:[7 * Math.sin(yellowAngle * Math.PI / 90), 7 * Math.cos(yellowAngle * Math.PI / 180), 7 * Math.cos(yellowAngle * Math.PI / 90)]];
  101. [_cyanLight setPosition:[7 * Math.sin(cyanAngle * Math.PI / 145), 7 * Math.cos(cyanAngle * Math.PI / 180), 7 * Math.cos(cyanAngle * Math.PI / 145)]];
  102. [_magentaLight setPosition:[7 * Math.sin(magentaAngle * Math.PI / 60), 7 * Math.cos(magentaAngle * Math.PI / 180), 7 * Math.cos(magentaAngle * Math.PI / 60)]];
  103. // Render the scene
  104. [[RendererManager getInstance] render];
  105. _sphereAngle = (_sphereAngle + 2) % 360;
  106. _lightAngle = _lightAngle + 2 % 360;
  107. // flush
  108. [_glContext flush];
  109. }
  110. @end