PDA

View Full Version : How To Penetrate Objects?



Muhammad Asif Hussain
02-09-2011, 11:40 AM
Can anyone help me to penetrate a cylinder in the cube(made up of Quads)?
code is here.

#include <GL/glut.h>
#include <GL/gl.h>
float angle;
float length, width, height, constx,consty, constz;
double xLocation, yLocation, zLocation,x,y,z;
void display()
{angle=100;

length=0.5; //x
width=0.5; //y
height=0.5; //z

constx=length;
consty=width;
constz=height;
xLocation=0.0; yLocation=0.0; zLocation=0.0;
x=0; y=0; z=-0.2;
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslated(xLocation, yLocation, zLocation);
glRotatef(angle, 0.5, 0.5, 0.0f);

glBegin(GL_QUADS);

// glTranslated(xLocation, yLocation, zLocation);
// FACE ONE!!
glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
glVertex3f(length+x, consty+y, 0.0+z);
glVertex3f(0.0+x, consty+y, 0.0+z);
glVertex3f(0.0+x, consty+y, height+z);
glVertex3f(length+x, consty+y, height+z);

//glTranslated(xLocation, yLocation, zLocation);
// FACE TWO!!
glColor4f(1.0f, 0.0f, 1.0f, 1.0f);//glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
glVertex3f(constx+x, width+y, 0.0+z);
glVertex3f(constx+x, width+y, height+z);
glVertex3f(constx+x, 0.0+y, height+z);
glVertex3f(constx+x, 0.0+y, 0.0+z);

// glTranslated(xLocation, yLocation, zLocation);
// FACE THREE!!
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);//glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
glVertex3f(length+x, width+y, constz+z);
glVertex3f(0.0+x, width+y, constz+z);
glVertex3f(0.0+x, 0.0+y, constz+z);
glVertex3f(length+x, 0.0+y, constz+z);

// glTranslated(xLocation, yLocation, zLocation);
// FACE FOUR!!
glColor4f(1.0f, 1.0f, 0.0f, 1.0f);//glColor4f(0.0f, 0.0f, 1.0f, 1.0f); yellow =facefour
glVertex3f(length+x, width+y, 0.0+z);
glVertex3f(0.0+x, width+y, 0.0+z);
glVertex3f(0.0+x, 0.0+y, 0.0+z);
glVertex3f(length+x, 0.0+y, 0.0+z);

// glTranslated(xLocation, yLocation, zLocation);
// FACE FIVE!!
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);//glColor4f(1.0f, 1.0f, 0.0f, 1.0f); green=face five
glVertex3f(constx+x, width+y, 0.0+z);
glVertex3f(constx+x, width+y, height+z);
glVertex3f(constx+x, 0.0+y, height+z);
glVertex3f(constx+x, 0.0+y, 0.0+z);

// glTranslated(xLocation, yLocation, zLocation);
// FACE SIX!!
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);//glColor4f(0.0f, 1.0f, 1.0f, 1.0f); balck=face six
glVertex3f(length+x, 0.0+y, 0.0+z);
glVertex3f(0.0+x, 0.0+y, 0.0+z);
glVertex3f(0.0+x, 0.0+y, height+z);
glVertex3f(length+x, 0.0+y, height+z);

glEnd();

glutSwapBuffers();
}

void rotation(int timer)
{
angle += 0.5f;
glutTimerFunc(1000 / 50, rotation, timer);
}

void framerate(int timer)
{
glutPostRedisplay();
glutTimerFunc(1000 / 100, framerate, timer);
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(600, 600);
glutCreateWindow("dimension3");
glEnable(GL_BLEND);
glutDisplayFunc(display);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
//glutTimerFunc(1000 / 100, framerate, 1);
//glutTimerFunc(1000 / 50, rotation, 2);
glutMainLoop();
return 0;
}

MaxH
02-09-2011, 04:59 PM
Can anyone help me to penetrate a cylinder in the cube(made up of Quads)? Good idea to post the code. I ran it and it worked (sort of). First step is to be able to correctly display a single cube. That's not quite working in your code. Have you been able to rotate your cube with an Idle function? By setting 'angle = 100' in your 'display' function you are preventing rotation from taking place. Comment out that line and set angle = 0.0 at the top of your code (i.e. NOT in a subroutine). Then give each face of the cube a different color. Once you get the cube to rotate automatically you'll see there's a problem. Let us know if you get that far.

NeXEkho
02-09-2011, 06:31 PM
You never enable depth testing, so the depth buffer isn't used (at least from my experience, I think it's meant to be on by default but never seems to be) and although you've commented it out matrix operations (such as your translate) cannot be used between begin/end.

Leith Bade
02-09-2011, 09:14 PM
I'm pretty certain depth test is always off by default just like backface culling is.

MaxH
02-09-2011, 10:22 PM
I'm pretty certain depth test is always off by default just like backface culling is. Right. That's why he has to let his cube rotate AND give each face a different color.
Then he'll clearly see the need for enabling depth testing.