PDA

View Full Version : Light at objects



DarkShadow44
10-30-2009, 09:13 AM
Sorry for this silly post, but lights (with AND without shader) are created for ALL objects, or not ?

How can I set for which objects they work ?

And how can I use more than 8 Lights ? Or only 8 for all/one object ?

How could I give the lights to my shader ?

Sorry but I couldn't figure this out...

Aleksandar
10-30-2009, 10:53 AM
Sorry for this silly post, but lights (with AND without shader) are created for ALL objects, or not ?There is no silly questions! :)


How can I set for which objects they work ?For the FFP (without shaders) it can be achieved by using glEnable()/glDisable(). Each particular light-source can be enabled or disabled by glEnable(GL_LIGHTx)/glDisable(GL_LIGHTx). For example use


glEnable(GL_LIGHT1);
//... DrawSomeObjectsWithLight1Effects();
glDisable(GL_LIGHT1);
//... DrawSomeObjectsWithoutLight1Effects();
//... etc

If you want to disable all light sources, than use


glDisable(GL_LIGTING);
//... DrawSomeObjectsWithoutLight();



And how can I use more than 8 Lights ? Or only 8 for all/one object ? For the FFP only 8! But it is also expensive. Tell me, in what situation you would like to use more than 3 light sources? If there is such need, try to simulate those lights. It is common case to create a texture to simulate lighting effects. It is much cheaper than having lights. Nowadays hardware is strong enough to deal with enormous scenes with multiple lights, but my advice is not to waste the power. :)



How could I give the lights to my shader ?It depends on the version of the GLSL. If you are using GLSL up to the version 1.20, then you can get the parameters of the light-sources through build-in uniform variables gl_LightSource[]. The number of light sources is confined to the gl_MaxLights, which is the same value as in FFP, hence 8. In GLSL 1.30 and up, you are free to organize lighting as you like. There are limitations in the number of variables you can have, but you'll probably experience performance degradation before depletion of memory resources for the variables.


Sorry but I couldn't figure this out... Did I clarify anything, or make things even worse? :)

DarkShadow44
10-30-2009, 11:23 AM
First thanks for your answers, they helped very much ^^

I want ot create one or two lights for a house, and draw this a few times (different houses and each a few times), so light would be nice.

Could I limit lights for the radius of my house ?

And how I set the lights with shaders if I would use more than 8 ? I mean, the shader is executed for every model...

Another question, my lights just can make models(textured) more dark, not brighter, but why ?

But now if the problem that my shader doesn't move my models :mad:

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

Sorry that doesn't belong here...

Aleksandar
10-30-2009, 02:11 PM
Could I limit lights for the radius of my house ? In FFP this can be done by setting attenuation parameters, but I suggest to relocate lights when moving to another house, keeping the total number of lights to just one or two.


And how I set the lights with shaders if I would use more than 8 ? I mean, the shader is executed for every model...In GLSL 1.3+ you don't have lights at all! Neither vertices, nor normals... Just attributes! And you can interpret them as you like. So, you can fill the buffer with thousands of coordinates and assume that those are lights' positions and do the lighting calculations.


Another question, my lights just can make models(textured) more dark, not brighter, but why ?You are probably "modulating" color of the objects with the textures, and the material is too dark, or the amount of light that shade the objects is not enough to make them bright.


But now if the problem that my shader doesn't move my models :mad:

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

This part I didn't understand.

DarkShadow44
10-30-2009, 02:52 PM
You mean if I move from one house to another to ""turn the light off" ? But what if there are more houses on the screen ?

You mean, in the shader lights are like vectors ?

If I set the light to maximum (1.0, 1.0, 1.0), then my model is as bright as if lightning is turned off :(


If I use shaders instead of FFP, my model couldn't be translated (I mean moved by "glTranslatef"). All calls to "glTranslatef" and "glRotatef" change nothing...

Could you possibly show your code of loading shaders ??
Because my programm crashes with some shaders at "glBegin" (and my models are "frozen")

Aleksandar
10-30-2009, 03:18 PM
You mean if I move from one house to another to ""turn the light off" ? But what if there are more houses on the screen ?Set the lights and draw one house, than move lights to another position and draw another house etc.


You mean, in the shader lights are like vectors ?Put whatever you need for lighting into some uniforms and use the data to calculate lighting.


If I set the light to maximum (1.0, 1.0, 1.0), then my model is as bright as if lightning is turned off :(That's mean your lighting does not work at all. Post some code to take a look.


If I use shaders instead of FFP, my model couldn't be translated (I mean moved by "glTranslatef"). All calls to "glTranslatef" and "glRotatef" change nothing...Definitely I need to see the code.


Could you possibly show your code of loading shaders ??
Because my programm crashes with some shaders at "glBegin" (and my models are "frozen")
http://sites.google.com/site/opengltutorialsbyaks/

DarkShadow44
10-30-2009, 04:06 PM
Set the lights and draw one house, than move lights to another position and draw another house etc.

That works ?

I wanted to create a spot light: (that should be max. brightness)

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

GLfloat spot_direction[] = {0.0, 0.0, -5.0};
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,
spot_direction);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 90);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0);

GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

GLfloat light_ambient[] = {1, 1, 1, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);

GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);

Here is my vertex shader:

void main(void)
{

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord1;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Here is my shader loading code:

Sorry that it is so long but I have no clue what could be wrong :o


GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

glViewport(0,0,width,height); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix

}


GLhandleARB ProgramObject;
GLhandleARB VertexShaderObject;
GLhandleARB FragmentShaderObject;


void CheckError(GLhandleARB glObject) ;
GLhandleARB my_program;

int LoadShaderv(const char* name);
int LoadShaderf(const char* name);

int ShaderVertex;
int ShaderFragment;

GLuint texture;

int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_TEXTURE_2D);

GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
//fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
//fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));

// Create Shader And Program Objects
my_program = glCreateProgramObjectARB();

// VertexShaderObject = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
//FragmentShaderObject = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);


//LoadShader("geo_shdr.s",GL_GEOMETRY_SHADER_EXT);

int ShaderVertex=LoadShaderv("vert_shdr.s");
int ShaderFragment=LoadShaderf("frag_shdr.s");

// Link The Program Object
glLinkProgramARB(my_program);
CheckError(my_program);

// Use The Program Object Instead Of Fixed Function OpenGL
if(use)
{
glUseProgramObjectARB(my_program);
}

//int uColor , uDepth;
//uColor = glGetUniformLocationARB(s2,"tex1");
//uDepth = glGetUniformLocationARB(s2,"tex2");


//glUniform1i(uColor,1);
// glUniform1i(uDepth,0);

BMPClass bmp;

BMPLoad("C:/!data/dat.bmp",bmp);



glGenTextures(1, &texture);

glBindTexture(GL_TEXTURE_2D, texture);

//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (GLfloat) anisotropic);

// opj_dinfo_t* JPEG;
//JPEG=opj_create_decompress(CODEC_JP2);



glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
// glTexImage2D(GL_TEXTURE_2D, 0, 3, bmp.width, bmp.height, 0, GL_RGB, GL_UNSIGNED_BYTE, bmp.bytes);
glTexImage2D(GL_TEXTURE_2D, 0, 3, bmp.width, bmp.height, 0, GL_RGB, GL_UNSIGNED_BYTE, bmp.bytes);
return TRUE; // Initialization Went OK
}

char* data;

void readShader(FILE*file)
{
fseek(file, 0, SEEK_END);
int size=ftell(file);
//size=filelength(file);
data=(char*)malloc(size);

fseek(file,-size, SEEK_CUR); //zum Anfang zurück

fread(data,size,1,file);
data[size]='\0';
}

void CheckError(GLhandleARB glObject)
{
int blen,slen ;
char* InfoLog;

glGetObjectParameterivARB(glObject, GL_OBJECT_INFO_LOG_LENGTH_ARB , &blen);
if (blen > 1)
{
InfoLog=(char*)malloc( blen*sizeof(GLhandleARB));
glGetInfoLogARB(glObject, blen, &slen, InfoLog);
MessageBox(hWnd, InfoLog,NULL,NULL);
free(InfoLog);
}
}
int LoadShaderf(const char* name)
{


FILE *file=fopen(name,"rb");

readShader(file);
char* shader_data2=data;

fclose(file);

GLhandleARB shader;

shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);



// Load Shader Sources
glShaderSourceARB(shader, 1, (const GLcharARB**)&shader_data2, NULL);

// Compile The Shaders
glCompileShaderARB(shader);
CheckError(shader);

// Attach The Shader Objects To The Program Object
glAttachObjectARB(my_program, shader);


return shader;
}

int LoadShaderv(const char* name)
{


FILE *file=fopen(name,"rb");

readShader(file);
char* shader_data2=data;

fclose(file);

GLhandleARB shader;

shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);



