opengl android,为什么我的三角形不显示都是照着写的我的render哪里写错了吗

package com.cheerchip.opengl5;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;

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

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

/**

  • Created by noname on 2017/6/28. */

public class GLRender implements GLSurfaceView.Renderer {
//顶点颜色
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 int programs;
//三角形顶点
private float[] vertexes = new float[]{
1f, 0f, 0f,
0f, 1f, 0f,
-1f, 0f, 0f

};
private FloatBuffer floatBuffer;

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    GLES20.glClearColor(0.5f,0.5f,0.5f,1.0f);//设置背景色灰色
    init();
    //生成shader调色板
    //顶点和片元一般两种

    int verextype = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(verextype, vertexShaderCode);
    GLES20.glCompileShader(verextype);
    int fragmenttype = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fragmenttype, fragmentShaderCode);
    GLES20.glCompileShader(fragmenttype);

    // GLES20.
    programs = GLES20.glCreateProgram();
    //连接到调色板
    GLES20.glAttachShader(programs, verextype);
    GLES20.glAttachShader(programs, fragmenttype);
    //连接programs
    GLES20.glLinkProgram(programs);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    GLES20.glViewport(0,0,width,height);
    float ratio = (float) width / height;

    // create a projection matrix from device screen geometry
    //Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}

@Override
public void onDrawFrame(GL10 gl) {

    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
    GLES20.glUseProgram(programs);
    //获取顶点着色器的vPosition成员句柄
    int mPositionHandle = GLES20.glGetAttribLocation(programs, "vPositon");
    //启用三角形顶点的句柄
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    //准备三角形数据3*4顶点偏移量
    GLES20.glVertexAttribPointer(mPositionHandle,3,GLES20.GL_FLOAT,false,3*4,floatBuffer);
    int uniformhandle= GLES20.glGetUniformLocation(programs,"vColor");
    //绘制颜色
    GLES20.glUniform4fv(uniformhandle,1,color,0);
    //h绘制图像
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,3);
    GLES20.glDisableVertexAttribArray(mPositionHandle);
}

//设置颜色,依次为红绿蓝和透明通道
float color[] = { 1.0f, 1.0f, 1.0f, 1.0f };
// 准备数据
private void init(){

    ByteBuffer buffer=ByteBuffer.allocateDirect(vertexes.length*4);//每个数4个字节
    buffer.order(ByteOrder.nativeOrder());
    floatBuffer = buffer.asFloatBuffer();
    floatBuffer.put(vertexes);
    floatBuffer.position(0);
}

}

1个回答

vPosition 少加了一个i麻痹看了我半天

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!