Ray testing
parent
2d1275f6fb
commit
0c1f0c05a3
|
@ -2,14 +2,23 @@
|
|||
|
||||
in vec2 texCoords;
|
||||
in vec3 normal;
|
||||
in vec3 blend;
|
||||
in vec3 add;
|
||||
|
||||
uniform sampler2D tex;
|
||||
uniform bool highlight;
|
||||
|
||||
out vec3 color;
|
||||
out vec3 outColor;
|
||||
|
||||
void main() {
|
||||
vec4 spec = texture(tex, texCoords);
|
||||
if (spec.a < 0.1) discard;
|
||||
vec3 color = spec.xyz;
|
||||
|
||||
color = spec.xyz;
|
||||
if (highlight) {
|
||||
color *= 1.4;
|
||||
color += 0.025;
|
||||
}
|
||||
|
||||
outColor = color;
|
||||
}
|
|
@ -10,7 +10,8 @@ App::App() :
|
|||
renderer(&window, &camera),
|
||||
input(window.getInput()),
|
||||
|
||||
controller(input, camera) {
|
||||
controller(input, camera),
|
||||
blockManager(&camera, &window) {
|
||||
|
||||
while (!window.shouldEnd()) {
|
||||
update();
|
||||
|
@ -21,6 +22,7 @@ App::App() :
|
|||
void App::update() {
|
||||
window.update();
|
||||
controller.update();
|
||||
blockManager.update(input);
|
||||
}
|
||||
|
||||
void App::render() {
|
||||
|
|
|
@ -22,6 +22,7 @@ Renderer::Renderer(Window *window, Camera* camera) :
|
|||
uProjection = sWorld.get("projection");
|
||||
uModel = sWorld.get("model");
|
||||
uView = sWorld.get("view");
|
||||
uHighlight = sWorld.get("highlight");
|
||||
}
|
||||
|
||||
void Renderer::setModelMatrix(const glm::mat4& modelMatrix) {
|
||||
|
@ -44,6 +45,8 @@ void Renderer::reset() {
|
|||
|
||||
sWorld.set(uProjection, camera->getProjectionMatrix());
|
||||
sWorld.set(uView, camera->getViewMatrix());
|
||||
|
||||
sWorld.set(uHighlight, false);
|
||||
}
|
||||
|
||||
void Renderer::swap() {
|
||||
|
|
|
@ -18,15 +18,14 @@ public:
|
|||
|
||||
void reset();
|
||||
void swap();
|
||||
private:
|
||||
Window* window;
|
||||
Camera* camera;
|
||||
|
||||
Shader sWorld;
|
||||
unsigned int uProjection;
|
||||
unsigned int uModel;
|
||||
unsigned int uView;
|
||||
unsigned int uHighlight;
|
||||
|
||||
bool dir = true;
|
||||
unsigned char color = 0;
|
||||
private:
|
||||
Window* window;
|
||||
Camera* camera;
|
||||
};
|
||||
|
|
|
@ -30,7 +30,7 @@ glm::ivec3 BlockModel::getPos() {
|
|||
return pos;
|
||||
}
|
||||
|
||||
void BlockModel::setPos(glm::ivec3 pos) {
|
||||
void BlockModel::setPos(glm::vec3 pos) {
|
||||
this->pos = pos;
|
||||
}
|
||||
|
||||
|
@ -63,5 +63,10 @@ void BlockModel::render(Renderer &renderer) {
|
|||
glm::mat4 model = glm::mat4(1.0);
|
||||
model = glm::translate(model, pos);
|
||||
renderer.setModelMatrix(model);
|
||||
renderer.sWorld.set(renderer.uHighlight, highlighted);
|
||||
mesh.draw();
|
||||
}
|
||||
|
||||
void BlockModel::setHighlighted(bool highlighted) {
|
||||
this->highlighted = highlighted;
|
||||
}
|
||||
|
|
|
@ -13,12 +13,15 @@ public:
|
|||
BlockModel();
|
||||
|
||||
glm::ivec3 getPos();
|
||||
void setPos(glm::ivec3 pos);
|
||||
void setPos(glm::vec3 pos);
|
||||
|
||||
void updateMesh();
|
||||
void render(Renderer& renderer);
|
||||
|
||||
void setHighlighted(bool highlighted);
|
||||
private:
|
||||
glm::vec3 pos;
|
||||
bool highlighted = false;
|
||||
|
||||
BlockMesh mesh;
|
||||
std::vector<BlockFace> faces {};
|
||||
|
|
|
@ -2,22 +2,47 @@
|
|||
// Created by aurailus on 2020-05-31.
|
||||
//
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "BlockModelManager.h"
|
||||
|
||||
#include "../graph/Camera.h"
|
||||
#include "../graph/Window.h"
|
||||
#include "../graph/Renderer.h"
|
||||
|
||||
BlockModelManager::BlockModelManager() :
|
||||
dirt("../assets/textures/dirt.png") {
|
||||
BlockModelManager::BlockModelManager(Camera* camera, Window* window) :
|
||||
dirt("../assets/textures/dirt.png"),
|
||||
camera(camera),
|
||||
window(window) {
|
||||
|
||||
models.emplace_back();
|
||||
models.emplace_back();
|
||||
models.emplace_back();
|
||||
models.emplace_back();
|
||||
models.emplace_back();
|
||||
|
||||
models[1].setPos({1, 0, 0});
|
||||
models[2].setPos({-1, 0, 0});
|
||||
models[3].setPos({0, 0, 1});
|
||||
models[4].setPos({0, 0, -1});
|
||||
models[1].setPos({1, 0, 1});
|
||||
models[2].setPos({-1, 0, 1});
|
||||
models[3].setPos({1, 0, -1});
|
||||
models[4].setPos({-1, 0, -1});
|
||||
}
|
||||
|
||||
void BlockModelManager::update(Input& input) {
|
||||
for (auto& model : models) model.setHighlighted(false);
|
||||
|
||||
glm::vec4 ray_clip = glm::vec4 {
|
||||
(2.f * input.mousePos().x) / window->getSize().x - 1.f,
|
||||
1.f - (2.f * input.mousePos().y) / window->getSize().y, -1.f, 1.f };
|
||||
|
||||
|
||||
glm::vec4 ray_eye = glm::inverse(camera->getProjectionMatrix()) * ray_clip;
|
||||
ray_eye = glm::vec4(ray_eye.x, ray_eye.y, -1.0, 0.0);
|
||||
|
||||
glm::vec3 ray = glm::vec3(glm::inverse(camera->getViewMatrix()) * ray_eye);
|
||||
ray *= 20;
|
||||
models[0].setPos(camera->getPos() + glm::vec3(ray));
|
||||
|
||||
std::cout << ray.x << ", " << ray.y << ", " << ray.z << std::endl;
|
||||
}
|
||||
|
||||
void BlockModelManager::render(Renderer &renderer) {
|
||||
|
|
|
@ -4,18 +4,25 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "BlockModel.h"
|
||||
#include "../graph/Mesh.h"
|
||||
#include "../graph/Texture.h"
|
||||
#include "BlockModel.h"
|
||||
|
||||
class Input;
|
||||
class Window;
|
||||
class Renderer;
|
||||
class Camera;
|
||||
|
||||
class BlockModelManager {
|
||||
public:
|
||||
BlockModelManager();
|
||||
BlockModelManager(Camera* camera, Window* window);
|
||||
|
||||
void render(Renderer& renderer);
|
||||
void update(Input& input);
|
||||
private:
|
||||
Camera* camera;
|
||||
Window* window;
|
||||
|
||||
std::vector<BlockModel> models;
|
||||
Texture dirt;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue