Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: 'double *' to 'double'

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2002
    Location
    Swansea, Wales, UK
    Posts
    20

    'double *' to 'double'

    hi

    I am using gluUnProject to generate some opbject coordinates. I want to use these in a glVertex2d call to draw some lines on the screen. however, gluUnProject returns parameters of type double * and glVertex2d takes parameters of type double. How can I change it from double * to double? does it involve dereferencing it?
    I really have no idea what I need to do here, and can't find any information on it.

    any help would be most appreciated

    thanks
    Gareth

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jul 2001
    Posts
    442

    Re: 'double *' to 'double'

    Here is an example (I use this in my picking function):

    GLdouble x, y, z;

    gluUnProject ( static_cast<double> ( m_x ),
    Invert_m_y,
    Inverted mouse y.
    Result.z,
    Model,
    Projection,
    Viewport,
    &x,
    &y,
    &z );


    You are passing in pointers to x, y, z to gluUnProject. It will fill those memory locations with the required values. You simply use x, y, z as you would any ordinary double.

    ie. :

    GLdouble Distance = sqrt ( x * x + y * y + z * z );

    etc....

    [This message has been edited by Robbo (edited 04-25-2002).]

  3. #3
    Member Contributor
    Join Date
    Feb 2000
    Location
    Gothenburg, Sweden
    Posts
    68

    Re: 'double *' to 'double'

    Hello!

    Say you have a variable x that is of type double * then you can access the value with *x.
    Example :
    double *x;
    x==pointer to the memory address of containing the value of x.
    *x==value of x.

    Osku

  4. #4
    Senior Member Regular Contributor
    Join Date
    Jul 2001
    Posts
    442

    Re: 'double *' to 'double'

    Not quite. Look:

    double *x, y;

    x is undefined - so could point to anywhere. Initialised to y looks like this:

    x = &y; (x points to address of y).

    now, *x = 12345.0 and then print out the value of y,

    y = 12345.0

    Because you put 12345 into the address that x points to (which is y) - so y now equals that value.

    When you use gluUnProject, you are passing in the addresses of 3 variables so that OpenGL can store them in the place you want them. The variables themselves are GLdoubles, you just pass pointers to them into gl, ie. &x, &y and &z.

    Hope this explains things somewhat.

  5. #5
    Senior Member Regular Contributor
    Join Date
    Jan 2002
    Location
    Kingston, Jamaica, W.I.
    Posts
    282

    Re: 'double *' to 'double'

    If you use
    Code :
    GLdouble x, y, z;
    to define x, y, z then you don't need to dereference them. In this case your call to gluUnproject looks like
    Code :
    gluUnproject(..., &amp;x, &amp;y, &amp;z);
    and you use the values as shown in the following example.
    Code :
    len = sqrt(x * x + y * y + z * z);
    If you used
    Code :
    GLdouble *x, *y, *z;
    to define x, y, z then you have to dereference them to get their data. In this case your call to gluUnproject looks like
    Code :
    gluUnproject(..., x, y, z);
    and you use the values as shown in the following example.
    Code :
    len = sqrt(*x * *x + *y * *y + *z * *z);

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: 'double *' to 'double'

    The problem with your second method, Furrage, is that you aren't properly giving those pointers a memory location. Your code would likely cause a crash. When they are first created, a pointer points god knows where. You either need to give it the address to another variable, assign it the same value as another pointer that has a proper address, or dynamically allocate memory to it with new or malloc.

    Always, always, ALWAYS, think about how you're using memory when using pointers. It will save you tons of headaches.



    [This message has been edited by Deiussum (edited 04-25-2002).]
    Deiussum
    Software Engineer and OpenGL enthusiast

  7. #7
    Senior Member Regular Contributor
    Join Date
    Jan 2002
    Posts
    292

    Re: 'double *' to 'double'

    Pointers need to either be initialized by allocating memory to it such malloc or the new operator or you assign the address of an existing allocated memory address...

    So
    GLfloat *x, *y, *z;

    x = new GLfloat;// new GLfloat[size]; //Array
    y = new GLfloat;// new GLfloat[size]; //Array
    z = new GLfloat;// new GLfloat[size]; //Array

    or

    GLfloat w;
    x = &w;

    Pointers dont hold values, they hold a memory addresses.

  8. #8
    Junior Member Newbie
    Join Date
    Apr 2002
    Posts
    28

    Re: 'double *' to 'double'

    Remember each call of malloc/new should have a corresponding free/delete to release the memory again. Otherwise we get many memory leaks I think for dfanuk's purpose he should use the second alternative.

  9. #9
    Senior Member Regular Contributor
    Join Date
    Jan 2002
    Posts
    292

    Re: 'double *' to 'double'

    Oh yeah Jimmi... At least I expect the programmer to know this if they are using it... Can't use it, without knowing it.
    But I guess I should have posted that extra line of code for completeness.


    Originally posted by jimmi:
    Remember each call of malloc/new should have a corresponding free/delete to release the memory again. Otherwise we get many memory leaks I think for dfanuk's purpose he should use the second alternative.

  10. #10
    Senior Member Regular Contributor
    Join Date
    Jan 2002
    Location
    Kingston, Jamaica, W.I.
    Posts
    282

    Re: 'double *' to 'double'

    Originally posted by Deiussum:
    The problem with your second method, Furrage, is that you aren't properly giving those pointers a memory location. Your code would likely cause a crash. When they are first created, a pointer points god knows where. You either need to give it the address to another variable, assign it the same value as another pointer that has a proper address, or dynamically allocate memory to it with new or malloc.
    Duh... I thought that was so standard that I didn't need to mention it. Anyway, its good you mentioned it, as someone might have thought it was the complete code and get no end of errors if they tried to copy it

Page 1 of 2 12 LastLast

Similar Threads

  1. double PBO ? or simple ?
    By devel in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 10-15-2005, 09:28 AM
  2. Seeing Double
    By Charles in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 05-29-2004, 10:35 AM
  3. Double projection
    By stephanh in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-26-2003, 06:46 AM
  4. double buffering
    By cobbal in forum OpenGL: macOS
    Replies: 1
    Last Post: 03-17-2003, 05:27 PM
  5. float vs. double
    By jabe in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 04-20-2001, 01:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean