开发者

help with drawing a wedge with opengl es

开发者 https://www.devze.com 2023-03-29 04:04 出处:网络
I\'m trying to do some basic opengl es programming to get started on the basics. I have a drawing function tries to draw a wedge of a circle. Something is going wrong because its actually just drawin

I'm trying to do some basic opengl es programming to get started on the basics.

I have a drawing function tries to draw a wedge of a circle. Something is going wrong because its actually just drawing a circle.

I'm still just trying to grasp the basics of opengl es here. Heres what I have so far.

- (void)drawView

{
    [EAGLContext setCurrentContext:context];

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, 60, 60);


    int i;
    float angle_start=90;
    float angle_stop=180;
    int segments=360;
    float const angle_step = (angle_stop - angle_start)/segments;

    GLfloat *arc_vertices;
    arc_vertices = malloc(2*sizeof(GLfloat) * (segments+2));

    arc_vertices[0] = arc_vertices[1] = 0.0;

    for(i=0; i<segments+1; i++) {
        arc_vertices[2 + 2*i    ] = cos(angle_start + i*angle_step);
        arc_vertices[2 + 2*i + 1] = sin(angle_start + i*angle_step);
    }
    glVertexPoin开发者_JAVA技巧ter(2, GL_FLOAT, 0, arc_vertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    glDrawArrays(GL_TRIANGLE_FAN, 0, segments+2);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
    free(arc_vertices);





}


sin() and cos() take radians as input:

float angle_start=90;
float angle_stop=180;
int segments=360;
float const angle_step = (angle_stop - angle_start)/segments;

GLfloat* verts = (GLfloat*)malloc(2*sizeof(GLfloat) * (segments+2));
unsigned int pos = 0;
verts[pos++] = 0;
verts[pos++] = 0;  

float radius = 10;
for( unsigned int i = 0; i < segments; ++i )
{
    float rads = (angle_start + i*angle_step) * (3.14159 / 180);

    verts[pos++] = ( cos( rads ) * radius );
    verts[pos++] = ( sin( rads ) * radius );
}

glVertexPointer(2, GL_FLOAT, 0, verts);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLE_FAN, 0, segments+1);
glDisableClientState(GL_VERTEX_ARRAY);


I see something wrong. You access vertices[i] and vertices[i+1], but i always increments by 1.

Try replacing

GLfloat vertices[720];

with

GLfloat vertices[2*720];

and replace

vertices[i]=p1; vertices[i+1]=p2;

by

vertices[2*i]=p1; vertices[2*i+1]=p2;


this works.

Anti aliasing is horrible but it works. [credit1

-(void)drawcircelofSlice2
{
    amt+=20;

    if(amt>360.0)
    {
        amt=0;
    }

    [EAGLContext setCurrentContext:context];

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(20, 20, 50,50);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(30.0f, 30.0f, -1.5f, 1.5f, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);


    float x=0;
    float y=0;
    //float radius=20;
    float lowAngle=0;
    float highAngle=(amt/360) *360;
   // float highAngle=360;

    float numcirclePts=360;

    lowAngle=DEGREES_TO_RADIANS(lowAngle);
    highAngle=DEGREES_TO_RADIANS(highAngle);

    float res=numcirclePts;

    float angle=lowAngle;
    float anglerange=highAngle-lowAngle;
    float angleAdder=anglerange/ res;

    int k=0;

    GLfloat verts[720];

    for (int i = 0; i < numcirclePts; i++){
        verts[k] = x + cos(angle) ;
        verts[k+1] = y - sin(angle) ;
        angle += angleAdder;
        k+=2;
    }





    verts[0] = x;  
    verts[1] = y;  
    k = 2;  
    for (int i = 2; i < numcirclePts; i++){  
        verts[k] = verts[k];  
        verts[k+1] = verts[k+1];          
        k+=2;  
    }  





    glVertexPointer(2, GL_FLOAT, 0, verts);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColor4f(0.0f, 0.0f, 1.0f, 0.0f);
    glDrawArrays(GL_TRIANGLE_FAN, 0, numcirclePts);

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    glDisableClientState(GL_VERTEX_ARRAY);    
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消