objective c - Shader doesn't work - OpenGL ES -
i have created program allows me display 3d objects
, want make cut
in relation object. here result : screen.
the screen shows can see through cut portion. decided use shader
fill cut part.
i tried load shader
, use in gluniform3f
doesn't work. did several searches on internet, without results.
here class load shader
:
- (id)initwithvertexshaderfilename:(nsstring *)vshaderfilename fragmentshaderfilename:(nsstring *)fshaderfilename { nslog(fshaderfilename); if (self = [super init]) { attributes = [[nsmutablearray alloc] init]; nsstring *vertshaderpathname, *fragshaderpathname; program = glcreateprogram(); vertshaderpathname =[[nsbundle mainbundle] pathforresource:vshaderfilename oftype:@"vsh" indirectory:@"shader"]; if (![self compileshader:&vertshader type:gl_vertex_shader file:vertshaderpathname]) nslog(@"failed compile vertex shader"); else nslog(@"vertex shader ok"); fragshaderpathname = [[nsbundle mainbundle] pathforresource:fshaderfilename oftype:@"fsh" indirectory:@"shader"]; if (![self compileshader:&fragshader type:gl_fragment_shader file:fragshaderpathname]) nslog(@"failed compile fragment shader"); else nslog(@"fragment shader ok"); glattachshader(program, vertshader); glattachshader(program, fragshader); } return self; } - (bool)compileshader:(gluint *)shader type:(glenum)type file:(nsstring *)file { nslog(@"bonjour"); glint status; const glchar *source; source = (glchar *)[[nsstring stringwithcontentsoffile:file encoding:nsutf8stringencoding error:nil] utf8string]; if (!source) { nslog(@"failed load vertex shader"); return no; } *shader = glcreateshader(type); glshadersource(*shader, 1, &source, null); glcompileshader(*shader); glgetshaderiv(*shader, gl_compile_status, &status); nsstring *intstring = [nsstring stringwithformat:@"%d", status]; return status = gl_true; } #pragma mark - - (void)addattribute:(nsstring *)attributename { if (![attributes containsobject:attributename]) { [attributes addobject:attributename]; glbindattriblocation(program, [attributes indexofobject:attributename], [attributename utf8string]); } } - (gluint)attributeindex:(nsstring *)attributename { return [attributes indexofobject:attributename]; } - (gluint)uniformindex:(nsstring *)uniformname { return glgetuniformlocation(program, [uniformname utf8string]); } #pragma mark - - (bool)link { glint status; gllinkprogram(program); glvalidateprogram(program); glgetprogramiv(program, gl_link_status, &status); if (status == gl_false) return no; if (vertshader) gldeleteshader(vertshader); if (fragshader) gldeleteshader(fragshader); return yes; } - (void)use { gluseprogram(program); }
here function initialize shaders
in main class:
- (void)setup { glshader *theprogram = [[glshader alloc] initwithvertexshaderfilename:@"shader" fragmentshaderfilename:@"shader"]; self.program = theprogram; [self.program addattribute:@"position"]; [self.program addattribute:@"texturecoordinates"]; if (![self.program link]) { nslog(@"link failed"); nsstring *proglog = [self.program programlog]; nslog(@"program log: %@", proglog); nsstring *fraglog = [self.program fragmentshaderlog]; nslog(@"frag log: %@", fraglog); nsstring *vertlog = [self.program vertexshaderlog]; nslog(@"vert log: %@", vertlog); //[(glview *)self.view stopanimation]; self.program = nil; } texturecoordinateattribute = [program attributeindex:@"texturecoordinates"]; coloruniform = [program uniformindex:@"ucolor"]; textureuniform = [program uniformindex:@"texture"]; //coloruniform = glgetuniformlocation(self.program, "ucolor"); glenable(gl_depth_test); glenable(gl_cull_face); glenable(gl_texture_2d); glenable(gl_blend); glblendfunc(gl_one, gl_zero); }
here function want use shader
:
-(void) draw { [self.program use]; glclear(gl_color_buffer_bit | gl_depth_buffer_bit | gl_stencil_buffer_bit); gldepthmask(true); luniform3f(coloruniform,1.0, 1.0, 0.5); [self drawsubtraction:image1 with:image2]; }
here fragment shader
:
precision highp float; uniform vec3 ucolor; uniform vec3 ulight; varying vec3 vnormal; const float alpha = 0.7; void main(void) { float val = dot( vnormal, ulight ) * 0.4 + 0.6; gl_fragcolor = vec4( ucolor * val, alpha); }
am doing wrong ? has idea me ?
thanks.
your shader code looks ok. make sure passing in "texturecoordinates" attribute correctly vertex shader. need call glenablevertexattribarray()
, glvertexattribpointer()
@ point of initialization. depending on how rendering models might need ensure passing in vertices correctly well.
also i'm assuming [self drawsubtraction:image1 with:image2]
calls gldrawarrays()
or gldrawelements()
@ point.
the xcode default opengl game project great place start loading shaders if want project compare against.
Comments
Post a Comment