Skip to content

Commit

Permalink
added LightingDemoView: 7 lights, 27 texture mapped spheres
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcaunt committed Nov 12, 2009
1 parent cc12356 commit 3b034a3
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 22 deletions.
7 changes: 2 additions & 5 deletions AppController.j
@@ -1,9 +1,7 @@
@import <Foundation/CPObject.j>
@import <Foundation/CPRunLoop.j>
@import "utils/Framerate.j"
@import "demos/camera/SphereView.j"
@import "demos/pointLight/LightingView.j"
@import "demos/pointLight/TextureLightingView.j"
@import "demos/pointLight/LightingDemoView.j"

@implementation AppController : CPObject {
CPLabel _label;
Expand All @@ -20,7 +18,7 @@ app = self;
[contentView setBackgroundColor:[CPColor colorWithHexString:@"EEEEEE"]];

// Create GL View
_view = [[TextureLightingView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
_view = [[LightingDemoView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[_view setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
[_view setCenter:[contentView center]];
[contentView addSubview:_view];
Expand All @@ -32,7 +30,6 @@ app = self;
[_label setFont:[CPFont boldSystemFontOfSize:16.0]];
[_label setBackgroundColor:[CPColor colorWithHexString:@"FF0000"]];
[_label sizeToFit];
// [_label setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
[contentView addSubview:_label];

// Framerate
Expand Down
4 changes: 2 additions & 2 deletions OJGL/GLLight.j
Expand Up @@ -10,12 +10,12 @@
float _attenuation;
}

- (id)initWithHexColor:(String)color specularColor:(String)specularColor {
- (id)initWithHexColor:(String)color specularColor:(String)specularColor attenuation:(float)attenuation {
self = [super init];

if (self) {
_position = [0, 0, 0];
_attenuation = 0;
_attenuation = attenuation;

_ambientLight = hexToRGB(color);
_diffuseLight = [_ambientLight[0], _ambientLight[1], _ambientLight[2], 1.0];
Expand Down
150 changes: 150 additions & 0 deletions demos/pointLight/LightingDemoView.j
@@ -0,0 +1,150 @@
@import "../../OJGL/GLView.j"
@import "../../OJGL/GLU.j"
@import "../../OJGL/GLLight.j"
@import "../../primitives/Sphere.j"
@import "../../materials/TextureMaterial.j"
@import "../../renderers/RendererManager.j"

@implementation LightingDemoView : GLView {
GLContext _glContext;

GLLight _blueLight;
GLLight _redLight;
GLLight _greenLight;
GLLight _yellowLight;
GLLight _cyanLight;
GLLight _magentaLight;

float _lightAngle;
float _sphereAngle;

Array _sceneObjects;

BOOL _ready;
}

- (id)initWithFrame:(CGRect)aFrame {
self = [super initWithFrame:aFrame];

if (self) {
_ready = NO;

// Get the OpenGL Context
_glContext = [self glContext];

_sceneObjects = [];

// Initialise the renderer manager
[[RendererManager alloc] initWithContext:_glContext];

// build the scene
[self initScene];

_lightAngle = 0;
}

return self;
}

- (void)initScene {

// Prepare (initialise) context
[_glContext prepare:[0.0, 0.0, 0.0, 1] clearDepth:1.0];
[_glContext enableBackfaceCulling];

// Create the spheres with texture materials
for (var k = 0; k < 3; k++) {
for (var j = 0; j < 3; j++) {
for (var i = 0; i < 3; i++) {

var textureMaterial = [[TextureMaterial alloc] initWithTextureFile:"Resources/images/mars.jpg" shininess:0.7 precise:YES];
var primitive = [[Sphere alloc] initWithGeometry:textureMaterial radius:1.3 longs:25 lats:25];
[primitive setTranslation:((i - 1) * 4) y:((j - 1) * 4) z:((k - 1) * 4)];

[primitive prepareGL:_glContext];
[[RendererManager getInstance] addPrimitive:primitive];
_sceneObjects.push(primitive);
}
}
}


// Create the lights
_blueLight = [[GLLight alloc] initWithHexColor:"0000FF" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_blueLight];

_redLight = [[GLLight alloc] initWithHexColor:"FF0000" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_redLight];

_greenLight = [[GLLight alloc] initWithHexColor:"00FF00" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_greenLight];

_yellowLight = [[GLLight alloc] initWithHexColor:"FFFF00" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_yellowLight];

_cyanLight = [[GLLight alloc] initWithHexColor:"00FFFF" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_cyanLight];

_magentaLight = [[GLLight alloc] initWithHexColor:"FF00FF" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_magentaLight];

var whiteLight = [[GLLight alloc] initWithHexColor:"FFFFFF" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:whiteLight];
[whiteLight setPosition:[7, 7, 4]];

// Set the scene ambient color
[[RendererManager getInstance] setSceneAmbient:"333333"];

// Initialise view and projection matrices
var lookat = [GLU lookAt:5 eyey:10 eyez:15 centerx:0 centery:0 centerz:0 upx:0 upy:1 upz:0];
[[RendererManager getInstance] setViewMatrix:lookat];

var perspective = [GLU perspective:60 aspect:[self width]/[self height] near:1 far:10000];
[[RendererManager getInstance] setProjectionMatrix:perspective];

// reshape
[_glContext reshape:[self width] height:[self height]];

_ready = YES;
}

- (void)drawRect:(CPRect)dirtyRect {

// Only render the scene if ready
if (!_ready) {
return;
}

// Clear context
[_glContext clearBuffer];

// Rotate the spheres
for (var i = 0; i < _sceneObjects.length; i++) {
[_sceneObjects[i] setRotation:_sphereAngle x:0 y:1 z:0];
}

// Move the lights
var blueAngle = _lightAngle;
var redAngle = -(blueAngle + 120 % 360);
var greenAngle = redAngle + 12 % 360;
var yellowAngle = _lightAngle;
var cyanAngle = -(yellowAngle + 90 % 360);
var magentaAngle = cyanAngle + 90 % 360;
[_blueLight setPosition:[7 * Math.sin(blueAngle * Math.PI / 90), 7 * Math.cos(blueAngle * Math.PI / 90), 7 * Math.cos(blueAngle * Math.PI / 180)]];
[_redLight setPosition:[7 * Math.sin(redAngle * Math.PI / 145), 7 * Math.cos(redAngle * Math.PI / 145), 7 * Math.cos(redAngle * Math.PI / 180)]];
[_greenLight setPosition:[7 * Math.sin(greenAngle * Math.PI / 60), 7 * Math.cos(greenAngle * Math.PI / 60), 7 * Math.cos(greenAngle * Math.PI / 180)]];
[_yellowLight setPosition:[7 * Math.sin(yellowAngle * Math.PI / 90), 7 * Math.cos(yellowAngle * Math.PI / 180), 7 * Math.cos(yellowAngle * Math.PI / 90)]];
[_cyanLight setPosition:[7 * Math.sin(cyanAngle * Math.PI / 145), 7 * Math.cos(cyanAngle * Math.PI / 180), 7 * Math.cos(cyanAngle * Math.PI / 145)]];
[_magentaLight setPosition:[7 * Math.sin(magentaAngle * Math.PI / 60), 7 * Math.cos(magentaAngle * Math.PI / 180), 7 * Math.cos(magentaAngle * Math.PI / 60)]];

// Render the scene
[[RendererManager getInstance] render];

_sphereAngle = (_sphereAngle + 2) % 360;
_lightAngle = _lightAngle + 2 % 360;

// flush
[_glContext flush];
}

@end
9 changes: 3 additions & 6 deletions demos/pointLight/LightingView.j
Expand Up @@ -49,16 +49,13 @@
[_sphere prepareGL:_glContext];
[[RendererManager getInstance] addPrimitive:_sphere];

_light1 = [[GLLight alloc] initWithHexColor:"0000FF" specularColor:"FFFFFF"];
[_light1 setAttenuation:0.02];
_light1 = [[GLLight alloc] initWithHexColor:"0000FF" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_light1];

_light2 = [[GLLight alloc] initWithHexColor:"FF0000" specularColor:"FFFFFF"];
[_light2 setAttenuation:0.05];
_light2 = [[GLLight alloc] initWithHexColor:"FF0000" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_light2];

_light3 = [[GLLight alloc] initWithHexColor:"00FF00" specularColor:"FFFFFF"];
[_light3 setAttenuation:0.05];
_light3 = [[GLLight alloc] initWithHexColor:"00FF00" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_light3];

// Set the scene ambient color
Expand Down
11 changes: 3 additions & 8 deletions demos/pointLight/TextureLightingView.j
Expand Up @@ -62,16 +62,13 @@


// Create the lights
_light1 = [[GLLight alloc] initWithHexColor:"0000FF" specularColor:"FFFFFF"];
[_light1 setAttenuation:0.02];
_light1 = [[GLLight alloc] initWithHexColor:"0000FF" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_light1];

_light2 = [[GLLight alloc] initWithHexColor:"FF0000" specularColor:"FFFFFF"];
[_light2 setAttenuation:0.02];
_light2 = [[GLLight alloc] initWithHexColor:"FF0000" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_light2];

_light3 = [[GLLight alloc] initWithHexColor:"00FF00" specularColor:"FFFFFF"];
[_light3 setAttenuation:0.02];
_light3 = [[GLLight alloc] initWithHexColor:"00FF00" specularColor:"FFFFFF" attenuation:0.02];
[[RendererManager getInstance] addLight:_light3];

// Set the scene ambient color
Expand Down Expand Up @@ -107,8 +104,6 @@
[_light1 setPosition:[6 * Math.sin(_lightAngle * Math.PI / 60), 4 * Math.cos(_lightAngle * Math.PI / 60), 2]];
[_light2 setPosition:[6 * Math.cos(_lightAngle * Math.PI / 90), 4 * Math.sin(_lightAngle * Math.PI / 45), 3]];
[_light3 setPosition:[6 * Math.cos(_lightAngle * Math.PI / 60), 4 * Math.cos(_lightAngle * Math.PI / 120), 3]];

CPLog.info(5 * Math.cos(_lightAngle * Math.PI / 90));

// Render the scene
[[RendererManager getInstance] render];
Expand Down
2 changes: 1 addition & 1 deletion index.html
Expand Up @@ -45,7 +45,7 @@
<script type = "text/javascript">
document.write("<div id='container'><p id='content'>" +
"<img id='loadgraphic' width='16' height='16' src='Resources/images/spinner.gif' /> " +
"Loading myFirstApp...</p></div>");
"Loading ojgl test...</p></div>");
</script>

<noscript>
Expand Down

0 comments on commit 3b034a3

Please sign in to comment.