PDA

View Full Version : Sphere problem



mathx
07-05-2001, 01:55 AM
Hello,

Ok, I was able to create a gluSphere, and add a texture to it, create the normals, add lighting, and everything was fine, except for a wierd problem: when the sphere rotates a little, it stops, the texture does a wierd movement, and the sphere starts to rotate backwards. Anyone knows what can cause this?

Thanks,
Matheus.

almok
07-05-2001, 03:12 AM
post your code

Hi.

mathx
07-05-2001, 01:12 PM
Hello,

Here it is, it's Delphi source. Deleted some parts that didn't had to do with the sphere code (fullscreen mode and lighting). Shouldn't be hard to anyone to understand.

unit Unit1;
{

Teste básico de OpenGl demo01
2 de julho de 2001
Matheus Degiovani
matheus@subdimension.com

Este demo ccria uma janela opengl e possibilita mudar entre
modo janela e fullscreen

}

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
opengl12, Menus;

type
TForm1 = class(TForm)
Popup1: TPopupMenu;
Fullscreen1: TMenuItem;
Janela1: TMenuItem;
luzes1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure Fullscreen1Click(Sender: TObject);
procedure Janela1Click(Sender: TObject);
procedure luzes1Click(Sender: TObject);
private
procedure WMPaint(var message: TWMPaint); message WM_Paint;
public
procedure InitGl;
end;

var
Form1: TForm1;
mdc: HBitmap;
mrc: hglRc;
bfs: boolean; //fullscreen
ang: single;
sp: PGLUQuadricObj;
txi: integer;
lst: integer;

implementation

{$R *.DFM}

procedure TForm1.InitGl;
var
pfd: PixelFormatDescriptor;
pfi: integer;
begin
mdc:= GetDc(form1.Handle);

InitOpengl;

pfd.nVersion:= 1;
pfd.dwFlags:= PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
pfd.iPixelType:= PFD_TYPE_RGBA;
pfd.cColorBits:= 32;
pfd.cDepthBits:= 24;
pfd.iLayerType:= PFD_MAIN_PLANE;
pfd.nSize:= SizeOf(pfd);

pfi:= choosePixelFormat(mdc, @pfd);
SetPixelFormat(mdc, pfi, @pfd);

mrc:= wglCreateContext(mdc);
wglMakeCurrent(mdc, mrc);

glClearColor(0, 0, 0, 0);

bfs:= false;

form1.FormResize(self);

end;


procedure TForm1.WMPaint(var message: TWMPaint);
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

glLoadIdentity;

glTranslatef (0.0, 0.0, -8);

glRotatef(ang, 0, 1, 0);

//gluSphere(sp, 1, 20, 20);
glCallList(lst);
ang:= ang + 2;

swapBuffers(mdc);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
tx: TMemoryStream;
i: integer;
front, back: Longint;
begin
InitGl;

sp:= gluNewQuadric;
gluQuadricOrientation(sp, GLU_OUTSIDE);
gluQuadricTexture(sp, GL_TRUE);
//gluQuadricDrawStyle(sp, GLU_LINE);

tx:= TMemoryStream.Create;
tx.LoadFromFile('est1.bmp');

//bitmaps são guardadas como BGR
//Inverter para RGB
i:= 1;
while i < tx.Size -3 do begin

tx.Position:= i -1;
tx.ReadBuffer(front, 1);
tx.Position:= i + 1;
tx.ReadBuffer(back, 1);
tx.Position:= i -1;
tx.WriteBuffer(back, 1);
tx.Position:= i +1;
tx.WriteBuffer(front, 1);
i:= i + 3;

end;

glGenTextures(1, @txi);
glBindTexture(GL_TEXTURE_2D, txi);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glTexImage2d(GL_TEXTURE_2D, 0, 3, 256, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, tx.Memory);

glEnable(GL_TEXTURE_2d);

lst:= glGenLists(1);
glNewList(lst, GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, txi);
glRotatef(90, 1, 0, 0);
gluSphere(sp, 2, 20, 20);
glLoadIdentity;
glEndList;

tx.Free;
end;


procedure TForm1.FormDestroy(Sender: TObject);
var
dm: DevMode;
begin
wglMakeCurrent(0, 0);
wglDeleteContext(mrc);
if bfs = true then begin
dm.dmPelsWidth:= 1024;
dm.dmPelsHeight:= 768;
dm.dmFields:= DM_PELSWIDTH or DM_PELSHEIGHT;
ChangeDisplaySettings(dm, 0);
end;
CloseOpengl;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
glViewport(0, 0, form1.Width, form1.Height);

//acertear perspectiva
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(45, form1.Width / form1.Height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
end;

almok
07-06-2001, 12:02 AM
I don't know delphi but try to change in display function the line ang=ang+2 with
the line ang=ang+2 % 360.
if the operator % in delphi not exist
try with the following code:

initialize ang to zero

if (ang==360) ang=ang+2;
else ang=0;


Hi.

almok
07-06-2001, 12:06 AM
sorry,
correction:

inizialize ang to 0.

if (ang==360) ang=0;
else ang=ang+2;

mathx
07-06-2001, 11:48 AM
Hello,

Actually, I made it work by my self http://www.opengl.org/discussion_boards/ubb/smile.gif Just turned on culling...

Thanks anyway,
Matheus.

FatalXC
07-06-2001, 03:40 PM
Culling will fix that problem, but you really need to enable the z buffer for anything else.

FatalXC

mathx
07-08-2001, 04:45 AM
Hello,

How? With glEnable(GL_DEPTH_TEST)? Yeah, I forgot that..

thanks,
Matheus.