【发布时间】:2021-12-04 02:37:29
【问题描述】:
我希望我的窗口内容在我的窗口调整大小时保持居中。 OpenGL(或GLFW)-我不确定哪个-在水平调整大小时确实会产生这种预期的效果,但是当我垂直调整窗口大小时,窗口似乎会显示立方体底部的更多部分。我希望它会像水平调整大小时一样切断立方体,
注意:我不做任何glViewPort(),否则窗口内容会调整为新的宽度和高度。
有没有办法改变这种行为?这个问题可能是相关的,但我认为这不能解决我的问题。 How to keep the OpenGL viewport in an SDL window stationary ...
#include <iostream>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include "../includes/Shader.h"
#include "../includes/Texture.h"
#include "../includes/glm/glm.hpp"
const int SCR_WIDTH = 800;
const int SCR_HEIGHT = 600;
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void processInput(GLFWwindow *window);
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "OpenGL", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create window! " << std::endl;
glfwTerminate();
return -1;
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW! " << std::endl;
glfwTerminate();
return -1;
}
float vertices[] = {
...
};
unsigned int VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (GLvoid *) nullptr);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (GLvoid *) (sizeof(float) * 3));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
Texture texture1("../images/container.jpg");
Texture texture2("../images/awesomeface.png");
Shader shader("../shaders/shader.vs", "../shaders/shader.fs");
shader.use();
shader.setUniform1i("texture1", 0);
shader.setUniform1i("texture2", 1);
while (!glfwWindowShouldClose(window)) {
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
texture1.bind(0);
texture2.bind(1);
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
model = glm::rotate(model, (float) glfwGetTime(), glm::vec3(1.0f, 0.5f, 0.5f));
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f);
shader.setUniformMat4("model", model);
shader.setUniformMat4("view", view);
shader.setUniformMat4("projection", projection);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
//glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
}
【问题讨论】:
-
OpenGL 已经 可以满足您的需求。此可以仅适用于 4 个边框中的 2 个,并且在右侧和顶部执行 - 这是 OpenGL 使用左下角作为原点的结果。
-
感谢您的回复您知道有什么方法可以解决底部和左侧的问题。
-
好吧,您可以调整视口并通过更改投影矩阵来基本裁剪图像来补偿它,这归结为预乘一些扫描和平移。虽然这不是您正在寻找的内容,但在 this answer 中,我提供了有关所需数学的一些详细信息。
标签: c++ opengl viewport glfw opengl-3