Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Wrathnut

Member Since 02 Feb 2000
Offline Last Active May 15 2013 11:41 AM
-----

Topics I've Started

Rendering Multiple models problem

12 March 2013 - 01:01 PM

Hi,

 

I think this problem might be related to the one I had the other day. But after an exhaustive search I am still not sure what I am doing wrong. Or if I am trying to render correctly.

 

I was able to get a model to render properly. So my next step was to try to make an stl::vector of different models and try to render them in the same way. When I do this I get some strange effects. I have attached an image of what is going on. I basically arranged a bunch of cubes into a square. It renders the cubes for the most part. (it seems to have messed up the color of one of them though) And then it also draws some extra stuff. I am guessing I am not addressing my models correctly but I am unsure how to do it.

 

I am also wondering if I am even using the right method to draw my models. The code below is when I am using to loop through my vector list of models and try to draw them:

 

        glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	
	for(int i=0; i < this->cubes.size(); i++){
		glColorPointer(4, GL_FLOAT, 0, &this->cubes[i]->colors[0]);
		glVertexPointer(3, GL_FLOAT, 0, &this->cubes[i]->vertices[0]);
		glNormalPointer(GL_FLOAT, 0, &this->cubes[i]->normals[0]);

		glDrawArrays(GL_QUADS, 0, this->cubes[i]->vertices.size());
	}

	

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);

 

 

Any help would be greatly appreciated!

 

 


Vertex Arrays

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!

 


PARTNERS