Skip to main content
gamedev.net
🔒 Locked

Light Class Problem

Started by hpesoj 3 replies 600+ views
Original Post
hpesoj
hpesoj
The lights in OpenGL are based on the constants GL_LIGHTi, and in my light class I was hoping to return a handle to the light just created. Like... myLight = new lightClass(); Only to do this, within my constructor I would need to find a free light (I could store which lights have been created in an array or something), but for that I need to be able to dynamically change the i in the constant (ie. loop through the constants GL_LIGHT0, GL_LIGHT1, GL_LIGHT2 etc.). Is there anyway to do this? If there isn't I can always just pass a light constant into the constuctor, but I just thought this way would be in-keeping with the rest of my classes. Thanks.
Joseph Thomson - CLS Productions
hplus0603
hplus0603
You could either make a resource manager, configure it with the eight available light constants in start-up, and "check out" and "check in" lights from the light class as necessary.

OR you could take advantage of the fact that GL_LIGHTi is the same as ((GLenum)GL_LIGHT0+i) for i in [0,8).

That being said, it seems like you're assuming that you'll only ever draw to a single context. That means you can have globals or singletons for everything (like the light resource manager). However, if you want to do offscreen rendering, or use more than one window, or other multi-context things, then you probably want to encapsulate your state and context handling into a GraphicsState class, which then should serve as the factory for lights, textures, etc within its context.
enum Bool { True, False, FileNotFound };
hpesoj
hpesoj
Thanks for the reply.

State and context handling? What does that mean?

I'm guessing what you mean by the graphics state class is that I should store several versions of the light layout, and switch between them for rendering to different contexts. I don't see how this applies to textures though, and wouldn't that mean that I should have all my 3D object and sprite classes etc. in the same class, otherwise my scene will be the same apart from the lights (which seems pointless). I don't get why I would have to change my lights between contexts :S.
Joseph Thomson - CLS Productions
JoeCoder
JoeCoder
In my engine, I have a linked list that allows me to add in as many lights as I want. Whenever I render an object, I do a check to find which lights are within range of it and then enable 0-8 lights each with the properties of the closest lights found. Well, it actually only does the 8 closest lights, but I plan for it to take into account things like brightness, etc. in the future. It may not be the best way, but here's a sample of what I'm doing...

	// Create linked list of lights in range	// Should perhaps make this list include only the 8 brightest lights?	list<cLight>::iterator clht = Scene->Light.begin();	// Loop through all lights;	int lightnum = 0;	while ((clht != Scene->Light.end()) && (lightnum<8))	{	float dist = Dist3D(obj->vloc, obj->vloc) - obj->scale*SR2;		// Light is in range		if (clht->radius > dist)		{	glPushMatrix();			glTranslatef(-obj->vloc[0], -obj->vloc[1], -obj->vloc[2]);						// Set light position			int drc;  // directional light?			if (clht->arc<0) drc = 0; else drc = 1;			float position[] = { clht->vloc[0], clht->vloc[1], clht->vloc[2], (float)drc};			glLightfv(GL_LIGHT0+lightnum, GL_POSITION, position);			// Set light color			glLightfv(GL_LIGHT0+lightnum, GL_DIFFUSE, clht->color);		// Set Diffuse Color			glLightfv(GL_LIGHT0+lightnum, GL_AMBIENT, clht->ambient);	// Set Ambient Color						glLightfv(GL_LIGHT0+lightnum, GL_SPECULAR, clht->color);	// Specular (defined by material)			// Spot Light			if (drc==1)  glLightf(GL_LIGHT0+lightnum, GL_SPOT_CUTOFF, clht->arc);			else         glLightf(GL_LIGHT0+lightnum, GL_SPOT_CUTOFF, 180);			glLightfv(GL_LIGHT0+lightnum, GL_SPOT_DIRECTION, clht->direction);						// Set light attenuation			float attn = 1.0f/clht->radius;			glLightfv(GL_LIGHT0+lightnum, GL_QUADRATIC_ATTENUATION, &attn);			glEnable (GL_LIGHT0+lightnum);			glPopMatrix();						lightnum++;		}		clht++;	}// Disable Other Lights    for (int i=lightnum; i<8; i++)		glDisable(GL_LIGHT0+i);	
------------------------------yage3d.net—a free 3D game engine written in D.listpod.net—a community powered web 2.0 site of lists.
kburkhart84
kburkhart84
I have a simple light system class for openGL lights. You just say which light you are using. That is good and bad. You have to remember which lights you are using, but you have pretty good control of it.


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.