我正在使用 GLKit 选择 opengl es 2.0。我可以成功地绘制正方形、三角形、立方体等形状。
我在绘制点时遇到问题,这是我的代码在设备(iPod 4)上不起作用,尽管我可以在视网膜模拟器中看到点/点。
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
//White background color
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
[self.effect prepareToDraw];
//USING _program
glUseProgram(_program);
GLfloat vert[] = {0,0, 0.1,0.1, 0.2,0.2, 0.3,0.3, 0.4,0.4, 0.5,0.5};
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, vert);
glDrawArrays(GL_POINTS, 0, 5);
glDisableVertexAttribArray(GLKVertexAttribPosition);
}
这是我的更新方法,仅供参考:
- (void)update {
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 4.0f, 10.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
//Since visible range is 4 to 10 so have to mave back in z
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -6.0f);
self.effect.transform.modelviewMatrix = modelViewMatrix;
}
以下是我如何定义效果:
self.effect = [[GLKBaseEffect alloc] init];
self.effect.useConstantColor = GL_TRUE;
self.effect.constantColor = GLKVector4Make(0.0, 0.0, 1.0, 1.0); //Blue color
这样这些点在白色背景上可见。
有人可以指出这段代码中的任何缺陷吗?我不确定为什么它没有显示在设备上,因为三角形、正方形和立方体都可见。任何帮助表示赞赏。
编辑:
我以前没有使用过着色器,所以这是我第一次。正如本提到的示例代码。这就是我现在正在做的事情:
我有VertexShader.glsl和FragmentShader.glsl文件:
顶点着色器.glsl
void main()
{
gl_PointSize = 10.0;
}
片段着色器.glsl
void main()
{
}
我在创建效果之前和设置 EAGLContext 之后设置着色器:
// Setup shader
GLuint vertexShader = [self createShaderWithFile:@"VertexShader.glsl" type:GL_VERTEX_SHADER];
GLuint fragmentShader = [self createShaderWithFile:@"FragmentShader.glsl" type:GL_FRAGMENT_SHADER];
_program = glCreateProgram();
glAttachShader(_program, vertexShader);
glAttachShader(_program, fragmentShader);
glLinkProgram(_program);
GLint linked = 0;
glGetProgramiv(_program, GL_LINK_STATUS, &linked);
if (linked == 0) {
glDeleteProgram(_program);
return;
}
这里是 createShaderWithFile 方法:
- (GLuint)createShaderWithFile:(NSString *)filename type:(GLenum)type
{
GLuint shader = glCreateShader(type);
if (shader == 0) {
return 0;
}
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename];
NSString *shaderString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
const GLchar *shaderSource = [shaderString cStringUsingEncoding:NSUTF8StringEncoding];
glShaderSource(shader, 1, &shaderSource, NULL);
glCompileShader(shader);
GLint success = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (success == 0) {
char errorMsg[2048];
glGetShaderInfoLog(shader, sizeof(errorMsg), NULL, errorMsg);
NSString *errorString = [NSString stringWithCString:errorMsg encoding:NSUTF8StringEncoding];
NSLog(@"Failed to compile %@: %@", filename, errorString);
glDeleteShader(shader);
return 0;
}
return shader;
}
我还更新了上面的 drawRect 方法。
这是该项目的链接:http: //dl.dropbox.com/u/36638938/Template.zip