Square is not moving...

I’m trying move square, but it’s not happening. I’m just started using “glm”. So my doubt is how to move object using “glm”?. Or if you not able to understand following code, then send code for this using “glfw” & “glm” .Here is code:

#include <GLFW/glfw3.h>

#include<glm/glm.hpp>
#include<glm/gtc/matrix_transform.hpp>
#include<glm/gtc/type_ptr.hpp>

#define SCREEN_WIDTH 500
#define SCREEN_HEIGHT 500

#include<iostream>
using namespace std;

int posx = 0;

void Coordinate_System(void)
{
    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-100, 100, -100, 100, 0, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Process_Input(GLFWwindow* window)  
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true); 
}

int main(void)
{
    glfwInit();
    GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    float vertices[] = {
        0,0,0,
        10,0,0,
        10,10,0,
        0,10,0
    };

    Coordinate_System();

    while (!glfwWindowShouldClose(window))
    {
        Process_Input(window);

        glClearColor(0.2, 0.3, 0.3, 0.1);
        glClear(GL_COLOR_BUFFER_BIT);

        glEnableClientState(GL_VERTEX_ARRAY);   
        glVertexPointer(3, GL_FLOAT, 0, vertices);

        glm::vec4 vec(1.0f, 0.0f, 0.0f, 1.0f);
        glm::mat4 trans = glm::mat4(1.0f);
        trans = glm::translate(trans, glm::vec3(1.0f, 100.0f, 0.0f));
        vec = trans * vec;
        cout << vec.x << " " << vec.y << " " << vec.z << endl;

        glDrawArrays(GL_POLYGON, 0, 4);
        glDisableClientState(GL_VERTEX_ARRAY);
    
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
  }

You need to update the GL_MODELVIEW matrix before rendering. Note that this is OpenGL, not GLFW - GLFW handles the creation of windows, mouse and keyboard feedback etc. and has functions named glfw* whereas OpenGL has functions named gl*.

You could do that with glTranslatef and your translation or create the translation matrix and call glLoadMatrix.

You will also need to alter the translation every frame to get some movement, otherwise you will just be setting the coordinates.

I don’t know glm, but looking online I found this article on glm and so I think you would need to use something like:

glm::mat4 trans = glm::mat4(1.0f);
trans = glm::translate(trans, glm::vec3(1.0f, 100.0f, 0.0f));
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf( glm::value_ptr( trans ) );

Note this will place the square, but if you want it to keep moving you need to change the value of the translation vector every frame.

Dear Doug Binks,
Please accept my many thanks for your message. I’m beginner in graphics programming. Snippet you suggests throws error “‘glLoadMatrix’: identifier not found”.
According to your link glLoadMatrix two options are legal: glLoadMatrixd and glLoadMatrixf. Second works in my program. Regards.

Many thanks for the correction, I’ve fixed the code snippet now.

Good luck in your programming journey!

Dear Doug Binks,
Please accept my many thanks for you replied and that immediately.
By the way let me ask you how I can “change the value of the translation vector every frame”. Program I work is yours at A very basic issue with draw line.
Regards.

In the case of the draw lines program rather than changing the GL_MODELVIEW matrix it is simplest to just change the line coordinates.

So we change:

void render_loop()
{
	glClearColor ( 1.0f, 1.0f, 1.0f, 1.0f );
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPointSize(10.0f);
	glLineWidth(2.5f); 
	glColor3f(1.0f, 0.0f, 0.0f);
	glBegin(GL_LINES);
	glVertex3f(10.0f,10.0f,0.0f);
	glVertex3f(200.0f,200.0f,0.0f);
	glEnd();
}

to something like:

void render_loop( float x, float y, float z)
{
	glClearColor ( 1.0f, 1.0f, 1.0f, 1.0f );
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPointSize(10.0f);
	glLineWidth(2.5f); 
	glColor3f(1.0f, 0.0f, 0.0f);
	glBegin(GL_LINES);
	glVertex3f(10.0f+x,10.0f+y,0.0f+z);
	glVertex3f(200.0f+x,200.0f+y,0.0f+z);
	glEnd();
}

and then in your main function you change the main loop from:

	// Main loop
	while( !glfwWindowShouldClose(window) )
	{
		// Draw gears
		render_loop();

		// Swap buffers
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

to:

	// Main loop
	float x = 0.0f, y=0.0f,z=0.0f;
	float speed = 10.0f;
	double time = glfwGetTime();
	while( !glfwWindowShouldClose(window) )
	{
		// Update time and position
		double timeNow = glfwGetTime();
		double deltaTime = timeNow - time;
		x = x + speed * deltaTime;
		if( x > 400.0f ) // move back to beginning once out of window
		{
		    x = 0.0f;
		}

		// Draw
		render_loop(x,y,z);

		// Swap buffers
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

Note that I’ve not compiled or tested this code, so it may need changes.

Dear Teacher,
Please let me express my sincere gratitude for your instructive answer, and my sorrow for delay in response. I have to learn a lot from you.
Regards.

Dear Teacher,
Please let me say that Visual Studio 2019 compiler throws some warnings for double and float, and one error:
‘time’: undeclared identifier
I added
double time{ 0 }; above
double deltaTime = timeNow - time; and program works.
I’m interested for a horizontal line moving to left with stable speed and never return, though. I have modified your snippets in below program. My question now is how many pixels per second correspond to speed = 1.0? Regards.

#include <GLFW/glfw3.h>
#include <gl/GL.h>
#include <iostream>


void render_loop(float x)
{
float a1{ 0.2 }, b1{ 0.2 }, c1{ 1.0 };
float a2{ 1.0 }, b2{ 1.0 }, c2{ 0.2 };

glClearColor(a1, b1, c1, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPointSize(10);
glLineWidth(2);
glColor3f(a2, b2, c2);
glBegin(GL_LINES);
glVertex3f(800.0f+x, 700.0f, 0.0f);
glVertex3f(1000.0f+x, 700.0f, 0.0);
glEnd();
}

/* program entry */
int main(int argc, char* argv[])
{
GLFWwindow* window;

if (!glfwInit())
{
	std::cout << "Failed to initialize GLFW\n";
	exit(EXIT_FAILURE);
}

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(1200, 800, "LearnOpenGL", NULL, NULL);
if (!window)
{
	std::cout << "Failed to open GLFW window\n";
	glfwTerminate();
	exit(EXIT_FAILURE);
}


glfwMakeContextCurrent(window);
glfwSwapInterval(1);

// set up view
glViewport(0, 0, 1200, 800);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// see https://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml
glOrtho(0.0, 800.0, 0.0, 800.0, 0.0, 1.0); // this creates a canvas you can do 2D drawing on


// Main loop
float x = 0.0f, y = 0.0f, z = 0.0f;
float speed = 10.0f;
while (!glfwWindowShouldClose(window))
{
	//Update time and position
	//double timeNow = glfwGetTime();
	//double time{ 0 };
	//double deltaTime = timeNow - time;
	x = x - speed;// *deltaTime;

	// Draw gears
	render_loop(x);

	// Swap buffers
	glfwSwapBuffers(window);
	glfwPollEvents();
}

// Terminate GLFW
glfwTerminate();

// Exit program
exit(EXIT_SUCCESS);
}

I’m afraid I don’t have the time to help with general math and programming questions not related to using GLFW, but I think the corrected code (again completely untested) is:

	// Main loop
	float x = 0.0f, y=0.0f,z=0.0f;
	float speed = 10.0f;
	double time = glfwGetTime();
	while( !glfwWindowShouldClose(window) )
	{
		// Update time and position
		double timeNow = glfwGetTime();
		float deltaTime = static_cast<float>( timeNow - time );
                time  = timeNow;
		x = x + speed * deltaTime;
		if( x > 400.0f ) // move back to beginning once out of window
		{
		    x = 0.0f;
		}

		// Draw
		render_loop(x,y,z);

		// Swap buffers
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

Good luck with getting this working from here!

Mr. Doug Binks,
Please let me the question. In snippets you suggest me parameters in
glVertex3f(200.0f+x,200.0f+y,0.0f+z);
are floats, so do not represent pixels. Then what do they represent?
Thanks in advance. Regards.

These represent the x,y,z coordinates of a 3d vertex position which is then transformed by the OpenGL matrix stack and viewport to result in a screen position.

I suggest you try searching for OpenGL tutorials to find some which will guide you. At the moment you are using what is called ‘fixed function OpenGL’, and modern graphics are drawn using the programmable pipeline with shaders. If you find the later too complex try searching for ‘fixed function OpenGL tutorials’.

Mr. Doug Binks,
Please accept my many thanks for you answered my question and many more for your instructive suggestions.
Regards.

Dear Doug Binks,
Please let me say you that I can’t find tutorials for ‘fixed function OpenGL’. I’m interested in 2D graphics and vertex’s parameters represent pixels, not numbers betwwen -1 and 1. Then do you suggest me 2D tutorials or 3D and me apply 0.0 as third dimension? Also do you suggest me GLAD?
Regards.

If you are interested in only 2D graphics then you might find using a 2D library like NanoVg or dear imgui.

If you want to learn OpenGL then perhaps https://learnopengl.com/Introduction is a good start, even though it uses shaders.

Good luck - note that this forum is for GLFW rather than OpenGL and you might find better help on other forums.

1 Like

Dear Doug Binks,
Please accept my thanks for you replied and many more for your suggestion.
Regards

Dear Doug Binks,

Could you please suggest me a website for learn NanoVg?

With regards and friendship
Georges Theodosiou

Sadly I don’t know any, but there is a demo as part of the code on Github.

Mr Doug Binks,
Please accept my thanks for you responded my message. It’s helpful though.
With regards and friendship,
Georges Theodosiouy