🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

OpenGL Scaling Problem (What are World and Object coordinates???)

Started by
1 comment, last by sarbz 6 years, 1 month ago

 

Well i found out Here what's the problem and how to solve it (Something about world coordinates and object coordinates) but i can't understand how ti works. Can you show me some examples in code on how you implement this???

 

Scaling Matrix:


	m_Impl->scale = glm::mat4(1.0f);
	m_Impl->scale = glm::scale(m_Impl->scale, glm::vec3(width, height, 0));

Verticies:


	//Verticies.
	float verticies[] =
	{

		//Positions.		//Texture Coordinates.
		1.0f,    1.0f,      0.0f, 0.0f,
		2.0f,    1.0f,      1.0f, 0.0f,
		2.0f,    2.0f,      1.0f, 1.0f,
		1.0f,    2.0f,      0.0f, 1.0f
	};

Rendering:


	//Projection Matrix.
	glm::mat4 proj = glm::ortho(0.0f, (float)window->GetWidth(), 0.0f, (float)window->GetHeight(), -1.0f, 1.0f);

	//Set the uniform.
	material->program->setUniformMat4f("u_MVP", proj * model); //model is the scale matrix from the previous code.

	//Draw.
	glDrawElements(GL_TRIANGLES, material->ibo->GetCount(), GL_UNSIGNED_INT, NULL);

 

Shader:


#shader vertex
#version 330 core

layout(location = 0) in vec4 aPos;
layout(location = 1) in vec2 aTexCoord;

out vec2 texCoord;

uniform mat4 u_MVP;

void main()
{
	gl_Position  = u_MVP*aPos;
	texCoord     = aTexCoord;
}







#shader fragment
#version 330 core

out vec4 colors;
in vec2  texCoord;

uniform sampler2D u_Texture;

void main()
{
	colors = texture(u_Texture, texCoord);
}

 

Before Scaling (It's down there on the bottom left corner as a dot).

first.PNG

 

After Scaling

second.PNG

 

Problem: Why does the position also changes?? If you see my Verticies, the first position starts at 1.0f, 1.0f , so when i'm scaling it should stay at that position


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

Advertisement

When scaling, only the number 0 is used as an anchor. Your initial position of (1.0, 1.0) was multiplied by the scaling factor of (image width, image height), resulting in the image being translated by that amount. For your vertices, have the position coordinates be centered around 0, and not 1. So to fix your problem, replace the 1's to 0's, and replace the 2's to 1's for all your position values:


float verticies[] =
{
  //Positions.		//Texture Coordinates.
  0.0f,    0.0f,      0.0f, 0.0f,
  1.0f,    0.0f,      1.0f, 0.0f,
  1.0f,    1.0f,      1.0f, 1.0f,
  0.0f,    1.0f,      0.0f, 1.0f
};

 

This topic is closed to new replies.

Advertisement