OpenGL Transform

Hi,
In opengl, it is said that, always scale first then translate. But in the below code it is not working.
It only works if translate is called before scaling.
Can anyone tell me how to do the transformation is legacy opengl?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdlib.h>
#include <stdio.h>

#include <glfw3.h>

struct Transform
{
    float x = 0;
    float y = 0;
};

Transform position;
Transform scale;

static void error_callback(int error, const char* description)
{
    fputs(description, stderr);
}

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

//! draws a single frame
static void Render()
{
    glPushMatrix();

    glScalef(scale.x, scale.y, 0);
    glTranslatef(position.x, position.y, 0);

    glBegin(GL_QUADS);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(1.0, 1.0, 0.0);
        glVertex3f(0.0, 1.0, 0.0);
    glEnd();

    glPopMatrix();
}

int main(void)
{
    GLFWwindow* window;

    glfwSetErrorCallback(error_callback);

    if (!glfwInit())
        exit(EXIT_FAILURE);

    window = glfwCreateWindow(800, 600, "Simple example", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwMakeContextCurrent(window);

    glfwSetKeyCallback(window, key_callback);

    glViewport(0, 0, 800, 600);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 800, 600, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClearColor(0.6f, 0.6f, 0.6f, 1.0f);

    // Set the properties
    position.x = 110.0f;
    position.y = 200.0f;
    scale.x = 64.0f;
    scale.y = 64.0f;

    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);

        //! Render
        Render();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}


Thank you.
Last edited on
Please describe the "not working".

Have you tried adjusting the position according to the scale?

The problem sounds similar to these:

A pin is on a map. One inch on the map corresponds to a mile.

Case A
Move pin for one inch.
Zoom the map so that one inch corresponds to a quarter-mile.
The pin is now 4 inches, a mile, from original.

Case B
Zoom the map so that one inch corresponds to a quarter-mile.
Move pin for one inch.
The pin is now one inch, a quarter-mile, from original.

Case C
Zoom the map so that one inch corresponds to a quarter-mile.
Move pin for not one but four inches (to counter the zoom).
The pin is now 4 inches, a mile, from original.
That's why I have provided the full code.
Answer is not required anymore. Thinking to implement backward compatibility support was a bad decision.
Topic archived. No new replies allowed.