简体   繁体   中英

Drawing circle in OpenGL ES 2.0

I got a problem with OpenGL ES 2.0 for iPhone. I'm trying to draw a simple circle textured with this code:

double qualite = 40.0f;
double rayon = 0.5;
double TWOPI=2*M_PI;
double step = TWOPI/qualite;

// CENTRE DU CERCLE
// X Y Z NMR1
gCircleVertexData[0]=0.0f;
gCircleVertexData[1]=0.0f;
gCircleVertexData[2]=0.0f;

// NORMALES X Y Z
gCircleVertexData[3]=0.0f;
gCircleVertexData[4]=0.0f;
gCircleVertexData[5]=1.0f;

// TEXTURES
gCircleVertexData[6]=0.5f;
gCircleVertexData[7]=0.5f;
int j=8;
int k=0;
for(GLfloat i = 0.0f; i <= TWOPI; i+=step)
{
    // X Y Z NMR2
    gCircleVertexData[j]=rayon*sinf(i);
    gCircleVertexData[j+1]=rayon*cosf(i);
    gCircleVertexData[j+2]=0.0f;

    // NORMALES X Y Z
    gCircleVertexData[j+3]=0.0f;
    gCircleVertexData[j+4]=0.0f;
    gCircleVertexData[j+5]=1.0f;

    // TEXTURES
    gCircleVertexData[j+6]=0.5+0.5*cosf(i);
    gCircleVertexData[j+7]=0.5+0.5*sinf(i);
    j=j+8;
    k++;
}

With a quality of 30.0 all works fine but if I go upper a part of the circle doesn't appear. This is how I bind the object:

 glGenVertexArraysOES(1, &_vertexArrayCercle);
glBindVertexArrayOES(_vertexArrayCercle);

glGenBuffers(1, &_vertexBuffer4);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer4);
glBufferData(GL_ARRAY_BUFFER, sizeof( gCircleVertexData),  gCircleVertexData, GL_STATIC_DRAW);

// LES SOMMETS
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribPosition);

// LES PERPENDICULAIRES POUR LA LUMIERE
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(12));
glEnableVertexAttribArray(GLKVertexAttribNormal);

// LA COULEUR DE BASE
// glEnableVertexAttribArray(GLKVertexAttribColor);

// LA TEXTURE
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(24));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);

glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArrayOES(0);

And this is how i'll draw it:

  //----------------------------------- Le DISQUE MOBILE -------------------------------//
// Activation de la texture
glEnable(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _disqueMobileTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// Activation du shader
glUseProgram(_program2);

// Verrouillage du VAO pour l'utiliser
glBindVertexArrayOES(_vertexArrayCercle);

//ON CALCUL LES MATRICES
baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, 0, 1.0f, 1.0f, 1.0f);
modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -0.001f);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix,_rotation/8, 0, 0, 1.0f);
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);

_normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix), NULL);
_modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);
_rotation += self.timeSinceLastUpdate * 0.5f;

// ON ENVOI LES MATRICE DANS LE SHADER
glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);
glUniform1i(uniforms[UNIFORM_TEXTURE], 0);

// DESSIN DU CUBE AVEC LE SHADER
glDrawArrays(GL_TRIANGLE_FAN, 0, 36);

// Désactivation du VAO
glBindVertexArrayOES(0);

// Désactivation du shader
glUseProgram(0);
glDisable(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D);

My circles are really ugly with a 30 quality! I also use a simple shader.

I've found ! It's a really carelessly mistake... I've just forgot to increment the value inside glDrawArrays to match with the number of object I've want to draw...

Mistake are often between the keyboard and the chair!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM