Finnrir
Finnrir

Reputation: 25

AIDE OpenGL es 2.0 triangle will not render

Hello this is my first time using this site and my first time dealing with OpenGL es 2.0. So my problem is I am using AIDE and trying to learn how to use OpenGL es 2.0 I'm just at a loss now and hit a brick wall. The compiler shows no errors and the app itself doesn't crash. It just will not render a f*ing triangle. I've tried everything I could think of. Looked at multiple examples. Nothing is working. So here is my code. (Totally not ripped off of android developers website)

f0xTriangle

public class f0xTriangle
{
    private final int program;
    private final int vertexCount = triangleCoords.length / 
COORDS_PER_VERTEX;
    private final int vertexStride = COORDS_PER_VERTEX * 4;

    private int positionHandle;
    private int colorHandle;

    private final String vertexShaderCode 
"attribute vec4 vPosition;" +
"void main() {" +
"  gl_Position = vPosition;" +
"}";

private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
"  gl_FragColor = vColor;" +
"}";

private FloatBuffer vertexBuffer;
static final int COORDS_PER_VERTEX = 3;
static float triangleCoords[] = { //in counter clockwise order
    0.0f, 0.622008459f, 0.0f, //top
    -0.5f, 0.311004243f, 0.0f, //bottom left
    0.5f, 0.311004243f, 0.0f  //bottom right
};

// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

public f0xTriangle(){
    ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(triangleCoords);
    vertexBuffer.position(0);
    int vertexShader = f0xGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = f0xGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
    program = GLES20.glCreateProgram();
    GLES20.glAttachShader(vertexShader, program);
    GLES20.glAttachShader(fragmentShader, program);
    GLES20.glLinkProgram(program);
}

public void draw(){
    GLES20.glUseProgram(program);
    positionHandle = GLES20.glGetAttribLocation(program, "vPosition");
    GLES20.glEnableVertexAttribArray(positionHandle);
    GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
                                GLES20.GL_FLOAT, false,
                                vertexStride, vertexBuffer);
    colorHandle = GLES20.glGetUniformLocation(program, "vColor");
    GLES20.glUniform4fv(colorHandle, 1, color, 0);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
    GLES20.glDisableVertexAttribArray(positionHandle);
}
}

f0xGLRenderer

public class f0xGLRenderer implements GLSurfaceView.Renderer
{
    private f0xTriangle triangle;

    public void onSurfaceCreated(GL10 p1, EGLConfig config)
    {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        triangle = new f0xTriangle();
    }
    public void onDrawFrame(GL10 p1)
    {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        triangle.draw();
    }
    public void onSurfaceChanged(GL10 p1, int width, int height)
    {
        GLES20.glViewport(0, 0, width, height);
    }
    public static int loadShader(int type, String shaderCode) {
        int shader = GLES20.glCreateShader(type);
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);
        return shader;
    }
}

f0xGLSurfaceView

public class f0xGLSurfaceView extends GLSurfaceView
{
    private final f0xGLRenderer f0xrender;

    public f0xGLSurfaceView(Context context) {
        super(context);
        setEGLContextClientVersion(2);
        f0xrender = new f0xGLRenderer();
        setRenderer(f0xrender);
        setRenderMode(f0xGLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
}

OpenGl2Activity

public class OpenGL2Activity extends Activity
{
    private f0xGLSurfaceView glView;

    @Override
    public void onCreate(Bundle savedInstantStates) {
        super.onCreate(savedInstantStates);
        glView = new f0xGLSurfaceView(this);
        setContentView(glView);
    }
}

Clearly I have no idea what is going on. Any pointers and advice would be greatly appreciated. Thank you. On another note the reason that I am using AIDE is because it is the only thing that I can use at the moment to code. No computer for me... Sigh I hope that it's not the compiler.

Upvotes: 1

Views: 308

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

You swapped the parameters when you attach the shader object to the program object. The 1st parameter of glAttachShader is the program object and the 2nd is the shader object:

GLES20.glAttachShader(vertexShader, program);   // <--- this parameters have to be swapped
GLES20.glAttachShader(fragmentShader, program); // <--- this parameters too

See the Android developers documentation - glAttachShader:

glAttachShader

public static void glAttachShader (int program, int shader)

See the Khronos OpenGL ES 2.0 reference page - glAttachShader:

glAttachShader — attach a shader object to a program object

void glAttachShader( GLuint program, GLuint shader);

Parameters

  • program Specifies the program object to which a shader object will be attached.
  • shader Specifies the shader object that is to be attached.


The wrong usage of the parameters causes a GL_INVALID_OPERATION error and no shader object is attached to the program object.
Note, OpenGL errors can be checked by glGetError

Upvotes: 2

Related Questions