GPWiki.org
GPWiki.org
It is currently Sat May 25, 2013 10:57 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Need an OpenGL expert
PostPosted: Tue Aug 21, 2012 4:17 pm 
Novice

Joined: Sat Jul 21, 2012 3:45 am
Posts: 5
Hello to any who might of decided to at least read this. I'm in the process of learning OpenGL and I' am having so much trouble >:( >:( . To say the least I'm not happy about my many failures thus far but moving on. I'm trying to use phong shading, and a number of other things, but lets focus on shading. I have been to countless sites and online guides and every single one of them has the same problem. They all throw a bunch of information at me without explaining the code at all.

Here is an example chunk of source code from one such site:

struct gl_LightSourceParameters
{
vec4 ambient; // Aclarri
vec4 diffuse; // Dcli
vec4 specular; // Scli
vec4 position; // Ppli
vec4 halfVector; // Derived: Hi
vec3 spotDirection; // Sdli
float spotExponent; // Srli
float spotCutoff; // Crli
// (range: [0.0,90.0], 180.0)
float spotCosCutoff; // Derived: cos(Crli)
// (range: [1.0,0.0],-1.0)
float constantAttenuation; // K0
float linearAttenuation; // K1
float quadraticAttenuation;// K2
};

uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];

The above code supposidly has something to do with phong shading in OpenGL. I won't deny that, mostly because I don't know, but the site says nothing about how this code works. Its great that glLightSource[] is a built in array, wonderful :) . However, I would like to know how the code works like for instance why is vec4 ambient or vec4 specular in the code; what is their purpose? What is float spotCosCutoff for and whats up with with the 3 floaters on the bottom; what does any of this stuff do >:( >:( :spin !!! The site, as well as the others I visited, all failed to explain any of the code they used as well as its syntax and I'm sick of it.

Please, those reading this forgive my bad attitude, I just...I'm so tired of trying to learn this stuff and not making any progress :( .

Anyway, if there is someone on this forum who can truly help me or give me advice then please do so. If you know of a guide, a good one, send a link. My thanks goes to any who try to help, Thank You :) .


Top
 Profile  
 
PostPosted: Tue Aug 21, 2012 4:23 pm 
Novice

Joined: Sat Jul 21, 2012 3:45 am
Posts: 5
By the way I forgot to mention I learn best by first seeing how something is done right at least once and then I take from that. Keep that in mind when you reply.

Once again thanks to anyone who can help.


Top
 Profile  
 
PostPosted: Tue Aug 21, 2012 7:37 pm 
Shake'n'Baker

Joined: Sun May 27, 2012 6:01 pm
Posts: 62
Hi Nabunta,

Im sorry, but the code you have posted has nothing specifically to do with Phong shading. You have posted the structure and attributes of all light sources that you can access in your shader code.

Here is some code for a phong shader[1]

Code:
varying vec3 N;
varying vec3 v;   
void main (void) 

   vec3 L = normalize(gl_LightSource[0].position.xyz - v);   
   vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0) 
   vec3 R = normalize(-reflect(L,N)); 
 
   //calculate Ambient Term: 
   vec4 Iamb = gl_FrontLightProduct[0].ambient;   

   //calculate Diffuse Term: 
   vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(N,L), 0.0);
   Idiff = clamp(Idiff, 0.0, 1.0);     
   
   // calculate Specular Term:
   vec4 Ispec = gl_FrontLightProduct[0].specular
                * pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
   Ispec = clamp(Ispec, 0.0, 1.0);

   // write Total Color: 
   gl_FragColor = gl_FrontLightModelProduct.sceneColor + Iamb + Idiff + Ispec;     
}


Notice "gl_LightSource[0].position.xyz" ? This is accessing the first lightsource's position. Which was listed in the struct you posted :)

To help with understanding the phong shader, check out the wiki page http://en.wikipedia.org/wiki/Phong_shad ... tion_model

The code above calculates the Ambient, Diffuse, and Specular terms, and then adds them together at the end (gl_FragColor = ..).

Mikey

[1] http://www.opengl.org/sdk/docs/tutorial ... ghting.php


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group