使用SFML,现代OpenGL转换从2D点到3D
Modern opengl conversion from 2d point to 3d using SFML
我想创建一个游戏,其中我的立方体在每个脸上都有n*n正方形。我使用C ,现代OpenGL,SFML和GLM。立方体上有一对具有相同颜色的正方形。该游戏的目标是为每个正方形创建一个从Square1到Square2的路径,而每个正方形都没有相同的颜色,而不会重叠路径。您还可以在此立方体上有"墙壁",正方形无法形成路径。用户应该能够按照他的意愿旋转立方体,当他按下时单击一个正方形时,它应该会改变其颜色。为此,我知道我必须使用深度缓冲区在屏幕上的2D点和世界上的3D坐标之间进行转换。但是它不起作用,我不知道为什么。这是我的代码(我刚刚启动了此项目):main.cpp:
#include "Includes.h"
#include "Shader.h"
#define WIDTH 800
#define HEIGHT 600
using namespace std;
sf::Event event;
GLfloat get_gl_depth(int x, int y);
glm::vec4 get3dPoint(glm::vec2 point2D, int width,int height, glm::mat4 viewMatrix, glm::mat4 projectionMatrix);
int main()
{
sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 4;
settings.majorVersion = 3;
settings.minorVersion = 0;
sf::Window window(sf::VideoMode(WIDTH, HEIGHT), "OpenGL", sf::Style::Default, settings);
GLfloat vertices[] = {
-2.0f, -2.0f, -2.0f, 0.0f, 0.0f,
2.0f, -2.0f, -2.0f, 1.0f, 0.0f,
2.0f, 2.0f, -2.0f, 1.0f, 1.0f,
2.0f, 2.0f, -2.0f, 1.0f, 1.0f,
-2.0f, 2.0f, -2.0f, 0.0f, 1.0f,
-2.0f, -2.0f, -2.0f, 0.0f, 0.0f,
-2.0f, -2.0f, 2.0f, 0.0f, 0.0f,
2.0f, -2.0f, 2.0f, 1.0f, 0.0f,
2.0f, 2.0f, 2.0f, 1.0f, 1.0f,
2.0f, 2.0f, 2.0f, 1.0f, 1.0f,
-2.0f, 2.0f, 2.0f, 0.0f, 1.0f,
-2.0f, -2.0f, 2.0f, 0.0f, 0.0f,
-2.0f, 2.0f, 2.0f, 1.0f, 0.0f,
-2.0f, 2.0f, -2.0f, 1.0f, 1.0f,
-2.0f, -2.0f, -2.0f, 0.0f, 1.0f,
-2.0f, -2.0f, -2.0f, 0.0f, 1.0f,
-2.0f, -2.0f, 2.0f, 0.0f, 0.0f,
-2.0f, 2.0f, 2.0f, 1.0f, 0.0f,
2.0f, 2.0f, 2.0f, 1.0f, 0.0f,
2.0f, 2.0f, -2.0f, 1.0f, 1.0f,
2.0f, -2.0f, -2.0f, 0.0f, 1.0f,
2.0f, -2.0f, -2.0f, 0.0f, 1.0f,
2.0f, -2.0f, 2.0f, 0.0f, 0.0f,
2.0f, 2.0f, 2.0f, 1.0f, 0.0f,
-2.0f, -2.0f, -2.0f, 0.0f, 1.0f,
2.0f, -2.0f, -2.0f, 1.0f, 1.0f,
2.0f, -2.0f, 2.0f, 1.0f, 0.0f,
2.0f, -2.0f, 2.0f, 1.0f, 0.0f,
-2.0f, -2.0f, 2.0f, 0.0f, 0.0f,
-2.0f, -2.0f, -2.0f, 0.0f, 1.0f,
-2.0f, 2.0f, -2.0f, 0.0f, 1.0f,
2.0f, 2.0f, -2.0f, 1.0f, 1.0f,
2.0f, 2.0f, 2.0f, 1.0f, 0.0f,
2.0f, 2.0f, 2.0f, 1.0f, 0.0f,
-2.0f, 2.0f, 2.0f, 0.0f, 0.0f,
-2.0f, 2.0f, -2.0f, 0.0f, 1.0f
};
glewInit();
glViewport(0, 0, WIDTH, HEIGHT);
glEnable(GL_DEPTH_TEST);
Shader ourShader("shaders/default.vs", "shaders/default.frag");
GLuint VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// TexCoord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindVertexArray(0); // Unbind VAO
bool running = true;
glm::vec3 cubePosition;
cubePosition.z=-3;
window.setFramerateLimit(30);
glm::mat4 model;
glm::mat4 view;
glm::mat4 projection;
while (running)
{
model=glm::mat4();
view=glm::mat4();
projection=glm::mat4();
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
projection = glm::perspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);
while (window.pollEvent(event))
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
cubePosition.y-=0.2;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
cubePosition.y+=0.2;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
cubePosition.x+=0.2;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
cubePosition.x-=0.2;
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
int x=sf::Mouse::getPosition(window).x;
int y=sf::Mouse::getPosition(window).y;
glm::vec4 result=get3dPoint(glm::vec2(x,y),WIDTH,HEIGHT,view,projection);
cout<<result.x<<' '<<result.y<<' '<<result.z<<'n';
}
if (event.type == sf::Event::Closed)
{
// end the program
running = false;
}
else if (event.type == sf::Event::Resized)
{
// adjust the viewport when the window is resized
glViewport(0, 0, event.size.width, event.size.height);
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ourShader.Use();
GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
window.display();
}
return 0;
}
glm::vec4 get3dPoint(glm::vec2 point2D, int width,int height, glm::mat4 viewMatrix, glm::mat4 projectionMatrix)
{
double x = 2.0 * point2D.x / WIDTH - 1;
double y = - 2.0 * point2D.y / HEIGHT + 1;
glm::mat4 viewProjectionInverse = glm::inverse(projectionMatrix * viewMatrix);
glm::vec4 point3D =glm::vec4(x, y, get_gl_depth(x,y),1.0);
//cout<<point2D.x<<' '<<point2D.y<<' '<<get_gl_depth(point2D.x,point2D.y)<<'n';
return viewProjectionInverse*point3D;
}
GLfloat get_gl_depth(int x, int y)
{
GLfloat depth_z = 0.0f;
glReadBuffer(GL_FRONT);
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth_z);
return depth_z;
}
,着色器是非常标准的:顶点:
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 2) in vec2 texCoord;
out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(position, 1.0f);
TexCoord = vec2(texCoord.x, 1.0 - texCoord.y);
}
片段:
#version 330 core
in vec2 TexCoord;
out vec4 color;
void main()
{
color = vec4(1.0,0.0,0.0,1.0);
}
我只希望我的转换工作,将来生病的工作能够很好地纹理,以组织我的代码等。
我可以在您的代码中发现几个问题:
glm::vec4 get3dPoint(glm::vec2 point2D, int width,int height, glm::mat4 viewMatrix, glm::mat4 projectionMatrix) { double x = 2.0 * point2D.x / WIDTH - 1; double y = - 2.0 * point2D.y / HEIGHT + 1;
在这里,您正在转换为NDC范围。但是,您使用的是WIDHT
和HEIGHT
,而不是width
和height
为功能参数。这无关紧要,因为您将其称为具有这些值的excalt,但从。
glm::mat4 viewProjectionInverse = glm::inverse(projectionMatrix * viewMatrix); glm::vec4 point3D =glm::vec4(x, y, get_gl_depth(x,y),1.0);
您现在在[-1,1]
范围内使用NDC x
和y
作为get_gl_depth
的输入,该输入确实希望获得窗口空间(Pixel)坐标,并且实际上也将int
用于这些参数。因此,您最终要么是对glReadPixels
的无效调用(x
或y
负),要么在左下2x2 Pixel区域中获得像素的深度。
return viewProjectionInverse*point3D; }
您忘了通过结果向量的w
坐标来驱散结果。
相关文章:
- 直接 2D 呈现到命令列表和打印:图片压缩
- static_cast浮点到枚举
- 从/到 UTF-8/UTF-16 的转换需要(例如:utf8 -> 代码点,然后代码点到 utf16)或(例如:utf8 -> utf16)?
- 如何使用条件计算 3D 网格中从一个点到另一个点的所有路径
- C++中的多维数据集:从 std::vector 的 2D 数据到 std::vector 的 2D 网格的最干净方法?
- C++ 2D 矢量到 2D pybind11 数组
- 使用点云库 ICP 进行 2D 点匹配
- Unordered_map使用 2D 点进行查找
- 浮点到整数转换出错(即使浮点数已经是整数)
- 从字符的 ASCII 代码点到 wstring
- 使用 opencv 凸胡尔 获取给定点的面积. - 点到垫子转换期间发生错误
- 矢量浮点到整数/符号转换
- 如何在无限轴上找到 N 个点,以便从 M 点到其最近的 N 的距离总和最小
- 给定 2 点和从第一点到我必须找到的点的范围.如何找到我必须找到的点(在3D空间上)
- 写入和读取时的不同值浮点到二进制文件
- (无符号__int16浮点)和浮点到(无符号__int16)类型转换与C++联合
- 使用SFML,现代OpenGL转换从2D点到3D
- OpenCV立体视觉3D坐标到2D相机平面投影不同于将2D点三角化到3D
- 从非结构化点到非结构化点的 2D 插值
- 点到线的距离(2D)和交点的坐标