0

This code must draw two sinus-lines. Here two usual lines are transformed via shader into sinus-lines. I have a task to rotate one of them(for example, for 45 or 90 degrees), how to do it?

void CMyApplication::OnDraw()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgramObjectARB(m_program);

    glUniform1fARB(m_amplitudeUniformLocation, 0.3f);
    glUniform1fARB(m_phaseUniformLocation, m_phase);
    glUniform1fARB(m_frequencyUniformLocation, M_PI);
    glUniform1fARB(m_colourUniformLocation, 0.0f);
    //glUniform1fARB(m_rotateUniformLocation, true);
    //glLoadIdentity();
    //glRotatef(90.0f, 0.0f, 0.0f, 0.0f);
    glBegin(GL_LINE_STRIP);
    {
        for (float x = -1; x <= 1.05; x += 0.01)
        {
            glVertex3f(x, 0, 0);
            glColor3f(0, 0.5, -0.8);
        }
    }
    glEnd();
    //glLoadIdentity();
    //glRotatef(-90.0f, 0.0f, 0.0f, 0.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glUniform1fARB(m_phaseUniformLocation, m_phase - 0.5);
    glUniform1fARB(m_colourUniformLocation, 0.5f);
    //glUniform1fARB(m_rotateUniformLocation, false);

    glBegin(GL_LINE_STRIP);
    {
        for (float x = -1; x <= 1.05; x += 0.01)
        {
            glVertex3f(x, -0.5, 0);
            glColor3f(0, -0.5, 0.8);
        }
    }
    glEnd();

    __glewMatrixRotatefEXT(GL_MODELVIEW, 45, 0, 0, 1); //THIS STRING HAS BEEN RESOLVED         //MY PROBLEM

    glUseProgramObjectARB(NULL);

    glutSwapBuffers();
}

Shader below:

uniform float phase;                
uniform float amplitude;            
uniform float frequency;                           
void main()                     
{                                   
    vec4 v = gl_Vertex;                                 
    v.y += amplitude * sin(frequency * v.x + phase);    
    gl_Position = gl_ModelViewProjectionMatrix * v;     
    gl_FrontColor = gl_Color;       
 }
  • Holy [`ARB_shader_objects`](http://www.opengl.org/registry/specs/ARB/shader_objects.txt) Batman! – genpfault Jan 15 '14 at 16:08
  • 1
    You should edit in a [SSCCE](http://sscce.org/) that contains your shaders. – genpfault Jan 15 '14 at 17:12
  • OK.Is any function to rotate image? I have tried to rotate it, sending to shader glRotate function, but compilation files. It seems, that this function doesn't match for shader language – Андрей Бобков Jan 15 '14 at 17:55
  • 1
    It is impossible to answer your question without showing us the shaders. – Andon M. Coleman Jan 15 '14 at 19:01
  • Shader has been added. – Андрей Бобков Jan 15 '14 at 19:33
  • Problem has been resolved by adding the next string : __glewMatrixRotatefEXT(GL_MODELVIEW, 45, 0, 0, 1); – Андрей Бобков Jan 15 '14 at 19:44
  • 1
    @user3164957: You may not be aware of this, but `glMatrixRotatefEXT (...)` is part of an extension called `GL_EXT_direct_state_access`. Only drivers published by NV and AMD after 2008 actually implement this extension. If you are trying to write a code path that supports ***very old*** drivers (as your use of `GL_ARB_shader_objects` suggests), it is best that you stay away from this extension. – Andon M. Coleman Jan 15 '14 at 20:10
  • 1
    The only thing that `glMatrixRotatefEXT (...)` does for you in this example is allow you to rotate the ModelView matrix without setting the matrix mode to `GL_MODELVIEW`. It is completely redundant as only a few lines prior to calling this, you already set the matrix mode to modelview. You can use `glRotatef` in its place. – Andon M. Coleman Jan 15 '14 at 20:11

1 Answers1

0

Because you are using shaders, glRotatef will not function as expected. The fixed-function pipeline transformation calls are turned off once the programmable pipeline is enabled with a shader. So in essence, glRotatef does nothing here unless you are already using the correct matrix in your vertex shader.

To achieve the result you are looking for, you need to generate the appropriate rotation matrix, pass it to the vertex shader in a uniform variable, then multiply it on to your created line before applying the WVP transform. The end result should give you the rotated line.

mbillock
  • 52
  • 2
  • 2
    `glRotatef (...)` is not "turned off." Assuming you have a context where the matrix stack still exists (e.g. not 3.2+ core), it still multiplies the current matrix by a rotation matrix. In fact, since this code is using the ancient ARB extension for GLSL, you can even access the fixed-function matrices from within GLSL using `gl_ModelViewProjectionMatrix`, etc. Core profiles do not support this, which may be the source of your confusion. – Andon M. Coleman Jan 15 '14 at 18:59
  • I should note that the original question did not include the appropriate use of the gl_ModelViewProjectionMatrix in a vertex shader, but instead implied that nothing was being done other than simply calling glRotatef which would require additional effort if the shader wasn't written correctly. I've updated my answer to reflect this, but it is still a generalized answer with some caveats. That's probably the true source of my confusion. – mbillock Jan 20 '14 at 17:57