Sign in to follow this  

Transparency issue

This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm trying to figure out how to make transparency work correctly on this. Pretty much its a simple solar system... i'm working on making the sun so its half transparent and then the earth so the alpha value is adjustable with the context menu. However, when the earth/moon is behind the sun i'm not able to see them through the sun. Any help would be great.
#include <GL\glut.h>

#define MENU_OPTION1	100
#define MENU_OPTION2	101
#define MENU_OPTION3	102
#define MENU_OPTION4	103

static int year = 0, days = 0, day = 0, mday = 0;
double anum = 1.0;
float winWidth, winHeight;

void menuCallback(int option)
{
	if(option == MENU_OPTION1)
	{
		if (anum <= 1.0)
			anum += 0.1;
	}
	else if(option == MENU_OPTION2)
	{
		if (anum >= 0.2)
			anum -= 0.1;
	}
	else if(option == MENU_OPTION3)
		glEnable(GL_DEPTH_TEST);
	else if(option == MENU_OPTION4)
		glDisable(GL_DEPTH_TEST);

glutPostRedisplay();
}

void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);


	glEnable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);
	
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	glShadeModel(GL_FLAT);

}

void display(void)
{
	//glClear(GL_COLOR_BUFFER_BIT);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


	
glPushMatrix();
		glColor4f(1, 1, 0, 0.5f);
		glutSolidSphere(1.0, 20, 16); /* draw sun */
		glRotatef((GLfloat) year, 0.0, 1.0, 0.0);
		glTranslatef(2.0, 0.0, 0.0);
		glRotatef((GLfloat) day, 0.0, 1.0, 0.0);
		glColor4f(0, 1, 0, anum);
		glutSolidSphere(0.3, 10, 8); /* draw earth planet */ 

		glRotatef((GLfloat) year, 0.0, 1.0, 0.0);
		glTranslatef(0.7, 0.0, 0.0);
		glRotatef((GLfloat) mday, 0.0, 1.0, 0.0);
		glColor4f(1, 1, 1, 1);
		glutSolidSphere(0.2, 10, 8); /* draw moon */ 
	glPopMatrix();


	glutSwapBuffers();

}
void reshape(int w, int h)
{
	winWidth = w;
	winHeight = h;

	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}


void keyboard(unsigned char key, int x, int y)
{
	float aspect;

	switch (key) 
	{
	case 'd':
		//days+=1.f/360.f;

	year = (year + 5) % 360;
	day = (day + 1) % 360;
	mday = (mday + 28) % 360;
	glutPostRedisplay();
	
	break;
	case 'D':
	
	year = (year + 5) % 360;
	day = (day + 10) % 360;
	mday = (mday + 1) % 28;

	glutPostRedisplay();
	
	break;
	case 'y':	
	year = (year + 5) % 360;
	glutPostRedisplay();
	
	break;
	case 'o':
		//glOrtho can burn. the end.
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		aspect = (float)winWidth/ (float)winHeight;
		//glOrtho(-30, 90, -90, 80, -90, 100);
		glOrtho(-50, 50,-50, 50, 20, -80);
		
		//glOrtho(-0.5,0.5,-0.5,0.5,-1.0,1.0);
		//glOrtho(
		//glOrtho(
		
		//glOrtho( 0.0f, 800.0f, 600.0f, 0.0f, 0.0f, -2.0f );
		//glOrtho(-80,80,-80,10,40,100);
		//glOrtho( 0.0f, 800.0f, 600.0f, 0.0f, 0.0f, -2.0f );
		glMatrixMode(GL_MODELVIEW);
		glutPostRedisplay();
		
	break;
	case 'p':
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(60.0, (float)winWidth/ (float)winHeight, 1, 300);
		glMatrixMode(GL_MODELVIEW);
		glutPostRedisplay();
			
	break;
	default:

	break;
	}
}


int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(700, 400);
	glutInitWindowPosition(100, 100);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutCreateMenu(menuCallback);
	glutAddMenuEntry("Increase Earth Alpha", MENU_OPTION1);
	glutAddMenuEntry("Decrease Earth Alpha", MENU_OPTION2);
	glutAddMenuEntry("Enable Depth Test", MENU_OPTION3);
	glutAddMenuEntry("Disable Depth Test", MENU_OPTION4);

	glutAttachMenu(GLUT_RIGHT_BUTTON);

	glutMainLoop();
	return 0;

}

Share this post


Link to post
Share on other sites

This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this