Sign in to follow this  
PrestoChung

OpenGL Lighting sparse meshes

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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_[i] = (GLchar)file.get(); //Get character from file
if( !file.eof() ){
++i;
}
}

source_[i] = '\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);
}



Share this post


Link to post
Share on other sites
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?


Share this post


Link to post
Share on other sites
Ah, yes thanks for catching that. The function in the tutorial had source as a GLchar**, I have changed it to GLchar* and missed to change that part.

Doesn't seem to be related to my crash error though.

How do I check that my video card supports it? I have a newer card Geforce GTS250 i think it is.

Share this post


Link to post
Share on other sites
O, yea I have looked at that and GLEW has pulled in the proper extensions as far as I can see. I think it would tell me that the function is undefined if that was the problem.

Edit:
I just noticed in the debugger that the vshadesource and fshadesource pointers are still at 0 when I get to glShaderSourceARB so it must be a failure of of the loading function. I'll step through it and see what I can find out.

Share this post


Link to post
Share on other sites
Fixed by passing the source pointer by reference. It looks funny though
Sint32 LoadShader(char* filename_, GLchar*& source_)


Compiled successfully. Now I just need to get glDrawElements to work without crashing.

Edit: Ack! forgot to create the shader program :)

Share this post


Link to post
Share on other sites
It's working now but I'm a little confused because:

Even if my vert shader or frag shader does not compile, or contains and empty main function that does nothing, I still get some drawing.

I set 3 attrib pointers (interleaved), but nowhere do I specify that the first one is the position.

Does OpenGL automatically use the first attribute as a position vertex?

Otherwise I'm a little confused as to how it could know how to render my VBO data.

Share this post


Link to post
Share on other sites
Might this have something to do with it? Quote from http://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/attributes.php

Quote:
In other words, NVidia hardware indices are reserved for built-in attributes:
gl_Vertex 0
gl_Normal 2
gl_Color 3
gl_SecondaryColor 4
gl_FogCoord 5
gl_MultiTexCoord0 8
gl_MultiTexCoord1 9
gl_MultiTexCoord2 10
gl_MultiTexCoord3 11
gl_MultiTexCoord4 12
gl_MultiTexCoord5 13
gl_MultiTexCoord6 14
gl_MultiTexCoord7 15


When enable or set an attribute pointer
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TexNormVert), 0);

in this case attribute "0", whenever I use gl_Vertex in my shader source, it is looking at this attribute?

It seems to be so. When I use attribute 8 for the texture coordinate part of my Vertex
glEnableVertexAttribArray(8);
glVertexAttribPointer(8, 2, GL_FLOAT, GL_FALSE, sizeof(TexNormVert), (GLvoid*) (NULL + sizeof (GL_FLOAT) * 6) );

I suddenly get texture to display properly!

This shader code is generating an error C5052: gl_MultiTexCoord0 is not accessible in this profile

But at the same time the texture is rendering! I'm really mystified as to how this is possible.

vertshader.vert
void main()
{
gl_Position = ftransform();
}
fragshader.frag
uniform sampler2D tex;

void main()
{
gl_FragColor = texture2D(tex, gl_MultiTexCoord0);
}

Share this post


Link to post
Share on other sites
Edit: updated a bit. Still no drawing visible:

Here is setup after shaders are compiled:
Shader_Program = glCreateProgramObjectARB();
glAttachObjectARB(Shader_Program, vshadehandle);
glAttachObjectARB(Shader_Program, fshadehandle);

glBindAttribLocationARB( Shader_Program, 0, "inVertex" );
glBindAttribLocationARB( Shader_Program, 1, "inNormal" );
glBindAttribLocationARB( Shader_Program, 2, "inTexCoord0" );
glBindFragDataLocation( Shader_Program, 0, "outFragColor" );

glLinkProgramARB( Shader_Program );
glUseProgramObjectARB( Shader_Program );



Here is building VAO and VBO for each object:
glGenVertexArrays(1, &Vao_Handle);
glBindVertexArray(Vao_Handle);

glGenBuffersARB(1, &Vbo_Handle);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, Vbo_Handle);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vertices[0])*vertices.size(), &vertices[0], GL_STATIC_DRAW_ARB);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TexNormVert), 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_TRUE, sizeof(TexNormVert), (GLvoid*) (NULL + sizeof (GL_FLOAT) * 3) );
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(TexNormVert), (GLvoid*) (NULL + sizeof (GL_FLOAT) * 6) );

glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, Vbo_Element_Index);

glBindVertexArray(0);

Then the drawing step:
glBindVertexArray(Vao_Handle);
glDrawElements(GL_TRIANGLES, NumIndices(Portal_Walls.size()), GL_UNSIGNED_BYTE, 0);
glBindVertexArray(0);


And right now the shaders are like so:
/////////Vertex Shader
#version 150

uniform mat4 ProjectionModelviewMatrix;
in vec4 inVertex;
in vec2 inTexCoord0;

out vec2 outTexCoord0;