// Load Shader Sources
glShaderSourceARB(shader, 1, (const GLcharARB**)&shader_data2, NULL);

// Compile The Shaders
glCompileShaderARB(shader);
CheckError(shader);

// Attach The Shader Objects To The Program Object
glAttachObjectARB(my_program, shader);


return shader;
}

float time=0;

float right=0;
float up=0;



int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glTranslatef(right,0,0);
glRotatef(up*10,0.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis ( NEW )

glTranslatef(right,0,0);

glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_TRIANGLES); // Start Drawing A Triangle

glTexCoord2f(1.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)

glTexCoord2f(2.0f,1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)

glTexCoord2f(3.0f,0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f); // Right Of Triangle (Front)

glEnd();
time+=0.01;
return TRUE; // Keep Going
}



GLvoid KillGLWindow(GLvoid) // Properly Kill The Window
{
if (fullscreen) // Are We In Fullscreen Mode?
{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
}

if (hRC) // Do We Have A Rendering Context?
{
if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
{
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}

if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL; // Set RC To NULL
}

if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // Set DC To NULL
}

if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL
}

if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class
{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL
}
}

/* This Code Creates Our OpenGL Window. Parameters Are: *
* title - Title To Appear At The Top Of The Window *
* width - Width Of The GL Window Or Fullscreen Mode *
* height - Height Of The GL Window Or Fullscreen Mode *
* bits - Number Of Bits To Use For Color (8/16/24/32) *
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */

BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc; // Windows Class Structure
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=(long)width; // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height

fullscreen=fullscreenflag; // Set The Global Fullscreen Flag

hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = hInstance; // Set The Instance
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
wc.hbrBackground = NULL; // No Background Required For GL
wc.lpszMenuName = NULL; // We Don't Want A Menu
wc.lpszClassName = "OpenGL"; // Set The Class Name

if (!RegisterClass(&wc)) // Attempt To Register The Window Class
{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (fullscreen) // Attempt Fullscreen Mode?
{
DEVMODE dmScreenSettings; // Device Mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings )); // Makes Sure Memory's Cleared
dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWID TH|DM_PELSHEIGHT;

// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLS CREEN)!=DISP_CHANGE_SUCCESSFUL)
{
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE
}
else
{
// Pop Up A Message Box Letting User Know The Program Is Closing.
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE; // Return FALSE
}
}
}

if (fullscreen) // Are We Still In Fullscreen Mode?
{
dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
dwStyle=WS_POPUP; // Windows Style
ShowCursor(FALSE); // Hide Mouse Pointer
}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
}

AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size

// Create The Window
if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
"OpenGL", // Class Name
title, // Window Title
dwStyle | // Defined Window Style
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN, // Required Window Style
0, 0, // Window Position
WindowRect.right-WindowRect.left, // Calculate Window Width
WindowRect.bottom-WindowRect.top, // Calculate Window Height
NULL, // No Parent Window
NULL, // No Menu
hInstance, // Instance
NULL))) // Dont Pass Anything To WM_CREATE
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
bits, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};

if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

ShowWindow(hWnd,SW_SHOW); // Show The Window
SetForegroundWindow(hWnd); // Slightly Higher Priority
SetFocus(hWnd); // Sets Keyboard Focus To The Window
ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen

if (!InitGL()) // Initialize Our Newly Created GL Window
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

return TRUE; // Success
}

LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
UINT uMsg, // Message For This Window
WPARAM wParam, // Additional Message Information
LPARAM lParam) // Additional Message Information
{
switch (uMsg) // Check For Windows Messages
{
case WM_ACTIVATE: // Watch For Window Activate Message
{
// LoWord Can Be WA_INACTIVE, WA_ACTIVE, WA_CLICKACTIVE,
// The High-Order Word Specifies The Minimized State Of The Window Being Activated Or Deactivated.
// A NonZero Value Indicates The Window Is Minimized.
if ((LOWORD(wParam) != WA_INACTIVE) && !((BOOL)HIWORD(wParam)))
active=TRUE; // Program Is Active
else
active=FALSE; // Program Is No Longer Active

return 0; // Return To The Message Loop
}

case WM_SYSCOMMAND: // Intercept System Commands
{
switch (wParam) // Check System Calls
{
case SC_SCREENSAVE: // Screensaver Trying To Start?
case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
return 0; // Prevent From Happening
}
break; // Exit
}

case WM_CLOSE: // Did We Receive A Close Message?
{
PostQuitMessage(0); // Send A Quit Message
return 0; // Jump Back
}

case WM_KEYDOWN: // Is A Key Being Held Down?
{
keys[wParam] = TRUE; // If So, Mark It As TRUE
return 0; // Jump Back
}

case WM_KEYUP: // Has A Key Been Released?
{
keys[wParam] = FALSE; // If So, Mark It As FALSE
return 0; // Jump Back
}

case WM_SIZE: // Resize The OpenGL Window
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height
return 0; // Jump Back
}
}

