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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:43:53+00:00 2026-06-09T13:43:53+00:00

I am trying to create a GLSL example on Mac. I am trying to

  • 0

I am trying to create a GLSL example on Mac. I am trying to set up one color attribute per vertex. However, when the program runs, I just get a purple screen (the purple comes from glClearColor). I post the relevant code snippets.

-(void)drawRect:(NSRect)dirtyRect
{    
    // get program ID for shader program
    GLuint programID = [self loadShaders];

    // get new dimensions
    NSSize dim = [self frame].size;

    // clear the background with color
    glClearColor(0.4f, 0.1f, 0.7f, 1.0f);
    glDepthRange(1.0, -1.0);
    glViewport(0, 0, dim.width, dim.height);
    glClear(GL_COLOR_BUFFER_BIT);

    // generate a buffer for our triangle
    glGenBuffers(2, vertexBuffer);

    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);

    glUseProgram(programID);

    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(VERTEX_POS_INDEX, VERTEX_POS_SIZE, GL_FLOAT, GL_FALSE, VERTEX_POS_SIZE*sizeof(vertices), vertices);

    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[1]);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(VERTEX_COLOR_INDEX, VERTEX_COLOR_SIZE, GL_FLOAT, GL_FALSE, VERTEX_POS_SIZE*sizeof(colors), colors);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glUseProgram(0);

    // flush buffer
    glFlush();
    [[self openGLContext] flushBuffer];
}

And then the loading shadings code:

-(GLuint)loadShaders
{
    printf("GLSL version %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
    printf("GL Version: %s", glGetString(GL_VERSION));

    // Create the shaders
    GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    // get handle for app bundle
    NSBundle *appBundle = [NSBundle mainBundle];

    // get the path for the vertex shader file
    NSString *vertexFilePath = [appBundle pathForResource:vertexShaderFile ofType:nil];
    // get the path for the fragment shader file
    NSString *fragmentFilePath = [appBundle pathForResource:fragmentShaderFile ofType:nil];

    // get the contents of the vertex shader file into a string
    NSString *vertexFileContents = [NSString stringWithContentsOfFile:vertexFilePath encoding:NSUTF8StringEncoding error:NULL];
    NSLog(@"%@", vertexFileContents);
    // get the contents of the fragment shader file into a string
    NSString *fragmentFileContents = [NSString stringWithContentsOfFile:fragmentFilePath encoding:NSUTF8StringEncoding error:NULL];
    NSLog(@"%@", fragmentFileContents);

    GLint Result = GL_FALSE;
    int infoLogLength;

    // get a pointer the vertex shader program source, compile shader program
    const char *vertexSourcePointer = [vertexFileContents UTF8String];
    glShaderSource(vertexShaderID, 1, &vertexSourcePointer, NULL);
    glCompileShader(vertexShaderID);
    // check the vertex shader
    glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(vertexShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
    char vertexShaderErrorMessage[infoLogLength];
    glGetShaderInfoLog(vertexShaderID, infoLogLength, NULL, vertexShaderErrorMessage);
    // print error message
    NSLog(@"%@", [NSString stringWithUTF8String:vertexShaderErrorMessage]);

    // get a pointer to the fragment shader program source, compile shader program
    const char *fragmentSourcePointer = [fragmentFileContents UTF8String];
    glShaderSource(fragmentShaderID, 1, &fragmentSourcePointer, NULL);
    glCompileShader(fragmentShaderID);
    // check the fragment shader
    glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(fragmentShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
    char fragmentShaderErrorMessage[infoLogLength];
    glGetShaderInfoLog(fragmentShaderID, infoLogLength, NULL, fragmentShaderErrorMessage);
    // print error message
    NSLog(@"%@", [NSString stringWithUTF8String:fragmentShaderErrorMessage]);

    // link the program
    NSLog(@"Linking program...");
    GLuint programID = glCreateProgram();
    glAttachShader(programID, vertexShaderID);
    glAttachShader(programID, fragmentShaderID);

    glBindAttribLocation(programID, 0, "position");
    glBindAttribLocation(programID, 1, "inColor");

    glLinkProgram(programID);

    // check the program
    glGetProgramiv(programID, GL_LINK_STATUS, &Result);
    glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &infoLogLength);
    char shaderProgramErrorMessage[max(infoLogLength, (int)1)];
    glGetProgramInfoLog(programID, infoLogLength, NULL, shaderProgramErrorMessage);
    // pring error message
    NSLog(@"%@", [NSString stringWithUTF8String:shaderProgramErrorMessage]);

    // delete shaders
    glDeleteShader(vertexShaderID);
    glDeleteShader(fragmentShaderID);

    return programID;

}

And finally the shaders:

#version 120

attribute vec4 position;
attribute vec4 inColor;
varying vec4 outColor;
void main()
{
    gl_Position = position;
    outColor = inColor;
}

And

#version 120

varying vec4 outColor;
void main()
{
    gl_FragColor = outColor;
}
  • 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-09T13:43:55+00:00Added an answer on June 9, 2026 at 1:43 pm

    Looks fairly good, though I do see one mistake.

    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[0]);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(VERTEX_POS_INDEX, 
                          VERTEX_POS_SIZE, 
                          GL_FLOAT, 
                          GL_FALSE, 
                          VERTEX_POS_SIZE*sizeof(vertices), 
                          vertices);
    

    If you are using VBO, then the final attribute to glVertexAttribPointer should be the offset from the start of the currently bound buffer.

    If you were using vertex arrays (no glBindBuffer), then you would point to vertices with the final argument of glVertexAttribPointer.

    However, since you’ve already uploaded vertices to a buffer and bound it, the final value of glVertexAttribPointer should be the offset from the start of the vertices buffer, which in your case, should be 0.

    Same mistake for the colors buffer as well.

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

Sidebar

Related Questions

I'm trying to write a basic vertex shader in GLSL and just for the
Trying to create a facebook app just to learn and coming across a strange
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
I trying create a class derivated from System.Web.UI.Page and in override Render i set
I am trying create a prototype where one of the features is a dropdown
I trying create iis7 web application by msbuild community tasks and set property Enabled
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
Ok so I am trying create a login script, here I am using PHP5
Trying to create a black line in my view to separate text blocks but

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.