Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Can't pass values into the shader

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2007
    Posts
    3

    Can't pass values into the shader

    Hi I'm working on Blender's source code trying to insert glsl shading into Blender's 3d Viewport.

    I've tried everything and I can't understand why I can't pass values into the shader. I don't get any error from gdb nor from gldb.

    First of all when Blender starts, if glsl is supported a simple shader is initialized:

    Code :
      void init_lambert_shader(void)
    {
      unsigned int local_vertex_shader, local_fragment_shader;
      GLint  vert_compiled, frag_compiled;
      GLint  linked;
      GLint local_program;
     
      char *vertex_shader = read_shader_file("/home/maike/shaders/l.vert");
      char *fragment_shader = read_shader_file("/home/maike/shaders/l.frag");
     
      local_vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER);
      local_fragment_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER);
     
      glShaderSourceARB(local_vertex_shader, 1, &vertex_shader, NULL);
      glShaderSourceARB(local_fragment_shader, 1, &fragment_shader, NULL);
     
      glCompileShaderARB(local_vertex_shader);
      glGetObjectParameterivARB(local_vertex_shader, GL_COMPILE_STATUS, &vert_compiled);
     
      glCompileShaderARB(local_fragment_shader);
      glGetObjectParameterivARB(local_fragment_shader, GL_COMPILE_STATUS, &frag_compiled);
     
      if (!vert_compiled){
        printf("\nerro\n");
        return;
      }
      if(!frag_compiled){
        printf("\nerrof\n");
        return;
      }
     
      local_program = glCreateProgramObjectARB();
      glAttachObjectARB(local_program, local_vertex_shader);
      glAttachObjectARB(local_program, local_fragment_shader);
     
      glLinkProgramARB(local_program);
      glGetProgramivARB(local_program, GL_LINK_STATUS, &linked);
     
      if (!linked){
        printf("\nerro2\n");
        return;
      }
     
      lambert = local_program;
      lambert_ref = glGetUniformLocationARB(local_program, "ref");
    }
    where lambert and lambert_ref are static globals.

    then everytime anything needs update this function is called:

    Code :
     void glsl_draw_mesh(Base *base){
      Object *ob = base->object;
      DerivedMesh *dm= mesh_get_derived_final(ob, get_viewedit_datamask());
      CDDerivedMesh *cddm = (CDDerivedMesh *) dm;
      MFace *face = cddm->mface;
      MVert *vert = cddm->mvert;
      DrawColors colors;
     
      float *nors = dm->getFaceDataArray(dm, CD_NORMAL);
      int i;
     
      get_colors(&colors);
      draw_outline(base, ob, &colors);
      glUseProgramObjectARB(get_material_shader());
      glShadeModel(face->flag & ME_SMOOTH?GL_SMOOTH:GL_FLAT);
     
      print_log();
     
      for(i = 0; i < dm->numFaceData; i++, face++){
        set_lambert_param(ob->data, face);    
        set_draw_color(ob->data, face, &amp;colors);
     
        glBegin(face->v4?GL_QUADS:GL_TRIANGLES);
        if(!(face->flag & ME_SMOOTH)){
          if(nors){
    	glNormal3fv(nors);
          }
          else{
    	float nor[3];
    	if(face->v4) {
    	  CalcNormFloat4(vert[face->v1].co, vert[face->v2].co, vert[face->v3].co, vert[face->v4].co, nor);
    	} 
    	else {
    	  CalcNormFloat(vert[face->v1].co, vert[face->v2].co, vert[face->v3].co, nor);
    	}
    	glNormal3fv(nor);	
          }
        }
     
        create_vertex(ob, vert, face->v1, face->flag);
        create_vertex(ob, vert, face->v2, face->flag);
        create_vertex(ob, vert, face->v3, face->flag);
        if(face->v4)
           create_vertex(ob, vert, face->v4, face->flag);
     
        if(nors) nors += 3;
     
        glEnd();
      }
     
      glUseProgramObjectARB(0);
      glShadeModel(GL_FLAT);
    }
    and I try to pass the uniform's value in:

    Code :
     void set_lambert_param(Mesh *mesh, MFace *face)
    { 
      Material *mat; 
      GLint location = get_lambert_ref();
      int error;
     
      if(location == -1)
          exit(0);
     
      if(mesh->mat){
        mat = mesh->mat[face->mat_nr];
        glUniform1fARB(location, mat->ref);
      }
      else
        glUniform1fARB(location, 0.8); 
    }
    I've tried to do the same with Attribute but it doesn't work either.
    The shaders compile and work fine, but it receives any value as 0.0 or as 1.0.

    I've corrected every single error that Gdb and Gldb detected and still I can't send values into the shader.

    Help on this subject would be really great.

    Thanks.

  2. #2
    Intern Newbie
    Join Date
    Jan 2002
    Location
    Steamboat Springs, CO, USA
    Posts
    43

    Re: Can't pass values into the shader

    I'd sprinkle in a few calls to glGetError() to see if you're doing something wrong somewhere.

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2007
    Posts
    3

    Re: Can't pass values into the shader

    I've printed location, and if i have 2 uniforms it prints out 0 and 1 as respective locations.

    I've put a glGetError() after passing the uniform and it prints out 0.

    The shaders compile perfectly and the program is successfully linked. The calculations within the shader work fine except when passing values by uniforms ou attributes which are set to 0.0 or 1.0 as I declared them as floats.

  4. #4
    Junior Member Regular Contributor
    Join Date
    Feb 2004
    Posts
    249

    Re: Can't pass values into the shader

    You would need to call
    Code :
    glUseProgramObjectARB(get_material_shader());
    before passing the uniform's value.
    The ProgramObject needs to be made part of the current state.

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2007
    Posts
    3

    Re: Can't pass values into the shader

    It's a Blender problem.
    I've copied my code into another app and it works fine.
    Now I have to discover what the problem is.

Posting Permissions

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