c++ - 使用 OpenGL 3+ 平移和缩放矩形
问题描述
我想将矩形转换并缩放到特定坐标。此时我必须仅在 y 平面上缩放矩形。翻译效果很好,我有缩放问题,矩形没有放在正确的位置。我试图应用我在这里找到的另一个解决方案以及堆栈溢出的另一个帖子,但没有成功。这应该很容易,但我找不到解决方案。我从我的源头变成了顶部、底部、左侧和右侧的坐标。这是代码的一部分。顶点和片段着色器:
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";
顶点:
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 = 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));
在渲染循环之前:
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;
我必须将投影缩放到设备。这运行良好,没有问题。这是渲染循环内的平移和缩放:
//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);
编辑:真正的问题如下:当我缩放对象时,它向上/向下跳跃,我必须将它转换回目标坐标,这不起作用。我拍了一些照片,也许我的意思很清楚:没有比例的物体:
这应该是一个微不足道的问题,我找不到解决这个问题的方程式。
解决方案
推荐阅读
- python - Python3 + ctypes回调在简单示例中导致内存泄漏
- javascript - react-native ScrollView中scrollTo的替代方案
- postgresql - 我是否需要将 CREATE TABLE 语句的列顺序与导入数据表的列顺序相匹配?
- android - Management API 删除被阻止的应用程序
- python - Python内部容器类
- function - Web 部署任务失败...在 Visual Studio 中发布 Azure 函数
- javascript - 在更新时反应弹簧动画
- python - 我可以在 Pytest 中重试失败的测试吗
- python - 如何创建非阻塞 Python tkinter 窗口
- jquery - 使用 JSON 时有什么方法可以转义字符?