Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

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

Thread: forward declaring functions

  1. #1
    Senior Member OpenGL Guru knackered's Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    3,032

    forward declaring functions

    Why isn't it possible to forward-declare a function within the same source file as the function body itself.
    Example:

    vec3 getNormal();

    void main(void)
    {
    vec3 normal = getNormal();
    }

    vec3 getNormal()
    {
    return gl_Normal;
    }

    I get "getNormal is already defined..." errors.
    It works fine if the getNormal function body is in another shader attachment...
    Knackered

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    658

    Re: forward declaring functions

    forward declaration is an instrument which makes one-pass compilation possible. maybe your compiler works with 2 passes and therefore does not need- maybe does not even understand- a forward declaration, but interprets a forward declared function to be external.

    does it work without forward declaration and the getNormal function body at the end of the source?

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2004
    Posts
    19

    Re: forward declaring functions

    OpenGL 2.0 says that forward declaring a function, like you did in your example, works. You can give the prototype, then call it, then provide the body. This feature is intentionally provided, and is part of the core OpenGL 2.0 specification.

    JohnK

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    658

    Re: forward declaring functions

    ladies and gentlemen, tonight it is knackered vs. the OpenGl2.0 specification

    seriously, a spec says how something should work. knackered says it doesn't. so what's wrong? wrong hardware, driver, compiler...? or maybe the wrong OS? i'm getting out of this discussion right now, since i've never created even the smallest possible shader program

  5. #5
    Senior Member OpenGL Guru knackered's Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    3,032

    Re: forward declaring functions

    Right, so it's in the spec. Ok, I'll report it to nvidia....again.
    The other day, in a shader I was writing, "vec3 v(1,1,1);" made the GLSL compiler crash! Changing it to "vec3 v(1.0, 1.0, 1.0);" stopped it crashing. A compiler error or warning would be fair enough, but crashing the application is bad. This is just another in a long line of weirdness with the nVidia GLSL implementation.

    RigidBody, have a word with yourself will you.
    Knackered

  6. #6
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,527

    Re: forward declaring functions

    The given example doesn't write gl_Position or uses normal. So what would a reproducing shader source be?
    Seems to work for me just fine.

  7. #7
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    658

    Re: forward declaring functions

    hmm... didn't want to bother you, knackered. i will stand in the corner for the weekend
    (by the way, i'm not quite sure about "have a word with yourself", didn't find it it in my dictionairy. i even asked a native speaker, but she couldn't translate it. i assume it means that you want me to punish myself for being naughty?)

  8. #8
    Senior Member OpenGL Guru knackered's Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    3,032

    Re: forward declaring functions

    It's a south-london slang term, it means "act like a third-party in the situation, and advise yourself that you're behaving inappropriately". Not necessarily that you're naughty, just that you're not making much sense. It's often said to people as a more friendly alternative to "shut up, ars*hole".
    So I meant to be friendly.
    Knackered

  9. #9
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    658

    Re: forward declaring functions

    ah, i see. so i'll try to be more serious from now on

  10. #10
    Member Regular Contributor
    Join Date
    Jun 2005
    Location
    USA
    Posts
    277

    Re: forward declaring functions

    Originally posted by knackered:
    Right, so it's in the spec. Ok, I'll report it to nvidia....again.
    The other day, in a shader I was writing, "vec3 v(1,1,1);" made the GLSL compiler crash! Changing it to "vec3 v(1.0, 1.0, 1.0);" stopped it crashing. A compiler error or warning would be fair enough, but crashing the application is bad. This is just another in a long line of weirdness with the nVidia GLSL implementation.

    RigidBody, have a word with yourself will you.
    AFAIK GLSL will not typecast from a int to a float... I know that Nivida cards bark at that kind of coding.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •