Translate and scale a rectangle with OpenGL 3+
I want to translate and scale a rectangle to specific coordinates. At this time I have to scale the rectangle only in the y plane. The translation works well, I have problems with the scaling, the rectangle is not placed at the right position. I tried to apply another solutions I found here and in another posts in stack overflow without success. This should be very easy, but I can't find the solution. I become from my source the top, bottom, left and right coordinates.
This is a part of the code.
Vertex and Fragment Shader:
vertexShaderSource = "#version 150 n"
"in vec2 aPos;n"
"in vec4 aColor;n"
"uniform mat4 projection;n"
"uniform mat4 model;n"
"uniform bool isLine;n"
"out vec4 oColor;n"
"void main() {n"
" gl_Position = projection * model * vec4(aPos, 0.0, 1.0);n"
" oColor = aColor;n"
"}n";
fragmentShaderSource = "#version 150 n"
"in vec4 oColor;n"
"out vec4 outColor; n"
"void main() {n"
" outColor = oColor;n"
"}n";
Vertices:
GLfloat vertices = {
// positions //colors //alpha
coordsLine->lineRight, coordsLine->top, 0.8f, 0.498f, 0.1960f, 0.4f, // top right
coordsLine->lineRight, coordsLine->bottom, 0.8f, 0.498f, 0.1960f, 0.4f, // bottom right
coordsLine->lineLeft, coordsLine->bottom, 0.8f, 0.498f, 0.1960f, 0.4f, // bottom left
coordsLine->lineLeft, coordsLine->top, 0.8f, 0.498f, 00.1960f, 0.4f // top left
};
Projection:
projection = glm::ortho(0.f, (float)dev->GetWidth(),(float)dev->GetHeight(), 0.f, 0.f, 1.f);
projection = glm::scale(projection, glm::vec3(dev->GetXScale(), dev->GetYScale(), 1.f));
Before the render loop:
tempLineLeft = coordsLine->lineLeft;
tempLineTop = coordsLine->top;lineHeight = coordsLine->bottom - coordsLine->top;
sourceLineMiddleHeight =(coordsLine->bottom - coordsLine->top) / 2.f;
sourceLineMiddleWidth = (coordsLine->lineRight - coordsLine->lineLeft) / 2.f;
I have to scale the projection to a device. This is working well without problems. Here is the translation and scaling, inside the render loop:
//Inside the Render Loop
if(tempLineLeft != coordsLine->lineLeft) {
glUseProgram(shaderProgram);
if(lineHeight != coordsLine->bottom - coordsLine->top) {
destinationLineMiddleHeight = (coordsLine->bottom - coordsLine->top) / 2.f;
scaleY = (coordsLine->bottom - coordsLine->top) / lineHeight;
model = glm::translate(model, glm::vec3(coordsLine->lineLeft - tempLineLeft, coordsLine->top - tempLineTop, 0.f));
model = glm::scale(model, glm::vec3(1.f, scaleY, 1.f));
model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f));
}
else {
model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f));
}
}
if(lineHeight != coordsLine->bottom - coordsLine->top)
lineHeight = coordsLine->bottom - coordsLine->top;
tempLineLeft = coordsLine->lineLeft;
tempLineTop = coordsLine->top;
sourceLineMiddleHeight =(coordsLine->bottom - coordsLine->top) / 2.f;
sourceLineMiddleWidth = (coordsLine->lineRight - coordsLine->lineLeft) / 2.f;
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
EDIT:
The real problem is the following: when I scale the object, it is jumping upwards/downwards, I have to translate it back to the target coordinates, and this doesn't works. I take some pictures, maybe it's clear what I'm meaning:
Object witouth Scale:
Object scaled:
This should be a trivial problem, I cann't find an equation to solve that.
c++ c++11 opengl opengl-3 glm-math
add a comment |
I want to translate and scale a rectangle to specific coordinates. At this time I have to scale the rectangle only in the y plane. The translation works well, I have problems with the scaling, the rectangle is not placed at the right position. I tried to apply another solutions I found here and in another posts in stack overflow without success. This should be very easy, but I can't find the solution. I become from my source the top, bottom, left and right coordinates.
This is a part of the code.
Vertex and Fragment Shader:
vertexShaderSource = "#version 150 n"
"in vec2 aPos;n"
"in vec4 aColor;n"
"uniform mat4 projection;n"
"uniform mat4 model;n"
"uniform bool isLine;n"
"out vec4 oColor;n"
"void main() {n"
" gl_Position = projection * model * vec4(aPos, 0.0, 1.0);n"
" oColor = aColor;n"
"}n";
fragmentShaderSource = "#version 150 n"
"in vec4 oColor;n"
"out vec4 outColor; n"
"void main() {n"
" outColor = oColor;n"
"}n";
Vertices:
GLfloat vertices = {
// positions //colors //alpha
coordsLine->lineRight, coordsLine->top, 0.8f, 0.498f, 0.1960f, 0.4f, // top right
coordsLine->lineRight, coordsLine->bottom, 0.8f, 0.498f, 0.1960f, 0.4f, // bottom right
coordsLine->lineLeft, coordsLine->bottom, 0.8f, 0.498f, 0.1960f, 0.4f, // bottom left
coordsLine->lineLeft, coordsLine->top, 0.8f, 0.498f, 00.1960f, 0.4f // top left
};
Projection:
projection = glm::ortho(0.f, (float)dev->GetWidth(),(float)dev->GetHeight(), 0.f, 0.f, 1.f);
projection = glm::scale(projection, glm::vec3(dev->GetXScale(), dev->GetYScale(), 1.f));
Before the render loop:
tempLineLeft = coordsLine->lineLeft;
tempLineTop = coordsLine->top;lineHeight = coordsLine->bottom - coordsLine->top;
sourceLineMiddleHeight =(coordsLine->bottom - coordsLine->top) / 2.f;
sourceLineMiddleWidth = (coordsLine->lineRight - coordsLine->lineLeft) / 2.f;
I have to scale the projection to a device. This is working well without problems. Here is the translation and scaling, inside the render loop:
//Inside the Render Loop
if(tempLineLeft != coordsLine->lineLeft) {
glUseProgram(shaderProgram);
if(lineHeight != coordsLine->bottom - coordsLine->top) {
destinationLineMiddleHeight = (coordsLine->bottom - coordsLine->top) / 2.f;
scaleY = (coordsLine->bottom - coordsLine->top) / lineHeight;
model = glm::translate(model, glm::vec3(coordsLine->lineLeft - tempLineLeft, coordsLine->top - tempLineTop, 0.f));
model = glm::scale(model, glm::vec3(1.f, scaleY, 1.f));
model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f));
}
else {
model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f));
}
}
if(lineHeight != coordsLine->bottom - coordsLine->top)
lineHeight = coordsLine->bottom - coordsLine->top;
tempLineLeft = coordsLine->lineLeft;
tempLineTop = coordsLine->top;
sourceLineMiddleHeight =(coordsLine->bottom - coordsLine->top) / 2.f;
sourceLineMiddleWidth = (coordsLine->lineRight - coordsLine->lineLeft) / 2.f;
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
EDIT:
The real problem is the following: when I scale the object, it is jumping upwards/downwards, I have to translate it back to the target coordinates, and this doesn't works. I take some pictures, maybe it's clear what I'm meaning:
Object witouth Scale:
Object scaled:
This should be a trivial problem, I cann't find an equation to solve that.
c++ c++11 opengl opengl-3 glm-math
You have to do the scaling first, this means at the end in the code.glm::translate(model,...)
glm::translate(model,...)
glm::scale(model,1.f, scaleY, 1.f)
– Rabbid76
Nov 16 '18 at 6:35
I tried this solution, but the object is not always moved to the right place, sometimes it doesn't jumps to the right coordinates, f.e.model = glm::translate(model, glm::vec3(coordsLine->lineLeft - tempLineLeft, coordsLine->top - tempLineTop, 0.f)); model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f)); model = glm::scale(model, glm::vec3(1.f, scaleY, 1.f));
or should I do the translation to the middle first?
– Luis
Nov 25 '18 at 3:32
add a comment |
I want to translate and scale a rectangle to specific coordinates. At this time I have to scale the rectangle only in the y plane. The translation works well, I have problems with the scaling, the rectangle is not placed at the right position. I tried to apply another solutions I found here and in another posts in stack overflow without success. This should be very easy, but I can't find the solution. I become from my source the top, bottom, left and right coordinates.
This is a part of the code.
Vertex and Fragment Shader:
vertexShaderSource = "#version 150 n"
"in vec2 aPos;n"
"in vec4 aColor;n"
"uniform mat4 projection;n"
"uniform mat4 model;n"
"uniform bool isLine;n"
"out vec4 oColor;n"
"void main() {n"
" gl_Position = projection * model * vec4(aPos, 0.0, 1.0);n"
" oColor = aColor;n"
"}n";
fragmentShaderSource = "#version 150 n"
"in vec4 oColor;n"
"out vec4 outColor; n"
"void main() {n"
" outColor = oColor;n"
"}n";
Vertices:
GLfloat vertices = {
// positions //colors //alpha
coordsLine->lineRight, coordsLine->top, 0.8f, 0.498f, 0.1960f, 0.4f, // top right
coordsLine->lineRight, coordsLine->bottom, 0.8f, 0.498f, 0.1960f, 0.4f, // bottom right
coordsLine->lineLeft, coordsLine->bottom, 0.8f, 0.498f, 0.1960f, 0.4f, // bottom left
coordsLine->lineLeft, coordsLine->top, 0.8f, 0.498f, 00.1960f, 0.4f // top left
};
Projection:
projection = glm::ortho(0.f, (float)dev->GetWidth(),(float)dev->GetHeight(), 0.f, 0.f, 1.f);
projection = glm::scale(projection, glm::vec3(dev->GetXScale(), dev->GetYScale(), 1.f));
Before the render loop:
tempLineLeft = coordsLine->lineLeft;
tempLineTop = coordsLine->top;lineHeight = coordsLine->bottom - coordsLine->top;
sourceLineMiddleHeight =(coordsLine->bottom - coordsLine->top) / 2.f;
sourceLineMiddleWidth = (coordsLine->lineRight - coordsLine->lineLeft) / 2.f;
I have to scale the projection to a device. This is working well without problems. Here is the translation and scaling, inside the render loop:
//Inside the Render Loop
if(tempLineLeft != coordsLine->lineLeft) {
glUseProgram(shaderProgram);
if(lineHeight != coordsLine->bottom - coordsLine->top) {
destinationLineMiddleHeight = (coordsLine->bottom - coordsLine->top) / 2.f;
scaleY = (coordsLine->bottom - coordsLine->top) / lineHeight;
model = glm::translate(model, glm::vec3(coordsLine->lineLeft - tempLineLeft, coordsLine->top - tempLineTop, 0.f));
model = glm::scale(model, glm::vec3(1.f, scaleY, 1.f));
model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f));
}
else {
model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f));
}
}
if(lineHeight != coordsLine->bottom - coordsLine->top)
lineHeight = coordsLine->bottom - coordsLine->top;
tempLineLeft = coordsLine->lineLeft;
tempLineTop = coordsLine->top;
sourceLineMiddleHeight =(coordsLine->bottom - coordsLine->top) / 2.f;
sourceLineMiddleWidth = (coordsLine->lineRight - coordsLine->lineLeft) / 2.f;
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
EDIT:
The real problem is the following: when I scale the object, it is jumping upwards/downwards, I have to translate it back to the target coordinates, and this doesn't works. I take some pictures, maybe it's clear what I'm meaning:
Object witouth Scale:
Object scaled:
This should be a trivial problem, I cann't find an equation to solve that.
c++ c++11 opengl opengl-3 glm-math
I want to translate and scale a rectangle to specific coordinates. At this time I have to scale the rectangle only in the y plane. The translation works well, I have problems with the scaling, the rectangle is not placed at the right position. I tried to apply another solutions I found here and in another posts in stack overflow without success. This should be very easy, but I can't find the solution. I become from my source the top, bottom, left and right coordinates.
This is a part of the code.
Vertex and Fragment Shader:
vertexShaderSource = "#version 150 n"
"in vec2 aPos;n"
"in vec4 aColor;n"
"uniform mat4 projection;n"
"uniform mat4 model;n"
"uniform bool isLine;n"
"out vec4 oColor;n"
"void main() {n"
" gl_Position = projection * model * vec4(aPos, 0.0, 1.0);n"
" oColor = aColor;n"
"}n";
fragmentShaderSource = "#version 150 n"
"in vec4 oColor;n"
"out vec4 outColor; n"
"void main() {n"
" outColor = oColor;n"
"}n";
Vertices:
GLfloat vertices = {
// positions //colors //alpha
coordsLine->lineRight, coordsLine->top, 0.8f, 0.498f, 0.1960f, 0.4f, // top right
coordsLine->lineRight, coordsLine->bottom, 0.8f, 0.498f, 0.1960f, 0.4f, // bottom right
coordsLine->lineLeft, coordsLine->bottom, 0.8f, 0.498f, 0.1960f, 0.4f, // bottom left
coordsLine->lineLeft, coordsLine->top, 0.8f, 0.498f, 00.1960f, 0.4f // top left
};
Projection:
projection = glm::ortho(0.f, (float)dev->GetWidth(),(float)dev->GetHeight(), 0.f, 0.f, 1.f);
projection = glm::scale(projection, glm::vec3(dev->GetXScale(), dev->GetYScale(), 1.f));
Before the render loop:
tempLineLeft = coordsLine->lineLeft;
tempLineTop = coordsLine->top;lineHeight = coordsLine->bottom - coordsLine->top;
sourceLineMiddleHeight =(coordsLine->bottom - coordsLine->top) / 2.f;
sourceLineMiddleWidth = (coordsLine->lineRight - coordsLine->lineLeft) / 2.f;
I have to scale the projection to a device. This is working well without problems. Here is the translation and scaling, inside the render loop:
//Inside the Render Loop
if(tempLineLeft != coordsLine->lineLeft) {
glUseProgram(shaderProgram);
if(lineHeight != coordsLine->bottom - coordsLine->top) {
destinationLineMiddleHeight = (coordsLine->bottom - coordsLine->top) / 2.f;
scaleY = (coordsLine->bottom - coordsLine->top) / lineHeight;
model = glm::translate(model, glm::vec3(coordsLine->lineLeft - tempLineLeft, coordsLine->top - tempLineTop, 0.f));
model = glm::scale(model, glm::vec3(1.f, scaleY, 1.f));
model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f));
}
else {
model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f));
}
}
if(lineHeight != coordsLine->bottom - coordsLine->top)
lineHeight = coordsLine->bottom - coordsLine->top;
tempLineLeft = coordsLine->lineLeft;
tempLineTop = coordsLine->top;
sourceLineMiddleHeight =(coordsLine->bottom - coordsLine->top) / 2.f;
sourceLineMiddleWidth = (coordsLine->lineRight - coordsLine->lineLeft) / 2.f;
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
EDIT:
The real problem is the following: when I scale the object, it is jumping upwards/downwards, I have to translate it back to the target coordinates, and this doesn't works. I take some pictures, maybe it's clear what I'm meaning:
Object witouth Scale:
Object scaled:
This should be a trivial problem, I cann't find an equation to solve that.
c++ c++11 opengl opengl-3 glm-math
c++ c++11 opengl opengl-3 glm-math
edited Nov 29 '18 at 15:26
genpfault
41.8k95298
41.8k95298
asked Nov 16 '18 at 2:30
LuisLuis
8029
8029
You have to do the scaling first, this means at the end in the code.glm::translate(model,...)
glm::translate(model,...)
glm::scale(model,1.f, scaleY, 1.f)
– Rabbid76
Nov 16 '18 at 6:35
I tried this solution, but the object is not always moved to the right place, sometimes it doesn't jumps to the right coordinates, f.e.model = glm::translate(model, glm::vec3(coordsLine->lineLeft - tempLineLeft, coordsLine->top - tempLineTop, 0.f)); model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f)); model = glm::scale(model, glm::vec3(1.f, scaleY, 1.f));
or should I do the translation to the middle first?
– Luis
Nov 25 '18 at 3:32
add a comment |
You have to do the scaling first, this means at the end in the code.glm::translate(model,...)
glm::translate(model,...)
glm::scale(model,1.f, scaleY, 1.f)
– Rabbid76
Nov 16 '18 at 6:35
I tried this solution, but the object is not always moved to the right place, sometimes it doesn't jumps to the right coordinates, f.e.model = glm::translate(model, glm::vec3(coordsLine->lineLeft - tempLineLeft, coordsLine->top - tempLineTop, 0.f)); model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f)); model = glm::scale(model, glm::vec3(1.f, scaleY, 1.f));
or should I do the translation to the middle first?
– Luis
Nov 25 '18 at 3:32
You have to do the scaling first, this means at the end in the code.
glm::translate(model,...)
glm::translate(model,...)
glm::scale(model,1.f, scaleY, 1.f)
– Rabbid76
Nov 16 '18 at 6:35
You have to do the scaling first, this means at the end in the code.
glm::translate(model,...)
glm::translate(model,...)
glm::scale(model,1.f, scaleY, 1.f)
– Rabbid76
Nov 16 '18 at 6:35
I tried this solution, but the object is not always moved to the right place, sometimes it doesn't jumps to the right coordinates, f.e.
model = glm::translate(model, glm::vec3(coordsLine->lineLeft - tempLineLeft, coordsLine->top - tempLineTop, 0.f)); model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f)); model = glm::scale(model, glm::vec3(1.f, scaleY, 1.f));
or should I do the translation to the middle first?– Luis
Nov 25 '18 at 3:32
I tried this solution, but the object is not always moved to the right place, sometimes it doesn't jumps to the right coordinates, f.e.
model = glm::translate(model, glm::vec3(coordsLine->lineLeft - tempLineLeft, coordsLine->top - tempLineTop, 0.f)); model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f)); model = glm::scale(model, glm::vec3(1.f, scaleY, 1.f));
or should I do the translation to the middle first?– Luis
Nov 25 '18 at 3:32
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330607%2ftranslate-and-scale-a-rectangle-with-opengl-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330607%2ftranslate-and-scale-a-rectangle-with-opengl-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You have to do the scaling first, this means at the end in the code.
glm::translate(model,...)
glm::translate(model,...)
glm::scale(model,1.f, scaleY, 1.f)
– Rabbid76
Nov 16 '18 at 6:35
I tried this solution, but the object is not always moved to the right place, sometimes it doesn't jumps to the right coordinates, f.e.
model = glm::translate(model, glm::vec3(coordsLine->lineLeft - tempLineLeft, coordsLine->top - tempLineTop, 0.f)); model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f)); model = glm::scale(model, glm::vec3(1.f, scaleY, 1.f));
or should I do the translation to the middle first?– Luis
Nov 25 '18 at 3:32