PDA

View Full Version : Light is not lighting



reDo reDo
09-11-2011, 08:13 AM
I have this code and I dont know why it is not working and can somebody explain me this line please

glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
?
Here is code

#include <GL/glu.h>
#include "Primitives.hpp"
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
float angle=0;
void InitGL(void) // All Setup For OpenGL Goes Here
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,400,400);
glOrtho(-1, 1, -1, 1, -1, 1);

// Lighting set up
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

// Set lighting intensity and color
GLfloat qaAmbientLight[] = {0.2, 0.2, 0.2, 1.0};
GLfloat qaDiffuseLight[] = {0.8, 0.8, 0.8, 1.0};
GLfloat qaSpecularLight[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, qaAmbientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, qaDiffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, qaSpecularLight);

// Set the light position
GLfloat qaLightPosition[] = {0.5, 0.5, 0.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);
}

void DrawGLScene(void) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT);

// Set material properties
GLfloat qaBlack[] = {0.0, 0.0, 0.0, 1.0};
GLfloat qaGreen[] = {0.0, 1.0, 0.0, 1.0};
GLfloat qaWhite[] = {1.0, 1.0, 1.0, 1.0};
glMaterialfv(GL_FRONT, GL_AMBIENT, qaGreen);
glMaterialfv(GL_FRONT, GL_DIFFUSE, qaGreen);
glMaterialfv(GL_FRONT, GL_SPECULAR, qaWhite);
glMaterialf(GL_FRONT, GL_SHININESS, 60.0);

glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(-0.9,0.9, -.2);
glVertex3f(0.9,0.9, -.2);
glVertex3f(0.9,-0.9, -.2);
glVertex3f(-0.9,-0.9, -.2);
glEnd();

glFlush();
}

int main()
{
// Create the main window
sf::Window App(sf::VideoMode(400, 400, 32), "SFML OpenGL", sf::Style::Close);
InitGL();
// Start game loop
App.SetFramerateLimit(60);
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.PollEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();

// Resize event : adjust viewport
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}

DrawGLScene();
App.Display();
}

return 0;
}

it looks like
http://i51.tinypic.com/fb9w90.jpg

BionicBytes
09-11-2011, 10:49 AM
I'm not sure what you are expecting to see but it looks as expected; you have only one normal defined and only 4 verticies. Fixed function lighting with such a low resolution model will always look bad and unrealistic.

Dark Photon
09-11-2011, 06:44 PM
I have this code and I dont know why it is not working and can somebody explain me this line please

glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
Like BionicBytes says, use more vertices to draw your quad for better lighting results. The fixed-function pipeline only computes lighting at the vertices. That is actually one good reason to write your own shaders at some point, because then you can do the lighting per pixel without having to throw more verts at it to get better lighting.

As to your above question, when the GPU is computing lighting it has need to compute the vector from the vertex (surface point) to the eye (for specular). By default, for maximum speed, it is approximated as the eye-space Z axis for all vertices (surface points) -- i.e. (0,0,1). However, when something gets close to the eye or your FOV gets wide, you'll see errors in specular highlights with this approximation.

GL_LIGHT_MODEL_LOCAL_VIEWER = GL_TRUE says to not use this approximation and to instead compute the true normalized vector from the vertex to the eye, for every vertex.

That normalized vector is basically just -normalize( vertex_eye ), where vertex_eye is the eye-space position of the vertex (surface-point). On today's GPUs, this and the dot product used to apply it to the specular lighting term are pretty cheap, but it used to be worth avoiding when you didn't need it.

reDo reDo
09-12-2011, 10:38 AM
Thanks for replies, so if I wanna something like this I have to use many small quads, right?
http://i51.tinypic.com/n5qy2x.jpg

BionicBytes
09-12-2011, 10:55 AM
That looks like per pixel shading to me. You'll have to use shaders to get that quality. The number of verticies won't matter then

reDo reDo
09-12-2011, 11:28 PM
That picture is created by this code

// Lesson 4: Basic Lighting
// Author: Michael Hall
//
// This program is available for download through our website XoaX.net with no guarantees.
// Disclaimer: While we have made every effort to ensure the quality of our content, all risks associated
// with downloading or using this solution, project and code are assumed by the user of this material.
//
// Copyright 2009 XoaX - For personal use only, not for distribution
#include <glut.h>

void Draw() {
glClear(GL_COLOR_BUFFER_BIT);

// Set material properties
GLfloat qaBlack[] = {0.0, 0.0, 0.0, 1.0};
GLfloat qaGreen[] = {0.0, 1.0, 0.0, 1.0};
GLfloat qaWhite[] = {1.0, 1.0, 1.0, 1.0};
glMaterialfv(GL_FRONT, GL_AMBIENT, qaGreen);
glMaterialfv(GL_FRONT, GL_DIFFUSE, qaGreen);
glMaterialfv(GL_FRONT, GL_SPECULAR, qaWhite);
glMaterialf(GL_FRONT, GL_SHININESS, 60.0);

// Draw square with many little squares
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, 1.0);
const GLfloat kqDelta = .01;
for (int i = -90; i < 90; ++i) {
for (int j = -90; j < 90; ++j) {
glVertex3f(j*kqDelta, i*kqDelta, -.2);
glVertex3f((j+1)*kqDelta, i*kqDelta, -.2);
glVertex3f((j+1)*kqDelta, (i+1)*kqDelta, -.2);
glVertex3f(j*kqDelta, (i+1)*kqDelta, -.2);
}
}
glEnd();

glFlush();
}

void Initialize() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

// Lighting set up
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

// Set lighting intensity and color
GLfloat qaAmbientLight[] = {0.2, 0.2, 0.2, 1.0};
GLfloat qaDiffuseLight[] = {0.8, 0.8, 0.8, 1.0};
GLfloat qaSpecularLight[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, qaAmbientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, qaDiffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, qaSpecularLight);

// Set the light position
GLfloat qaLightPosition[] = {.5, .5, 0.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);
}

int main(int iArgc, char** cppArgv) {
glutInit(&amp;iArgc, cppArgv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(200, 200);
glutCreateWindow("XoaX.net");
Initialize();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}

BionicBytes
09-13-2011, 02:28 AM
Yep, now that I'm viewing this on something other than an iPhone I can see that the image is composed of many small quads - that's why there is a slight 'block' look to it. Still the specular highlight looks good for fixed-function.
So, yes.....the more quads you show at this the better the lighting.

reDo reDo
09-13-2011, 02:47 AM
So, if I wanna nice lighting effect I have to use many small quads, ok and thanks for help