PageRenderTime 28ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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