Skip to content

Commit

Permalink
Integration of point lighting. Shader allows for up to 8 point lights…
Browse files Browse the repository at this point in the history
… with different properties, plus ShadedColorMaterial allows for material properties. Normalisation of shader attribute and uniform names. demos directory added to show different features: modify the GLView instantiated inside AppController.
  • Loading branch information
stuartcaunt committed Nov 7, 2009
1 parent 1099142 commit c74710c
Show file tree
Hide file tree
Showing 18 changed files with 655 additions and 63 deletions.
19 changes: 11 additions & 8 deletions AppController.j
Expand Up @@ -2,10 +2,11 @@
@import <Foundation/CPRunLoop.j>
@import "utils/Framerate.j"
@import "SphereView.j"
@import "demos/pointLight/LightingView.j"

@implementation AppController : CPObject {
CPLabel _label;
SphereView _sphereView;
GLView _view;
Framerate _framerate;
int _speed;
}
Expand All @@ -14,12 +15,13 @@

var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView];

[contentView setBackgroundColor:[CPColor colorWithHexString:@"EEEEEE"]];

// Create GL View
_sphereView = [[SphereView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[_sphereView setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
[_sphereView setCenter:[contentView center]];
[contentView addSubview:_sphereView];
_view = [[LightingView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[_view setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
[_view setCenter:[contentView center]];
[contentView addSubview:_view];

// FPS label
_label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
Expand All @@ -41,13 +43,14 @@
[CPTimer scheduledTimerWithTimeInterval:1/_speed target:self selector:@selector(run) userInfo:nil repeats:NO];

[theWindow orderFront:self];
app = self;

app = self;
}

- (void)run {
// update framerate
[_framerate tick];
[_sphereView setNeedsDisplay:YES];
[_view setNeedsDisplay:YES];
[CPTimer scheduledTimerWithTimeInterval:1/_speed target:self selector:@selector(run) userInfo:nil repeats:NO];
}

Expand Down
22 changes: 21 additions & 1 deletion OJGL/GLContext.j
Expand Up @@ -134,10 +134,30 @@
_gl.bindTexture(_gl.TEXTURE_2D, textureIndex);
}

- (void)setUniformMatrix:(int)uniformIndex matrix:(Matrix4D)matrix {
- (void)setUniformMatrix3:(int)uniformIndex matrix:(Matrix4D)matrix {
_gl.uniformMatrix3fv(uniformIndex, false, matrix.getAs3x3ColumnMajorCanvasFloatArray());
}

- (void)setUniformMatrix4:(int)uniformIndex matrix:(Matrix4D)matrix {
_gl.uniformMatrix4fv(uniformIndex, false, matrix.getAsColumnMajorCanvasFloatArray());
}

- (void)setUniform1i:(int)uniformIndex value:(int)value {
_gl.uniform1i(uniformIndex, value);
}

- (void)setUniform1f:(int)uniformIndex value:(float)value {
_gl.uniform1f(uniformIndex, value);
}

- (void)setUniform3f:(int)uniformIndex values:(Array)values {
_gl.uniform3f(uniformIndex, values[0], values[1], values[2]);
}

- (void)setUniform4f:(int)uniformIndex values:(Array)values {
_gl.uniform4f(uniformIndex, values[0], values[1], values[2], values[3]);
}

- (void)setUniformSampler:(int)samplerIndex {
_gl.uniform1i(samplerIndex, 0);
}
Expand Down
57 changes: 57 additions & 0 deletions OJGL/GLLight.j
@@ -0,0 +1,57 @@
@import <Foundation/CPObject.j>

@implementation GLLight : CPObject {
Array _position;

Array _ambientLight;
Array _diffuseLight;
Array _specularLight;

float _attenuation;
}

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

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

_ambientLight = hexToRGB(color);
_diffuseLight = [_ambientLight[0], _ambientLight[1], _ambientLight[2], 1.0];
_specularLight = hexToRGB(specularColor);
}

return self;
}

- (void)setPosition:(Array)position {
_position = position;
}

- (void)setAttenuation:(float)attenuation {
_attenuation = attenuation;
}

- (Array)position {
return [_position[0], _position[1], _position[2], 1];
}

- (Array)ambientLight {
return _ambientLight;
}

- (Array)diffuseLight {
return _diffuseLight;
}

- (Array)specularLight {
return _specularLight;
}

- (float)attenuation {
return _attenuation;
}


@end
18 changes: 14 additions & 4 deletions OJGL/GLPrimitive.j
Expand Up @@ -10,6 +10,7 @@
Array _indices;

int _vertexBufferId;
int _normalBufferId;
int _uvBufferId;
int _indicesBufferId;

Expand Down Expand Up @@ -53,6 +54,11 @@
_uvBufferId = [glContext createBufferFromArray:_uvs];
}

- (void)prepareNormals:(GLContext)glContext {
// Create and initialise normal buffer data
_normalBufferId = [glContext createBufferFromArray:_normals];
}

- (void)render:(GLRenderer)renderer {

// Prepare the material to be rendered
Expand All @@ -71,10 +77,6 @@
[renderer drawElements:_indices.length];
}

- (void)getUVBufferId {
return _uvBufferId;
}

/**
* Rotates by given angle about given vector
*/
Expand Down Expand Up @@ -124,6 +126,14 @@
return _vertices.length;
}

- (int)getUVBufferId {
return _uvBufferId;
}

- (int)getNormalBufferId {
return _normalBufferId;
}



@end
2 changes: 1 addition & 1 deletion OJGL/GLProgram.j
Expand Up @@ -123,7 +123,7 @@ GL_FRAGMENT_SHADER = 1;
if (!compiled) {
// Something went wrong during compilation; get the error
var error = _gl.getShaderInfoLog(shader);
CPLog.error(@"Error compiling shader '" + shaderId + "':" + error);
CPLog.error(@"Error compiling " + ((shaderType == _gl.VERTEX_SHADER) ? "vertex" : "fragment") + " shader:" + error);
_gl.deleteShader(shader);
return nil;
}
Expand Down
57 changes: 41 additions & 16 deletions OJGL/GLRenderer.j
Expand Up @@ -11,11 +11,15 @@
GLShadersLoader _glShadersLoader;
CPString _vertexShaderFile;
CPString _fragmentShaderFile;
int _perspectiveUniformLocation;
int _mvMatrixUniformLocation;
int _mvpMatrixUniformLocation;
int _vertexAttributeLocation;
Matrix4D _viewMatrix;
Matrix4D _modelMatrix;

Matrix4D _viewMatrix;
Matrix4D _modelMatrix;
Matrix4D _projectionMatrix;
Matrix4D _mvMatrix;
Matrix4D _mvpMatrix;
}

