Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: gluperpective not working

Hybrid View

  1. #1
    Newbie Newbie
    Join Date
    Sep 2013
    Posts
    3

    gluperpective not working

    Hello, I am trying to draw a big sphere in background, so it is supposed to look small, but for some reason its just not working. I am a newbie in OpenGL. Can someone please tell me what am I doing wrong? I am drawing the sphere in render function, and translating it by -50 Z. Thank you very much.

    Code :
    #include <stdio.h>     // - Just for some ASCII messages
    #include "gl/glut.h"   // - An interface and windows 
    #include <math.h>                       //   management library
    #include "visuals.h"   // Header file for our OpenGL functions
     
     
    int angle = 0;
     
     
     
     
    void Render()
    {    
      	glClear(GL_COLOR_BUFFER_BIT);
    	glClear(GL_DEPTH_BUFFER_BIT);
     
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
     
    	///////////////////////////////////////////////////////// sphere in background
    	glColor4f(0.5, 0.5, 0.8,0.3);
    	glTranslatef(0.0, 0.0, -50);
    	makeSilver();	
    	glutSolidSphere (30, 50, 50);	
    	/////////////////////////////////////////////////////////// sphere in background
     
      drawTorus(angle); //
    drawSpheres(angle);	
    	glutSwapBuffers();
    }
     
     
    void drawTorus(int a)
    {
    	 glPushMatrix();
    	glColor3f(0.5, 0.5, 0.8);
    	glTranslatef(0.0, 50, 0.0);
    	 glRotatef(a, 0.0, 1.0, 0.0);
    	glutSolidTorus( 4, 16, 20, 15);	
    	glPopMatrix();
    }
     
    void drawSpheres(int a)
    {
    	 glPushMatrix();
    	glColor3f(1.0, 0.5, 0.2);	
    	makeCopper();
    	glRotatef(a, 0.0, 0.0, 1.0);
     
    	glPushMatrix();	
    	glTranslatef(-18.0, 14.0, 13.0);	
    	glutSolidSphere (10, 50, 50);
    	glPopMatrix();
     
    	glPushMatrix();
    	glTranslatef(14.0, -15.0, -16.0);
    	glutSolidSphere (10, 50, 50);
    	glPopMatrix();
     
    	glPushMatrix();
    	glTranslatef(-4.0, -27.0, 15.0);
    	glutSolidSphere (10, 50, 50);
    	glPopMatrix();
     
    	glPushMatrix();
    	glTranslatef(19, -28, 12.0);
    	glutSolidSphere (10, 50, 50);
    	glPopMatrix();
     
    	glPushMatrix();
    	glTranslatef(-17, 36.0,19.0);
    	glutSolidSphere (10, 50, 50);
    	glPopMatrix();
     
    	glPushMatrix();
    	glTranslatef(41.0, -19.0, 35.0);
    	glutSolidSphere (10, 50, 50);
    	glPopMatrix();
     
    	glPushMatrix();
    	glTranslatef(-35.0, -25.0, 0.0);
    	glutSolidSphere (10, 50, 50);
    	glPopMatrix();
     
    	glPushMatrix();
    	glTranslatef(30.0, 40.0, 0.0);
    	glutSolidSphere (10, 50, 50);
    	glPopMatrix();
    	glPopMatrix();
     
    }
     
    void makeCopper() /////////////////////////////////////// FOR COPPER TEXTURING FOR SMALL SPHERES
    {
     
    	GLfloat specref[4];
    	specref[0] = 0.247; specref[1] = 0.225; specref[2] = 0.065; specref[3] = 1.0;
    	glMaterialfv(GL_FRONT,GL_AMBIENT,specref);
    	specref[0] = 0.346; specref[1] = 0.314; specref[2] = 0.090; specref[3] = 1.0;
    	glMaterialfv(GL_FRONT,GL_DIFFUSE,specref);
    	specref[0] = 0.797; specref[1] = 0.724; specref[2] = 0.208; specref[3] = 1.0;
    	glMaterialfv(GL_FRONT,GL_SPECULAR,specref);
    	glMaterialf(GL_FRONT,GL_SHININESS,51.2);
     
    }
     
    void makeSilver()//////////////////////////////// FOR SILVER TEXTURING FOR SPHERE
    {
     
     
    		GLfloat specref[4];
    	specref[0] = 0.231; specref[1] = 0.231; specref[2] = 0.231; specref[3] = 1.0;
    	glMaterialfv(GL_FRONT,GL_AMBIENT,specref);
    	specref[0] = 0.275; specref[1] = 0.275; specref[2] = 0.275; specref[3] = 1.0;
    	glMaterialfv(GL_FRONT,GL_DIFFUSE,specref);
    	specref[0] = 0.774; specref[1] = 0.774; specref[2] = 0.774; specref[3] = 1.0;
    	glMaterialfv(GL_FRONT,GL_SPECULAR,specref);
    	glMaterialf(GL_FRONT,GL_SHININESS,59.5);
     
    }
     
     
     
    //-----------------------------------------------------------
     
    void Resize(int w, int h)
    {
    	if (h==0) h=1;
    	glViewport(0,0,w,h); 
     
    	// Setup viewing volume
     
    	glMatrixMode(GL_PROJECTION); 
    	//glLoadIdentity();	 
    ////(02b)	   
    	// L     R       B      T      N      F
    //	glOrtho (-50.0f, 50.0f, -50.0f, 50.0f,-500.0f,500.0f);
    	//glFrustum(-50.0f, 50.0f, -50.0f, 50.0f,-500.0f,500.0f);
    	float aspect = (float)w/(float)h;             /// aspect ratio
    	//glPushMatrix();
    	//glMatrixMode(GL_MODELVIEW ); 
    	gluPerspective(60.0, aspect, 1.0, 500.0);
     
    	//Resize(w,h);
    }
     
    void Idle()
    {
     
    if(angle+1 >360)
    	angle = 1;
    else
    	angle +=1;	
     
        glutPostRedisplay(); 
    }
     
     
    void Setup()
    { 
    	  glEnable( GL_CULL_FACE );
     
       glShadeModel( GL_SMOOTH );
     
       glEnable(GL_DEPTH_TEST);
       glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glDepthFunc( GL_LEQUAL );      
       glClearDepth(1.0); 	
       	glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
         glEnable(GL_COLOR_MATERIAL);
        glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
     
       //////////////////////////////////////////////////////////// IMPLEMENTATION OF LIGHT SOURCE////////////////////////////////////
    	GLfloat light_position[] = { 0.0, 0.0,50.0, 0.0 };
    	glLightfv( GL_LIGHT0, GL_POSITION, light_position);
     
    	GLfloat ambientLight[] = { 0.8, 0.8, 0.8, 1.0 };
    	GLfloat diffuseLight[] = { 0.5, 0.5, 0.5, 1.0 };
    	GLfloat specularLight[] = { 0.4, 0.4, 0.4, 1.0 };
     
     
    	glLightfv( GL_LIGHT0, GL_AMBIENT, ambientLight );
    	glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseLight );
    	glLightfv( GL_LIGHT0, GL_SPECULAR, specularLight );
     
     
    	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    	// polygon rendering mode and material properties
    	glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);  
     
    	glEnable( GL_LIGHTING );
    	glEnable( GL_LIGHT0);
     
     
    	glEnable(GL_BLEND);
       // incoming //  // stored //
       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     
     
    	// Black background
    	glClearColor(0.0f,0.0f,0.0f,1.0f);
    }

  2. #2
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    174
    Are you sure the background sphere isn't hidden behind the foreground spheres? If you don't draw any objects in the foreground does the background sphere show up?

  3. #3
    Newbie Newbie
    Join Date
    Sep 2013
    Posts
    3

    background sphere

    No, when I run the program, I can see the sphere in the background the problem is the size is not smaller the way it should be since it is in -50 Z axis.

  4. #4
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by the_hunter View Post
    No, when I run the program, I can see the sphere in the background the problem is the size is not smaller the way it should be since it is in -50 Z axis.
    I think that you're forgetting that all of the other spheres are being positioned relative to the coordinate system used for the first sphere.

    So the first sphere (the glutSolidSphere() call within Render() itself) is at -50, then the other spheres (the glutSolidSphere() calls within drawSpheres()) are at -50+13=-37, -50-16=-66, -50+15=-35, and so on.

  5. #5
    Newbie Newbie
    Join Date
    Sep 2013
    Posts
    3
    Actually I know that, but that is not the problem. The problem is the sphere looks same ( the same size) doesn't matter where I put it. Weather its in the front or the further in back its the same size which shouldn't be the case since the further away the object is the smaller it looks. Hope I explained the question better,so you can understand my question. Thank you for all your responses, but if you can please help me, that will be greatly appreciated. Thank you very much.

  6. #6
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by the_hunter View Post
    Actually I know that, but that is not the problem. The problem is the sphere looks same ( the same size) doesn't matter where I put it.
    Are you sure that the gluPerspective() call in Resize() is actually in effect? You haven't shown the entire program, so it's possible that something else is setting an orthographic projection. Is there any other code which affects the matrices?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •