user1834305
user1834305

Reputation:

Rendering triangle in opengles

I have found it really difficult to start with opengles. I looked for tutorials all around the web. Found a free book by Philips Rideout, tried out few chapters but due to lack of a good C++ skills, I left it in middle. Then tried out Ray Wenderlich's tutorial and got stucked with shaders and could not make upto a very simple tutorial. Now, I am lingering around with Jeff Lamarche's old blog. I know there is a quite nice object oriented framework called COCOS2D out there that does almost everything needed for a 2D games and graphics but I thought of making a good foundation before actually trying out COCOS2D. But, it seems like I will never reach there. I have trouble one after another and there's no way that I could find solution. So, I come to stack overflow again and again to clear my misunderstanding. Your help and support will always help me to clear bugs in my code and of course rub out my misunderstandings.

I have a issue with a really simple triangle in OpenGLES. This example uses a OpenGLES 1.0. The code for rendering the graphics in my view goes this way,

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;
}

The figure this code yields is like the figure shown below;

enter image description here

Since, I am using 2D coordinates with (1,0),(-1,0), (1,0), I have an assumption that it should give me a figure like this;

enter image description here

I am sure there is something very small that I am doing incorrectly. If anybody could point me out, it would be a great help to me. Thank you again.

Upvotes: 1

Views: 352

Answers (1)

warrenm
warrenm

Reputation: 31782

Looks like you're just rendering too many vertices, and OpenGL is reading past the end of your vertex array into uninitialized memory. The line

glDrawArrays(GL_TRIANGLES, 0, 9);

should probably be

glDrawArrays(GL_TRIANGLES, 0, 3);

if you just want to draw one triangle. The third parameter is the total number of vertices to render.

Upvotes: 2

Related Questions