Results 1 to 5 of 5

Thread: How To Penetrate Objects?

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2011
    Location
    Karachi
    Posts
    9

    How To Penetrate Objects?

    Can anyone help me to penetrate a cylinder in the cube(made up of Quads)?
    code is here.
    Code :
     #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;
    }
    M. Asif

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: How To Penetrate Objects?

    Quote Originally Posted by Muhammad Asif Hussain
    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.
    Am I doing your homework for you?

  3. #3
    Senior Member Regular Contributor
    Join Date
    Dec 2009
    Posts
    105

    Re: How To Penetrate Objects?

    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.

  4. #4
    Member Contributor
    Join Date
    Dec 2010
    Posts
    92

    Re: How To Penetrate Objects?

    I'm pretty certain depth test is always off by default just like backface culling is.

  5. #5
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: How To Penetrate Objects?

    Quote Originally Posted by Leith Bade
    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.
    Am I doing your homework for you?

Similar Threads

  1. depth buffer: far objects bleed through closer objects
    By geohoffman49431 in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 06-15-2004, 10:47 AM
  2. Replies: 4
    Last Post: 12-30-2003, 10:49 AM
  3. GLslang: no reuse of shader objects in other program objects?
    By Ostsol in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 12-29-2003, 11:22 PM
  4. Replies: 2
    Last Post: 05-24-2002, 06:21 AM
  5. removing objects and timing+objects movement
    By in forum OpenGL: Basic Coding
    Replies: 20
    Last Post: 07-23-2001, 11:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean