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

  • Home
  • SEARCH
  • 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 7836749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:17:51+00:00 2026-06-02T14:17:51+00:00

I am writing a program in C, using OpenGL, to draw some primitives (lines,

  • 0

I am writing a program in C, using OpenGL, to draw some primitives (lines, circles, etc.). I have succeeded in using the buffer objects to store the vertices of the primitives, and hence, in drawing them. But I seem to be stuck now since I don’t know how to set their colour. Part of the program was given to me by my teacher, so I know that the colour attributes must be specified using this structure:

typedef struct {
    float r;
    float g;
    float b;
    float a;
} vec4;

I know I am supposed to use a vec4 array to store colour values so I declared one such variable. The first one is a vec2 array which will be used to store the primitive vertices.

vec2 vertices[10000];
vec4 colours[10000];

The function which (as I understand) is used to set the limit of the vertex buffer looks like this:

void initShaders(void) 
{
    GLuint buffer;
    GLuint loc;
    GLuint vao;

    /*Create and initialize a program object with shaders */ 
  idProgram = initProgram("ass1vshader.glsl", "ass1fshader.glsl");

    /*installs the program object as part of current rendering state */ 
   glUseProgram(idProgram);

   /*Creat a vertex array object */ 
   glGenVertexArrays(1, &vao);
   glBindVertexArray(vao);

    /*Create buffer in the shared display list space and */ 
     /* bind it as GL_ARRAY_BUFFER */ 
    glGenBuffers( 1, &buffer);
    glBindBuffer( GL_ARRAY_BUFFER, buffer);
    glBufferData( GL_ARRAY_BUFFER, sizeof(vertex)+sizeof(colours), NULL,GL_STATIC_DRAW);

    /*Initialize attribute vPosition in program */ 
    loc = glGetAttribLocation( idProgram, "vPosition");
    glEnableVertexAttribArray(loc);
    glVertexAttribPointer(loc, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)BUFFER_OFFSET(0));

    /*Get location of the uniform attribute 'u_proj_matrix' */ 
    idProjection = glGetUniformLocation(idProgram, "u_proj_matrix");

    /*Set graphics attributes */ 
    glLineWidth(3.0);
    glPointSize(1.0);
    glClearColor(1.0, 1.0, 1.0, 1.0);

}

As you can see, I have initialized the vertex buffer size to the combined size of “vertex” and “colours”.

I am also given to understand that the fragment shader plays an important role in the colouring process. It looks like this:

#version 140

in vec4 color;
out vec4  fColor;

void
main()
{    
    fColor = color;
}

Can somebody tell me how I can go about giving colour to my primitives? I hope the information I have provided is sufficient.

  • 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-02T14:17:52+00:00Added an answer on June 2, 2026 at 2:17 pm

    One way to do it is to use a color array. You can do this by calling glColorPointer() in the same way as you do for glVertexPointer().

    Also, I don’t know if this is just a typo, or if you actually have this error, but you wrote:

    glBufferData( GL_ARRAY_BUFFER, sizeof(vertex)+sizeof(colours), NULL,GL_STATIC_DRAW);
    

    However, your vertex array is named “vertices” not “vertex” (at least according to your earlier statement). If it compiles OK, then it probably means you have another variable or data structure named vertex, but it’s not doing what you think.

    And since your data isn’t interleaved (you have separate vertex and color data), I think you’ll want to use separate buffers for them. If they were in one structure, you could use a single array, and set the stride appropriately, but since they aren’t interleaved, I’m not sure if what you’re attempting to do will work.

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

Sidebar

Related Questions

I am writing a graphics program in C/C++ using OpenGL. I have a bunch
I am writing a small program in C++ using OpenCV-2.3 API. I have an
I am writing a program in C# that runs some select statements using parameters
I am writing a program in OpenGL and I need some sort of interfacing
I am writing a program using MAC OSX 10.6.6 and xcode 3.2 I have
I am writing a web program using mvc php framework. I have index and
I am writing an android program in which I built a box using opengl
I'm writing a program using JOGL/openCL to utilize the GPU. I have code that
Basically I am writing a simple program using the boost socket library... I have
I've been writing a program using directx11, and I have written a basic camera

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.