// Pass All Unhandled Messages To DefWindowProc
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, // Instance
HINSTANCE hPrevInstance, // Previous Instance
LPSTR lpCmdLine, // Command Line Parameters
int nCmdShow) // Window Show State
{
MSG msg; // Windows Message Structure
BOOL done=FALSE; // Bool Variable To Exit Loop

// Ask The User Which Screen Mode They Prefer
//if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=FALSE; // Windowed Mode
}

// Create Our OpenGL Window
if (!CreateGLWindow("NeHe's Solid Object Tutorial",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}

while(!done) // Loop That Runs While done=FALSE
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
done=TRUE; // If So done=TRUE
}
else // If Not, Deal With Window Messages
{
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
else // If There Are No Messages
{
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was There A Quit Received?
{
done=TRUE; // ESC or DrawGLScene Signalled A Quit
}
else // Not Time To Quit, Update Screen
{
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
}

if (keys[VK_F1]) // Is F1 Being Pressed?
{
keys[VK_F1]=FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window
fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if (!CreateGLWindow("NeHe's Solid Object Tutorial",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
}

}
}

// Shutdown
KillGLWindow(); // Kill The Window
return (msg.wParam); // Exit The Program
}

DarkShadow44
10-31-2009, 05:11 AM
Sorry for this long post, I solved it. nvemulate "destroyed" my shaders :mad: :mad:

Aleksandar
10-31-2009, 09:57 AM
Why are you using nvemulate?
What kind of hardware are you using?
With the new drivers and nv8xxx cards you do not need to unlock any functionality. Everything works perfectly!

Alert: Don't install 195.39. OpenCL cease to function. :(
Maybe the problem is in the fact that I kept some dlls that are newer than the installation offered. Who knows...

Sanctus
10-31-2009, 11:20 AM
Sorry to just say this but there is a way to have more than 8 lights on the scene: Using a deferred renderer.
This is more complicated but once you set it up it works ok.
In my tests I have like 4000 lights on screen at a decent FPS (without shadows).

DarkShadow44
10-31-2009, 11:44 AM
Why are you using nvemulate?Because my graphic card doesn't support gemetry shaders for example...


What kind of hardware are you using?Nvidia GeForce 6200 TC



Alert: Don't install 195.39. OpenCL cease to function.My card doesn't support OpenCL :(


Sorry to just say this but there is a way to have more than 8 lights on the scene: Using a deferred renderer.
You mean to render and then move the lights and render again ?

But the lights of OpenGL only can make a model darker...

Would you know how to create a light, that is (like in Blender) a ball that emits light ? I mean like a real light bulb.

Because you can't set the position of ambient light...

And shadows for that light ? Is that even possible ??

Aleksandar
10-31-2009, 02:15 PM
Sorry to just say this but there is a way to have more than 8 lights on the scene: Using a deferred renderer.
Thank you Sanctus for this comment! I have completely forgotten that technique. :)
Yes, deferred rendering can enable greater number of lights, but requires additional memory space, and can have other disadvantages.

DarkShadow44
10-31-2009, 02:30 PM
Would you know how to create a light, that is (like in Blender) a ball that emits light ? I mean like a real light bulb.

Because you can't set the position of ambient light...

And shadows for that light ? Is that even possible ??

Aleksandar
10-31-2009, 02:42 PM
I'm sorry! You have to deal with very old hardware. :(
If there is any chance to upgrade it to some nv8xxx, the life will be much easier.


You mean to render and then move the lights and render again ?OpenGL is a state-machine. When you set something it stays until you change it and affects everything drawn after it.

Would you know how to create a light, that is (like in Blender) a ball that emits light ? I mean like a real light bulb.The light source is not a bulb. It is a mathematical model that enables lighting effects. I suggest you to read something about that. Chapter 5 from the Red book (OpenGL Programming Guide) can serve the purpose.

If you want to visualize the light source, you can draw a sphere with the center in the position of the light source. The material for that sphere must have a very high emission component to simulate a light source.


Because you can't set the position of ambient light...Why would you set the position of ambient light? Ambient light (GL_LIGHT_MODEL_AMBIENT) serves as a cheap trick to avoid lighting effects calculations that depend on the reflection of the objects in the scene. You've probably heard about radiosity algorithm. It enables very realistic effects of lighting by calculation of distribution of lighting energy in the space. But ... it is very complicated, time consuming, and never ends. In fact, the algorithm stops when differences in two successive iterations are approximately the same. Long time ago, when OpenGL (or IrisGL) was founded, there was no chance to implement such algorithm in hardware that can run in real-time. Ambient lighting was the only way to simulate light reflection from all objects in the scene.


And shadows for that light ? Is that even possible ?? Yes! Certainly! :)

