我创建了一个允许我显示的程序,3D objects
现在我想制作一个cut
与另一个对象相关的内容。这是我的结果:屏幕。
屏幕显示我们可以通过切割部分看到。所以我决定用 ashader
来填充这个切割部分。
我尝试加载 ashader
然后在 a 中使用它,glUniform3f
但这不起作用。我在互联网上进行了几次搜索,没有结果。
这是我要加载的课程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 to 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 to 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 to 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);
}
shaders
这是在主类中初始化 my 的函数:
- (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);
}
这是我要使用的功能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];
}
这是我的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);
}
难道我做错了什么 ?有人有可以帮助我的想法吗?
谢谢。