Results 1 to 6 of 6

Thread: My First 3D Application

  1. #1
    Member Contributor
    Join Date
    May 2008
    Posts
    67

    My First 3D Application

    Hi there, I have taken an existing 2D app which just draws a box, and I am trying to add depth to it. It draws a quad on the plane, then it trys ( and fails ) to draw a box of distance -5 units away. (So it will look smaller by my calculations).

    The app isn't drawing the second box... I cannot for the life of me figure out why..

    Here is the code:

    Code :
    #include <iostream>
    #include <windows.h>
    #include <gl/gl.h>
    #include <gl/glu.h>
    #include <gl/glut.h>
     
     
    const int windowWidth=300;
    const int windowHeight=400;
    using namespace std;
     
    void display(void) // draws top to bottom
    {
     
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
        glLoadIdentity();
     
        glColor3f (1.0, 0.0, 0.0);	// (red,green,blue) colour components
     
        glBegin(GL_QUADS); // draw quad on surface
            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(0.5, 0.0, 0.0);
            glVertex3f(0.5, 0.5, 0.0);
            glVertex3f(0.0, 0.5, 0.0);
        glEnd();
     
     
        glColor3f (0.0, 1.0, 0.0);
        glTranslatef(0, 0.6, -5); //has to be called outside of glBegin & end * 0.0f in third arg is default
     
        glPushMatrix();
     
        glBegin(GL_QUADS); // try and draw quad which is further away and thus will look smaller..
            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(0.5, 0.0, 0.0);
            glVertex3f(0.5, 0.5, 0.0);
            glVertex3f(0.0, 0.5, 0.0);
        glEnd();
     
        glPopMatrix();
     
        glutSwapBuffers(); // Need to use when glutdisplaymode(GLUT_DOUBLE) instead of glflush();
    }
     
    void reshape(int w, int h) {
        GLdouble halfWidth=(GLdouble) w/2.0;
    	GLdouble halfHeight=(GLdouble) h/2.0;
     
        glViewport(0,0,w,h);
     
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
     
    	//instead of gluortho2D
        // Calculate The Aspect Ratio Of The Window
    	gluPerspective(0.0f,(GLfloat)w/(GLfloat)h, 0.1f, 100.0f); //FOV Y, aspect, Znear, Zfar
    	//1: 45.0 = 45degrees above all objects drawn.. so 90 would be looking down on the objects
    	//2: always w/h radio
    	//3: The cloest objects we will draw ( not clip )
    	//4: the furtherest objects we will draw ( not clip )
    	//NOTE : gluPerspective means sets the entire window size to 2.0f, so objects need to be draw in proportion to this.
     
    	glMatrixMode(GL_MODELVIEW);      //It is used to move objects around scene
        glLoadIdentity();
     
     
    }
     
    void init(void)
    {
     
    	glClearColor (1.0, 1.0, 1.0, 0.0);	// RGB-value for white
        glViewport(0,0,windowWidth, windowHeight);
     
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
     
    	gluPerspective(0.0f,(GLfloat)windowWidth/(GLfloat)windowHeight,0.0f,100.0f); //FOV Y, aspect, Znear, Zfar
     
        glEnable(GL_DEPTH_TEST);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
     
     
    }
     
    int main(int argc, char** argv)
    {
    	glutInit(&amp;argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    	glutInitWindowSize(windowWidth, windowHeight);
    	glutInitWindowPosition(100, 100);
    	glutCreateWindow("My first OpenGL program");
    	init ();						// initialise view
    	glutDisplayFunc(display);		// draw scene
    	glutReshapeFunc(reshape);
    	glutMainLoop();
    	return 0;
    }

  2. #2
    Senior Member OpenGL Pro Zengar's Avatar
    Join Date
    Sep 2001
    Location
    Germany
    Posts
    1,931

    Re: My First 3D Application

    try +5 in translate...

  3. #3
    Member Contributor
    Join Date
    May 2008
    Posts
    67

    Re: My First 3D Application

    yeh tried that.. doesn't work

  4. #4
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: My First 3D Application

    Why do you set a 0 to the fov parameter??

    i don't don't know how gluPerspective handles that, but that implies a division by zero.

  5. #5
    Member Contributor
    Join Date
    May 2008
    Posts
    67

    Re: My First 3D Application

    Ok well I have changed that 0.1f...does anyone actually have any idea what the problem is? The snippet will compile fine....

  6. #6
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: My First 3D Application

    No I am talking about the 1st parameter 'fild of view", it should not be 0. Typically it is set to 45.0f

    and talking about near plane, set a greater value that 0, 0.1 would be fine at first.

    EDIT:

    and look at this to konw what I am talking about...

Similar Threads

  1. Copy back buffer from one application and use in another application
    By murrdpirate in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 06-24-2014, 12:26 PM
  2. First application, red book
    By onlyzanna in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 04-27-2014, 01:42 PM
  3. application not using shaders :((((
    By AdamVO in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-04-2014, 08:45 PM
  4. Use a texture from another application
    By NemsOne in forum OpenGL: Basic Coding
    Replies: 22
    Last Post: 07-04-2011, 07:04 AM
  5. RC's in MDI application
    By hElios in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 12-11-2000, 05:32 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