我正在尝试实现一个简单的绘画程序,但是现在缩放出现问题,我不知道该怎么做?我尝试将代码从here改编成我自己的代码,但是它没有用,只是出现了黑屏。我有什么问题?
不使用过剩或毛刺!
这是我的相机代码:
.h
track.enable
.cpp
class Camera2d
{
public:
Camera2d(const glm::vec3& pos = glm::vec3(0.f, 0.f, 0.f),
const glm::vec3& up = glm::vec3(0.f, 1.f, 0.f));
//~Camera2d();
void setZoom(const float& zoom);
float getZoom() const noexcept;
glm::mat4 getViewMatrix() const noexcept;
void mouseScrollCallback(const float& yOffset);
protected:
void update();
private:
// Camera zoom
float m_zoom;
// Euler Angles
float m_yaw;
float m_pitch;
public:
// Camera Attributes
glm::vec3 position;
glm::vec3 worldUp;
glm::vec3 front;
glm::vec3 up;
glm::vec3 right;
};
主要是在渲染循环中尝试这样的操作
Camera2d::Camera2d(
const glm::vec3& pos /* = glm::vec3(0.f, 0.f, 0.f) */,
const glm::vec3& up /* = glm::vec3(0.f, 1.f, 0.f) */
)
: m_zoom(45.f)
, m_yaw(-90.f)
, m_pitch(0.f)
, position(pos)
, worldUp(up)
, front(glm::vec3(0.f, 0.f, -1.f))
{
this->update();
}
void Camera2d::setZoom(const float& zoom)
{
this->m_zoom = zoom;
}
float Camera2d::getZoom() const noexcept
{
return this->m_zoom;
}
glm::mat4 Camera2d::getViewMatrix() const noexcept
{
return glm::lookAt(this->position, this->position + this->front, this->up);
}
void Camera2d::mouseScrollCallback(const float& yOffset)
{
if (m_zoom >= 1.f && m_zoom <= 45.f)
m_zoom -= yOffset;
else if (m_zoom <= 1.f)
m_zoom = 1.f;
else if (m_zoom >= 45.f)
m_zoom = 45.f;
}
void Camera2d::update()
{
// Calculate the new Front vector
glm::vec3 _front;
_front.x = cos(glm::radians(this->m_yaw)) * cos(glm::radians(this->m_pitch));
_front.y = sin(glm::radians(this->m_pitch));
_front.z = cos(glm::radians(this->m_pitch)) * sin(glm::radians(this->m_yaw));
this->front = glm::normalize(_front);
// Also re-calculate the Right and Up vector
this->right = glm::normalize(glm::cross(this->front, this->worldUp)); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
this->up = glm::normalize(glm::cross(this->right, this->front));
}
github上的所有代码:here
答案 0 :(得分:0)
您正在使用2D,忘记了照相机,忘记了投影,忘记了以下OpenGL教程,它们都是针对3D图形的。
您需要的只是一个矩形来填充屏幕。从屏幕角落的顶点开始,从左上角开始,然后逆时针移动:(-1,1,0)(-1,-1,0)(1,-1,0)(1 ,1,0)。忘了Z,您正在使用2D。
您在纹理上绘制,并且纹理坐标为(0,1)(0,0)(1,0)(1,1),与上述顺序相同。缩放现在仅是缩放矩形的问题。您有一个矩阵来确定比例,一个矩阵来确定位置。忘记旋转,前向矢量和所有其他东西。在顶点着色器中,您可以按比例缩放然后平移顶点。完成。
例如,要进行交互,您可以使鼠标滚轮向上增大比例因子,而使鼠标滚轮向下减小其比例。按住并移动鼠标以更改(x,y)位置。再次忘记Z。将这些值放入顶点着色器中并进行通常的转换。