Xiodine
Xiodine

Reputation: 125

Weird problems drawing a circle

UPDATE2: Tried rendering just a quad. FAIL!

UPDATE: The FULL code is here. Somebody can please confirm any problem with my code? http://dl.dropbox.com/u/8489109/HelloAndroid.7z

I've been trying to draw a circle with Opengl ES 1.0. I've used a lot of SDL and OpenGL on the Windows platform and been using mostly glBegin and glEnd because of the low polygon count that my games used.

Pasted down is my code that is called when the object is created.

    float ini[]=new float[360*3];
    ByteBuffer temp=ByteBuffer.allocateDirect(ini.length*4);
    temp.order(ByteOrder.nativeOrder());
    vertex=temp.asFloatBuffer();
    int i;
    float D2R=(float) (3.14159265/180);
    for (i=0;i<360;i++){
        float XX=(float)(Math.sin(i*D2R)*size);
        float YY=(float)(Math.cos(i*D2R)*size);
        ini[i*2]=XX;
        ini[i*2+1]=YY;
        ini[i*2+2]=0;
    }
    vertex.put(ini);
    Log.d("GAME","SPAWNED NEW OBJECT");
    length=ini.length;
    //vertex=ByteBuffer.allocateDirect(temp.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    //vertex.put(temp);
    vertex.position(0);

Now here is the draw code

Log.d("OBJECT","DUH WRITE");
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPushMatrix();
gl.glTranslatef((float)x,(float)y,0);
gl.glVertexPointer(3, GL10.GL_FLOAT,0, vertex);
gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, length);
gl.glPopMatrix();
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

It draws a circle (when it actually decides to run), and adds some wierd lines. An example here: enter image description here

It's the fault of?

gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
gl.glViewport(0, 0, arg1, arg2);
gl.glOrthof(0,(float)arg1,(float)arg2,0,-1,1);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();

Upvotes: 0

Views: 370

Answers (1)

datenwolf
datenwolf

Reputation: 162164

This doesn't make sense:

float ini[]=new float[360*3];
/* ... */
for (i=0;i<360;i++){
    float XX=(float)(Math.sin(i*D2R)*size);
    float YY=(float)(Math.cos(i*D2R)*size);
    ini[i*2]=XX;
    ini[i*2+1]=YY;
    ini[i*2+2]=0;
}

You allocate a multiple of 3 elements, but multiply with a stride of 2. Either do

float ini[]=new float[360*2];
/* ... */
for (i=0;i<360;i++){
    float XX=(float)(Math.sin(i*D2R)*size);
    float YY=(float)(Math.cos(i*D2R)*size);
    ini[i*2]=XX;
    ini[i*2+1]=YY;
}
/* ... */
gl.glVertexPointer(2, GL10.GL_FLOAT,0, vertex);

or

float ini[]=new float[360*3];
/* ... */
for (i=0;i<360;i++){
    float XX=(float)(Math.sin(i*D2R)*size);
    float YY=(float)(Math.cos(i*D2R)*size);
    ini[i*3]=XX;
    ini[i*3+1]=YY;
    ini[i*3+2]=0;
/*        ^         */
/*        ^         */
}
/* ... */
gl.glVertexPointer(3, GL10.GL_FLOAT,0, vertex);

Also you're using glDrawArrays wrong. You don't use the length of the array in bytes, but the count of vertices to draw – 360 in your case.

gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 360);

Upvotes: 1

Related Questions