/demos/pointLight/TextureLightingView.j

http://github.com/tartiflop/ojgl · Unknown · 118 lines · 84 code · 34 blank · 0 comment · 0 complexity · 6ee32217e09999ffe737673a0609d599 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 "../../materials/ColorMaterial.j"
  7. @import "../../renderers/RendererManager.j"
  8. @implementation TextureLightingView : GLView {
  9. GLContext _glContext;
  10. GLLight _light1;
  11. GLLight _light2;
  12. GLLight _light3;
  13. float _lightAngle;
  14. float _sphereAngle;
  15. BOOL _ready;
  16. }
  17. - (id)initWithFrame:(CGRect)aFrame {
  18. self = [super initWithFrame:aFrame];
  19. if (self) {
  20. _ready = NO;
  21. // Get the OpenGL Context
  22. _glContext = [self glContext];
  23. // Initialise the renderer manager
  24. [[RendererManager alloc] initWithContext:_glContext];
  25. // build the scene
  26. [self initScene];
  27. _lightAngle = 0;
  28. }
  29. return self;
  30. }
  31. - (void)initScene {
  32. // Prepare (initialise) context
  33. [_glContext prepare:[0.0, 0.0, 0.0, 1] clearDepth:1.0];
  34. [_glContext enableBackfaceCulling];
  35. // Create sphere with Color material
  36. var textureMaterial = [[TextureMaterial alloc] initWithTextureFile:"Resources/images/mars.jpg" shininess:0.7 precise:YES];
  37. _textureSphere = [[Sphere alloc] initWithGeometry:textureMaterial radius:2 longs:25 lats:25];
  38. [_textureSphere prepareGL:_glContext];
  39. [_textureSphere setTranslation:-2.5 y:0 z:0];
  40. [[RendererManager getInstance] addPrimitive:_textureSphere];
  41. // Create sphere with Color material
  42. var colorMaterial = [[ColorMaterial alloc] initWithHexColors:"BBBBBB" diffuse:"FFFFFF" specular:"FFFFFF" shininess:0.9 precise:YES];
  43. _colorSphere = [[Sphere alloc] initWithGeometry:colorMaterial radius:2 longs:25 lats:25];
  44. [_colorSphere prepareGL:_glContext];
  45. [_colorSphere setTranslation:2.5 y:0 z:0];
  46. [[RendererManager getInstance] addPrimitive:_colorSphere];
  47. // Create the lights
  48. _light1 = [[GLLight alloc] initWithHexColor:"0000FF" specularColor:"FFFFFF" attenuation:0.02];
  49. [[RendererManager getInstance] addLight:_light1];
  50. _light2 = [[GLLight alloc] initWithHexColor:"FF0000" specularColor:"FFFFFF" attenuation:0.02];
  51. [[RendererManager getInstance] addLight:_light2];
  52. _light3 = [[GLLight alloc] initWithHexColor:"00FF00" specularColor:"FFFFFF" attenuation:0.02];
  53. [[RendererManager getInstance] addLight:_light3];
  54. // Set the scene ambient color
  55. [[RendererManager getInstance] setSceneAmbient:"333333"];
  56. // Initialise view and projection matrices
  57. var lookat = [GLU lookAt:0 eyey:4 eyez:6 centerx:0 centery:0 centerz:0 upx:0 upy:1 upz:0];
  58. [[RendererManager getInstance] setViewMatrix:lookat];
  59. var perspective = [GLU perspective:60 aspect:[self width]/[self height] near:1 far:10000];
  60. [[RendererManager getInstance] setProjectionMatrix:perspective];
  61. // reshape
  62. [_glContext reshape:[self width] height:[self height]];
  63. _ready = YES;
  64. }
  65. - (void)drawRect:(CPRect)dirtyRect {
  66. // Only render the scene if ready
  67. if (!_ready) {
  68. return;
  69. }
  70. // Clear context
  71. [_glContext clearBuffer];
  72. // rotate spheres
  73. [_textureSphere setRotation:_sphereAngle x:0 y:1 z:0];
  74. [_light1 setPosition:[6 * Math.sin(_lightAngle * Math.PI / 60), 4 * Math.cos(_lightAngle * Math.PI / 60), 2]];
  75. [_light2 setPosition:[6 * Math.cos(_lightAngle * Math.PI / 90), 4 * Math.sin(_lightAngle * Math.PI / 45), 3]];
  76. [_light3 setPosition:[6 * Math.cos(_lightAngle * Math.PI / 60), 4 * Math.cos(_lightAngle * Math.PI / 120), 3]];
  77. // Render the scene
  78. [[RendererManager getInstance] render];
  79. _sphereAngle = (_sphereAngle + 0.5) % 360;
  80. _lightAngle = _lightAngle + 1 % 360;
  81. // flush
  82. [_glContext flush];
  83. }
  84. @end