Recent

Author Topic: Drawing a cone  (Read 12160 times)

ehj666

  • Jr. Member
  • **
  • Posts: 50
Drawing a cone
« on: February 28, 2017, 02:15:59 am »
I have the following code ported from Delphi.

Code: Pascal  [Select][+][-]
  1. procedure TBackplot.ShowTool(const X, Y, Z: Extended);
  2.  
  3.   var
  4.     Cone: gluQuadricObj;
  5.  
  6.   begin
  7.     Cone := gluNewQuadric;
  8.     gluQuadricNormals(Cone, GLU_SMOOTH);
  9.     glPushMatrix;
  10.     glColor3f(0.0, 0.75, 0.75);
  11.     glTranslatef(X - Cx, Y - Cy, Z - Cz);
  12.     gluCylinder(Cone, 0.0, 0.25, 1.0, 26, 13);
  13.     glPopMatrix;
  14.   end;
  15.  
  16.  

On the line "Cone := gluNewQuadric;" I get the error "Incompatible types: got <procedure variable of type function ^GLUquadric;CDecl> expected GLUquadric". I see that gluNewQuadric is defined in glu.pp as 

gluNewQuadric : function:PGLUquadric;extdecl;

But I do not know how to resolve the incompatible types;

I am using it with TOpenGlControl.

Thanks

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Drawing a cone
« Reply #1 on: February 28, 2017, 04:35:57 am »
PGLUquadric is a pointer type, gluQuadricObj you are using for Cone is not.

ehj666

  • Jr. Member
  • **
  • Posts: 50
Re: Drawing a cone
« Reply #2 on: February 28, 2017, 02:56:12 pm »
I realize that, my question is what construct do I use? This worked in D7 so it seems to me that gluQuadricObj or gluNewQuadric is defined differently in fp. I am trying to port an old application and haven't touched opengl in at least 10 years, so just asking how I draw a cone under fp.

balazsszekely

  • Guest
Re: Drawing a cone
« Reply #3 on: February 28, 2017, 03:57:55 pm »
@ehj666
What @User137 is saying, you need a @ before Cone.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Drawing a cone
« Reply #4 on: February 28, 2017, 10:54:53 pm »
If you want this Cone := gluNewQuadric; to work, you need
Code: Pascal  [Select][+][-]
  1. var
  2.     Cone: PGLUQuadric; // or Cone: ^gluQuadricObj;

But then check definition of gluCylinder. Is it a pointer or not? It depends on that if you can use Cone directly or Cone^.
« Last Edit: March 01, 2017, 02:12:56 am by User137 »

ehj666

  • Jr. Member
  • **
  • Posts: 50
Re: Drawing a cone
« Reply #5 on: March 01, 2017, 12:39:56 am »
That is not correct either, gluNewQuadric is defines as:
Code: Pascal  [Select][+][-]
  1. gluNewQuadric : function:PGLUquadric;extdecl;
  2.  

So, as I understand it, it does not return a PGLUquadric, it returns a pointer to a function which then returns a PGLUquadric.

Code: Pascal  [Select][+][-]
  1.   var
  2.     Cone: pgluQuadric;
  3.  
  4.   begin
  5.     Cone := gluNewQuadric;
  6.     gluQuadricNormals(Cone, GLU_SMOOTH);
  7.  

This does not work, nor does:
Code: Pascal  [Select][+][-]
  1.     Cone := gluNewQuadric^;
  2.  
« Last Edit: March 01, 2017, 12:58:45 am by ehj666 »

ehj666

  • Jr. Member
  • **
  • Posts: 50
Re: Drawing a cone
« Reply #6 on: March 01, 2017, 01:00:25 am »
From a C example:

Code: C  [Select][+][-]
  1.  
  2. GLUquadric* qobj;
  3.  
  4. void init() // only call once on startup
  5. {
  6.   qobj = gluNewQuadric();
  7.   gluQuadricNormals(qobj, GLU_SMOOTH);
  8. }
  9.  
  10. void wheel() // use object in your drawing
  11. {
  12.   gluCylinder(qobj, 1.0, 1.0, 0.4, 1, 16);
  13. }
  14.  
  15. void cleanup() // call once when you exit program
  16. {
  17.   gluDeleteQuadric(qobj);
  18. }
  19.  

ehj666

  • Jr. Member
  • **
  • Posts: 50
Re: Drawing a cone
« Reply #7 on: March 01, 2017, 01:16:10 am »
This at least compiles, if I cast gluNewQuadric as a pointer. Not far enough along to test at run time.

Code: Pascal  [Select][+][-]
  1.   var
  2.     Cone: pgluQuadric;
  3.  
  4.   begin
  5.     Cone := pointer(gluNewQuadric);
  6.     gluQuadricNormals(Cone, GLU_SMOOTH);
  7.  

ehj666

  • Jr. Member
  • **
  • Posts: 50
Re: Drawing a cone
« Reply #8 on: March 01, 2017, 01:54:21 am »
That compiles, but does not run. Blows out at:
Code: Pascal  [Select][+][-]
  1. gluQuadricNormals(Cone, GLU_SMOOTH);
  2.  

with "raised exception class 'External: SIGSEGV'".

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Drawing a cone
« Reply #9 on: March 01, 2017, 02:27:31 am »
I figured you are using Lazarus' headers (not dglOpengl), so these from glu.pas:
Code: Pascal  [Select][+][-]
  1. gluNewQuadric : function:PGLUquadric;extdecl;
  2. gluDeleteQuadric : procedure(quad:PGLUquadric);extdecl;
  3. gluCylinder : procedure(quad:PGLUquadric; base:GLdouble; top:GLdouble; height:GLdouble; slices:GLint;
  4.     stacks:GLint);extdecl;
From that it's clear that you should have "var Cone: PGLUquadric;". But you also have to use gluDeleteQuadric(), otherwise you have a memory leak sort of thing. It's fine to dismiss in a simple demo that only calls gluNewQuadric once per program runtime. You can also keep reusing the same quadric index instead of always making a new one, if you only work with 1 model type at the time. Like if you want to render 1000 cones you still only need to call the gluNewQuadric just once.

Other than that, all glu* based commands have been deprecated for many years and new drivers have no obligation to support them. I would guess they already don't work on android at all.

And i don't know why you get sigsegv. What if you just draw a triangle with glbegin..glend?
« Last Edit: March 01, 2017, 02:30:25 am by User137 »

ehj666

  • Jr. Member
  • **
  • Posts: 50
Re: Drawing a cone
« Reply #10 on: March 01, 2017, 03:23:16 am »
That was a simplified version for testing. Here is the full code:
Code: Pascal  [Select][+][-]
  1. constructor TBackplot.Create(AGLControl: TopenGLControl);
  2.  
  3.   begin
  4.     BackplotControl := AGLControl;
  5.     InitOpengl;
  6.   end;
  7.  
  8. destructor TBackplot.Destroy;
  9.  
  10.   begin
  11.     inherited Destroy;
  12.     gluDeleteQuadric(Cone);
  13.   end;
  14.  
  15. procedure TBackplot.InitOpengl;
  16.  
  17.   begin
  18.     Cone := pointer(gluNewQuadric);
  19.     gluQuadricNormals(Cone, GLU_SMOOTH);
  20.   end;
  21.  
  22. procedure TBackplot.ShowTool(const X, Y, Z: Extended);
  23.  
  24.   begin
  25.     glPushMatrix;
  26.     glColor3f(0.0, 0.75, 0.75);
  27.     glTranslatef(X - Cx, Y - Cy, Z - Cz);
  28.     gluCylinder(Cone, 0.0, 0.25, 1.0, 26, 13);
  29.     glPopMatrix;
  30.   end;
  31.  
  32.  

A simple triangle does work, implemented as follows:
Code: Pascal  [Select][+][-]
  1. procedure TBackplot.Paint;
  2.  
  3.   begin
  4.     glClearColor(0.0, 0.0, 0.0, 1.0); //Set black background
  5.     glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  6.     glLoadIdentity;
  7.     glBegin(GL_TRIANGLES);
  8.       glColor3f(1, 0, 0);
  9.       glVertex3f( 0.0, 1.0, 0.0);
  10.       glColor3f(0, 1, 0);
  11.       glVertex3f(-1.0,-1.0, 0.0);
  12.       glColor3f(0, 0, 1);
  13.       glVertex3f( 1.0,-1.0, 0.0);
  14.     glEnd;
  15.     BackplotControl.SwapBuffers;
  16.   end;
  17.  

What should I be using instead of glu to render a cone? The code I am porting is 10-15 years old.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Drawing a cone
« Reply #11 on: March 01, 2017, 04:57:54 am »
Is there any difference if you do

Code: Pascal  [Select][+][-]
  1. Cone := gluNewQuadric();
instead of
Code: Pascal  [Select][+][-]
  1. Cone := pointer(gluNewQuadric);

Both sides should already be of identical types, so you don't need type conversion.

ehj666

  • Jr. Member
  • **
  • Posts: 50
Re: Drawing a cone
« Reply #12 on: March 01, 2017, 05:22:19 am »
Yes, the first gives an incompatible types compiler error. See first post.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Drawing a cone
« Reply #13 on: March 01, 2017, 05:56:44 am »
User137 is correct. At least mode objfpc assumes the cone variable to match the function, while it is of course just a PGLUquadric.

I was able to solve that by doing:
Code: [Select]
var
  Cone: PGLUquadric;
begin
  Cone := gluNewQuadric();
end.
To explicitly tell to _call_ the function.

So, something seems off in ehj666's code.

In addition to that, always check the return value in order to make sure that function is supported by your hardware/software to begin with.
« Last Edit: March 01, 2017, 06:00:44 am by molly »

ehj666

  • Jr. Member
  • **
  • Posts: 50
Re: Drawing a cone
« Reply #14 on: March 01, 2017, 01:09:44 pm »
Sorry, I misunderstood. I thought the '()' at the end of gluNewQuadric was a typo, using C syntax. I did not realize that was valid syntax in fpc. That does compile, and so far is not generating a run time error.

Thanks.

 

TinyPortal © 2005-2018