Forum > OpenGL

[SOLVED] OpenGL Lighting Issue

(1/3) > >>

Handoko:
Can anyone tell me what I did wrong why rotating the pyramid will cause it become darker then gradually bright again?

Download the code and run, press left/right shift to rotate. The demo requires LazOpenGLContext (and libgl1-mesa-dev if you use Linux).


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, ExtCtrls, LCLType, LclIntf,  OpenGLContext, GL, math; type   { TForm1 }   TForm1 = class(TForm)    OpenGLControl1: TOpenGLControl;    Timer1: TTimer;    procedure FormCreate(Sender: TObject);    procedure OpenGLControl1Paint(Sender: TObject);    procedure Timer1Timer(Sender: TObject);  private    FRotation: GLfloat;    procedure InitGL;  end; var  Form1: TForm1; const  LightDiffuse: array[0..3] of Single = (1, 1, 1, 1); implementation procedure gluPerspective(fovy, aspect, zNear, zFar: GLdouble);var  Width, Height: Double;begin  Height := tan(fovy * pi / 360) * zNear;  Width  := Height * aspect;  glFrustum(-Width, Width, -Height, Height, zNear, zFar);end; {$R *.lfm} { TForm1 } procedure TForm1.OpenGLControl1Paint(Sender: TObject);const  AlreadyInit: Boolean = False;begin  if not(AlreadyInit) then    InitGL;   glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);  glLoadIdentity;  glTranslatef(0, 0, -5);  glRotatef(FRotation, 0, 1, 0);  glBegin(GL_TRIANGLES);    glColor3f (1, 0, 0); glVertex3f( 0,  1,  0);    glColor3f (0, 1, 0); glVertex3f(-1, -1,  1);    glColor3f (0, 0, 1); glVertex3f( 1, -1,  1);    glColor3f (1, 0, 0); glVertex3f( 0,  1,  0);    glColor3f (0, 0, 1); glVertex3f( 1, -1,  1);    glColor3f (0, 1, 0); glVertex3f( 1, -1, -1);    glColor3f (1, 0, 0); glVertex3f( 0,  1,  0);    glColor3f (0, 1, 0); glVertex3f( 1, -1, -1);    glColor3f (0, 0, 1); glVertex3f(-1, -1, -1);    glColor3f (1, 0, 0); glVertex3f( 0,  1,  0);    glColor3f (0, 0, 1); glVertex3f(-1, -1, -1);    glColor3f (0, 1, 0); glVertex3f(-1, -1,  1);  glEnd;   OpenGLControl1.SwapBuffers;end; procedure TForm1.FormCreate(Sender: TObject);begin  OpenGLControl1.Anchors := [akTop, akLeft, akBottom, akRight];  OpenGLControl1.MultiSampling := 8;  FRotation := 0;end; procedure TForm1.Timer1Timer(Sender: TObject);begin  OpenGLControl1.Invalidate;   if GetKeyState(VK_RSHIFT) < 0 then    FRotation := FRotation - 0.8  else    if GetKeyState(VK_LSHIFT) < 0 then      FRotation := FRotation + 0.8;end; procedure TForm1.InitGL;begin  glShadeModel(GL_SMOOTH);  glClearColor(0, 0, 0, 0);  glClearDepth(1);  glEnable(GL_DEPTH_TEST);  glDepthFunc(GL_LEQUAL);  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);   glViewport(0, 0, OpenGLControl1.Width, OpenGLControl1.Height);  glMatrixMode(GL_PROJECTION);  glLoadIdentity;  gluPerspective(45, OpenGLControl1.Width/OpenGLControl1.Height, 0.1, 100);  glMatrixMode(GL_MODELVIEW);  glLoadIdentity;   glEnable(GL_COLOR_MATERIAL);  glEnable(GL_LIGHTING);  glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);  glEnable(GL_LIGHT1);end; end.

eljo:
I didn't looked at the code yet but from my experience the problem is usually that you rotate the camera instead of the pyramid and you do not rotate the light as well.
Just a quick fyi I'll take a closer look later.

Handoko:
That's sound possible. But I don't think it happens on my case. Actually in my 'complete' code, it has several objects. Only the object that rotates becomes darker, while the others stay correct as what they should be (as long as they don't rotate). What I submitted here was a simplified version, consisted only 1 object.

eljo:
Oh well shoot in dark missed I'll look at the code from home later tonight.

Handoko:
I just found an article saying that we can get weird result if enabling light without setting the polygon normal.

Problem solved.

Navigation

[0] Message Index

[#] Next page

Go to full version