Jump to content
  • Advertisement
Sign in to follow this  
AhmedSaleh

OpenGL Cube Class

This topic is 2530 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 write a cube class for opengl, which has features like translating, and rotating it , and drawing a specific texture to it. I would like to translate the cube if I pressed the Z button, but that doesn't work.


Cube::Cube()
{
_pos = Vec3f( 0.0f, 0.0f, 0.0f);
_rot = Vec3f( 0.0f, 0.0f, 0.0f);
}

Cube::Cube(Vec3f p, Vec3f r)
{
_pos = p;
_rot = r;
}
void Cube::Set_Position(Vec3f p)
{
_pos = p;
glPushMatrix();
ci::gl::translate(p);
glPopMatrix();


}
void Cube::draw(float s, float t)
{
const float sizex = 2.5f;
const float sizey = 2.5f;
const float sizez = 2.5f;
glPushMatrix();

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

// FRONT
glTexCoord2f(s, s);
glVertex3f(-sizex, -sizey, sizez);
glTexCoord2f(t, s);
glVertex3f(sizex, -sizey, sizez);
glTexCoord2f(t, t);
glVertex3f(sizex, sizey, sizez);
glTexCoord2f(s, t);
glVertex3f(-sizex, sizey, sizez);


// BACK
glTexCoord2f(s, s);
glVertex3f(-sizex, -sizey, -sizez);
glTexCoord2f(t, s);;
glVertex3f(-sizex, sizey, -sizez);
glTexCoord2f(t, t);
glVertex3f(sizex, sizey, -sizez);
glTexCoord2f(s, t);
glVertex3f(sizex, -sizey, -sizez);




// LEFT
glTexCoord2f(s, s);
glVertex3f(-sizex, -sizey, sizez);
glTexCoord2f(t, s);
glVertex3f(-sizex, sizey, sizez);
glTexCoord2f(t, t);
glVertex3f(-sizex, sizey, -sizez);
glTexCoord2f(s, t);
glVertex3f(-sizex, -sizey, -sizez);


// RIGHT
glTexCoord2f(s, s);
glVertex3f(sizex, -sizey, -sizez);
glTexCoord2f(t, s);
glVertex3f(sizex, sizey, -sizez);
glTexCoord2f(t, t);
glVertex3f(sizex, sizey, sizez);
glTexCoord2f(s, t);
glVertex3f(sizex, -sizey, sizez);



// TOP
glTexCoord2f(s, s);
glVertex3f(-sizex, sizey, sizez);
glTexCoord2f(t, s);
glVertex3f(sizex, sizey, sizez);
glTexCoord2f(t, t);
glVertex3f(sizex, sizey, -sizez);
glTexCoord2f(s, t);
glVertex3f(-sizex, sizey, -sizez);

// BOTTOM

glTexCoord2f(s, s);
glVertex3f(-sizex, -sizey, sizez);
glTexCoord2f(t, s);
glVertex3f(-sizex, -sizey, -sizez);
glTexCoord2f(t, t);
glVertex3f(sizex, -sizey, -sizez);
glTexCoord2f(s, t);
glVertex3f(sizex, -sizey, sizez);

glEnd();

ci::gl::translate(-1*_pos);
glPopMatrix();
}


Share this post


Link to post
Share on other sites
Advertisement

Your code does not show any keyboard handling at all, so we can't tell you why your key press doesn't work.



Sorry
Here is it


void ocvCaptureApp::keyDown( KeyEvent event )
{

if( event.getChar() == 'd' ) {

x+=10.0f;


cube[0][5].Set_Position(Vec3f(0,-x,0));
}

}



void ocvCaptureApp::draw()
{

if( ! mTexture )
return;
gl::clear( Color::black() );
mTexture.bind();

glPushMatrix();
gl::multModelView( mCubeTranslation );

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

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

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

// draw block
//drawCube(Vec3f(x1-5,-15.0f,y1+5),Vec3f(4.0f,4.0f,4.0f));
cube[row][col].Set_Position(Vec3f(x1,-15.0f,y1-40));
cube[row][col].draw(0,1);
} // end if
// advance column position
x1+=BLOCK_X_GAP;

} // end for col

// advance to next row position
y1+=BLOCK_Y_GAP;

} // end for row

glPopMatrix();


}


Share this post


Link to post
Share on other sites
Nothing should happen when you press Z, because your keyboard handler check for D. But ignoring that, I don't see how the position in the keyboard handler can have any effect on the cube. You do set a new position in the keyboard handler, but int he draw function, you set a new position for every cube you draw.

Share this post


Link to post
Share on other sites
Thanks for your reply.
Actually, I would like for example when I press any button, I move one specific cube in the grid .. so how do I do that ?

Share this post


Link to post
Share on other sites
Unrelated to your problem but this bit:

void Cube::Set_Position(Vec3f p)
{
_pos = p;
glPushMatrix();
ci::gl::translate(p);
glPopMatrix();
}


The push/pop/translate part does nothing. You push the matrix, translate it but then just pop it again (reverting the changes you just made). you should remove that part.

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!