PDA

View Full Version : How To Draw Sphere With GLES2.0



lovelyboy
09-22-2010, 01:25 AM
Hi group,

I am recently developing an application that uses the OpenGL ES 2.0 to render a sphere. But I am not familiar with the shading language.

I have done sth like this:

const qreal phi=0.0809016994374947f;

const float u0 = 0.0f;

const float u1 = 0.173205081f;

const float u2 = 0.346410162f;

const float u3 = 0.519615242f;

const float u4 = 0.692820323f;

const float u5 = 0.866025402f;

const float v0 = 0.0f;

const float v1 = 0.111111111f;

const float v2 = 0.222222222f;

const float v3 = 0.333333333f;

const float v4 = 0.444444444f;

const float v5 = 0.555555555f;

const float v6 = 0.666666666f;

const float v7 = 0.777777777f;

const float v8 = 0.888888888f;

const float v9 = 1.0f;

QMatrix4x4 modelview(
R[0], R[4], R[8], 0.0f,
R[1], R[5], R[9], 0.0f,
R[2], R[6], R[10], 0.0f,
P[0], P[1], P[2], 1.0f);
modelview = modelview.transposed(); // ODE matrix needs to be transposed for OpenGL

program2.bind();
program1.setUniformValue(matrixUniform2, modelview);
glBindTexture(GL_TEXTURE_2D, m_uiTexture);
GLfloat afVertices[] = {
0.0f, 0.05f, phi , // A - 0

0.0f, 0.05f, -phi , // B - 1

0.0f, -0.05f, phi , // C - 2

0.0f, -0.05f, -phi , // D - 3

0.05f, phi, 0.0f , // E - 4

0.05f, -phi, 0.0f , // F - 5

-0.05f, phi, 0.0f , // G - 6

-0.05f, -phi, 0.0f , // H - 7

phi, 0.0f, 0.05f , // I - 8

phi, 0.0f, -0.05f , // J - 9

-phi, 0.0f, 0.05f , // K - 10

-phi, 0.0f, -0.05f // L - 11
};
program2.setAttributeArray(vertexAttr2, afVertices, 3);

GLfloat afTexCoord[] = {
u0, v1 , u1, v2 , u1, v0 , // E-A-I

u0, v3 , u1, v2 , u0, v1 , // G-A-E

u0, v3 , u1, v4 , u1, v2 , // G-K-A

u0, v5 , u1, v4 , u0, v3 , // L-K-G

u1, v2 , u2, v3 , u2, v1 , // A-C-I

u1, v4 , u2, v3 , u1, v2 , // K-C-A

u1, v4 , u2, v5 , u2, v3 , // K-H-C

u1, v6 , u2, v5 , u1, v4 , // L-H-K

u2, v3 , u3, v4 , u3, v2 , // C-F-I

u2, v5 , u3, v4 , u2, v3 , // H-F-C

u2, v5 , u3, v6 , u3, v4 , // H-D-F

u2, v7 , u3, v6 , u2, v5 , // L-D-H

u3, v4 , u4, v5 , u4, v3 , // F-J-I

u3, v6 , u4, v5 , u3, v4 , // D-J-F

u3, v6 , u4, v7 , u4, v5 , // D-B-J

u3, v8 , u4, v7 , u3, v6 , // L-B-D

u4, v5 , u5, v6 , u5, v4 , // J-E-I

u4, v7 , u5, v6 , u4, v5 , // B-E-J

u4, v7 , u5, v8 , u5, v6, // B-G-E

u4, v9 , u5, v8 , u4, v7
};


program2.setAttributeArray(texCoordAttr2, afTexCoord, 2);

GLfloat afNormals[] = {

4, 0, 8 , // E-A-I

6, 0, 4 , // G-A-E

6, 10, 0 , // G-K-A

11, 10, 6 , // L-K-G

0, 2, 8 , // A-C-I

10, 2, 0 , // K-C-A

10, 7, 2 , // K-H-C

11, 7, 10 , // L-H-K

2, 5, 8 , // C-F-I

7, 5, 2 , // H-F-C

7, 3, 5 , // H-D-F

11, 3, 7 , // L-D-H

5, 9, 8 , // F-J-I

3, 9, 5 , // D-J-F

3, 1, 9 , // D-B-J

11, 1, 3 , // L-B-D

9, 4, 8 , // J-E-I

1, 4, 9 , // B-E-J

1, 6, 4 , // B-G-E

11, 6, 1
};


program2.setAttributeArray(normalAttr2, afNormals, 3);

program2.setUniformValue(textureUniform2, 0);

program2.enableAttributeArray(vertexAttr2);
program2.enableAttributeArray(normalAttr2);
program2.enableAttributeArray(texCoordAttr2);

glDrawArrays(GL_TRIANGLES, 0, 36);

But what I got is just a sliced trianglte. So I wonder whether the problem goes to adjusting the parameters of each matrix, or there is another way to do so.

Thx

marshats
09-23-2010, 07:42 PM
take a look at How_to_draw_a_sphere (http://learningwebgl.com/cookbook/index.php/How_to_draw_a_sphere) and the two demos at the top of the page.

lovelyboy
09-28-2010, 07:58 AM
take a look at How_to_draw_a_sphere (http://learningwebgl.com/cookbook/index.php/How_to_draw_a_sphere) and the two demos at the top of the page.

Thx, it helps. Now i have another question about the data type. Since I should use GLfloat to acclaim the vertices array, I try to replace all the "var" data type in the example to "GLfloat", but it doesn't work and nothing shows on the screen. If I use "float" to initialize those data, how can I transfer the array to "GLfloat" type? Does it make sense?