0
\$\begingroup\$

hi guys i'm new of these topics(opengl and others) To begin i want only to create a simple circle that can be route on touchevent. So i decided to learn opengl and to try.

These is my class called circle

public class Circle
{

private ByteBuffer byteBuffer;
private FloatBuffer floatBuffer;
private FloatBuffer colorBuffer;
private ShortBuffer indicesbuffer;


float[] buffer=new float[360*3];

float[] color={
        1f,0f,0f,0f,
        1f,0f,0f,0f,
        1f,0f,0f,0f,
        1f,0f,0f,0f,

        1f,0f,0f,0f,
        1f,0f,0f,0f,
        1f,0f,0f,0f,
        1f,0f,0f,0f,
};

short indices[]=new short[180*2];


public Circle()
{

    for(int i=0;i<360;i++)
    {
        buffer[i]=(float)Math.cos(Math.toRadians((double)i))*0.5f;
        buffer[++i]=(float)Math.sin(Math.toRadians((double)i))*0.5f;
        buffer[++i]=0f;
       // Log.i("",""+buffer[i-2]+" "+buffer[i-1]+" "+buffer[i]);
    }
    short count=0;

    for(int i=0;i<360;i++)
    {
        indices[i++]=count;
        indices[i]=(short)(360-count);
        count++;
        Log.i("Prova",""+indices[i-1]+" "+indices[i]);
    }
    byteBuffer=ByteBuffer.allocateDirect(4*buffer.length);
    byteBuffer.order(ByteOrder.nativeOrder());
    floatBuffer= byteBuffer.asFloatBuffer();
    floatBuffer.put(buffer);
    floatBuffer.position(0);

    byteBuffer=ByteBuffer.allocateDirect(4*color.length);
    byteBuffer.order(ByteOrder.nativeOrder());
    colorBuffer= byteBuffer.asFloatBuffer();
    colorBuffer.put(color);
    colorBuffer.position(0);

    byteBuffer=ByteBuffer.allocateDirect(indices.length*4);
    byteBuffer.order(ByteOrder.nativeOrder());

    indicesbuffer=byteBuffer.asShortBuffer();
    indicesbuffer.put(indices);
    indicesbuffer.position(0);

}

public void draw(GL10 gl10)
{
    gl10.glMatrixMode(GL10.GL_PROJECTION);
    gl10.glLoadIdentity();

    gl10.glVertexPointer(3, GL10.GL_FLOAT,0,floatBuffer);
    gl10.glColorPointer(4,GL10.GL_FLOAT,0,colorBuffer);


    gl10.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl10.glEnableClientState(GL10.GL_COLOR_ARRAY);

    gl10.glDrawElements(GL10.GL_LINES,360
            ,GL10.GL_UNSIGNED_SHORT,indicesbuffer);

    gl10.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl10.glDisableClientState(GL10.GL_COLOR_ARRAY);
}

public void route(float x,float y,GL10 gl)
{
    gl.glClearColor(255,255,255,0);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();

    gl.glRotatef(x,0,1,0);
    gl.glRotatef(y,-1,0,0);

    this.draw(gl);
}
}

it was really simply..i use goniometric function to get polar coordinates of circle and then i draw it. I want to draw it red colored. So i use indicies to unite the points that are opposite respect x axis. But the output is thisenter image description here

These is the main and render code

public class MainActivity extends AppCompatActivity implements GLSurfaceView.Renderer,View.OnTouchListener{

int wi,he;
    Circle cube= new Circle();



    float la_x,la_y,diff_x,diff_y=0;
    GLSurfaceView gl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gl= new GLSurfaceView(this);
        final Prova prova= new Prova(100,100);
        gl.setRenderer(this);
        this.addContentView(gl,new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        gl.setOnTouchListener(this);
        gl.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

    }



    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        gl.glClearColor(255,255,255,0);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);





        cube.draw(gl);


    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {

    }

    @Override
    public void onDrawFrame(GL10 gl) {

            cube.route(diff_x, diff_y,gl);
            Log.i("Differencese",""+diff_x+" "+diff_y);


    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x=event.getX();
        float y= event.getY();

        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                la_x=x;
                la_y=y;
                break;

            case MotionEvent.ACTION_MOVE:
                diff_x=x-la_x;
                diff_y= y-la_y;

                la_x=x;
                la_y=y;
                gl.requestRender();
                break;

            case  MotionEvent.ACTION_UP:
                la_x=0;
                la_y=0;
                break;

        }

        return false;
    }

}

Can anyone plese tell where is my errors.. Sorry for the english and thanks to all

\$\endgroup\$
0
\$\begingroup\$

You only need to draw 179 vertical lines. You want to skip point 0, since you didn't define a point 360, and if you had, it would have had the same location as point 0.

short indices[]=new short[179*2];

count = 1;
for(int i=0;i<358;i++)
{
    indices[i++]=count;
    indices[i]=(short)(360-count);
    count++;
    Log.i("Prova",""+indices[i-1]+" "+indices[i]);
}

As for color, you've only assigned the color for the first 8 points.

For those 8 lines, the color will start out red, then vary towards black.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.