Lighting sparse meshes

Started by
25 comments, last by PrestoChung 13 years, 4 months ago
Is there some way to tweak OpenGL lighting to give better results for sparse meshes? There are very obvious planes of light & dark since lighting is calculated per-vertex.

I would prefer not to subdivide the meshes but if that's the only way...

Example
Advertisement
You should investigate per-pixel lighting. Instead of doing your lighting calculations in the vertex shader you do them in the pixel shader. You'll need to make a few extra tweaks, like interpolating the normal across the triangle so you have a per-pixel normal available.
Is this a good tutorial on the subject? http://www.lighthouse3d.com/opengl/glsl/

It sounds like I have to somehow make my own vertex and fragment shader? It also sounds like it depends on what version of OpenGL I have.

gluGetString(GL_VERSION) tells me I am running 1.2.2.0 Microsoft Corporation

I would assume my machine/OS is a higher version, and this 1.2.2.0 version is the one I'm working with in my program?

And what does glGetString (GL_VERSION) give you? gluGetString is something completely different: http://msdn.microsoft.com/en-us/library/dd368660%28VS.85%29.aspx

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

glGetString(GL_VERSION) gets me 3.3.0.

So is that the version of GL I'm using in my program? Or does that return the version of GL running on my machine at runtime?
Right now I draw with these calls:
void DrawVbo()const{		glBindBufferARB(GL_ARRAY_BUFFER_ARB, Vbo_Vert_Id);		glVertexPointer(3, GL_FLOAT, 32, 0);		glNormalPointer (GL_FLOAT, 32, (GLvoid*) (NULL + sizeof (float) * 3));		glTexCoordPointer (2, GL_FLOAT, 32, (GLvoid*) (NULL + sizeof (float) * 6));		glDrawElements(GL_TRIANGLES, NumIndices(Portal_Walls.size()), GL_UNSIGNED_BYTE, 0);	}

Is this usage compatible with using fragment shader?

Do I need to use a vertex shader if I am using a fragment shader? The vertexes are fine, I just want the fragment shader.

Do I need to change to glVertexAttribPointerARB() instead?
Quote:Original post by PrestoChung
Is this usage compatible with using fragment shader?

of course.
however you should change gl*Pointer to glVertexAttribPointer (i think its not necessary tho)

actually i dont know wether you need a vertex shader, as i have never encountered a situation where i only needed a fragment shader.

LightHouse3D GLSL tutorial is quite good btw ;)
EDIT:
Quote:IF you have a pair vertex/fragment of shaders you'll need to attach both to the program.

from lighthouse3d tut... looks like you dont need both. just try it.
but youll need a vertex shader anyway for interpolating the normal :PP
------------------------------
Join the revolution and get a free donut!
Quote:Original post by blubberbert
of course.
however you should change gl*Pointer to glVertexAttribPointer (i think its not necessary tho)


Nope, it is not necessary, but it is a good idea, since generic vertex attributes are modern.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Thanks.

Any clue as to why glShaderSourceARB is crashing here? I adapted the file loading from this tutorial I had to change some things. Here's my adapted code:

inline Uint32 GetFileLength(std::ifstream& file_){	if(!file_.good()){		return 0;	}	file_.seekg(0, std::ios::end);	Uint32 len = file_.tellg();	file_.seekg(std::ios::beg);	return len;}Sint32 LoadShader(char* filename_, GLchar* source_){	std::ifstream file;	GLint len;	file.open(filename_, std::ios::in);  //open ASCII	if(!file){		return -1;	}	len = GetFileLength(file);	if( len==0 ){		return -2;  //Is empty	}	source_ = new GLchar[len+1];	if ( *source_==0 ){		return -3;  //Memory not reserved	}	source_[len] = '\0';	Uint32 i = 0;	while( file.good() ){		source_ = (GLchar)file.get();  //Get character from file		if( !file.eof() ){			++i;		}	}	source_ = '\0';  //0-terminate at correct position	file.close();	return 0;  //No errors}CompileShaders(){	GLhandleARB vshadehandle;	GLhandleARB fshadehandle;	GLcharARB* vshadesource=0;	GLcharARB* fshadesource=0;	vshadehandle = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB );	fshadehandle = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB );	LoadShader("vertshader.vert", vshadesource );	LoadShader("fragshader.frag", fshadesource );	glShaderSourceARB(vshadehandle, 1, (const GLcharARB**)&vshadesource, NULL); //<-----CRASH	glShaderSourceARB(fshadehandle, 1, (const GLcharARB**)&fshadesource, NULL);	glCompileShaderARB(vshadehandle);	glCompileShaderARB(fshadehandle);}
check wether the extension is supported on your graphics card(it should be o.0)

anyway check your file loading code... it looks strange to me
Quote:Original post by PrestoChung
if ( *source_==0 ){
return -3; //Memory not reserved
}

are you sure you want the content of source?? because after a new[] you dont know whats in there so why check it for 0?


------------------------------
Join the revolution and get a free donut!

This topic is closed to new replies.

Advertisement