How to rotate image primitive? I have tried to use glRotatef function, but it does not get result

StackOverflow https://stackoverflow.com/questions/21140908

  •  28-09-2022
  •  | 
  •  

Question

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;       
 }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top