spheres not centered?

I am using this code to draw multiple copies of a sphere with increasing radii and decreasing alpha values


for(int i = 1; i <= num; i++)
{
double alpha = MaxAlpha - AlphaStep * i;
	
glPushMatrix();
					glTranslatef(Position_.getX(), Position_.getY(), Position_.getZ());
					
double radius = MinRadius + i*RadiusStep;
glColor4f(1.0f, 1.0f, 0.0f, alpha); //yellow
//draw colored sphere
GLUquadricObj *quadratic;
quadratic = gluNewQuadric();
					gluQuadricNormals(quadratic, GLU_SMOOTH);
gluSphere(quadratic, radius, SphereParams, SphereParams);
					
glPopMatrix();

The result is shown here:
http://rpi.edu/~doriad/screenshot.jpg

If you look at the lower right side, you can see the layers that are produced. However, if you look at the upper left side, these layers are not visible. It’s almost like the spheres are not all centered at the same location (I expected cirlces showing this same laying all the way around).

Does anyone see a silly bug or have an explanation for why it looks like this?

Also, even if I set the alpha value very low (for example here I used alpha = .01) and then draw even as little as 5 of these spheres on top of each other, the texture underneath is almost completely covered up. Notice the dark yellow band around the outside - inside of that there is a texture (http://rpi.edu/~doriad/sun.png) that is supposed the be the main part of the image and then these other spheres are supposed to create a glow.

It’s been mentioned to use GLSL to do this, but it seems like a reasonable effect should be able to be achieved with much less effort this way.

I am using this for blending:
glEnable (GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

Thoughts/comments?

Thanks,

Dave

Do you call glEnable(GL_CULL_FACE) somewhere.

nope - should I ?

Yes, see this thread.

I added GL_CULL_FACE and saw no change.

I am also calling:

glEnable(GL_DEPTH_TEST);
and
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

as recommended in the link you just posted, so whats the next thing to try?

Dave

I will try your sample code to see.

I tried without the glTranslatef and it work. Can you comment the glTranslatef to see.

nope, same thing - actually in this case Position_ is (0,0,0), so that isn’t doing anything at all. What do you mean it worked? Can you post a screenshot?

Try this:


#include <iostream>
#include <GL/glut.h>
#include <cmath>

double MaxAlpha = 0.5;
double AlphaStep = 0.02;
double MinRadius = 1.0;
double RadiusStep = 0.2;
int num = 10;

void display(){
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	gluLookAt(5,5, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
        
        for(int i = 1; i <= num; i++)
        {
          double alpha = MaxAlpha - AlphaStep * i;
	
          glPushMatrix();
          //glTranslatef(Position_.getX(), Position_.getY(), Position_.getZ());
					
          double radius = MinRadius + i*RadiusStep;
          glColor4f(1.0f, 1.0f, 0.0f, alpha); //yellow
//draw colored sphere
          GLUquadricObj *quadratic;
          quadratic = gluNewQuadric();
          gluQuadricNormals(quadratic, GLU_SMOOTH);
          gluSphere(quadratic, radius, 10, 10);
					
          glPopMatrix();
        }
        /*
	glutSolidSphere(3.0, 32, 32);
        */
	glutSwapBuffers();
}

void init(void){
	glClearColor (1.0, 1.0, 1.0, 0.0);
	glShadeModel (GL_SMOOTH);
	//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE); 
  glCullFace(GL_BACK);
  GLfloat diffuse[] = { 1.0, 0.0, 0.0, 0.3 };
  glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
}

void reshape(int w, int h){
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, 1.0*w/h, 1.5, 200.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void idle(void){
  glutPostRedisplay();
}

int main(int argc, char** argv){
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
   glutInitWindowSize (800, 600);
   glutInitWindowPosition (100, 100);
   glutCreateWindow("OpenGL");
   init();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutIdleFunc(idle);
   glutMainLoop();
   return 0;
}

Edit: Oups I forget to change the alpha colors of the material.

Replace the constants with this


double MaxAlpha = 0.7;
double AlphaStep = 0.05;
double MinRadius = 0.1;
double RadiusStep = 0.2;
int num = 10;

Replace the drawing code with this:


void display(){
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	gluLookAt(5,5, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
        
        for(int i = 1; i <= num; i++)
        {
          double alpha = MaxAlpha - AlphaStep * i;
	
          glPushMatrix();
          //glTranslatef(Position_.getX(), Position_.getY(), Position_.getZ());
					
          double radius = MinRadius + i*RadiusStep;
         // glColor4f(1.0f, 1.0f, 0.0f, alpha); //yellow
          float diffuse[4] =  {1.0f, 1.0f, 0.0f, alpha};
          glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
//draw colored sphere
          GLUquadricObj *quadratic;
          quadratic = gluNewQuadric();
          gluQuadricNormals(quadratic, GLU_SMOOTH);
          gluSphere(quadratic, radius, 10, 10);
					
          glPopMatrix();
        }
        /*
	glutSolidSphere(3.0, 32, 32);
        */
	glutSwapBuffers();
}

yea that worked - I realized I had a directed light on, and when i turned it off the spheres appeared centered as they should be - very odd…

I tried using the diffuse[] instead of the color() and I get this:
http://rpi.edu/~doriad/sun.jpg

Whats the difference between setting the material and setting the color?

Thanks for the help!

Dave

Whats the difference between setting the material and setting the color?

Maybe you will find the answer here and here. Notice that the lighting is done with the fixed function pipeline of OpenGL.

Have you tried with a different blending function? Maybe you can try glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);.