构造一个三角形,每个顶点都有自己的颜色

时间:2014-06-23 06:38:07

标签: opengl

    int COORDS_PER_VERTEX = 2;
    int vertexStride = COORDS_PER_VERTEX * 4;

    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                                 GLES20.GL_FLOAT, false,
                                 vertexStride, vertexBuffer);
    GLES20.glEnableVertexAttribArray(mPositionHandle);


    // Pass in the color information
    GLES20.glVertexAttribPointer(mColorHandle, COLOR_DATA_SIZE, GLES20.GL_FLOAT, false,
            colorStride, colorBuffer);

    GLES20.glEnableVertexAttribArray(mColorHandle);

我试图让我的三角形的每个顶点都有自己的颜色。我只使用x,y坐标来表示一个点。因此,例如,三角形可以表示为{1,0,1,1,0,0}。

我的问题是,我应该如何制作colorStrideCOLOR_DATA_SIZE变量来实现这一目标。

编辑:我的颜色和顶点数组......

float[] colors = new float[] {1.0f, 0.0f, 0.0f, 1.0f,
                              0.0f, 1.0f, 0.0f, 1.0f,
                              0.0f, 0.0f, 1.0f, 1.0f }
float[] tCoords= new float[] {0.0f, 0.0f
                              0.0f, 1.0f
                              1.0f, 1.0f }

EDIT2:

private void init() {
        vertexCount = triangleCoords.length/COORDS_PER_VERTEX;
        vertexStride = COORDS_PER_VERTEX * 4;
        // initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(
                // (number of coordinate values * 4 bytes per float)
                triangleCoords.length * 4);
        // use the device hardware's native byte order
        bb.order(ByteOrder.nativeOrder());

        // create a floating point buffer from the ByteBuffer
        vertexBuffer = bb.asFloatBuffer();
        // add the coordinates to the FloatBuffer
        vertexBuffer.put(triangleCoords);
        // set the buffer to read the first coordinate
        vertexBuffer.position(0);

        // initialize vertex byte buffer for shape coordinates
        bb = ByteBuffer.allocateDirect(
                // (number of coordinate values * 4 bytes per float)
                color.length * 4);
        // use the device hardware's native byte order
        bb.order(ByteOrder.nativeOrder());

        // create a floating point buffer from the ByteBuffer
        colorBuffer = bb.asFloatBuffer();
        // add the coordinates to the FloatBuffer
        colorBuffer.put(color);
        // set the buffer to read the first coordinate
        colorBuffer.position(0);

        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

        mProgram = GLES20.glCreateProgram();             // create empty OpenGL ES Program
        GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
        GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
        GLES20.glLinkProgram(mProgram);                  // creates OpenGL ES program executables
    }

1 个答案:

答案 0 :(得分:2)

COLOR_DATA_SIZE =每个顶点的颜色分量(3或4个你想要的alpha)

colorStride = sizeof(GLfloat) * COLOR_DATA_SIZE

或者,你可以将步幅保持为0,这意味着数据紧密排列,两者之间没有间隙。