void main()
{
gl_Position = ProjectionModelviewMatrix * inVertex;
outTexCoord0 = inTexCoord0;
}
/////////////Fragment Shader
#version 150

uniform sampler2D tex;

in vec2 outTexCoord0;

out vec4 outFragColor;

vec4 mycolor;

void main()
{
mycolor = texture2D(tex, outTexCoord0);

outFragColor = mycolor;
}


Screen is blank, no drawing at all.

[Edited by - PrestoChung on December 12, 2010 8:22:58 AM]

Share this post


Link to post
Share on other sites
Since I can't use ftransform() I think I am not getting the Projection and Modelview Matrix to my shader properly.

I added this code before linking with:
glLinkProgramARB(Shader_Program)
GLfloat modelview[16]; 
glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
GLfloat projection[16];
glGetFloatv(GL_PROJECTION_MATRIX, projection);
GLuint loc;
GLuint loc2;
loc = glGetUniformLocationARB(Component::Shader_Program,"ModelViewMatrix");
glUniform4fvARB(loc,4,modelview);
loc2 = glGetUniformLocationARB(Component::Shader_Program,"ProjectionMatrix");
glUniform4fvARB(loc2,4,projection);
Now I think I'll have to call some of these functions every frame if the view has changed? This is all a bit new to me as before I just called
gluLookAt()
and never had to touch the matrices directly.

This changes the vert shader to:
#version 150

uniform mat4 ProjectionMatrix;
uniform mat4 ModelviewMatrix;

in vec4 inVertex;
in vec2 inTexCoord0;

out vec2 outTexCoord0;

void main()
{
gl_Position = ProjectionMatrix * ModelviewMatrix * inVertex;
outTexCoord0 = inTexCoord0;
}

Share this post


Link to post
Share on other sites
I suggest you upconvert your GL_UNSIGNED_BYTE to GL_UNSIGNED_SHORT. The reason is that I suspect it is not hw accelerated.
This page has the basic problems to avoid. It doesn't list about your case.


http://www.opengl.org/wiki/Common_Mistakes

Your case is here
http://www.opengl.org/wiki/Vertex_Arrays

and at the bottom it says, Use 16 bit integer.
I'll update the Common Mistakes page.

For your
gluLookAt()
glTranslatef()
and all that shit, you can use a library such as my glhlib
http://glhlib.sourceforge.net

but that's Windows 32 bit only.

There are others such as the GLM
and many others over at
http://www.gamedev.net/community/forums/topic.asp?topic_id=339189

My own glhlib looks like GL

float Matrix_3DNoiseTexture[16];
glhLoadIdentityf2(Matrix_3DNoiseTexture);
glhTranslatef2(Matrix_3DNoiseTexture, Translate3DNoiseTexture[0], Translate3DNoiseTexture[1], Translate3DNoiseTexture[2]);
glhRotateAboutZf2(Matrix_3DNoiseTexture, Rotate3DNoiseTexture[2]);
glhRotateAboutYf2(Matrix_3DNoiseTexture, Rotate3DNoiseTexture[1]);
glhRotateAboutXf2(Matrix_3DNoiseTexture, Rotate3DNoiseTexture[0]);
glhScalef2(Matrix_3DNoiseTexture, Scale3DNoiseTexture, Scale3DNoiseTexture, Scale3DNoiseTexture);



All the functions are there. There are also the SSE versions for mass data processing.

Share this post


Link to post
Share on other sites
Why would I want to get rid of glLookAt()?

I did change the indices to GLushort s but nothing happens still. They were working before.

Is this what I should call after updating the viewpoint?
CameraUpdate(){
glLoadIdentity();
gluLookAt( Pos_.X(), Pos_.Y(), Pos_.Z(),
Loo_.X(), Loo_.Y(), Loo_.Z(),
Up_.X(), Up_.Y(), Up_.Z() );

GLfloat modelview[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
GLfloat projection[16];
glGetFloatv(GL_PROJECTION_MATRIX, projection);
GLuint loc;
GLuint loc2;
loc = glGetUniformLocationARB(Component::Shader_Program,"ModelViewMatrix");
glUniform4fvARB(loc,4,modelview);
loc2 = glGetUniformLocationARB(Component::Shader_Program,"ProjectionMatrix");
glUniform4fvARB(loc2,4,projection);
}

Share this post


Link to post
Share on other sites
I noticed I have
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
before rendering and
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
after rendering.

Should these be removed if I am trying to use generic vertex attributes?

Share this post


Link to post
Share on other sites
One thing I noticed is a glGetError 1280 (invalid enum) that occurs after this line
glGetProgramivARB(Component::Shader_Program, GL_LINK_STATUS, &linked);
This would seem to indicate that GL_LINK_STATUS is not a valid enum for this function. Is there an ARB version of this enum? If so, what is it? GL_LINK_STATUS_ARB seems to be invalid.

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  

  • Forum Statistics

    • Total Topics
      627715
    • Total Posts
      2978782
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now