DarkShadow44
11-01-2009, 06:18 AM
NOW I'm confused...


high emission componentIf I set the emission to the maximum it still doesn't illuminate other objects:
GLfloat mat[] = { 10, 1, 1, 1.0 };
glMaterialfv(GL_FRONT, GL_EMISSION, mat);



So how can I create a normal light in a house (house made of many objects), which illuminates the house inside like a real light (for example a torch or oven, but without flickering) ? :o

Aleksandar
11-01-2009, 06:52 AM
If I set the emission to the maximum it still doesn't illuminate other objectsOf course that it does not illuminate other objects. :)
High emission component of the material creates effect that the object covered with that material looks like it is a source of light, BUT IT IS NOT! You mast have a real light-source at the same place.


So how can I create a normal light in a house (house made of many objects), which illuminates the house inside like a real light (for example a torch or oven, but without flickering) ? :o
Set a light-source (for example GL_LIGHT0) in the center of the room, and let it be white light-source (to preserve original colors of your objects, or to be more precise the properties of the materials). Besides the material, you have to setup normals correctly.
This was the lab activity that I gave to my students in the year of 2004. They had to implement this using GL 1.1 (fixed functionality).
http://sites.google.com/site/opengltutorialsbyaks/events/oldgllabs/Old_AllLabs.rar
Is this what you want to implement? Use icons on the toolbar to change display-mode (colored/lighted/textured), and tasters Q,W,A,S,Z,X,E,R.

Aleksandar
11-01-2009, 07:19 AM
Hey, I have just seen that you had posted the same questions on another thread. :)
Do you need to check my answers? ;)

DarkShadow44
11-01-2009, 07:37 AM
Wow. That's very good :)

I still don't know how to use Material or these "connected bones" at the lamp. ^^

Is that a spot light ?
A 360 degrees light would be perfect. ^^

But why does the light has so many corners ?

And now I don't know if I should use shaders or not. :(
Because I have to re-implement ALL fixed function functionality...

Can I also throw light/shadows at bumpmapped images ??

Aleksandar
11-01-2009, 07:56 AM
Wow. That's very good :)
My students did not share your enthusiasm, because they had to do it for a grade. ;)


I still don't know how to use Material or these "connected bones" at the lamp. ^^
Before starting any implementation, my advice is to take a look at Read and Orange Books.


Is that a spot light ?
There is an "omny" light at the ceiling, and a directional light in the lamp.


But why does the light has so many corners ?This is the negative impact of the fixed functionality. It is per vertex lighting, and the surfaces are flat with few polygons (walls and table are tessellated). Per fragment lighting can be achieve only through shaders.


And now I don't know if I should use shaders or not. :(
Because I have to re-implement ALL fixed function functionality...It is up to you!

DarkShadow44
11-01-2009, 08:24 AM
Would you know how to create a "360° Lamp" with per pixel lightning ? I only can find spot lights.

Aleksandar
11-01-2009, 08:48 AM
What is the difference between the spot light and a "360° lamp"?

DarkShadow44
11-01-2009, 12:09 PM
A spot light only illuminates the objects in ONE direction, I would need a allround illuminating lanp like in blender.

Aleksandar
11-02-2009, 06:08 AM
A spot light only illuminates the objects in ONE direction, I would need a allround illuminating lanp like in blender. By default, a positional light radiates in all directions, but you can restrict it to producing a cone of illumination by using GL_SPOT_CUTOFF parameter. To reproduce omni-light effect in the shader is much easier than calculate spot-light effects, because you don't have to deal with the following parameters:
- spotDirection,
- spotCosCutoff,
- spotExponent.
So, if you have a complete code for the spot-light, just simplify it.