Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 1 of 1

Thread: Translate Transformation Does Not Behave As I Expected!!!

  1. #1
    Junior Member Newbie
    Join Date
    May 2018

    Translate Transformation Does Not Behave As I Expected!!!

    First take a look of the behavior:

    Youtube Video

    I was expecting to see the player moving around and not that C image. I believe, for some reason i'm giving a "Camera" effect but i don't know why and also i don't want this for my game.

    Take a look at my rendering code and Move Method:

    Sprite Object Render Method
    Code :
    void Sprite::Render(Window * window)
    	//Create the projection Matrix based on the current window width and height.
    	glm::mat4 proj = glm::ortho(0.0f, (float)window->GetWidth(), 0.0f, (float)window->GetHeight(), -1.0f, 1.0f);
    	//Set the MVP Uniform.
    	m_Impl->program->setUniformMat4f("u_MVP", proj * m_Impl->model);
    	//Run All The Brains (Scripts) of this game object (sprite).
    	for (unsigned int i = 0; i < m_Impl->brains.size(); i++)
    		//Get Current Brain.
    		Brain *brain = m_Impl->brains[i];
    		//Call the start function only once!
    		if (brain->GetStart())
    		//Call the update function every frame.
    	window->GetRenderer()->Draw(m_Impl->vao, m_Impl->ibo, m_Impl->texture, m_Impl->program);

    Code :
    void Renderer::Draw(VertexArray * vao, IndexBuffer * ibo, Texture *texture, Shader * program)
    	if (texture != NULL)
    	GLCall(glDrawElements(GL_TRIANGLES, ibo->GetCount(), GL_UNSIGNED_INT, NULL));

    Move Method
    Code :
    void Sprite::Move(float speed, bool left, bool right, bool up, bool down)
    	if (left)
    		m_Impl->pos.x -= speed;
    		m_Impl->model = glm::translate(m_Impl->model, glm::vec3(-speed, 0, 0));
    	if (right)
    		m_Impl->pos.x += speed;
    		m_Impl->model = glm::translate(m_Impl->model, glm::vec3(speed, 0, 0));
    	if (up)
    		m_Impl->pos.y += speed;
    		m_Impl->model = glm::translate(m_Impl->model, glm::vec3(0, speed, 0));
    	if (down)
    		m_Impl->pos.y -= speed;
    		m_Impl->model = glm::translate(m_Impl->model, glm::vec3(0, -speed, 0));

    Also, please notice that my vertex data are fixed:
    Code :
    Sprite::Sprite(std::string image_path, std::string tag, float x, float y)
    	//Create Pointer To Implementaion.
    	m_Impl = new Implementation();
    	//Set the Position of the Sprite object.
    	m_Impl->pos.x = x;
    	m_Impl->pos.y = y;
    	//Set the tag.
    	m_Impl->tag = tag;
    	//Create The Texture.
    	m_Impl->texture = new Texture(image_path);
    	//Initialize the model Matrix.
    	m_Impl->model = glm::mat4(1.0f);
    	//Get the Width and the Height of the Texture.
    	int width  = m_Impl->texture->GetWidth();
    	int height = m_Impl->texture->GetHeight();
    	//Create the Verticies.
    	float verticies[] =
    		//Positions                //Texture Coordinates.
    		x, y,                      0.0f, 0.0f,
    		x + width, y,              1.0f, 0.0f,
    		x + width, y + height,     1.0f, 1.0f,
    		x, y + height,             0.0f, 1.0f
    	//Create the Indicies.
    	unsigned int indicies[] =
    		0, 1, 2,
    		2, 3, 0
    	//Create Vertex Array.
    	m_Impl->vao = new VertexArray();
    	//Create the Vertex Buffer.
    	m_Impl->vbo = new VertexBuffer((void *)verticies, sizeof(verticies));
    	//Create The Layout.
    	m_Impl->layout = new VertexBufferLayout();
    	m_Impl->vao->AddBuffer(m_Impl->vbo, m_Impl->layout);
    	//Create the Index Buffer.
    	m_Impl->ibo = new IndexBuffer(indicies, 6);
    	//Create the new shader.
    	m_Impl->program = new Shader("Shaders/SpriteShader.shader");

    As you can see in my Sprite Class i have a m_Impl->model Matrix which i update every time inside the Move Method.
    Inside the Sprite Render Method, i multiply the Projection (proj) Matriw with the model (m_Impl->model) Matrix and setting a uniform.

    Take a look also at my Shader:
    Code :
    #shader vertex
    #version 330 core
    layout(location = 0) in vec4 aPos;
    layout(location = 1) in vec2 aTexCoord;
    out vec2 t_TexCoord;
    uniform mat4 u_MVP;
    void main()
    	gl_Position = u_MVP * aPos;
    	t_TexCoord = aTexCoord;
    #shader fragment
    #version 330 core
    out vec4 aColor;
    in vec2 t_TexCoord;
    uniform sampler2D u_Texture;
    void main()
    	aColor = texture(u_Texture, t_TexCoord);

    Probably i didn't understand the Transformation Matrices correctly or the ViewPort option and i'm doing something wrong.

    I'm also setting a glViewPort(0, 0, screen_width, screen_height)

    This seems so weird. How it is possible to translate the Player's vertices but the Player is always being drawn at the same position in the screen
    and that C image which i do not translate, is being drawn elsewhere every frame. I literally can't understand what's happening.

    Thanks for your help!!!
    Last edited by babaliaris; 05-16-2018 at 02:33 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts