Ahmed Saad
Ahmed Saad

Reputation: 177

Error when Declaring a Float array to be Drawn using OpenGL in Android Java

I am a Beginner Android Developer, I am Trying to create a Simple AirHockey Game Using OpenGL ES 2.0, Everything was going fine until i defined the vertex array i am going to draw.

package com.AirHockey.android;

import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glViewport;

import android.opengl.GLSurfaceView.Renderer;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class AirHocky_ActivityRenderer implements Renderer {

    private static final int POSITION_COMPONENT_COUNT = 2;
    private static final int BYTES_PER_FLOAT = 4;
    private final FloatBuffer vertexData;

    float[] tableVerticesWithTriangles = {
                    // Triangle 1
                    0f, 0f,
                    9f, 14f,
                    0f, 14f,
                    // Triangle 2
                    0f, 0f,
                    9f, 0f,
                    9f, 14f,
                    // Line 1
                    0f, 7f,
                    9f, 7f,
                    // Mallets
                    4.5f, 2f,
                    4.5f, 12f
                    };

    vertexData = ByteBuffer
        .allocateDirect(tableVerticesWithTriangles.length * BYTES_PER_FLOAT)
        .order(ByteOrder.nativeOrder())
        .asFloatBuffer();

    vertexData.put(tableVerticesWithTriangles);

    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
            glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    }

    public void onDrawFrame(GL10 arg0) {
            glClear(GL_COLOR_BUFFER_BIT);
    }

    public void onSurfaceChanged(GL10 arg0, int width, int height) {
            glViewport(0, 0, width, height);
    }

}

the Problem is right after the float array "tableVerticesWithTriangles" it says something about ',' is expected instead of the ';', also there is a problem with vertexData.put(tableVerticesWithTriangles) it highlights the '.' and the array name;

Thanks in Advance.

Upvotes: 1

Views: 186

Answers (2)

zbr
zbr

Reputation: 7037

You can't do something like:

public class Something {
    int a;
    int b;
    a = 3;
    // ...
}

Instead you should do:

public class Something {
    int a = 3;
    int b;
    // ...
}

You are doing exactly the same thing with the vertexData, so you need to fix that.

Also, you are calling the put() method outside any method, which I think won't work.

Also vertexData is final, so you might not be able to put anything in it anyway.

Upvotes: 2

takendarkk
takendarkk

Reputation: 3443

It looks like you are calling

 vertexData.put(tableVerticesWithTriangles);

outside of any method.

Something like this

private int i;
private final String s;
private float[] array;
vertexData.put(); //<----- HERE
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
}

Maybe it's just a typo in your question's code sample, but check it out.

Upvotes: 1

Related Questions