mirror of
https://github.com/Linloir/SceneEditor.git
synced 2025-12-18 07:58:11 +08:00
[CORE][FIX] Fix terrain render issue
- unbind after render
This commit is contained in:
parent
8c6e6ec16c
commit
54c5e6d52e
@ -1,11 +1,12 @@
|
|||||||
#include "terrain.h"
|
|
||||||
#include "utils.h"
|
|
||||||
#include "vertex.h"
|
|
||||||
#include <STBImage/stb_image.h>
|
#include <STBImage/stb_image.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <GLM/glm.hpp>
|
#include <GLM/glm.hpp>
|
||||||
#include <GLM/gtc/type_ptr.hpp>
|
#include <GLM/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
|
#include "terrain.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include "vertex.h"
|
||||||
|
|
||||||
Terrain::Terrain(std::string path){
|
Terrain::Terrain(std::string path){
|
||||||
// Convert '\ ' to '/' for Windows
|
// Convert '\ ' to '/' for Windows
|
||||||
std::replace(path.begin(), path.end(), '\\', '/');
|
std::replace(path.begin(), path.end(), '\\', '/');
|
||||||
@ -56,7 +57,7 @@ Terrain::Terrain(std::string path){
|
|||||||
|
|
||||||
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &terrainVBO);
|
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &terrainVBO);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, terrainVBO);
|
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, terrainVBO);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW);
|
OPENGL_EXTRA_FUNCTIONS->glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
// position attribute
|
// position attribute
|
||||||
OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
||||||
@ -67,9 +68,9 @@ Terrain::Terrain(std::string path){
|
|||||||
|
|
||||||
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &terrainIBO);
|
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &terrainIBO);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, terrainIBO);
|
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, terrainIBO);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned), &indices[0], GL_STATIC_DRAW);
|
OPENGL_EXTRA_FUNCTIONS->glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned), indices.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//textureID = loadTexture2(texName, GL_REPEAT, GL_REPEAT, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
|
//textureID = loadTexture2(texName, GL_REPEAT, GL_REPEAT, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
|
||||||
tex = loadTexture(path + "/grass.jpg");
|
tex = loadTexture(path + "/grass.jpg");
|
||||||
|
|
||||||
@ -129,7 +130,10 @@ void Terrain::render() {
|
|||||||
numTrisPerStrip + 2, // number of indices to render
|
numTrisPerStrip + 2, // number of indices to render
|
||||||
GL_UNSIGNED_INT, // index data type
|
GL_UNSIGNED_INT, // index data type
|
||||||
(void*)(sizeof(unsigned) * (numTrisPerStrip + 2) * strip)); // offset to starting index
|
(void*)(sizeof(unsigned) * (numTrisPerStrip + 2) * strip)); // offset to starting index
|
||||||
|
|
||||||
}
|
}
|
||||||
|
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||||
|
OPENGL_EXTRA_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
float Terrain::GetHeight(float px, float pz) {
|
float Terrain::GetHeight(float px, float pz) {
|
||||||
@ -159,7 +163,10 @@ glm::vec3 Terrain::GetNormal(glm::vec3 pos) {
|
|||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Terrain::hitPoint(glm::vec3 orig, glm::vec3 dir) {
|
HitRecord Terrain::hit(const Ray& ray) {
|
||||||
|
glm::vec3 orig = ray.origin();
|
||||||
|
glm::vec3 dir = ray.direction();
|
||||||
|
|
||||||
// A good ray step is half of the blockScale
|
// A good ray step is half of the blockScale
|
||||||
glm::vec3 rayStep = dir * (float)width * 0.25f;
|
glm::vec3 rayStep = dir * (float)width * 0.25f;
|
||||||
glm::vec3 rayStartPosition = orig;
|
glm::vec3 rayStartPosition = orig;
|
||||||
@ -190,4 +197,11 @@ void Terrain::hitPoint(glm::vec3 orig, glm::vec3 dir) {
|
|||||||
}
|
}
|
||||||
glm::vec3 position = (startPosition + endPosition) * 0.5f;
|
glm::vec3 position = (startPosition + endPosition) * 0.5f;
|
||||||
glm::vec3 normal = GetNormal(position);
|
glm::vec3 normal = GetNormal(position);
|
||||||
|
|
||||||
|
// If t > 200, consider the ray as not hitted
|
||||||
|
float t = glm::length(position - rayStartPosition);
|
||||||
|
if (t > 200.0f)
|
||||||
|
return HitRecord();
|
||||||
|
else
|
||||||
|
return HitRecord(t, position, normal);
|
||||||
}
|
}
|
||||||
@ -1,11 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "utils.h"
|
|
||||||
#include "vertex.h"
|
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
#include "vertex.h"
|
||||||
|
#include "ray.h"
|
||||||
|
#include "hitrecord.h"
|
||||||
|
|
||||||
class Terrain {
|
class Terrain {
|
||||||
private:
|
private:
|
||||||
std::vector<float> vertices;
|
std::vector<float> vertices;
|
||||||
@ -24,6 +26,6 @@ public:
|
|||||||
Terrain(std::string path);
|
Terrain(std::string path);
|
||||||
void render();
|
void render();
|
||||||
unsigned int loadTexture(std::string path);
|
unsigned int loadTexture(std::string path);
|
||||||
void hitPoint(glm::vec3 orig, glm::vec3 dir);
|
HitRecord hit(const Ray& ray);
|
||||||
glm::mat4 modelMatrix() const { return glm::mat4(1.0f); }
|
glm::mat4 modelMatrix() const { return glm::mat4(1.0f); }
|
||||||
};
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user