我想知道是否有人能够告诉我如何将以下Javascript着色器加载器转换为Open GL ES。 javascript版本使用Web GL。
以下是Javascript的代码片段:
-(void)Shader (gl, vsPath, fsPath)
{
// Load shaders.
m_vs = loadShader(gl, vsPath);
m_fs = loadShader(gl, fsPath);
// Create and link the program.
m_prog = gl.createProgram();
gl.attachShader(m_prog, m_vs);
gl.attachShader(m_prog, m_fs);
gl.linkProgram(m_prog);
if(!gl.getProgramParameter(m_prog, gl.LINK_STATUS))
alert("Unable to create shader program:\nVS = " + vsPath + "\nFS = " + fsPath);
else
console.log("Created shader program: VS = %s FS = %s", vsPath, fsPath);
// Grab uniforms.
m_nUniforms = gl.getProgramParameter(m_prog, gl.ACTIVE_UNIFORMS);
for(var i = 0; i < m_nUniforms; ++i)
{
var info = gl.getActiveUniform(m_prog, i);
m_uniforms[info.name] = gl.getUniformLocation(m_prog, info.name);
}
// Grab attributes.
m_nAttributes = gl.getProgramParameter(m_prog, gl.ACTIVE_ATTRIBUTES);
for(var i = 0; i < m_nAttributes; ++i)
{
var info = gl.getActiveAttrib(m_prog, i);
m_attributes[info.name] = gl.getAttribLocation(m_prog, info.name);
}
到目前为止,这是我的尝试,但我有一些错误:
- (BOOL)loadShaders
{
GLuint vertShader, fragShader;
NSString *vertShaderPathname, *fragShaderPathname;
// Create shader program.
_program = glCreateProgram();
// Create and compile vertex shader.
vertShaderPathname = [[NSBundle mainBundle] pathForResource:@"rigid" ofType:@"vert"];
if (![self compileShader:&vertShader type:GL_VERTEX_SHADER file:vertShaderPathname]) {
NSLog(@"Failed to compile vertex shader");
return NO;
}
// Create and compile fragment shader.
fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"rigid" ofType:@"frag"];
if (![self compileShader:&fragShader type:GL_FRAGMENT_SHADER file:fragShaderPathname]) {
NSLog(@"Failed to compile fragment shader");
return NO;
}
// Attach vertex shader to program.
glAttachShader(_program, vertShader);
// Attach fragment shader to program.
glAttachShader(_program, fragShader);
// Bind attribute locations.
m_nAttributes = glGetProgrmix(_program, GL_ACTIVE_ATTRIBUTES);
for(int i = 0; i < m_nAttributes; i++)
{
var info = glGetActiveAttrib(_program, i);
m_attributes[info.name] = glGetActiveAttrib(_program, info.name);
}
// Link program.
if (![self linkProgram:_program]) {
NSLog(@"Failed to link program: %d", _program);
if (vertShader) {
glDeleteShader(vertShader);
vertShader = 0;
}
if (fragShader) {
glDeleteShader(fragShader);
fragShader = 0;
}
if (_program) {
glDeleteProgram(_program);
_program = 0;
}
return NO;
}else{
NSLog(@"SHADER LOADED");
}