PageRenderTime 473ms CodeModel.GetById 259ms app.highlight 2ms RepoModel.GetById 210ms app.codeStats 0ms

/OJGL/GLProgram.j

http://github.com/tartiflop/ojgl
Unknown | 139 lines | 97 code | 42 blank | 0 comment | 0 complexity | 00383313b0adc395e6a1fd9e2f4a3179 MD5 | raw file
  1@import <Foundation/CPObject.j>
  2
  3GL_VERTEX_SHADER = 0;
  4GL_FRAGMENT_SHADER = 1;
  5
  6@implementation GLProgram : CPObject {
  7
  8	DOMElement _gl;
  9
 10	int _program;
 11	CPArray _shaders;
 12}
 13
 14- (id)initWithGL:(DOMElement)gl {
 15	self = [super init];
 16	
 17	if (self) {
 18		_gl = gl;
 19
 20
 21		// Create the program object
 22		_program = _gl.createProgram();
 23		if (!_program) {
 24			CPLog.error(@"Could not create program");
 25			return nil;
 26		}
 27		
 28		// create shader array
 29		_shaders = [[CPArray alloc] init]
 30	}
 31	
 32	return self;
 33}
 34
 35- (int)glProgram {
 36	return _program;
 37}
 38
 39- (void)addShaderText:(string)shaderText shaderType:(int)shaderType {
 40
 41
 42	var glShaderType;
 43	if (shaderType == GL_VERTEX_SHADER) {
 44		glShaderType = _gl.VERTEX_SHADER;
 45	
 46	} else if (shaderType == GL_FRAGMENT_SHADER) {
 47		glShaderType = _gl.FRAGMENT_SHADER;
 48	}  
 49	
 50	var shader = [self _loadShader:shaderText shaderType:glShaderType];
 51	if (shader) {
 52		_gl.attachShader(_program, shader);
 53	}
 54}
 55
 56- (void)addShaderScript:(string)shaderId {
 57	
 58  var shaderScript = document.getElementById(shaderId);
 59	if (!shaderScript) {
 60		CPLog.error(@"Error: shader script '" + shaderId + "' not found");
 61		return;
 62	}
 63		
 64	if (shaderScript.type == "x-shader/x-vertex") {
 65		var glShaderType = _gl.VERTEX_SHADER;
 66	
 67	} else if (shaderScript.type == "x-shader/x-fragment") {
 68		var glShaderType = _gl.FRAGMENT_SHADER;
 69	
 70	} else {
 71		CPLog.error(@"Error: shader script '" + shaderId + "' of undefined type '" + shaderScript.type+"'");	   
 72		return nil;
 73	}
 74
 75	var shader = [self _loadShader:shaderScript.text shaderType:glShaderType];
 76	if (shader) {
 77		_gl.attachShader(_program, shader);
 78	}
 79}
 80
 81
 82- (void)linkProgram {
 83	
 84	_gl.linkProgram(_program);
 85
 86	if (!_gl.getProgrami(_program, _gl.LINK_STATUS)) {
 87		CPLog.error(@"Error in program linking: " + _gl.getProgramInfoLog(_program));
 88		
 89		_gl.deleteProgram(_program);
 90
 91		for (var shader in _shaders) {
 92			_gl.deleteProgram(shader);
 93		}
 94	}
 95	
 96}
 97
 98- (int)getAttributeLocation:(string)attributeName {
 99	return _gl.getAttribLocation(_program, attributeName);
100}
101
102- (int)getUniformLocation:(string)uniformName {
103	return _gl.getUniformLocation(_program, uniformName);
104}
105
106- (int)_loadShader:(string)shaderText shaderType:(int)shaderType {
107	
108	// Create the shader object
109	var shader = _gl.createShader(shaderType);
110	if (!shader) {
111		CPLog.error(@"Error: unable to create shader '" + shaderType + "'");	   
112		return nil;
113	}
114
115	// Load the shader source
116	_gl.shaderSource(shader, shaderText);
117
118	// Compile the shader
119	_gl.compileShader(shader);
120
121	// Check the compile status
122	var compiled = _gl.getShaderi(shader, _gl.COMPILE_STATUS);
123	if (!compiled) {
124		// Something went wrong during compilation; get the error
125		var error = _gl.getShaderInfoLog(shader);
126		CPLog.error(@"Error compiling " + ((shaderType == _gl.VERTEX_SHADER) ? "vertex" : "fragment") + " shader:" + error);
127		_gl.deleteShader(shader);
128		return nil;
129	}
130	
131	
132	[_shaders addObject:shader];
133
134	return shader;
135}
136
137
138
139@end