PageRenderTime 41ms CodeModel.GetById 20ms app.highlight 1ms RepoModel.GetById 19ms app.codeStats 0ms

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