我发现用opengles开始真的很难。我在网上寻找教程。发现飞利浦Rideout的一本免费书,尝试了几个章节,但由于缺乏良好的C ++技能,我把它留在中间。然后尝试了Ray Wenderlich的教程并且使用了着色器并且无法完成一个非常简单的教程。现在,我和Jeff Lamarche的老博客一起挥之不去。我知道有一个非常好的面向对象框架,称为COCOS2D,几乎可以完成2D游戏和图形所需的一切,但我想在实际尝试COCOS2D之前做好基础。但是,似乎我永远不会到达那里。我一个接一个地遇到麻烦,我无法找到解决办法。所以,我一次又一次地堆积溢出来清除我的误解。您的帮助和支持将始终帮助我清除代码中的错误,当然也可以消除我的误解。
我在OpenGLES中遇到一个非常简单的三角形问题。此示例使用OpenGLES 1.0。在我的视图中渲染图形的代码就是这样,
struct Vertex3D{
GLfloat x;
GLfloat y;
GLfloat z;
};
struct Triangle3D{
Vertex3D v1;
Vertex3D v2;
Vertex3D v3;
};
static inline Triangle3D Triangle3DMake(Vertex3D vertex1, Vertex3D vertex2, Vertex3D vertex3){
Triangle3D triangle;
triangle.v1 = vertex1;
triangle.v2 = vertex2;
triangle.v3 = vertex3;
return triangle;
};
static inline Vertex3D vertex3DMake(GLfloat x, GLfloat y, GLfloat z){
Vertex3D vertex;
vertex.x = x;
vertex.y = y;
vertex.z = z;
return vertex;
}
static inline GLfloat Vertex3DCalculateDistanceBetweemVertices(Vertex3D first, Vertex3D second){
GLfloat deltaX = second.x - first.x;
GLfloat deltaY = second.y - first.y;
GLfloat deltaZ = second.z - first.z;
return sqrtf(powf(deltaX, 2) + powf(deltaY, 2) + powf(deltaZ, 2));
}
@implementation GLView{
GLuint renderbuffer;
GLuint framebuffer;
EAGLContext *_context;
CAEAGLLayer *layer;
GLuint depthbuffer;
}
+(Class)layerClass{
return [CAEAGLLayer class];
}
-(void)setUpLayer{
layer = (CAEAGLLayer*)super.layer;
}
-(void)setUpContext{
EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES1;
_context = [[EAGLContext alloc] initWithAPI:api];
if(!_context){
NSLog(@"Could not create context");
abort();
}
if(![EAGLContext setCurrentContext:_context]){
NSLog(@"Could not set current context");
abort();
}
}
-(void)setUpRenderBuffer{
glGenRenderbuffersOES(1, &renderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, renderbuffer);
glGenRenderbuffers(1, &depthbuffer);
glBindRenderbufferOES(GL_DEPTH_COMPONENT16_OES, depthbuffer);
[_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer];
[_context renderbufferStorage:GL_DEPTH_COMPONENT16_OES fromDrawable:layer];
}
-(void)setUpFrameBuffer{
glGenFramebuffersOES(1, &framebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, renderbuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_DEPTH_COMPONENT16_OES, depthbuffer);
}
-(void)render{
Vertex3D vertex1 = vertex3DMake(0,1,0);
Vertex3D vertex2 = vertex3DMake(1.0, 0.0, 0);
Vertex3D vertex3 = vertex3DMake(-1.0, 0.0, 0.);
Triangle3D triangle = Triangle3DMake(vertex1, vertex2, vertex3);
glViewport(0, 0, self.bounds.size.width, self.bounds.size.height);
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertexPointer(3, GL_FLOAT, 0, &triangle);
glDrawArrays(GL_TRIANGLES, 0, 9);
[_context presentRenderbuffer:GL_RENDERBUFFER_OES];
glDisableClientState(GL_VERTEX_ARRAY);
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUpLayer];
[self setUpContext];
[self setUpRenderBuffer];
[self setUpFrameBuffer];
[self render];
}
return self;
}
此代码产生的数字如下图所示;
因为我正在使用带有(1,0),( - 1,0),(1,0)的2D坐标,所以我假设它应该给我这样的图形;
我确信我做的事情非常小。如果有人能指出我,那对我来说将是一个很大的帮助。再次感谢你。
答案 0 :(得分:2)
看起来你只是渲染了太多的顶点,而OpenGL正在读取你的顶点数组的末尾到未初始化的内存。这条线
glDrawArrays(GL_TRIANGLES, 0, 9);
应该是
glDrawArrays(GL_TRIANGLES, 0, 3);
如果您只想绘制一个三角形。第三个参数是要渲染的顶点总数。