Code from Opengl ES 1.0 tutorial not working (Java N-IDE)

Viewed 31

I'm using the Java N-IDE app on my android phone, and I'm trying to learn how to use OpenGL ES 1.0. I followed the instructions in two different tutorials, and my code still won't work. The compiled app draws a gray screen like it should, but it never draws the triangle.

Here's my code:

MainActivity.java

package com.example.dland;

import android.app.Activity;
import android.os.Bundle;
import android.opengl.GLSurfaceView;

import com.example.dland.R;

public class MainActivity extends Activity {
    
    private GLSurfaceView glview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        
        glview = new MyGLSurfaceView(this);
        
        setContentView(glview);
        
    }

}

MyGLSurfaceView.java

package com.example.dland;

import android.content.Context;
import android.opengl.GLSurfaceView;

import com.example.dland.R;

public class MyGLSurfaceView extends GLSurfaceView {
    
    private final MyGLRenderer renderer;

    public MyGLSurfaceView(Context context) {
        
        super(context);
        
        setEGLContextClientVersion(2);
        
        renderer = new MyGLRenderer();
        
        setRenderer(renderer);
        
    }

}

MyGLRenderer.java

package com.example.dland;

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

import android.opengl.GLSurfaceView;

import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import com.example.dland.R;

public class MyGLRenderer implements GLSurfaceView.Renderer {
    
    private FloatBuffer triangleVB;
    
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        
        // Set the background frame color
        gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        
        initShapes();
        
        // Enable use of vertex arrays
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    
    }
    
    public void onDrawFrame(GL10 gl) {
    
        // Redraw background color
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        
        // Draw the triangle
        gl.glColor4f(0.63671875f, 0.76953125f, 0.22265625f, 0.0f);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleVB);
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
        
    }
    
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
    }
    
    private void initShapes() {
        
        float triangleCoords[] = {
            // X, Y, Z
            -0.5f, -0.25f, 0,
             0.5f, -0.25f, 0,
             0.0f,  0.559016994f, 0
        }; 
        
        // initialize vertex Buffer for triangle  
        ByteBuffer vbb = ByteBuffer.allocateDirect(
                // (# of coordinate values * 4 bytes per float)
                triangleCoords.length * 4); 
        vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
        triangleVB = vbb.asFloatBuffer();  // create a floating point buffer from the ByteBuffer
        triangleVB.put(triangleCoords);    // add the coordinates to the FloatBuffer
        triangleVB.position(0);     // set the buffer to read the first coordinate
        
    }
    
}
0 Answers
Related