Jump to content

  • Log In with Google      Sign In   
  • Create Account

Vertex Arrays


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Wrathnut   Members   -  Reputation: 397

Like
0Likes
Like

Posted 10 March 2013 - 09:06 AM

Hi,

 

I was messing around with openGL and was able to get a simple cube drawn in immediate mode with quads. But I tried moving the cube to a vertex array and drawing the quads and I seem to be having a bit of trouble. If someone could take a look and see if they can spot something obvious I am doing wrong I would really appreciate it.

 

I am using SDL and openGL to do my drawing. The following code is what I am trying to use to render the cube:

 

        //Clear the color buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glTranslatef(0.0f, 0.0f, -10.0f);
	glRotatef(rotation, 1.0f, 1.0f, 0.0f);
        
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        //glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
        glColorPointer(4, GL_FLOAT, 0, &myCube->colors);
        glVertexPointer(3, GL_FLOAT, 0, &myCube->vertices);

        glDrawArrays(GL_QUADS, 0, myCube->vertices.size());

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);

        glLoadIdentity();

         rotation -= 0.5f;

        //Update Screen    
        SDL_GL_SwapBuffers();

The code I am using to generate the cube vertices and colors are as follows:

 

void model::createCube(float size, float x, float y, float z, float r, float g, float b, float a)
{
	//find half distance
	float half = size / 2.0f;

	//create the top face vertices
	this->vertices.push_back(x-half);	this->vertices.push_back(y+half);	this->vertices.push_back(z+half);
	this->vertices.push_back(x+half);	this->vertices.push_back(y+half);	this->vertices.push_back(z+half);
	this->vertices.push_back(x+half);	this->vertices.push_back(y+half);	this->vertices.push_back(z-half);
	this->vertices.push_back(x-half);	this->vertices.push_back(y+half);	this->vertices.push_back(z-half);

	//Create the top & bottom  face vertex colors
	float tr, tb, tg;
	tr = r / 3.0f;
	tg = g / 3.0f;
	tb = b / 3.0f;
	

	//Add top face vertex colors
	for(int i=0; i < 4; i++){
		this->colors.push_back(tr);		this->colors.push_back(tg);		this->colors.push_back(tb);		this->colors.push_back(a);
	}

	//create the bottom face vertices
	this->vertices.push_back(x-half);	this->vertices.push_back(y-half);	this->vertices.push_back(z+half);
	this->vertices.push_back(x+half);	this->vertices.push_back(y-half);	this->vertices.push_back(z+half);
	this->vertices.push_back(x+half);	this->vertices.push_back(y-half);	this->vertices.push_back(z-half);
	this->vertices.push_back(x-half);	this->vertices.push_back(y-half);	this->vertices.push_back(z-half);

	//Add bottom face vertex colors
	for(int i=0; i < 4; i++){
		this->colors.push_back(tr);		this->colors.push_back(tg);		this->colors.push_back(tb);		this->colors.push_back(a);
	}

	//create the front face vertices
	this->vertices.push_back(x-half);	this->vertices.push_back(y+half);	this->vertices.push_back(z+half);
	this->vertices.push_back(x-half);	this->vertices.push_back(y-half);	this->vertices.push_back(z+half);
	this->vertices.push_back(x+half);	this->vertices.push_back(y-half);	this->vertices.push_back(z+half);
	this->vertices.push_back(x+half);	this->vertices.push_back(y+half);	this->vertices.push_back(z+half);

	//Add front face vertex colors
	for(int i=0; i < 4; i++){
		this->colors.push_back(r);		this->colors.push_back(g);		this->colors.push_back(b);		this->colors.push_back(a);
	}

	//create the back face vertices
	this->vertices.push_back(x-half);	this->vertices.push_back(y+half);	this->vertices.push_back(z-half);
	this->vertices.push_back(x-half);	this->vertices.push_back(y-half);	this->vertices.push_back(z-half);
	this->vertices.push_back(x+half);	this->vertices.push_back(y-half);	this->vertices.push_back(z-half);
	this->vertices.push_back(x+half);	this->vertices.push_back(y+half);	this->vertices.push_back(z-half);

	//Add back face vertex colors
	for(int i=0; i < 4; i++){
		this->colors.push_back(r);		this->colors.push_back(g);		this->colors.push_back(b);		this->colors.push_back(a);
	}

	//create the left face vertices
	this->vertices.push_back(x-half);	this->vertices.push_back(y+half);	this->vertices.push_back(z-half);
	this->vertices.push_back(x-half);	this->vertices.push_back(y-half);	this->vertices.push_back(z-half);
	this->vertices.push_back(x-half);	this->vertices.push_back(y-half);	this->vertices.push_back(z+half);
	this->vertices.push_back(x-half);	this->vertices.push_back(y+half);	this->vertices.push_back(z+half);

	//Create the left & right face vertex colors
	tr = r / 2.0f;
	tg = g / 2.0f;
	tb = b / 2.0f;

	//Add left face vertex colors
	for(int i=0; i < 4; i++){
		this->colors.push_back(tr);		this->colors.push_back(tg);		this->colors.push_back(tb);		this->colors.push_back(a);
	}

	//create the right face vertices
	this->vertices.push_back(x+half);	this->vertices.push_back(y+half);	this->vertices.push_back(z-half);
	this->vertices.push_back(x+half);	this->vertices.push_back(y+half);	this->vertices.push_back(z+half);
	this->vertices.push_back(x+half);	this->vertices.push_back(y-half);	this->vertices.push_back(z+half);
	this->vertices.push_back(x+half);	this->vertices.push_back(y-half);	this->vertices.push_back(z-half);

	//Add left face vertex colors
	for(int i=0; i < 4; i++){
		this->colors.push_back(tr);		this->colors.push_back(tg);		this->colors.push_back(tb);		this->colors.push_back(a);
	}
}

 

 

I have attached some images that show what I am getting rendered. When I try to use the color pointer it appears that only the top face is getting rendered correctly (Aside from the colors not being correct).

 

When I don't use a pointer to the color array and just use one color to render the cube I get the image in red.

 

Does anyone know where I am going wrong on this?

 

Thanks in advance!

 

Attached Thumbnails

  • cube colorp.JPG
  • cube flat.JPG


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8601

Like
1Likes
Like

Posted 10 March 2013 - 09:13 AM

If vertices is an object of type std::vector, then &vertices is a pointer to the std::vector object and not the data the std::vector stores. To get the pointer to the data, you need to use &vertices[0], or, if you're on a C++11 compiler, vertices.data(). Same with the color array, of course.


Edited by Brother Bob, 10 March 2013 - 09:14 AM.


#3 Wrathnut   Members   -  Reputation: 397

Like
0Likes
Like

Posted 10 March 2013 - 09:54 AM

Brother Bob,

 

You are the man!! Thank you again for helping me out. You are correct I was using std::vector to store my color and vertex data. I changed that for the color and the vertex arrays and it works perfectly!

 

Thanks again. I spent several hours last night and this morning trying to google the problem. Everything is working just like I expected it to now. :)

 

-Wrath






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS