Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7747133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:27:18+00:00 2026-06-01T10:27:18+00:00

I have a packed vertex buffer containing postion coordinates aswell as color values for

  • 0

I have a packed vertex buffer containing postion coordinates aswell as color values for a vertex in the format {X, Y, Z, R, G, B, A}.

I am able to display the rectangle properly with a hardcoded color when I alter the fragment shader by taking out the a_Color attribute and hard coding a vec4 value for gl_FragColor but I am not able to pass the color vec4 attribute into the fragment shader (the rectangle won’t display in that scenario).

What is the correct way to use glVertexAttribPointer(…) and glDrawElements(…) to draw from a packed vertex buffer in OpenGL ES 2.0?

See my code below:

public class GameRenderer implements Renderer {
    public static final int POS_SZ = 3;
    public static final int COL_SZ = 4;
    public static final int FLOAT_SZ = 4;
    public static final int SHORT_SZ = 2;

    private FloatBuffer gridVB;
    private ShortBuffer gridIndices;
    int programCode, paPositionHandle, paColorHandle, puMVPMatrixHandle;

    private final String vertexShaderCode =
        "uniform mat4 u_MVPMatrix;  \n" +
        "attribute vec4 a_Position; \n" +
        "void main(){               \n" +
        " gl_Position = u_MVPMatrix * a_Position;   \n" +
        "} \n";

    private String fragmentShaderCode = 
        "precision mediump float;   \n" +
        "attribute vec4 a_Color; \n" +
        "void main(){               \n" +
        " gl_FragColor = a_Color;   \n" +
        "}                          \n";

    public void staticGrid() {
        float vertexArray[] = {
            -0.75f, 0.75f, 0.0f,        // position
            0.0f, 0.0f, 1.0f, 1.0f,     // colour
            0.75f, 0.75f, 0.0f,
            0.0f, 0.0f, 1.0f, 1.0f,
            0.75f, -0.75f, 0.0f,
            0.0f, 0.0f, 1.0f, 1.0f,
            -0.75f, -0.75f, 0.0f,
            0.0f, 0.0f, 1.0f, 1.0f
        };

        short indicesArray[] = {
            0, 1, 2, 0, 2, 3
        };

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertexArray.length * FLOAT_SZ);
        vbb.order(ByteOrder.nativeOrder());
        gridVB = vbb.asFloatBuffer();
        gridVB.put(vertexArray);
        gridVB.position(0);

        ByteBuffer ibb = ByteBuffer.allocateDirect(indicesArray.length * SHORT_SZ);
        ibb.order(ByteOrder.nativeOrder());
        gridIndices = ibb.asShortBuffer();
        gridIndices.put(indicesArray);
        gridIndices.position(0);
    }

    public void onSurfaceCreated(GL10 unused, EGLConfig arg1) {
        GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        staticGrid();
        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
        programCode = GLES20.glCreateProgram();
        GLES20.glAttachShader(programCode, vertexShader);
        GLES20.glAttachShader(programCode, fragmentShader);
        GLES20.glLinkProgram(programCode);

        paPositionHandle = GLES20.glGetAttribLocation(programCode, "a_Position");
        paColorHandle = GLES20.glGetAttribLocation(programCode, "a_Color");
        puMVPMatrixHandle = GLES20.glGetUniformLocation(programCode, "u_MVPMatrix");
    }

    public void onDrawFrame(GL10 unused) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
        GLES20.glUseProgram(programCode);

        int stride = (POS_SZ + COL_SZ) * FLOAT_SZ;
        int indices_cnt = 6;

        gridVB.position(0);
        GLES20.glEnableVertexAttribArray(paPositionHandle);
        GLES20.glVertexAttribPointer(paPositionHandle, POS_SZ, GLES20.GL_FLOAT, false, stride, gridVB);

        gridVB.position(POS_SZ);
        GLES20.glEnableVertexAttribArray(paColorHandle);
        GLES20.glVertexAttribPointer(paColorHandle, COL_SZ, GLES20.GL_FLOAT, false, stride, gridVB);

        // matrix manipulation ...

        GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices_cnt, GLES20.GL_UNSIGNED_SHORT, gridIndices);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:27:19+00:00Added an answer on June 1, 2026 at 10:27 am

    Fragment shaders don’t use attributes. Attributes are per-vertex values, which a fragment shader wouldn’t know anything about. What you want to do is to take the color as an attribute to the vertex shader, and then use a varying to pass the color to the fragment shader.

    Also please start using error checking for your shaders, it will tell you when you make mistakes like this:

    http://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetProgramiv.xml (check for GL_LINK_STATUS)

    http://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetShaderiv.xml (check for GL_COMPILE_STATUS)

    http://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetProgramInfoLog.xml

    http://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetShaderInfoLog.xml

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a __m256d vector packed with four 64-bit floating-point values. I need to
Okay so i have a packed a proprietary binary format. That is basically a
I have made a java application and have packed it into an executable jar
I have created a java application and packed it into a jar file on
I have the following record definition E3Vector3T = packed record public x: E3FloatT; y:
I have question about interpreting strings as packed binary data in C++. In python,
I have a next code: type THead = packed record znmpc: byte; znmpcch: array
HI I have created a ActiveX componed and packed into a cab file 1)
I am working on a OpenGL ES 2.0 shader and I have tightly packed
I have a file which is UPX packed. Is there any way I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.