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 6390091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:29:03+00:00 2026-05-25T03:29:03+00:00

If anyone can shed light on what’s going wrong here, perhaps a misordering of

  • 0

If anyone can shed light on what’s going wrong here, perhaps a misordering of gl commands or some other incompatible command sequence, I would be tremendously grateful for your assistance. I have been trying to get this code working all day with no success, despite much Google research and poring over examples in “OpenGL ES 2.0 Programming Guide”.

I’m trying to use a Vertex Buffer Object and custom shaders in OpenGL ES 2.0 on iPhone. I am attempting to interleave vertex data from a series of custom structures of the following type:

typedef struct {
    float x, y; // Position.
    float radius;
    float colR,colG,colB,colA; // Color rgba components.
} VType;

The position, radius and color bytes are to be considered for vertex location, pointsize and color respectively. Ids for these are initialised:

ID_ATT_Pos = 0;
ID_ATT_Radius = 1;
ID_ATT_Color = 2;
// Note: I have also tried values of 1,2,3 but no difference.

The stride for these is specified in each glVertexAttribPointer call.

It is intended that each vertex be drawn at its x,y position with the specified color and a pointsize of its radius. Associated with each aforementioned attribute is a vertex shader attribute, these are “a_position”,”a_color” and “a_radius”. Here are the vertex and fragment shaders:

VertexShader.txt

attribute vec2 a_position;      
attribute vec4 a_color;     
attribute float a_radius;       
varying vec4 v_color;
void main()
{
    gl_Position = vec4(a_position,0.0,1.0);
    gl_PointSize = a_radius;
    v_color = a_color;
}

FragmentShader.txt

#ifdef GL_FRAGMENT_PRECISION_HIGH
    precision highp float;
#else
    precision mediump float;
#endif
void main()
{
    gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}

I wonder if a projection matrix is required in the vertex shader? All the points I create are 2D in the dimensions of the iPhone screen, and as you can see they each have ‘z,w’ appended as ‘0.0,1.0’ in the vertex shader above.

Remaining core code to setup the VBO and render using glDrawElements is listed below. When running this code it is visibly apparent that the glClear has been successful and indeed NSLog print confirms this, however no VType vertices are drawn in the viewport by the DrawFrame code listed below. The vertex coordinates are well within screen dimensions e.g. x,y: (92, 454).

Note that any undeclared variables in the following code are class properties, and of appropriate type so e.g. ‘vao’ is GLuint, ‘vbos’ is GLuint[2], ‘program’ is a GLuint program handle. I have also left out the boilerplate OpenGL setup code, which has been tested with different code internals and shown to work.

Load Shader Code

-(GLuint)loadShaderType:(GLenum)type From:(NSString*)shaderFile {
    GLuint shader;
    GLint compiled;

    // Create and compile vertex shader.
    NSString *filepath = [[NSBundle mainBundle] pathForResource:shaderFile ofType:@"txt"];

    const GLchar *shaderSrc = (GLchar *)[[NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil] UTF8String];
    if (!shaderSrc) {
        NSLog(@"Failed to load vertex shader");
        return 0;
    }

    // Create shader object.
    shader = glCreateShader(type);
    if (shader == 0) return 0;
    // Load shader source.
    glShaderSource(shader, 1, &shaderSrc, NULL);
    // Compile shader.
    glCompileShader(shader);
    // Check compile status.
    glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);

    if (!compiled) {
        GLint infoLen = 0;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);

        if (infoLen > 1) {
            char * infoLog = (char*)malloc(sizeof(char)*infoLen);
            glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
            NSLog(@"Error compiling shader:\n%s\n",infoLog);
            free(infoLog);
        }
        glDeleteShader(shader);
        return 0;
    }
    return shader;
}

Initialisation Code

    GLfloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
GLfloat screenWidth = [[UIScreen mainScreen] bounds].size.width;

glViewport(0, 0, screenWidth, screenHeight);


glGenVertexArraysOES(1, &vao);
glBindVertexArrayOES(vao);

// Generate buffer, bind to use now, set initial data.
glGenBuffers(2, vbos);

glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
glBufferData(GL_ARRAY_BUFFER, vxBufSize, squidVxs, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ixBufSize, squidIxs, GL_STATIC_DRAW);

glEnableVertexAttribArray(ID_ATT_Pos); // Pos   
glVertexAttribPointer(ID_ATT_Pos, 2, GL_FLOAT, GL_FALSE, sizeof(VType), BUFFER_OFFSET(0));
glEnableVertexAttribArray(ID_ATT_Radius);// Radius
glVertexAttribPointer(ID_ATT_Radius, 1, GL_FLOAT, GL_FALSE, sizeof(VType), BUFFER_OFFSET(sizeof(float)*2));
glEnableVertexAttribArray(ID_ATT_Color);// Color
glVertexAttribPointer(ID_ATT_Color, 4, GL_FLOAT, GL_FALSE, sizeof(VType), BUFFER_OFFSET(sizeof(float)*3));

GLuint shaders[2];
shaders[0] = [self loadShaderType:GL_VERTEX_SHADER From:@"VertexShader"];
shaders[1] = [self loadShaderType:GL_FRAGMENT_SHADER From:@"FragmentShader"];

program = glCreateProgram();
glAttachShader(program, shaders[0]);
glAttachShader(program, shaders[1]);

glBindAttribLocation(program, ID_ATT_Pos, "a_position");
glBindAttribLocation(program, ID_ATT_Radius, "a_radius");
glBindAttribLocation(program, ID_ATT_Color, "a_color");

glLinkProgram(program);

GLint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);

if (!linked) {
    GLint infoLen = 0;
    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
    if (infoLen > 1) {
        char* infoLog = (char*)malloc(sizeof(char)*infoLen);
        glGetProgramInfoLog(program, infoLen, NULL, infoLog);
        NSLog(@"Error linking program:\n%s\n",infoLog);
        free(infoLog);
    }
    glDeleteProgram(program);
}

DrawFrame Code

// Note: Framebuffer swapping is taken care of before/after these
//       lines, and has been shown to work.
glClearColor(0.33f, 0.0f, 0.33f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glDrawElements(GL_POINTS, numPoints, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));

Let me know if any other information is needed, thank you for your time.

  • 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-05-25T03:29:04+00:00Added an answer on May 25, 2026 at 3:29 am

    You do not necessarily need a projection matrix for your 2d vertices, but you definitely need to transform all 3 coordinates into the [-1,1] range (which you have already done for z by setting it to 0). These coordinates are then transformed by GL with the current viewport transformation (that should usually match your screen dimensions). So if the coordinates are in screen dimensions, then transform them into [-1,1] in the shader, or just use [-1,1] coordinates in the application (which would also be more resolution agnostic).

    EDIT: And I don’t know how OpenGL ES handles this, but in desktop GL (at least upto 2.1) you need to call glEnable(GL_VERTEX_PROGRAM_POINT_SIZE) for the vertex shader to be able to chnage the point size.

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

Sidebar

Related Questions

I'm curious to see if anyone can shed some light on some strange text
Just wondering if anyone can shed some light on the basics of getter setters
I wonder if anyone can shed some light on an issue that is driving
Im looking to see if anyone can shed some light on a problem im
I'm wondering if anyone can shed some light on the following issue with Matlab
Hope anyone can shed light on this so I can use pens with dash
Can anyone shed some insight onto why the following happens? irb(main):001:0> r = '1'..'30'
I was wondering if anyone can shed a light on the error I get
Anyone can read the GoF book to learn what design patterns are and how
Anyone can point me to any info regarding physics of a snooker game, if

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.