- (id)initWithContext:(GLContext)context vertexShaderFile:(CPString)vertexShaderFile fragmentShaderFile:(CPString)fragmentShaderFile {
Expand All @@ -26,8 +30,12 @@
_glProgram = [_glContext createProgram];
_vertexShaderFile = vertexShaderFile;
_fragmentShaderFile = fragmentShaderFile;

_modelMatrix = new Matrix4D();
_viewMatrix = new Matrix4D();
_projectionMatrix = new Matrix4D();
_mvMatrix = new Matrix4D();
_mvpMatrix = new Matrix4D();
}

return self;
Expand All @@ -48,9 +56,9 @@
[_glProgram addShaderText:[_glShadersLoader fragmentShader] shaderType:GL_FRAGMENT_SHADER];
[_glProgram linkProgram];

_perspectiveUniformLocation = [_glProgram getUniformLocation:"pMatrix"];
_mvMatrixUniformLocation = [_glProgram getUniformLocation:"mvMatrix"];
_vertexAttributeLocation = [_glProgram getAttributeLocation:"aVertex"];
_mvMatrixUniformLocation = [_glProgram getUniformLocation:"u_mvMatrix"];
_mvpMatrixUniformLocation = [_glProgram getUniformLocation:"u_mvpMatrix"];
_vertexAttributeLocation = [_glProgram getAttributeLocation:"a_vertex"];
}

- (void)callback {
Expand All @@ -68,25 +76,41 @@
[_glContext drawElements:numberOfElements];
}


- (void)setProjectionMatrix:(Matrix4D)projectionMatrix {
// Set the projection matrix
[_glContext setUniformMatrix:_perspectiveUniformLocation matrix:projectionMatrix];
_projectionMatrix = projectionMatrix;

[self setupMatrices];
}

- (void)setViewMatrix:(Matrix4D)viewMatrix {
_viewMatrix = viewMatrix;
var mvMatrix = new Matrix4D(_viewMatrix);
mvMatrix.multiply(_modelMatrix);
[_glContext setUniformMatrix:_mvMatrixUniformLocation matrix:mvMatrix];
_viewMatrix = viewMatrix;

[self setupMatrices];
}

- (void)setModelMatrix:(Matrix4D)modelMatrix {
_modelMatrix = modelMatrix;
var mvMatrix = new Matrix4D(_viewMatrix);
mvMatrix.multiply(_modelMatrix);
[_glContext setUniformMatrix:_mvMatrixUniformLocation matrix:mvMatrix];
_modelMatrix = modelMatrix;

[self setupMatrices];
}

- (void)setupMatrices {

// calculate model-view matrix
_mvMatrix = new Matrix4D(_viewMatrix);
_mvMatrix.multiply(_modelMatrix);

// calculate model-view-projection
_mvpMatrix = new Matrix4D(_projectionMatrix);
_mvpMatrix.multiply(_mvMatrix);

// Set the matrices
[_glContext setUniformMatrix4:_mvpMatrixUniformLocation matrix:_mvpMatrix];
[_glContext setUniformMatrix4:_mvMatrixUniformLocation matrix:_mvMatrix];
}


- (void)setVertexBufferData:(int)bufferId {
// Bind the vertex buffer data to the vertex attribute
[_glContext bindBufferToAttribute:bufferId attributeLocation:_vertexAttributeLocation size:3];
Expand All @@ -96,4 +120,5 @@
// Bind element index buffer
[_glContext bindElementBuffer:bufferId];
}

@end
4 changes: 2 additions & 2 deletions Resources/fragmentShader.glsl
@@ -1,7 +1,7 @@
varying vec4 vColor;
varying vec4 v_color;

void main(void) {
gl_FragColor = vColor;
gl_FragColor = v_color;
}


6 changes: 3 additions & 3 deletions Resources/fragmentTextureShader.glsl
@@ -1,8 +1,8 @@
varying vec2 vTexCoord;
uniform sampler2D sTexture;
varying vec2 v_texCoord;
uniform sampler2D s_texture;

void main(void) {
gl_FragColor = texture2D(sTexture, vTexCoord);
gl_FragColor = texture2D(s_texture, v_texCoord);
}


7 changes: 7 additions & 0 deletions Resources/pointLighting.fsh
@@ -0,0 +1,7 @@
varying vec4 v_color;

void main() {
gl_FragColor = v_color;
}


0 comments on commit c74710c

Please sign in to comment.