Sign in to follow this  
Garra

create glow with GL_EMISSION

Recommended Posts

pretty much i have a solar system that I'm using to learn lighting. I want to make it so the sun has a glow using GL_EMISSION, but haven't figured out how. Right now i believe the emission is being put on all of the planets and not just the sun. I'm assuming to make it work properly i would have to redo the rotations of the planets and put each one in a push/pop in order to set the individual materials on them? Any help so i can better understand this would be great.

#include <GL\glut.h>

//set values for context menu
#define MENU_INCREASE	100
#define MENU_DECREASE	101
#define MENU_ENABLE		102
#define MENU_DISABLE	103


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

//set what to do when menu item is clicked
void menuCallback(int option)
{
	if(option == 100)
	{
		if (anum <= 1.0)
			anum += 0.1;//increase alpha
	}
	else if(option == 101)
	{
		if (anum >= 0.2)
			anum -= 0.1;//decrease alpha
	}
	else if(option == 102)
		glEnable(GL_DEPTH_TEST);//enable depth
	else if(option == 103)
		glDisable(GL_DEPTH_TEST);//disable depth

glutPostRedisplay();
}

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

	GLfloat light_pos[4] = {0.0, 0.0, 0.0, 1.0};
	GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat mat_shininess[] = { 50.0 };
	//GLfloat light_position[] = { 0.0, 0.2, 0.2, 1.0 };
	GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat lmodel_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
	GLfloat mat_emission[] = {0.5, 0.5, 0.0, 1.0};

	//glClearColor(0.0, 0.0, 0.0, 0.0);
	//glShadeModel(GL_SMOOTH);
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
	glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);

	glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
	glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
	//glLightfv(GL_LIGHT0, GL_EMISSION, mat_emission);

	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_BLEND);//enable blend
	glEnable(GL_DEPTH_TEST);//enable depth
	
	//function for alpha
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	glShadeModel(GL_SMOOTH);

}

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


//draw objects	
glPushMatrix();
		glColor4f(1, 1, 0, 1);
		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, 0, 1, anum);
		glutSolidSphere(0.4, 10, 8); /* draw earth*/ 
		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, 0.7, 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); //say we are using RGBA and DEPTH
	glutInitWindowSize(700, 400);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("JBOProj04A");
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	//creates and populates context menu
	glutCreateMenu(menuCallback);
	glutAddMenuEntry("Increase Earth Alpha", 100);
	glutAddMenuEntry("Decrease Earth Alpha", 101);
	glutAddMenuEntry("Enable Depth Test", 102);
	glutAddMenuEntry("Disable Depth Test", 103);

	glutAttachMenu(GLUT_RIGHT_BUTTON); //assigns context menu to right mouse button

	glutMainLoop();
	return 0;

}


Share this post


Link to post
Share on other sites
ok i modified the code so the emission only effects the sun, my next question is if GL_EMISSION only has the effect of a "inner glow" or if it can also give the sun a outer glow so the glow is around the sun? Updated code is below.



#include <GL\glut.h>

//set values for context menu
#define MENU_INCREASE 100
#define MENU_DECREASE 101
#define MENU_ENABLE 102
#define MENU_DISABLE 103

GLfloat mat_emission[] = {1.0, 1.0, 0.0, 1.0};
GLfloat no_mat[] = {0, 0, 0, 0};
GLfloat light_pos[4] = {0.0, 0.0, 0.0, 1.0};
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
//GLfloat light_position[] = { 0.0, 0.2, 0.2, 1.0 };
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lmodel_ambient[] = { 0.1, 0.1, 0.1, 1.0 };

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

//set what to do when menu item is clicked
void menuCallback(int option)
{
if(option == 100)
{
if (anum <= 1.0)
anum += 0.1;//increase alpha
}
else if(option == 101)
{
if (anum >= 0.2)
anum -= 0.1;//decrease alpha
}
else if(option == 102)
glEnable(GL_DEPTH_TEST);//enable depth
else if(option == 103)
glDisable(GL_DEPTH_TEST);//disable depth

glutPostRedisplay();
}

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

//glClearColor(0.0, 0.0, 0.0, 0.0);
//glShadeModel(GL_SMOOTH);
//glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
//glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);


glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
//glLightfv(GL_LIGHT0, GL_EMISSION, mat_emission);

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_BLEND);//enable blend
glEnable(GL_DEPTH_TEST);//enable depth

//function for alpha
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glShadeModel(GL_SMOOTH);

}

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


//draw objects
glPushMatrix();
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
glColor4f(1, 1, 0, 1);
glutSolidSphere(1.0, 20, 16); /* draw sun */
glPopMatrix();
glPushMatrix();
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);

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, 0, 1, anum);
glutSolidSphere(0.4, 10, 8); /* draw earth*/
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, 0.7, 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); //say we are using RGBA and DEPTH
glutInitWindowSize(700, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("JBOProj04A");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
//creates and populates context menu
glutCreateMenu(menuCallback);
glutAddMenuEntry("Increase Earth Alpha", 100);
glutAddMenuEntry("Decrease Earth Alpha", 101);
glutAddMenuEntry("Enable Depth Test", 102);
glutAddMenuEntry("Disable Depth Test", 103);

glutAttachMenu(GLUT_RIGHT_BUTTON); //assigns context menu to right mouse button

glutMainLoop();
return 0;

}



Share this post


Link to post
Share on other sites
Emission lights do not actually add any light to the scene, they only change the appearance of the material they are applied to. If you want the sun to glow, you need to add some other kind of light (maybe point light at center of the sun).

Share this post


Link to post
Share on other sites

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