地形能显示出来了
但一旦显示出来模型就无法显示
@ -75,6 +75,8 @@ void SceneViewer::initializeGL() {
|
|||||||
vertexShader_sky.dispose();
|
vertexShader_sky.dispose();
|
||||||
fragmentShader_sky.dispose();
|
fragmentShader_sky.dispose();
|
||||||
|
|
||||||
|
|
||||||
|
terrainShader.ensureInitialized();
|
||||||
VertexShader vertexShader_ter("./temp/shaders/terrainShader.vs");
|
VertexShader vertexShader_ter("./temp/shaders/terrainShader.vs");
|
||||||
FragmentShader fragmentShader_ter("./temp/shaders/terrainShader.fs");
|
FragmentShader fragmentShader_ter("./temp/shaders/terrainShader.fs");
|
||||||
terrainShader.attachShader(vertexShader_ter);
|
terrainShader.attachShader(vertexShader_ter);
|
||||||
@ -107,34 +109,32 @@ void SceneViewer::paintGL() {
|
|||||||
// Set view and projection matrices
|
// Set view and projection matrices
|
||||||
glm::mat4 view = _camera.viewMatrix();
|
glm::mat4 view = _camera.viewMatrix();
|
||||||
glm::mat4 projection = glm::perspective(glm::radians(_camera.zoomVal()), (float)width() / (float)height(), 0.1f, 100.0f);
|
glm::mat4 projection = glm::perspective(glm::radians(_camera.zoomVal()), (float)width() / (float)height(), 0.1f, 100.0f);
|
||||||
/*_shaderProgram.setUniform("view", view);
|
_shaderProgram.setUniform("view", view);
|
||||||
_shaderProgram.setUniform("projection", projection);
|
_shaderProgram.setUniform("projection", projection);
|
||||||
|
|
||||||
for (auto object : _objects) {
|
for (auto object : _objects) {
|
||||||
object.render(_shaderProgram);
|
object.render(_shaderProgram);
|
||||||
}*/
|
}
|
||||||
|
|
||||||
_shaderProgram.unbind();
|
_shaderProgram.unbind();
|
||||||
|
|
||||||
terrainShader.bind();
|
//terrainShader.bind();
|
||||||
glm::mat4 Model = glm::mat4(1.0f);
|
//glm::mat4 Model = glm::mat4(1.0f);
|
||||||
Model = glm::translate(Model, glm::vec3(0.0f, 0.0f, -2.0f));
|
//Model = glm::translate(Model, glm::vec3(0.0f, 0.0f, -2.0f));
|
||||||
terrainShader.setUniform("view", view);
|
//terrainShader.setUniform("view", view);
|
||||||
terrainShader.setUniform("projection", projection);
|
//terrainShader.setUniform("projection", projection);
|
||||||
terrainShader.setUniform("model", Model);
|
//terrainShader.setUniform("model", Model);
|
||||||
terrainShader.setUniform("dep", 0);
|
//terrainShader.setUniform("texture1", 2);
|
||||||
terrainShader.setUniform("tex1", 1);
|
//ter->render();
|
||||||
terrainShader.setUniform("tex2", 2);
|
//terrainShader.unbind();
|
||||||
ter->render();
|
|
||||||
terrainShader.unbind();
|
|
||||||
|
|
||||||
|
|
||||||
/*skyShader.bind();
|
skyShader.bind();
|
||||||
view = glm::mat4(glm::mat3(view));
|
view = glm::mat4(glm::mat3(view));
|
||||||
skyShader.setUniform("view", view);
|
skyShader.setUniform("view", view);
|
||||||
skyShader.setUniform("projection", projection);
|
skyShader.setUniform("projection", projection);
|
||||||
sky->render();
|
sky->render();
|
||||||
skyShader.unbind();*/
|
skyShader.unbind();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,51 +6,99 @@
|
|||||||
|
|
||||||
Terrain::Terrain(int rows, int cols):row_num(rows),col_num(cols) {
|
Terrain::Terrain(int rows, int cols):row_num(rows),col_num(cols) {
|
||||||
Vertex.clear();
|
Vertex.clear();
|
||||||
Indicess.clear();
|
Indicess.clear();
|
||||||
float x = -50, z = -50;
|
int imgW, imgH, imgChannel;
|
||||||
|
|
||||||
for (int i = 0; i < rows; i++) {
|
unsigned char* data = stbi_load("D:/ProgrammingFile/SceneEditor/terrain/heightmap.jpg", &imgW, &imgH, &imgChannel, 1);
|
||||||
x = -5;
|
|
||||||
for (int j = 0; j < cols; j++) {
|
|
||||||
Vertex.push_back(x);
|
int index1 = 0, index = 0;
|
||||||
Vertex.push_back(0);
|
float xrate = imgW / 2.0f;
|
||||||
Vertex.push_back(z);
|
float rrate = imgH / 2.0f;
|
||||||
Vertex.push_back(1.0f / cols * j);
|
|
||||||
Vertex.push_back(1 - i * 1.0f / rows);
|
if (data) {
|
||||||
x += 0.1;
|
for (int r = 0; r < imgH; r++)
|
||||||
|
{
|
||||||
|
for (int c = 0; c < imgW; c++)
|
||||||
|
{
|
||||||
|
//生成顶点数组, 坐标按照三角网格处理 GL_TRIGANLES
|
||||||
|
index1 = r * imgW + c;
|
||||||
|
Vertex.push_back((c - xrate) / xrate);
|
||||||
|
Vertex.push_back(data[index1] / 255.0f);
|
||||||
|
Vertex.push_back((r - rrate) / rrate);
|
||||||
|
//顶点颜色
|
||||||
|
Vertex.push_back(1.0f);
|
||||||
|
Vertex.push_back(0.0f);
|
||||||
|
Vertex.push_back(0.0f);
|
||||||
|
//纹理坐标
|
||||||
|
Vertex.push_back((c - xrate) / imgW);
|
||||||
|
Vertex.push_back((r - rrate) / imgH);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
z += 0.1;
|
imgH -= 1;
|
||||||
}
|
int iW = imgW - 1;
|
||||||
|
for (int r = 0; r < imgH; r++)
|
||||||
|
{
|
||||||
|
for (int c = 0; c < iW; c++)
|
||||||
|
{
|
||||||
|
|
||||||
for (int i = 1; i < rows; i++) {
|
index1 = r * imgW + c;
|
||||||
for (int j = 1; j < cols; j++) {
|
index = (r + 1) * imgW + c;
|
||||||
Indicess.push_back((i - 1) * cols + j - 1);
|
Indicess.push_back(index1);
|
||||||
Indicess.push_back((i - 1) * cols + j);
|
Indicess.push_back(index1 + 1);
|
||||||
Indicess.push_back(i * cols + j - 1);
|
Indicess.push_back(index + 1);
|
||||||
|
|
||||||
Indicess.push_back(i * cols + j - 1);
|
Indicess.push_back(index1);
|
||||||
Indicess.push_back((i - 1) * cols + j);
|
Indicess.push_back(index);
|
||||||
Indicess.push_back(i * cols + j);
|
Indicess.push_back(index + 1);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
stbi_image_free(data);
|
||||||
|
|
||||||
|
|
||||||
tex1 = loadTexture("D:/ProgrammingFile/SceneEditor/terrain/rock.jpg");
|
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &VAO);
|
||||||
tex2 = loadTexture("D:/ProgrammingFile/SceneEditor/terrain/water.jpg");
|
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &VBO);
|
||||||
dep = loadTexture("D:/ProgrammingFile/SceneEditor/terrain/heightmap.png");
|
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &EBO);
|
||||||
|
|
||||||
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &TerrainVAO);
|
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(VAO);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &TerrainVBO);
|
|
||||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(TerrainVAO);
|
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, TerrainVBO);
|
OPENGL_EXTRA_FUNCTIONS->glBufferData(GL_ARRAY_BUFFER, Vertex.size() * sizeof(float), &Vertex[0], GL_STATIC_DRAW);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex), &Vertex, GL_STATIC_DRAW);
|
|
||||||
|
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||||
|
OPENGL_EXTRA_FUNCTIONS->glBufferData(GL_ELEMENT_ARRAY_BUFFER, Indicess.size() * sizeof(unsigned int), &Indicess[0], GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glEnableVertexAttribArray(0);
|
OPENGL_EXTRA_FUNCTIONS->glEnableVertexAttribArray(0);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
// color attribute
|
||||||
|
OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||||
|
OPENGL_EXTRA_FUNCTIONS->glEnableVertexAttribArray(1);
|
||||||
|
// texture coord attribute
|
||||||
|
OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||||
|
OPENGL_EXTRA_FUNCTIONS->glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
|
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||||
|
|
||||||
|
//textureID = loadTexture2(texName, GL_REPEAT, GL_REPEAT, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
|
||||||
|
tex = loadTexture("D:/ProgrammingFile/SceneEditor/terrain/rock.jpg");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Terrain::loadTexture(std::string path) {
|
unsigned int Terrain::loadTexture(std::string path) {
|
||||||
unsigned int textureID;
|
unsigned int textureID;
|
||||||
OPENGL_FUNCTIONS->glGenTextures(1, &textureID);
|
OPENGL_FUNCTIONS->glGenTextures(1, &textureID);
|
||||||
OPENGL_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, textureID);
|
OPENGL_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, textureID);
|
||||||
|
|
||||||
|
// Set the texture wrapping parameters
|
||||||
|
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT (usually basic wrapping method)
|
||||||
|
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
// Set texture filtering parameters
|
||||||
|
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
stbi_set_flip_vertically_on_load(true);
|
stbi_set_flip_vertically_on_load(true);
|
||||||
|
|
||||||
int width, height, nrChannels;
|
int width, height, nrChannels;
|
||||||
@ -83,15 +131,10 @@ unsigned int Terrain::loadTexture(std::string path) {
|
|||||||
void Terrain::render() {
|
void Terrain::render() {
|
||||||
OPENGL_EXTRA_FUNCTIONS->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
OPENGL_EXTRA_FUNCTIONS->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
OPENGL_EXTRA_FUNCTIONS->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
OPENGL_EXTRA_FUNCTIONS->glActiveTexture(GL_TEXTURE0);
|
|
||||||
OPENGL_EXTRA_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, dep);
|
|
||||||
OPENGL_EXTRA_FUNCTIONS->glActiveTexture(GL_TEXTURE1);
|
|
||||||
OPENGL_EXTRA_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, tex1);
|
|
||||||
OPENGL_EXTRA_FUNCTIONS->glActiveTexture(GL_TEXTURE2);
|
OPENGL_EXTRA_FUNCTIONS->glActiveTexture(GL_TEXTURE2);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, tex2);
|
OPENGL_EXTRA_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, tex);
|
||||||
|
|
||||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(TerrainVAO);
|
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(VAO);
|
||||||
OPENGL_EXTRA_FUNCTIONS->glDrawElements(GL_TRIANGLES, Indicess.size(), GL_UNSIGNED_SHORT, &Indicess.front());
|
OPENGL_EXTRA_FUNCTIONS->glDrawElements(GL_TRIANGLES, Indicess.size(), GL_UNSIGNED_INT, 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -9,10 +9,10 @@ class Terrain {
|
|||||||
private:
|
private:
|
||||||
std::vector<float> Vertex;
|
std::vector<float> Vertex;
|
||||||
std::vector<unsigned int> Indicess;
|
std::vector<unsigned int> Indicess;
|
||||||
unsigned int TerrainVAO, TerrainVBO;
|
unsigned int VAO, VBO, EBO;
|
||||||
int row_num, col_num;
|
int row_num, col_num;
|
||||||
public:
|
public:
|
||||||
unsigned int dep, tex1, tex2;
|
unsigned int dep, tex;
|
||||||
Terrain(int rows = 200, int cols = 200);
|
Terrain(int rows = 200, int cols = 200);
|
||||||
void render();
|
void render();
|
||||||
unsigned int loadTexture(std::string path);
|
unsigned int loadTexture(std::string path);
|
||||||
|
|||||||
BIN
terrain/123.jpg
Normal file
|
After Width: | Height: | Size: 475 KiB |
|
Before Width: | Height: | Size: 174 KiB After Width: | Height: | Size: 174 KiB |
BIN
terrain/789.jpg
Normal file
|
After Width: | Height: | Size: 1.4 MiB |
BIN
terrain/heightmap.jpg
Normal file
|
After Width: | Height: | Size: 39 KiB |
BIN
terrain/rock.jpg
|
Before Width: | Height: | Size: 475 KiB After Width: | Height: | Size: 271 KiB |
|
Before Width: | Height: | Size: 1.4 MiB After Width: | Height: | Size: 400 KiB |