Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this OpenGL ES 2.0 shader working with my VBO on iOS?

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.

like image 205
KomodoDave Avatar asked Aug 29 '11 15:08

KomodoDave


People also ask

Can I use OpenGL on iOS?

OpenGL ES provides a C-based interface for hardware-accelerated 2D and 3D graphics rendering. The OpenGL ES framework ( OpenGLES. framework ) in iOS provides implementations of versions 1.1, 2.0, and 3.0 of the OpenGL ES specification.

What is GLKit?

Overview. The GLKit framework provides functions and classes that reduce the effort required to create new shader-based apps or to port existing apps that rely on fixed-function vertex or fragment processing provided by earlier versions of OpenGL ES or OpenGL.


1 Answers

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.

like image 144
Christian Rau Avatar answered Nov 15 '22 09:11

Christian Rau