Results 1 to 3 of 3

Thread: glVertexPointer, glDrawArrays?

  1. #1
    Junior Member
    Join Date
    Oct 2004
    Posts
    9

    glVertexPointer, glDrawArrays?

    This is source code for drawing a sphere.
    but I can not draw a sphere. It was just drawing to a circular cylinder.
    what's the matter?
    You know I changed 'glTexCoord2f, glVertex3f' into 'glVertexPointer, glDrawArrays'.

    like this....
    ---------------------------------------------
    glBegin(GL_TRIANGLE_FAN);
    for (i=0; i < NumVertices; i++)
    {
    glTexCoord2f(Vertices[i].u, Vertices[i].v);
    glVertex3f(Vertices[i].x, Vertices[i].y, Vertices[i].z);
    }
    glEnd();
    -->
    glVertexPointer(3, GL_FLOAT, 0, Vertices);
    glTexCoordPointer(2, GL_FLOAT, 0, Vertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glBindTexture(GL_TEXTURE_2D, texName[0]);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, NumVertices);

    -------------------------------------------
    #define PI 3.1415926535897f
    #define DTOR (PI / 180.0f)
    typedef unsigned int DWORD;

    typedef struct {
    float x, y, z;
    DWORD color;
    float u, v;
    } VERTEX;

    static VERTEX *Vertices;

    void GenerateSphere(float radius, int theta, int phi, float hTile, float vTile)
    {
    int dtheta = 10;
    int dphi = 10;
    int n = 0;
    int i;
    float vx, vy, vz;
    float mag;

    int NumVertices = (int)((360/dtheta)*(360/dphi)*4);

    Vertices = malloc (NumVertices * sizeof(*Vertices));

    if (Vertices == NULL)
    {
    puts("Memory allocation error.");
    exit(1);
    }

    for (phi=0; phi <= 360 - dphi; phi += (int)dphi) {
    for (theta=0; theta <= 360 - dtheta; theta += (int)dtheta) {
    Vertices[n].x = radius * sin(phi*DTOR) * cos(DTOR*theta);
    Vertices[n].y = radius * sin(phi*DTOR) * sin(DTOR*theta);
    Vertices[n].z = radius * cos(phi*DTOR);

    vx = Vertices[n].x;
    vy = Vertices[n].y;
    vz = Vertices[n].z;
    mag = (float)sqrt((vx * vx) + (vy * vy) + (vz * vz));

    vx /= mag;
    vy /= mag;
    vz /= mag;

    Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
    Vertices[n].v = vTile * (float)(asin(vy) / PI) + 0.5f;
    n++;

    Vertices[n].x = radius * sin((phi+dphi)*DTOR) * cos(theta*DTOR);
    Vertices[n].y = radius * sin((phi+dphi)*DTOR) * sin(theta*DTOR);
    Vertices[n].z = radius * cos((phi+dphi)*DTOR);

    vx = Vertices[n].x;
    vy = Vertices[n].y;
    vz = Vertices[n].z;
    mag = (float)sqrt((vx * vx) + (vy * vy) + (vz * vz));

    vx /= mag;
    vy /= mag;
    vz /= mag;

    Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
    Vertices[n].v = vTile * (float)(asin(vy) / PI) + 0.5f;
    n++;

    Vertices[n].x = radius * sin(DTOR*phi) * cos(DTOR*(theta+dtheta));
    Vertices[n].y = radius * sin(DTOR*phi) * sin(DTOR*(theta+dtheta));
    Vertices[n].z = radius * cos(DTOR*phi);

    vx = Vertices[n].x;
    vy = Vertices[n].y;
    vz = Vertices[n].z;
    mag = (float)sqrt((vx * vx) + (vy * vy) + (vz * vz));

    vx /= mag;
    vy /= mag;
    vz /= mag;

    Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
    Vertices[n].v = vTile * (float)(asin(vy) / PI) + 0.5f;
    n++;

    if (phi >-180 && phi < 180) {
    Vertices[n].x = radius * sin((phi+dphi)*DTOR) * cos(DTOR*(theta+dtheta));
    Vertices[n].y = radius * sin((phi+dphi)*DTOR) * sin(DTOR*(theta+dtheta));
    Vertices[n].z = radius * cos((phi+dphi)*DTOR);

    vx = Vertices[n].x;
    vy = Vertices[n].y;
    vz = Vertices[n].z;
    mag = (float)sqrt((vx * vx) + (vy * vy) + (vz * vz));

    vx /= mag;
    vy /= mag;
    vz /= mag;

    Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
    Vertices[n].v = vTile * (float)(asin(vy) / PI) + 0.5f;
    n++;
    }
    }
    }

    glVertexPointer(3, GL_FLOAT, 0, Vertices);
    glTexCoordPointer(2, GL_FLOAT, 0, Vertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glBindTexture(GL_TEXTURE_2D, texName[0]);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, NumVertices);
    free(Vertices);
    }

    void display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glPushMatrix();

    glColor3f(0.2f, 0.7f, 0.7f);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texName[0]);
    GenerateSphere(0.65f, 50, 50, 1.0f, 1.0f);
    glDisable(GL_TEXTURE_2D);

    glPopMatrix();
    glFlush();
    }

    [ November 01, 2004: Message edited by: James Kay ]

  2. #2
    Member
    Join Date
    Oct 2004
    Location
    Bangalore, India
    Posts
    44

    Re: glVertexPointer, glDrawArrays?

    Maybe you need to enable depth testing.

    glEnable(GL_DEPTH_TEST);

  3. #3
    Junior Member
    Join Date
    Oct 2004
    Posts
    9

    Re: glVertexPointer, glDrawArrays?

    Sorry, I already added it to 'glEnable(GL_DEPTH_TEST)' like this source code.

    ---------------------------------------------
    void init()
    {
    glClearColor(1.0f ,1.0f, 1.0f, 1.0f);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_DEPTH_TEST);
    Textures();

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, texName);
    glBindTexture(GL_TEXTURE_2D, texName[0]);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_WIDTH, TEX_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    }

Similar Threads

  1. Opengl glDrawArrays
    By Eymerich in forum OpenGL ES general technical discussions
    Replies: 3
    Last Post: 11-07-2007, 05:35 AM
  2. glDrawArrays problem
    By Min in forum OpenGL ES general technical discussions
    Replies: 0
    Last Post: 02-17-2005, 04:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •