Vertex shader doesn't work well with cloned objects











up vote
1
down vote

favorite












I'm using OpenGL to create a sphere (approximation):



I'm "inflating" the triangle to create an eight of a sphere:



enter image description here



enter image description here



I'm then drawing that octant four times, and each time rotating the model transoformation by 90°, to achieve a hemisphere:



enter image description here



Code related to drawing calls:



for (int i = 0; i < 4; i++) {
model_trans = glm::rotate(model_trans, glm::radians(i * 90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
glUniformMatrix4fv(uniform_model, 1, GL_FALSE, glm::value_ptr(model_trans));
glDrawElementsBaseVertex(GL_TRIANGLES, sizeof(sphere_indices) / sizeof(sphere_indices[0]),
GL_UNSIGNED_INT, 0, (sizeof(grid_vertices)) / (ATTR_COUNT * sizeof(GLfloat)));
}


My goal is to color each vertex based on the angle of its projection in the XY-plane. Since I'm normalizing values, the resulting projection should behave like an trigonometric circle, x value being the cosine value of the angle with the positive end of x-axis. And because the cosine is an continuous function, my sphere should have continual color, too. However, it is not so:



enter image description here



Is this issue caused by cloning the object? That's the only thing I can think of, but it shouldn't matter, since the vertex shader only receives individual vertices. Speaking of which, here is my vertex shader:



#version 150 core

in vec3 position;
/* flat : the color will be sourced from the provoking vertex. */
flat out vec3 Color;

/* transformation matrices */
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main() {

gl_Position = projection * view * model * vec4(position, 1.0);

vec3 vector_proj = vec3(position.x, position.y, 0.0);
normalize(vector_proj);
/* Addition and division only for mapping range [-1, +1] to [0, 1] */
float cosine = (vector_proj.x + 1) / 2;
Color = vec3(cosine);
}









share|improve this question




























    up vote
    1
    down vote

    favorite












    I'm using OpenGL to create a sphere (approximation):



    I'm "inflating" the triangle to create an eight of a sphere:



    enter image description here



    enter image description here



    I'm then drawing that octant four times, and each time rotating the model transoformation by 90°, to achieve a hemisphere:



    enter image description here



    Code related to drawing calls:



    for (int i = 0; i < 4; i++) {
    model_trans = glm::rotate(model_trans, glm::radians(i * 90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
    glUniformMatrix4fv(uniform_model, 1, GL_FALSE, glm::value_ptr(model_trans));
    glDrawElementsBaseVertex(GL_TRIANGLES, sizeof(sphere_indices) / sizeof(sphere_indices[0]),
    GL_UNSIGNED_INT, 0, (sizeof(grid_vertices)) / (ATTR_COUNT * sizeof(GLfloat)));
    }


    My goal is to color each vertex based on the angle of its projection in the XY-plane. Since I'm normalizing values, the resulting projection should behave like an trigonometric circle, x value being the cosine value of the angle with the positive end of x-axis. And because the cosine is an continuous function, my sphere should have continual color, too. However, it is not so:



    enter image description here



    Is this issue caused by cloning the object? That's the only thing I can think of, but it shouldn't matter, since the vertex shader only receives individual vertices. Speaking of which, here is my vertex shader:



    #version 150 core

    in vec3 position;
    /* flat : the color will be sourced from the provoking vertex. */
    flat out vec3 Color;

    /* transformation matrices */
    uniform mat4 model;
    uniform mat4 view;
    uniform mat4 projection;

    void main() {

    gl_Position = projection * view * model * vec4(position, 1.0);

    vec3 vector_proj = vec3(position.x, position.y, 0.0);
    normalize(vector_proj);
    /* Addition and division only for mapping range [-1, +1] to [0, 1] */
    float cosine = (vector_proj.x + 1) / 2;
    Color = vec3(cosine);
    }









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm using OpenGL to create a sphere (approximation):



      I'm "inflating" the triangle to create an eight of a sphere:



      enter image description here



      enter image description here



      I'm then drawing that octant four times, and each time rotating the model transoformation by 90°, to achieve a hemisphere:



      enter image description here



      Code related to drawing calls:



      for (int i = 0; i < 4; i++) {
      model_trans = glm::rotate(model_trans, glm::radians(i * 90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
      glUniformMatrix4fv(uniform_model, 1, GL_FALSE, glm::value_ptr(model_trans));
      glDrawElementsBaseVertex(GL_TRIANGLES, sizeof(sphere_indices) / sizeof(sphere_indices[0]),
      GL_UNSIGNED_INT, 0, (sizeof(grid_vertices)) / (ATTR_COUNT * sizeof(GLfloat)));
      }


      My goal is to color each vertex based on the angle of its projection in the XY-plane. Since I'm normalizing values, the resulting projection should behave like an trigonometric circle, x value being the cosine value of the angle with the positive end of x-axis. And because the cosine is an continuous function, my sphere should have continual color, too. However, it is not so:



      enter image description here



      Is this issue caused by cloning the object? That's the only thing I can think of, but it shouldn't matter, since the vertex shader only receives individual vertices. Speaking of which, here is my vertex shader:



      #version 150 core

      in vec3 position;
      /* flat : the color will be sourced from the provoking vertex. */
      flat out vec3 Color;

      /* transformation matrices */
      uniform mat4 model;
      uniform mat4 view;
      uniform mat4 projection;

      void main() {

      gl_Position = projection * view * model * vec4(position, 1.0);

      vec3 vector_proj = vec3(position.x, position.y, 0.0);
      normalize(vector_proj);
      /* Addition and division only for mapping range [-1, +1] to [0, 1] */
      float cosine = (vector_proj.x + 1) / 2;
      Color = vec3(cosine);
      }









      share|improve this question















      I'm using OpenGL to create a sphere (approximation):



      I'm "inflating" the triangle to create an eight of a sphere:



      enter image description here



      enter image description here



      I'm then drawing that octant four times, and each time rotating the model transoformation by 90°, to achieve a hemisphere:



      enter image description here



      Code related to drawing calls:



      for (int i = 0; i < 4; i++) {
      model_trans = glm::rotate(model_trans, glm::radians(i * 90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
      glUniformMatrix4fv(uniform_model, 1, GL_FALSE, glm::value_ptr(model_trans));
      glDrawElementsBaseVertex(GL_TRIANGLES, sizeof(sphere_indices) / sizeof(sphere_indices[0]),
      GL_UNSIGNED_INT, 0, (sizeof(grid_vertices)) / (ATTR_COUNT * sizeof(GLfloat)));
      }


      My goal is to color each vertex based on the angle of its projection in the XY-plane. Since I'm normalizing values, the resulting projection should behave like an trigonometric circle, x value being the cosine value of the angle with the positive end of x-axis. And because the cosine is an continuous function, my sphere should have continual color, too. However, it is not so:



      enter image description here



      Is this issue caused by cloning the object? That's the only thing I can think of, but it shouldn't matter, since the vertex shader only receives individual vertices. Speaking of which, here is my vertex shader:



      #version 150 core

      in vec3 position;
      /* flat : the color will be sourced from the provoking vertex. */
      flat out vec3 Color;

      /* transformation matrices */
      uniform mat4 model;
      uniform mat4 view;
      uniform mat4 projection;

      void main() {

      gl_Position = projection * view * model * vec4(position, 1.0);

      vec3 vector_proj = vec3(position.x, position.y, 0.0);
      normalize(vector_proj);
      /* Addition and division only for mapping range [-1, +1] to [0, 1] */
      float cosine = (vector_proj.x + 1) / 2;
      Color = vec3(cosine);
      }






      opengl glsl vertex-shader opengl-3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 19:08









      Rabbid76

      30k112842




      30k112842










      asked Nov 10 at 18:45









      Aleksandar Stefanović

      6821926




      6821926
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You want to calculate the color associated to the vertex from the world coordinate of the vertex position.



          position is the model coordinate, but not the world coordinate. You have to apply the model matrix to position to transform from model space to world space, before calculating vector_proj:



          vec4 world_pos = model * vec4(position, 1.0);

          vec3 vector_proj = vec3( world_pos.xy, 0.0 );




          The parameter to normalize is not an in-out parameter. It is an input parameter, the normalized result is returned from the function:



          vector_proj = normalize(vector_proj);    




          You can simplify the code as follows:



          void main()
          {
          vec4 world_pos = model * vec4(position, 1.0);
          gl_Position = projection * view * world_pos;

          vec2 vector_proj = normalize(world_pos.xy);

          /* Addition and division only for mapping range [-1, +1] to [0, 1] */
          float cosine = (vector_proj.x + 1) / 2;
          Color = vec3(cosine);
          }





          share|improve this answer























          • That was beautiful. Thank you!
            – Aleksandar Stefanović
            Nov 10 at 20:27










          • @AleksandarStefanović You're welcome.
            – Rabbid76
            Nov 10 at 20:30











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242267%2fvertex-shader-doesnt-work-well-with-cloned-objects%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You want to calculate the color associated to the vertex from the world coordinate of the vertex position.



          position is the model coordinate, but not the world coordinate. You have to apply the model matrix to position to transform from model space to world space, before calculating vector_proj:



          vec4 world_pos = model * vec4(position, 1.0);

          vec3 vector_proj = vec3( world_pos.xy, 0.0 );




          The parameter to normalize is not an in-out parameter. It is an input parameter, the normalized result is returned from the function:



          vector_proj = normalize(vector_proj);    




          You can simplify the code as follows:



          void main()
          {
          vec4 world_pos = model * vec4(position, 1.0);
          gl_Position = projection * view * world_pos;

          vec2 vector_proj = normalize(world_pos.xy);

          /* Addition and division only for mapping range [-1, +1] to [0, 1] */
          float cosine = (vector_proj.x + 1) / 2;
          Color = vec3(cosine);
          }





          share|improve this answer























          • That was beautiful. Thank you!
            – Aleksandar Stefanović
            Nov 10 at 20:27










          • @AleksandarStefanović You're welcome.
            – Rabbid76
            Nov 10 at 20:30















          up vote
          1
          down vote



          accepted










          You want to calculate the color associated to the vertex from the world coordinate of the vertex position.



          position is the model coordinate, but not the world coordinate. You have to apply the model matrix to position to transform from model space to world space, before calculating vector_proj:



          vec4 world_pos = model * vec4(position, 1.0);

          vec3 vector_proj = vec3( world_pos.xy, 0.0 );




          The parameter to normalize is not an in-out parameter. It is an input parameter, the normalized result is returned from the function:



          vector_proj = normalize(vector_proj);    




          You can simplify the code as follows:



          void main()
          {
          vec4 world_pos = model * vec4(position, 1.0);
          gl_Position = projection * view * world_pos;

          vec2 vector_proj = normalize(world_pos.xy);

          /* Addition and division only for mapping range [-1, +1] to [0, 1] */
          float cosine = (vector_proj.x + 1) / 2;
          Color = vec3(cosine);
          }





          share|improve this answer























          • That was beautiful. Thank you!
            – Aleksandar Stefanović
            Nov 10 at 20:27










          • @AleksandarStefanović You're welcome.
            – Rabbid76
            Nov 10 at 20:30













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You want to calculate the color associated to the vertex from the world coordinate of the vertex position.



          position is the model coordinate, but not the world coordinate. You have to apply the model matrix to position to transform from model space to world space, before calculating vector_proj:



          vec4 world_pos = model * vec4(position, 1.0);

          vec3 vector_proj = vec3( world_pos.xy, 0.0 );




          The parameter to normalize is not an in-out parameter. It is an input parameter, the normalized result is returned from the function:



          vector_proj = normalize(vector_proj);    




          You can simplify the code as follows:



          void main()
          {
          vec4 world_pos = model * vec4(position, 1.0);
          gl_Position = projection * view * world_pos;

          vec2 vector_proj = normalize(world_pos.xy);

          /* Addition and division only for mapping range [-1, +1] to [0, 1] */
          float cosine = (vector_proj.x + 1) / 2;
          Color = vec3(cosine);
          }





          share|improve this answer














          You want to calculate the color associated to the vertex from the world coordinate of the vertex position.



          position is the model coordinate, but not the world coordinate. You have to apply the model matrix to position to transform from model space to world space, before calculating vector_proj:



          vec4 world_pos = model * vec4(position, 1.0);

          vec3 vector_proj = vec3( world_pos.xy, 0.0 );




          The parameter to normalize is not an in-out parameter. It is an input parameter, the normalized result is returned from the function:



          vector_proj = normalize(vector_proj);    




          You can simplify the code as follows:



          void main()
          {
          vec4 world_pos = model * vec4(position, 1.0);
          gl_Position = projection * view * world_pos;

          vec2 vector_proj = normalize(world_pos.xy);

          /* Addition and division only for mapping range [-1, +1] to [0, 1] */
          float cosine = (vector_proj.x + 1) / 2;
          Color = vec3(cosine);
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 19:09

























          answered Nov 10 at 19:03









          Rabbid76

          30k112842




          30k112842












          • That was beautiful. Thank you!
            – Aleksandar Stefanović
            Nov 10 at 20:27










          • @AleksandarStefanović You're welcome.
            – Rabbid76
            Nov 10 at 20:30


















          • That was beautiful. Thank you!
            – Aleksandar Stefanović
            Nov 10 at 20:27










          • @AleksandarStefanović You're welcome.
            – Rabbid76
            Nov 10 at 20:30
















          That was beautiful. Thank you!
          – Aleksandar Stefanović
          Nov 10 at 20:27




          That was beautiful. Thank you!
          – Aleksandar Stefanović
          Nov 10 at 20:27












          @AleksandarStefanović You're welcome.
          – Rabbid76
          Nov 10 at 20:30




          @AleksandarStefanović You're welcome.
          – Rabbid76
          Nov 10 at 20:30


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242267%2fvertex-shader-doesnt-work-well-with-cloned-objects%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues