Jump to content
  • Advertisement
Sign in to follow this  
AhmedSaleh

N*N Cubes Drawing

This topic is 2612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All,
I'm trying to draw an N*N Cubes in a grid. The following code works only for 4*4 and less, but can't work for more than that, I don't know why really.



// draw all the blocks
for (int row=0; row < mNumCubes; row++)
{
// draw this row of blocks
for (int col=0; col < mNumCubes; col++)
{
cube[row][col].draw(col/(float)mNumCubes,row/(float)mNumCubes, (1+col)/(float)mNumCubes,(1+row)/(float)(mNumCubes));
}
}


void Cube::draw(float s1, float t1, float s2, float t2)
{
const float sizex = 4.5f;
const float sizey = 4.5f;
const float sizez = 4.5f;
glPushMatrix();

ci::gl::translate(_pos);
ci::gl::rotate(_rot);
glBegin(GL_QUADS);

// FRONT
glTexCoord2f(s2, t1);
glVertex3f(-sizex, -sizey, sizez);
glTexCoord2f(s1, t1);
glVertex3f(sizex, -sizey, sizez);
glTexCoord2f(s1, t2);
glVertex3f(sizex, sizey, sizez);
glTexCoord2f(s2, t2);
glVertex3f(-sizex, sizey, sizez);


//// BACK
glTexCoord2f(s2, t1);
glVertex3f(-sizex, -sizey, -sizez);
glTexCoord2f(s1, t1);
glVertex3f(-sizex, sizey, -sizez);
glTexCoord2f(s1, t2);
glVertex3f(sizex, sizey, -sizez);
glTexCoord2f(s2, t2);
glVertex3f(sizex, -sizey, -sizez);


//

// LEFT
glTexCoord2f(s2, t1);
glVertex3f(-sizex, -sizey, sizez);
glTexCoord2f(s1, t1);
glVertex3f(-sizex, sizey, sizez);
glTexCoord2f(s1, t2);
glVertex3f(-sizex, sizey, -sizez);
glTexCoord2f(s2, t2);
glVertex3f(-sizex, -sizey, -sizez);


//// RIGHT
glTexCoord2f(s2, t1);
glVertex3f(sizex, -sizey, -sizez);
glTexCoord2f(s1, t1);
glVertex3f(sizex, sizey, -sizez);
glTexCoord2f(s1, t2);
glVertex3f(sizex, sizey, sizez);
glTexCoord2f(s2, t2);
glVertex3f(sizex, -sizey, sizez);

////
// // TOP
//glTexCoord2f(1, 0);
//glVertex3f(-sizex, sizey, sizez);
//glTexCoord2f(0, 0);
//glVertex3f(sizex, sizey, sizez);
//glTexCoord2f(0, 1);
//glVertex3f(sizex, sizey, -sizez);
//glTexCoord2f(1, 1);
//glVertex3f(-sizex, sizey, -sizez);


// TOP
glTexCoord2f(s2, t1);
glVertex3f(-sizex, sizey, sizez);
glTexCoord2f(s1, t1);
glVertex3f(sizex, sizey, sizez);
glTexCoord2f(s1, t2);
glVertex3f(sizex, sizey, -sizez);
glTexCoord2f(s2, t2);
glVertex3f(-sizex, sizey, -sizez);

//// BOTTOM
//
glTexCoord2f(s2, t1);
glVertex3f(-sizex, -sizey, sizez);
glTexCoord2f(s1, t1);
glVertex3f(-sizex, -sizey, -sizez);
glTexCoord2f(s1, t2);
glVertex3f(sizex, -sizey, -sizez);
glTexCoord2f(s2, t2);
glVertex3f(sizex, -sizey, sizez);

glEnd();


glPopMatrix();
}



Share this post


Link to post
Share on other sites
Advertisement
Hidden

Define "can't work."

I got where is the problem actually. To allocate the blocks, I'm defining it as in the following code, however I would like also to modify the size of the grid at run time also, but I don't know how to do it, and that was the main problem of not drawing more than 5*5 blocks.



static const int NUM_BLOCK_ROWS = 5;
static const int NUM_BLOCK_COLUMNS = 5;
unsigned char mCubes[NUM_BLOCK_ROWS][NUM_BLOCK_COLUMNS];
int mNumCubes;

void ocvCaptureApp::Init_Blocks()
{
// initialize the block field
for (int row=0; row < mNumCubes; row++)
for (int col=0; col < mNumCubes; col++)
{
cube[row][col]._rot=Vec3f(0,-90,0);
}

int x1 = BLOCK_ORIGIN_X, // used to track current position
y1 = BLOCK_ORIGIN_Y;

// draw all the blocks
for (int row=0; row < mNumCubes; row++)
{
// reset column position
x1 = BLOCK_ORIGIN_X;

// draw this row of blocks
for (int col=0; col < mNumCubes; col++)
{
// draw next block (if there is one)
if (mCubes[row][col]!=0)
{

cube[row][col].Set_Position(Vec3f(y1+20,-15.0f,40-x1));
} // end if
// advance column position
x1+=BLOCK_X_GAP;
} // end for col
// advance to next row position
y1+=BLOCK_Y_GAP;
} // end for row



} // end Init_Blocks






