Colour/color arrays

Hi,

I have drawn a traingle using this:


GLfloat vertices[] = { -0.5f, -0.5f, 0.0f,
        0.5f, -0.5f, 0.0f,
        0.0f, 0.5f, 0.0f };
    GLfloat colours[] = {
        0.5f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 1.0f
    };

    /*GLfloat colours[] = {
        0.0f, 1.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 1.0f, 0.0f
    };*/
    int i;

    double dVertices[9]; 
    
    SetUpProcs(); 
    i = 0;
    do
    {
        dVertices[i] = (double)vertices[i];
    } while (i++ < 8);

    dMinEast = 0.0;
    dMaxEast = 0.0;
    dMinNorth = 0.0;
    dMaxNorth = 0.0;
    dMaxElev = 0.0;
    dMinElev = 0.0;

    proc_max_min(&dMinEast, &dMaxEast,
        &dMinNorth, &dMaxNorth,
        &dMaxElev, &dMinElev,
        &dVertices[0],
        &dVertices[1],
        &dVertices[2]);

    proc_max_min(&dMinEast, &dMaxEast,
        &dMinNorth, &dMaxNorth,
        &dMaxElev, &dMinElev,
        &dVertices[3],
        &dVertices[4],
        &dVertices[5]);

    proc_max_min(&dMinEast, &dMaxEast,
        &dMinNorth, &dMaxNorth,
        &dMaxElev, &dMinElev,
        &dVertices[6],
        &dVertices[7],
        &dVertices[8]);

    //vbo stuff
    GLuint triangleVBO;

glGenBuffers(1, &triangleVBO);
    //glGenBuffers(1, &triangleVBO);
    glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);
    glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    GLuint triangleVBO_colour;
    glGenBuffers(1, &triangleVBO_colour);
    glBindBuffer(GL_ARRAY_BUFFER, triangleVBO_colour);
    glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), colours, GL_STATIC_DRAW);

    

    GLuint VAO;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
    glEnableVertexAttribArray(0);
    //bind triangles
    glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
    //bind colour
    glBindBuffer(GL_ARRAY_BUFFER, triangleVBO_colour);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    

    glDrawArrays(GL_TRIANGLES, 0, 6);
    //glFlush();
    SwapBuffers(ghDC);

I am not sure if I have used the color array correctly? Any help greatly appreciated.

MacSam

You probably intended to bind the positions VBO and colors VBO to different vertex attributes. But instead above you tried to bind them both to attribute 0. The net effect is after this, only the colors VBO is bound to vertex attribute 0. Your 2nd glVertexAttribPointer call overwrite the attribute 0 buffer binding.

Once you move the colors VBO to a different attribute number, then you’ll want to enable that attribute as well.

Finally, since you’re not interleaving your vertex attributes in one shared VBO, you have the option to just specify a stride of 0 if you want and the driver will auto-compute the same size as you’ve specified. But what you have is fine too.

Thank you.
I do not know how to create an attribute number for the colour and the coordinates

GLuint triangleVBO;

	//glDeleteBuffers(1, triangleVBO[0]);

	glTranslatef(glfHoriz, glfVert, -(glfZoom - (GLfloat)gldRadius));//100.0);
	polarView(((GLdouble)glfZoom), 0.0, (GLdouble)flatitude, (GLdouble)flongitude);
	
	glGenBuffers(1, &triangleVBO);
	//glGenBuffers(1, &triangleVBO);
	glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);
	glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	GLuint triangleVBO_colour;
	glGenBuffers(1, &triangleVBO_colour);
	glBindBuffer(GL_ARRAY_BUFFER, triangleVBO_colour);
	glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), colours, GL_STATIC_DRAW);

	/*GLuint prog;
	glUseProgram(prog);
	glLinkProgram(prog);
	int position_loc = glgetattiblocation(prog, "position");
	int color_loc = glgetattiblocation(prog, "color");*/

	GLuint VAO_tri;
	
	glEnableVertexAttribArray(0);
	glGenVertexArrays(1, &VAO_tri);
	glBindVertexArray(VAO_tri);
	//glEnableVertexAttribArray(0);
	//bind triangles
	glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);
	
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);

	//bind colour
	GLuint VAO_col;
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
	glGenVertexArrays(1, &VAO_col);
	glBindVertexArray(VAO_col);
	//glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, triangleVBO_colour);
	glEnableVertexAttribArray(VAO_col);
	

	//glBindVertexArray(0); causes gldrawarrays to crash

	/*glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);*/



	//drawscene stuff
	//GLuint shaderID = LoadPassThroughShader();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
	//glUseProgram(shaderID);

	glDrawArrays(GL_TRIANGLES, 0, 6);
	//glFlush();
	SwapBuffers(ghDC);

I have lost the triangle

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.