Lazarus
Programming => Graphics and Multimedia => OpenGL => Topic started by: Wodzu on February 20, 2010, 04:37:57 pm
-
Hi guys.
I am starting to learn OpenGL. I've tried to tesselate a 5-point star polygon. The problem is that when I want to use winding rule GLU_TESS_WINDING_POSITIVE I am getting a SIGSEGV error. :(
Also when I want to use more then one contour the same error occurs.
Here is the code:
program TessError;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this },
SysUtils, gl, glu, glut;
{$R TessError.res}
type
TCoords = array[0..2] of GLdouble;
PCoords = ^TCoords;
TVertexData = array[0..3] of GLdouble;
PVertexData = ^TVertexData;
TWeight = array[0..3] of GLfloat;
const
AppWidth = 640;
AppHeight = 480;
var
ScreenWidth, ScreenHeight, StarSize: Integer;
StarCoords: array[0..4] of T3DArray = ((0.0, 2.0, 0), (-1.0, -1.6, 0),
(1.8, 0.6, 0), (-1.8, 0.6, 0),
(1.0, -1.6, 0));
BoxCoords: array[0..3] of T3dArray = ((0.5, 0.5, 0), (-0.5, 0.5, 0), (-0.5, -0.5, 0),
(0.5, -0.5, 0));
ListID: Integer;
procedure GLDrawScene; cdecl;
var
i: Integer;
begin
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glOrtho(-10.0, 10.0, -10.0, 10.0, -1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glCallList(ListID);
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0, 10.0, 0);
glVertex3f(0.0, -10, 0);
glEnd();
glBegin(GL_LINES);
glVertex3f(-10.0, 0.0, 0);
glVertex3f(10.0, 0, 0);
glEnd();
glFlush();
glutSwapBuffers;
end;
procedure InitializeGL;
begin
glClearColor(0.0, 0.0, 0.0, 0);
end;
procedure GLResize(Width, Height: Integer); cdecl;
begin
if Height = 0 then
Height := 1;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glOrtho(-10.0, 10.0, -10.0, 10.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
end;
procedure GLKeyboard(Key: Byte; X, Y: Longint); cdecl;
begin
if Key = 27 then
Halt(0);
end;
procedure glutInitPascal(ParseCmdLine: Boolean);
var
Cmd: array of PChar;
CmdCount, I: Integer;
begin
if ParseCmdLine then
CmdCount := ParamCount + 1
else
CmdCount := 1;
SetLength(Cmd, CmdCount);
for I := 0 to CmdCount - 1 do
Cmd[I] := PChar(ParamStr(I));
glutInit(@CmdCount, @Cmd);
end;
procedure TessBegin(which: GLEnum); cdecl;
begin
glBegin(which);
end;
procedure TessEnd; cdecl;
begin
glEnd();
end;
procedure TessVertex(data: Pointer); cdecl;
begin
glVertex3dv(PGLDouble(data));
end;
procedure TessCombine(Coords: PCoords; VData: Pointer; Weight: PglFloat; out DataOut: Pointer); cdecl;
begin
DataOut := GetMem(3 * SizeOf(GLdouble));
PCoords(DataOut)^[0] := Coords^[0];
PCoords(DataOut)^[1] := Coords^[1];
PCoords(DataOut)^[2] := Coords^[2];
end;
procedure TessError(errno : TGLEnum); cdecl;
begin
Assert(False, IntToStr(errno)+': '+gluErrorString(errno));
end;
procedure Tesselation;
var
Tesselator: PGLUtesselator;
begin
ListID := glGenLists(1);
Tesselator := gluNewTess();
gluTessCallback(Tesselator, GLU_TESS_BEGIN, TCallBack(@TessBegin));
gluTessCallback(Tesselator, GLU_TESS_END, TCallBack(@TessEnd));
gluTessCallback(Tesselator, GLU_TESS_VERTEX, TCallBack(@TessVertex));
gluTessCallback(Tesselator, GLU_TESS_COMBINE, TCallBack(@TessCombine));
gluTessCallback(Tesselator, GLU_TESS_ERROR, TCallBack(@TessError));
glNewList(ListID, GL_COMPILE);
// gluTessProperty(Tesselator, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE); //uncomment this and you will get error
gluTessBeginPolygon(Tesselator, nil);
gluTessBeginContour(Tesselator);
gluTessVertex(Tesselator, StarCoords[0], @StarCoords[0]);
gluTessVertex(Tesselator, StarCoords[1], @StarCoords[1]);
gluTessVertex(Tesselator, StarCoords[2], @StarCoords[2]);
gluTessVertex(Tesselator, StarCoords[3], @StarCoords[3]);
gluTessVertex(Tesselator, StarCoords[4], @StarCoords[4]);
gluTessEndContour(Tesselator);
{gluTessBeginContour(Tesselator); //uncomment this and you will get error
gluTessVertex(Tesselator, BoxCoords[0], @BoxCoords[0]);
gluTessVertex(Tesselator, BoxCoords[1], @BoxCoords[1]);
gluTessVertex(Tesselator, BoxCoords[2], @BoxCoords[2]);
gluTessVertex(Tesselator, BoxCoords[3], @BoxCoords[3]);
gluTessEndContour(Tesselator); }
gluTessEndPolygon(Tesselator);
glEndList;
gluDeleteTess(Tesselator);
end;
begin
glutInitPascal(False);
glutInitDisplayMode(GLUT_DOUBLE or GLUT_RGB or GLUT_DEPTH);
glutInitWindowSize(AppWidth, AppHeight);
ScreenWidth := glutGet(GLUT_SCREEN_WIDTH);
ScreenHeight := glutGet(GLUT_SCREEN_HEIGHT);
glutInitWindowPosition((ScreenWidth - AppWidth) div 2, (ScreenHeight - AppHeight) div 2);
glutCreateWindow('TessError');
InitializeGL;
Tesselation;
glutDisplayFunc(@GLDrawScene);
glutReshapeFunc(@GLResize);
glutKeyboardFunc(@GLKeyboard);
glutMainLoop;
end.
What am I doing wrong?
Also, could you recommend me some Pascal-based OpenGL newsgroups?
Only newsgroup which I've found is here: news://forums.talkto.net/glscene.general but it doesn't seem to work...:|
Thanks for your time.
-
Hi! If you in Windows try it
{$IFDEF Win32} stdcall; {$ENDIF} {$IFDEF UNIX} cdecl; {$ENDIF}
In last version GLScene Pipe normally works.
-
Hi Rustam.
Yes that was the problem, unfortunately wiki tutorial about FPC and OpenGL doesn't say about different calling convetions under different OS.
Thanks for the answer tough.