Share this post


Link to post

Define "can't work."

I got where is the problem actually. To allocate the blocks, I'm defining it as in the following code, however I would like also to modify the size of the grid at run time also, but I don't know how to do it, and that was the main problem of not drawing more than 5*5 blocks.



static const int NUM_BLOCK_ROWS = 5;
static const int NUM_BLOCK_COLUMNS = 5;
unsigned char mCubes[NUM_BLOCK_ROWS][NUM_BLOCK_COLUMNS];
int mNumCubes;

void ocvCaptureApp::Init_Blocks()
{
// initialize the block field
for (int row=0; row < mNumCubes; row++)
for (int col=0; col < mNumCubes; col++)
{
cube[row][col]._rot=Vec3f(0,-90,0);
}

int x1 = BLOCK_ORIGIN_X, // used to track current position
y1 = BLOCK_ORIGIN_Y;

// draw all the blocks
for (int row=0; row < mNumCubes; row++)
{
// reset column position
x1 = BLOCK_ORIGIN_X;

// draw this row of blocks
for (int col=0; col < mNumCubes; col++)
{
// draw next block (if there is one)
if (mCubes[row][col]!=0)
{

cube[row][col].Set_Position(Vec3f(y1+20,-15.0f,40-x1));
} // end if
// advance column position
x1+=BLOCK_X_GAP;
} // end for col
// advance to next row position
y1+=BLOCK_Y_GAP;
} // end for row



} // end Init_Blocks






Share this post


Link to post
Share on other sites
std::vector. If I was going to do what you're doing, I'd do it something like this:


int numBlockRows = 5;
int numBlockColumns = 5;
std::vector<std::vector<Cube> > cubes;

// To change the number of cubes during runtime, simply put new values
// into numBlockRows and numBlockColumns and call this fuction again.
// Note that this is not a complete version of the function. I didn't
// write all of it for you. I just wrote some of the basics. You have
// to fill it in with the right details.
void ocvCaptureApp::Init_Blocks()
{
cubes.clear();
for (int row = 0; row < numBlockRows; ++row)
{
cubes.push_back(std::vector<Cube>());

for (int col = 0; col < numBlockColumns; ++col)
{
cubes[row].push_back(Cube());

cubes[row][col].Set_Position(Vec3f(0.0f, 0.0f, 0.0f));
}
}
}


But let me critique your code, because I'm having nightmares looking at it right now.

This:
static const int NUM_BLOCK_ROWS = 5;
static const int NUM_BLOCK_COLUMNS = 5;
unsigned char mCubes[NUM_BLOCK_ROWS][NUM_BLOCK_COLUMNS];
int mNumCubes;

is kind of ok, but why are you making those things static?

This:
for (int row=0; row < mNumCubes; row++)
for (int col=0; col < mNumCubes; col++)
{
cube[row][col]._rot=Vec3f(0,-90,0);
}

is not ok. Notice how there are no curly braces after the first for loop? That's bad. Put curly braces around the first for loop's block. Also, you're looping from 0 to mNumCubes... what about NUM_BLOCK_ROWS and NUM_BLOCK_COLUMNS? What if mNumCubes is different than NUM_BLOCK_ROWS or NUM_BLOCK_COLUMNS? You'll overflow as you iterate through that.

This:
int x1 = BLOCK_ORIGIN_X, // used to track current position
y1 = BLOCK_ORIGIN_Y;

is weird. x1 is getting declared for it's first time (because it has "int" in front of it), but y1 does not... why? [edit] Oh, ok, I see now. That's a comma at the end of the line, not a semicolon. I missed that. But I still think it's difficult to see that, especially with the comment and inconsistent indenting. Perhaps declare them separately (they're on two lines now anyway)?

This:


// draw all the blocks
for (int row=0; row < mNumCubes; row++)
{
// reset column position
x1 = BLOCK_ORIGIN_X;

// draw this row of blocks
for (int col=0; col < mNumCubes; col++)
{

is bad. You are declaring two new variables named row and col, which is crazy confusing because you already used row and col variables in the first two for loops. I know, there isn't anything wrong with this, but, remember how you didn't put any curly braces around the first for loop? And how your indentation style makes it look like the rest of the code is part of the first for block, when it's really not? Yeah, it just makes things crazy confusing. If you're going to use row and col again, fix the first for loop. And the indentation. Otherwise it looks like this second row variable is in the same scope as the first row variable.

I hope my critique doesn't sound harsh. It's not intended to be. Most of it was written in a very rushed style because I had to go (family stuff) so I was hurrying. Edited by Cornstalks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!