Open GL ES 2 sehe nur Rot.

  • Antworten:0
  • OffenNicht stickyNicht beantwortet
  • Forum-Beiträge: 14

27.03.2016, 13:39:59 via Website

Ich bin dabei mich in OpenGL ES 2 unter Java einzuarbeiten.

Habe keine Ahnung müsste eigentlich ein Blaues Quadrat darstellen gibt aber nur die rote Hintergrundfarbe wieder.

Hier meine Activity

package test.com.test;

import android.app.;
import android.os.
;

import android.opengl.*;

public class MainActivity extends Activity
{
private MyRenderer myRenderer;
private GLSurfaceView myView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    myView=new GLSurfaceView(this);
    myView.setEGLContextClientVersion(2);
    this.myRenderer=new MyRenderer(this);
    myView.setRenderer(myRenderer);
    setContentView(myView);
}

}

und jetzt meine Renderer-Class

package test.com.test;

import android.opengl.;
import static android.opengl.GLES20.
;
import javax.microedition.khronos.opengles.;
import javax.microedition.khronos.egl.EGLConfig;
import android.content.
;
import java.nio.*;

public class MyRenderer implements GLSurfaceView.Renderer
{
private float[] vertices={
-50f, 50f,0f,
50f, 50f,0f,
50f,-50f,0f,
-50f,-50f,0f
};
private short[] indices={
0,1,3,
1,2,3
};

private FloatBuffer vertexBuffer;
private ShortBuffer indexBuffer;
private int[] vbo,ibo;
private int programHandle,positionHandle;

public MyRenderer(Context context)
{
    vbo = new int[1];
    ibo = new int[1];

    ByteBuffer vbb=ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    ByteBuffer ibb=ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);

    glGenBuffers(1, vbo, 0);
    glGenBuffers(1, ibo, 0);

    glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
    glBufferData(GL_ARRAY_BUFFER, vertexBuffer.capacity() * 4, vertexBuffer, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo[0]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.capacity() * 2, indexBuffer, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

@Override
public void onSurfaceCreated(GL10 p1, EGLConfig p2)
{
    glClearColor(1f, 0f, 0f, 1f);

    String vertexShader=
            "attribute vec4 vPosition; \n"
                    + "void main()\n"
                    + "{\n"
                    + "gl_Position = vPosition;\n"
                    + "}\n";

    String fragmentShader=
            "void main()\n"
                    + "{\n"
                    + " gl_FragColor = vec4(0.0,1.0,0.0,1.0);\n"
                    + "}\n";

    int vertexShaderHandle = glCreateShader(GL_VERTEX_SHADER);
    if (vertexShaderHandle != 0)
    {
        glShaderSource(vertexShaderHandle, vertexShader);
        glCompileShader(vertexShaderHandle);
        final int[] compileStatus = new int[1];
        glGetShaderiv(vertexShaderHandle, GL_COMPILE_STATUS, compileStatus, 0);
        if (compileStatus[0] == 0)
        {
            GLES20.glDeleteShader(vertexShaderHandle);
            vertexShaderHandle = 0;
        }
    }

    int FragmentShaderHandle = glCreateShader(GL_FRAGMENT_SHADER);
    if (FragmentShaderHandle != 0)
    {
        glShaderSource(FragmentShaderHandle, fragmentShader);
        glCompileShader(FragmentShaderHandle);
        final int[] compileStatus = new int[1];
        glGetShaderiv(FragmentShaderHandle, GL_COMPILE_STATUS, compileStatus, 0);
        if (compileStatus[0] == 0)
        {
            GLES20.glDeleteShader(FragmentShaderHandle);
            FragmentShaderHandle = 0;
        }
    }
    programHandle = glCreateProgram();
    if (programHandle != 0)
    {
        glAttachShader(programHandle, vertexShaderHandle);
        glAttachShader(programHandle, FragmentShaderHandle);
        glBindAttribLocation(programHandle, 0, "vPosition");
        glLinkProgram(programHandle);
        final int[] linkStatus = new int[1];
        glGetProgramiv(programHandle, GL_LINK_STATUS, linkStatus, 0);
        if (linkStatus[0] == 0)
        {
            glDeleteProgram(programHandle);
            programHandle = 0;
        }
    }

}

@Override
public void onSurfaceChanged(GL10 p1, int width, int height)
{
    if (width == 0) width = 1;
    glViewport(0, 0, width, height);
}

@Override
public void onDrawFrame(GL10 p1)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
    glEnableVertexAttribArray(positionHandle);
    glVertexAttribPointer(positionHandle, 3, GL_FLOAT, false, 3 * 4, vertexBuffer);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo[0]);

    glUseProgram(programHandle);

    glDrawElements(GL_TRIANGLE_STRIP, 1, GL_UNSIGNED_SHORT, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

}

Das muss doch eigentlich funktionieren.

Antworten

Empfohlene Artikel