Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
ShinAli
May 2, 2003

The Kid better watch his step.
I've got an OpenGL graphics assignment problem where I draw 3D objects which may have concave faces, and one method I would have to do it in is via drawing the faces using the stencil buffer.

Right now, my implementation only draws one face, and for the life of me I cannot figure out why. A for-loop will iterate through a 3D object struct that contains the faces and pass them into this function:

code:
void gldisplay2(struct poly * p)
{
   struct poly *q;
   int i,j;
   
   glEnable(GL_STENCIL_TEST);
   glStencilFunc(GL_ALWAYS, 0x1, 0x1);
   glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);
   glClear(GL_STENCIL_BUFFER_BIT);
   glClearStencil(0x0);
   glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
   struct vector ori;
   struct vector pt1;
   struct vector pt2;
   q = p->f[0].p;
   ori.x = q->origin.x + q->f[0].t * q->d.x;
   ori.y = q->origin.y + q->f[0].t * q->d.y;
   ori.z = q->origin.z + q->f[0].t * q->d.z;
   // draw the triangles using GL_INVERT to get a stencil
   // of the face
   for(i = 1; i < p->curnum; i++) {
	q = p->f[i].p;
        pt1.x = q->origin.x + q->f[0].t * q->d.x;
        pt1.y = q->origin.y + q->f[0].t * q->d.y;
        pt1.z = q->origin.z + q->f[0].t * q->d.z;
	pt2.x = q->origin.x + q->f[1].t * q->d.x;
        pt2.y = q->origin.y + q->f[1].t * q->d.y;
        pt2.z = q->origin.z + q->f[1].t * q->d.z;
	if(!(PTEQU(ori, pt1)) && !(PTEQU(ori, pt2))) {
		//glNormal3f(p->n.x, p->n.y, p->n.z);
		glBegin(GL_POLYGON);
		glVertex3f(ori.x, ori.y, ori.z);
		glVertex3f(pt1.x, pt1.y, pt1.z);
		glVertex3f(pt2.x, pt2.y, pt2.z);
		glEnd();
	}
   }
   // redraw the triangles via color buffer
   glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glStencilFunc(GL_EQUAL, 0x1, 0x1);
   glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
   for(i = 1; i < p->curnum; i++) {
	q = p->f[i].p;
        pt1.x = q->origin.x + q->f[0].t * q->d.x;
        pt1.y = q->origin.y + q->f[0].t * q->d.y;
        pt1.z = q->origin.z + q->f[0].t * q->d.z;
	pt2.x = q->origin.x + q->f[1].t * q->d.x;
        pt2.y = q->origin.y + q->f[1].t * q->d.y;
        pt2.z = q->origin.z + q->f[1].t * q->d.z;
	if(!(PTEQU(ori, pt1)) && !(PTEQU(ori, pt2))) {
		//glNormal3f(p->n.x, p->n.y, p->n.z);
		glBegin(GL_POLYGON);
		glVertex3f(ori.x, ori.y, ori.z);
		glVertex3f(pt1.x, pt1.y, pt1.z);
		glVertex3f(pt2.x, pt2.y, pt2.z);
		glEnd();
	}
   }
   glDisable(GL_STENCIL_TEST);
}
I know it's really inefficient but I'm just trying to make this damned thing work. It's pretty much the exact method that the redbook talks about in chapter 14.

Adbot
ADBOT LOVES YOU

ShinAli
May 2, 2003

The Kid better watch his step.
I don't know if I asked before in this thread but I'll go ahead.

How would I go about making multiple lights in my phong lighting shader? What I've done is have a uniform array attribute which I pass lighting information like position/direction/size/type of a fixed size (say 100) and loop through about a 100 times through the array. From this thread I've heard that a variable loop is pretty bad so I kept it fixed at 100 and put in an if statement to see if the current element is enabled. If there are more than a 100 lights, I just render the scene again with the lights it didn't go through and blend it with the previous rendered scene.

I'm not sure if this is the right way, and if you guys want I'll put up the source code. It has some if statements in it anyways and I'm not sure how well shaders handle branching.

I'd also liked to know how to handle attenuation of spot lights as everywhere I looked, they seem to use fixed values. I assumed I'd just linearly make less light depending on the distance but went with the fixed attenuation values.

ShinAli
May 2, 2003

The Kid better watch his step.

Unormal posted:

If you can give up blended transparency look into using 'deferred shading', it's complicated but oh so good for lots of lights. (and there's ways to get transparency back if you really want it)

That's exactly what I've been using, and I seem to be able to go to about a 1000 lights before it slows down below 30 fps. I just don't know if I'm doing it right.

ShinAli
May 2, 2003

The Kid better watch his step.

OneEightHundred posted:

angle = dot(normalize(point - lightOrigin), normalize(lightDirection))
attenuation = saturate((angle - cosMaxAngle) / (cosMinAngle - cosMaxAngle))

Min angle = Minimum angle where the light source stops being visible at full intensity (might still be visible through a diffuser)
Max angle = End of the fade-out angle, i.e. the angle where neither the light source nor diffusers are visible.

Argh, I actually meant point lights but this is still helpful as I need to implement spot lights anyways.

For point lights, I'd assume you'd use two "sizes" where one is full intensity and the other is where the fall off would end. Would I just measure up the fall off size in some proportion of the intensity size? I'm trying to think on how to use angles as a part of this but it would seem that I need to know the range of the light anyways before I can take the angle into consideration.

Unormal posted:

Generally if you're using a deferred shader, you shouldn't be branching in a single shader, you should be rendering a single quad (or sphere or whatever) per light volume.

I mostly wanted to batch as many lights as possible in a single pass to avoid doing a draw call for every light. Does it not matter as much if I do one light per pass?

haveblue posted:

Very, very poorly. Avoid if at all possible.

Depending on what you are doing it may be faster to evaluate both branches and multiply the one you don't want to use by zero before combining it with the final result.

Actually I don't know why I didn't think of that, as I use a 1.0 for on and 0.0 for off.

ShinAli fucked around with this message at 22:37 on Jul 15, 2011

ShinAli
May 2, 2003

The Kid better watch his step.
Not really a programming question, but I'd like to know what you know-more-than-I-dos think.

I remember awhile back some ATi guy saying something like how much it'd be better if developers were able to directly program to the GPU rather than depending on the graphics company's implementation of a library, with a chorus of major engine developers saying that would be pretty awesome, then the ATi guy immediately back peddling and going "HEY WHAT I MEANT TO SAY WAS OH BOY ISN'T DIRECTX JUST GREAT?"

Has there been much consideration about going towards this route, given that graphics companies agree to some common instruction set? Would there be too many drawbacks? Given that everyone agrees on a instruction set, we could still have all those libraries except the development of them could be more transparent; more than that, developers don't have to be so restricted to them.

Is this just a case where its what everyone wants, its just nVidia and AMD/ATi wouldn't go along with it? Or is this thinking flawed?

ShinAli
May 2, 2003

The Kid better watch his step.

Woz My Neg rear end posted:

Formally don't-write-in-new-code deprecated, or just out of favor? I thought they were still better for frequently updated buffers.

I would think they'd be about the same performance as vertex arrays are always sent over to the GPU's memory.

ShinAli
May 2, 2003

The Kid better watch his step.
Where you get low level info like that? Just from experience?

Adbot
ADBOT LOVES YOU

ShinAli
May 2, 2003

The Kid better watch his step.

Heliotic posted:

holy poo poo

Should texOut in the vertex shader be texCoord? That or rename texCoord in the fragment program to texOut?

ShinAli fucked around with this message at 05:06 on Feb 19, 2013

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply