Sign in to follow this  
PrestoChung

OpenGL Lighting sparse meshes

Recommended Posts

PrestoChung    334
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
PolyVox    712
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
PrestoChung    334
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
PrestoChung    334
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
blubberbert    236
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
V-man    813
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
PrestoChung    334
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
blubberbert    236
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
PrestoChung    334
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
PrestoChung    334
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
PrestoChung    334
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
PrestoChung    334
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
PrestoChung    334
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
PrestoChung    334
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
PrestoChung    334
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
V-man    813
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
PrestoChung    334
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
PrestoChung    334
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
PrestoChung    334
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  

  • Similar Content

    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
  • Popular Now