Home Technical Talk

Anyone want tro help me out with shader code?

I can NOT see what I’m doing wrong here. I’m using OpenGL and have this vert shader:
[B]void main()[/B]
[B]{[/B]
[B]    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;[/B]

[B]    gl_FrontColor = vec4( 1.0, 0.5, 0.25, 1.0 );[/B]
[B]}[/B]
And this fragment shader:
[B]void main()[/B]
[B]{[/B]
[B]    gl_FragColor = gl_Color;[/B]
[B]}[/B]
No matter what I do, the primitives are always drawn as white. If I introduce errors into these shaders, my program complains so it’s clearly compiling and running the right ones.

Is there anything inherently wrong with what I’m doing here? I can’t imagine so, but I wanted to double check. Something I’ve overlooked?

These SHOULD work … right?

Replies

  • Zocky
    Options
    Offline / Send Message
    Zocky greentooth
    Hm, not programmer myself, but wouldn't this be better suited for www.gamedev.net ? I'm pretty sure if you ask this question on their forum, you'll get help very fast....
  • WarrenM
    Options
    Offline / Send Message
    Perhaps, I just know there are a bunch of technical artists here who would be able to spot any obvious errors or omissions...
  • obliviboy
    Options
    Offline / Send Message
    obliviboy polycounter lvl 12
    Vertex shader:
    #version 110
    varying vec4 myPerVertexColor;
    
    void main() 
    {     
         gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;      
         myPerVertexColor = vec4( 1.0, 0.5, 0.25, 1.0 ); 
    }
    
    Fragment shader:
    #version 110
    varying vec4 myPerVertexColor;
    
    void main() 
    {     
         gl_FragColor = myPerVertexColor; 
    }
    
    Where are you using this shader in? C++ or some app? Anyway you need normals in there to get some proper lighting.
  • WarrenM
    Options
    Offline / Send Message
    Obliviboy

    It's from a C# app I'm toying around with.

    If I try your code as written my app kicks up an error:

    ShaderError.jpg
  • WarrenM
    Options
    Offline / Send Message
    Do I need an OpenGL upgrade or something? I would think I'm current just by the nature of installing stuff over the years but ... I dunno.
  • obliviboy
    Options
    Offline / Send Message
    obliviboy polycounter lvl 12
    WarrenM wrote: »
    Do I need an OpenGL upgrade or something? I would think I'm current just by the nature of installing stuff over the years but ... I dunno.
    No, the opposite, you have a too new opengl.

    You are probably using opengl 3.3 or higher without a compatibility profile.
    Try it with those below.

    Vert:
    out vec4 myPerVertexColor;  
    void main()  
    {           
         gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;            
         myPerVertexColor = vec4( 1.0, 0.5, 0.25, 1.0 );  
    }
    
    Frag:
    in vec4 myPerVertexColor;  
    void main()  
    {           
         gl_FragColor = myPerVertexColor;  
    }
    
    Why are you defining a the color in the vertex shader when the color is constant over all verts? It could be done simpler if you do it in the fragment shader.

    Vert:
    void main()  
    {           
         gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;            
    }
    
    Frag:
    void main()  
    {           
         gl_FragColor = vec4( 1.0, 0.5, 0.25, 1.0 );  
    }
    
  • WarrenM
    Options
    Offline / Send Message
    The first set of code still gives an error:

    ShaderError2.jpg

    BUT, the second set of code compiled fine ... now, what I don't understand is that if I use the vertex and fragment shader together, the mesh renders white. If I only use the fragment shader, it renders in the color specified.

    Any thoughts on that?

    And I apologize for, what I'm sure, are super basic baby questions but I really am kind of in the dark on this. I haven't coded in years and I'm pretty rusty. However, I got a mesh loading and drawing so there's hope! :P

    At any rate, I appreciate you taking the time.

    As for:
    Why are you defining a the color in the vertex shader when the color is constant over all verts? It could be done simpler if you do it in the fragment shader.
    Because I'm sort of noodling trying to see if I can make anything happen via shaders. I'm sure I'm not taking the optimal route to anything...

    I have the orange book for shaders and am reading it but I can't get a basic shader working yet so it's sort of a roadblock. An annoying one...
  • WarrenM
    Options
    Offline / Send Message
    Ugh, never mind. Yay for code typos! I got that first set of code to compile and after fixing a stupid code bug, it works.

    Thanks a ton! I can finally start experimenting ...
  • obliviboy
    Options
    Offline / Send Message
    obliviboy polycounter lvl 12
    If the mesh renders white or black it means there is some kind of error. If you only use one shader type opengl will automatically add a default shader for the missing one. In you case the error was in the comunication between the two shaders.

    There are many ways to code is glsl and opengl. It depends on the version you are targeting and what functionality you require.

    Here are 2 articles on the history of opengl:
    http://en.wikipedia.org/wiki/OpenGL
    http://en.wikipedia.org/wiki/OpenGL_Shading_Language

    Good luck with your app.
  • CrazyButcher
    Options
    Offline / Send Message
    CrazyButcher polycounter lvl 18
    The version chaos is indeed a bit ugly with GL, ideally you would at least use #version 120 which is what GLSL ES 2.0 is similar to, #version 150 or 330 would be ES 3.0. Also if it's not too much work, try not to use any of the built-ins (gl_ModelView...) but manage uniforms yourself.

    Life should be easier by using a GLSL ide / offline compile tool. I am working on the shader related parts of an opensource IDE primarily meant for Lua. It originated from the luxinia engine and is now mostly maintained by someone else as zbstudio
    http://studio.zerobrane.com/

    estrela_glslc.png

    the original editor was estrela and both now share more or less the same code-base
    https://sourceforge.net/p/estrelaeditor/code/ci/master/tree/

    current snapshot as download, just unzip and run exe no installation required.

    http://sourceforge.net/code-snapshots/git/e/es/estrelaeditor/code.git/estrelaeditor-code-e886befe4cdb75ad54b2b9424a269a941a93699f.zip

    It has frontends to dx and glsl offline compilers, so you can easily check the shader syntax prior launching app.

    to enable the "estrela" legacy path with the glsl, hlsl support:
    * copy and rename the cfg/estrela.lua to cfg/user.lua

    for GLSL
    download https://github.com/CrazyButcher/glslc
    either set a global environment variable GLSLC_BIN_PATH to the glslc/bin_Windows_32 directory or edit the user.lua file so that it looks a bit like this
    path.glslcbin = [[C:\Users\Christoph\Projects\glslc\bin_Windows_32]] 
    
    -- load all tools, specs, and interpreters
    local all = function() return true end
    load.tools(all)
    load.specs(all)
    load.interpreters(all)
    

    for HLSL: download install directx sdk, the path the dx compiler is automatically found as the DX sdk installation sets the path.

    Hope that helps, I typically try to keep the tools up to date, as I use them at work more or less daily. glslc outputs a bit more information when used on a system with NVIDIA, but should work on AMD or Intel, too (however, I hardly test that).
Sign In or Register to comment.