[英]opengl from window point to 3d coordinate
對於我的一個大型項目,我有時需要在屏幕上繪制一個立方體,並且我需要知道是否單擊了立方體的一面。 為此,我知道我必須使用深度緩沖區。 但是我嘗試進行的轉換不起作用。 這是我的代碼:
#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 modelMatrix);
int main()
{
sf::ContextSettings settings;
settings.depthBits = 32;
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.0, -2.0, -2.0, 0.0f, 0.0f,
2.0, -2.0, -2.0, 1.0f, 0.0f,
2.0, 2.0, -2.0, 1.0f, 1.0f,
2.0, 2.0, -2.0, 1.0f, 1.0f,
-2.0, 2.0, -2.0, 0.0f, 1.0f,
-2.0, -2.0, -2.0, 0.0f, 0.0f,
-2.0, -2.0, 2.0, 0.0f, 0.0f,
2.0, -2.0, 2.0, 1.0f, 0.0f,
2.0, 2.0, 2.0, 1.0f, 1.0f,
2.0, 2.0, 2.0, 1.0f, 1.0f,
-2.0, 2.0, 2.0, 0.0f, 1.0f,
-2.0, -2.0, 2.0, 0.0f, 0.0f,
-2.0, 2.0, 2.0, 1.0f, 0.0f,
-2.0, 2.0, -2.0, 1.0f, 1.0f,
-2.0, -2.0, -2.0, 0.0f, 1.0f,
-2.0, -2.0, -2.0, 0.0f, 1.0f,
-2.0, -2.0, 2.0, 0.0f, 0.0f,
-2.0, 2.0, 2.0, 1.0f, 0.0f,
2.0, 2.0, 2.0, 1.0f, 0.0f,
2.0, 2.0, -2.0, 1.0f, 1.0f,
2.0, -2.0, -2.0, 0.0f, 1.0f,
2.0, -2.0, -2.0, 0.0f, 1.0f,
2.0, -2.0, 2.0, 0.0f, 0.0f,
2.0, 2.0, 2.0, 1.0f, 0.0f,
-2.0, -2.0, -2.0, 0.0f, 1.0f,
2.0, -2.0, -2.0, 1.0f, 1.0f,
2.0, -2.0, 2.0, 1.0f, 0.0f,
2.0, -2.0, 2.0, 1.0f, 0.0f,
-2.0, -2.0, 2.0, 0.0f, 0.0f,
-2.0, -2.0, -2.0, 0.0f, 1.0f,
-2.0, 2.0, -2.0, 0.0f, 1.0f,
2.0, 2.0, -2.0, 1.0f, 1.0f,
2.0, 2.0, 2.0, 1.0f, 0.0f,
2.0, 2.0, 2.0, 1.0f, 0.0f,
-2.0, 2.0, 2.0, 0.0f, 0.0f,
-2.0, 2.0, -2.0, 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();
model=glm::translate(model,cubePosition);
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);
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(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
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))
{
float x=sf::Mouse::getPosition(window).x;
float y=sf::Mouse::getPosition(window).y;
float z=1.0;
glReadPixels(x,HEIGHT-y-1,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&z);
x=2.0*(x/WIDTH)-1;
y=-2.0*(y/HEIGHT)+1;
glm::vec4 v=glm::vec4(x,y,z,1.0);
v=v*glm::inverse(projection*view*model);
v.x/=v.w;
v.y/=v.w;
v.z/=v.w;
cout<<v.x<<' '<<v.y<<' '<<v.z<<' '<<v.w<<'\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);
}
}
window.display();
}
return 0;
}
所以最初是在屏幕中央繪制立方體,當我嘗試單擊左上角(第一個紅色像素)時,我應該得到數字x = -2,y = 2但我得到x = 0.0150043,y = 0.0150043甚至不接近。 我的錯誤在哪里? 我遵循了本教程並在線閱讀了有關此主題的文章: http : //webglfactory.blogspot.ro/2011/05/how-to-convert-world-to-screen.html
投影意味着將世界坐標轉換為規范的規范坐標。 NCC是2x2x2立方體空間。 GPU使用您在glViewport中傳遞的參數“自動”完成了從NCC空間到窗口屏幕坐標的轉換。
因此,首先您必須撤消screen-NCC轉換。 使用https://www.opengl.org/sdk/docs/man2/xhtml/glViewport.xml公式。
因為窗口坐標是2D,所以您無法獲取深度坐標。 從您的眼睛到2D窗口點以及更遠處都有一條直線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.