PageRenderTime 796ms CodeModel.GetById 602ms app.highlight 2ms RepoModel.GetById 190ms app.codeStats 0ms

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