diff --git a/FinalProject/sceneviewer.cpp b/FinalProject/sceneviewer.cpp index 1111d49..0614ea9 100644 --- a/FinalProject/sceneviewer.cpp +++ b/FinalProject/sceneviewer.cpp @@ -75,6 +75,8 @@ void SceneViewer::initializeGL() { vertexShader_sky.dispose(); fragmentShader_sky.dispose(); + + terrainShader.ensureInitialized(); VertexShader vertexShader_ter("./temp/shaders/terrainShader.vs"); FragmentShader fragmentShader_ter("./temp/shaders/terrainShader.fs"); terrainShader.attachShader(vertexShader_ter); @@ -107,34 +109,32 @@ void SceneViewer::paintGL() { // Set view and projection matrices glm::mat4 view = _camera.viewMatrix(); 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); for (auto object : _objects) { object.render(_shaderProgram); - }*/ + } _shaderProgram.unbind(); - terrainShader.bind(); - glm::mat4 Model = glm::mat4(1.0f); - Model = glm::translate(Model, glm::vec3(0.0f, 0.0f, -2.0f)); - terrainShader.setUniform("view", view); - terrainShader.setUniform("projection", projection); - terrainShader.setUniform("model", Model); - terrainShader.setUniform("dep", 0); - terrainShader.setUniform("tex1", 1); - terrainShader.setUniform("tex2", 2); - ter->render(); - terrainShader.unbind(); + //terrainShader.bind(); + //glm::mat4 Model = glm::mat4(1.0f); + //Model = glm::translate(Model, glm::vec3(0.0f, 0.0f, -2.0f)); + //terrainShader.setUniform("view", view); + //terrainShader.setUniform("projection", projection); + //terrainShader.setUniform("model", Model); + //terrainShader.setUniform("texture1", 2); + //ter->render(); + //terrainShader.unbind(); - /*skyShader.bind(); + skyShader.bind(); view = glm::mat4(glm::mat3(view)); skyShader.setUniform("view", view); skyShader.setUniform("projection", projection); sky->render(); - skyShader.unbind();*/ + skyShader.unbind(); } diff --git a/FinalProject/terrain.cpp b/FinalProject/terrain.cpp index 68084b6..ad202ea 100644 --- a/FinalProject/terrain.cpp +++ b/FinalProject/terrain.cpp @@ -6,51 +6,99 @@ Terrain::Terrain(int rows, int cols):row_num(rows),col_num(cols) { Vertex.clear(); - Indicess.clear(); - float x = -50, z = -50; + Indicess.clear(); + int imgW, imgH, imgChannel; - for (int i = 0; i < rows; i++) { - x = -5; - for (int j = 0; j < cols; j++) { - Vertex.push_back(x); - Vertex.push_back(0); - Vertex.push_back(z); - Vertex.push_back(1.0f / cols * j); - Vertex.push_back(1 - i * 1.0f / rows); - x += 0.1; + unsigned char* data = stbi_load("D:/ProgrammingFile/SceneEditor/terrain/heightmap.jpg", &imgW, &imgH, &imgChannel, 1); + + + int index1 = 0, index = 0; + float xrate = imgW / 2.0f; + float rrate = imgH / 2.0f; + + if (data) { + 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++) { - for (int j = 1; j < cols; j++) { - Indicess.push_back((i - 1) * cols + j - 1); - Indicess.push_back((i - 1) * cols + j); - Indicess.push_back(i * cols + j - 1); + index1 = r * imgW + c; + index = (r + 1) * imgW + c; + Indicess.push_back(index1); + Indicess.push_back(index1 + 1); + Indicess.push_back(index + 1); - Indicess.push_back(i * cols + j - 1); - Indicess.push_back((i - 1) * cols + j); - Indicess.push_back(i * cols + j); + Indicess.push_back(index1); + Indicess.push_back(index); + Indicess.push_back(index + 1); + + } } } + stbi_image_free(data); + - tex1 = loadTexture("D:/ProgrammingFile/SceneEditor/terrain/rock.jpg"); - tex2 = loadTexture("D:/ProgrammingFile/SceneEditor/terrain/water.jpg"); - dep = loadTexture("D:/ProgrammingFile/SceneEditor/terrain/heightmap.png"); + OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &VAO); + OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &VBO); + OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &EBO); - OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &TerrainVAO); - OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &TerrainVBO); - OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(TerrainVAO); - OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, TerrainVBO); - OPENGL_EXTRA_FUNCTIONS->glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex), &Vertex, GL_STATIC_DRAW); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(VAO); + + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, VBO); + OPENGL_EXTRA_FUNCTIONS->glBufferData(GL_ARRAY_BUFFER, Vertex.size() * sizeof(float), &Vertex[0], 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->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 textureID; OPENGL_FUNCTIONS->glGenTextures(1, &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); int width, height, nrChannels; @@ -83,15 +131,10 @@ unsigned int Terrain::loadTexture(std::string path) { void Terrain::render() { 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->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->glBindTexture(GL_TEXTURE_2D, tex2); + OPENGL_EXTRA_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, tex); - OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(TerrainVAO); - OPENGL_EXTRA_FUNCTIONS->glDrawElements(GL_TRIANGLES, Indicess.size(), GL_UNSIGNED_SHORT, &Indicess.front()); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(VAO); + OPENGL_EXTRA_FUNCTIONS->glDrawElements(GL_TRIANGLES, Indicess.size(), GL_UNSIGNED_INT, 0); } \ No newline at end of file diff --git a/FinalProject/terrain.h b/FinalProject/terrain.h index 39e6fab..bfcc8c9 100644 --- a/FinalProject/terrain.h +++ b/FinalProject/terrain.h @@ -9,10 +9,10 @@ class Terrain { private: std::vector Vertex; std::vector Indicess; - unsigned int TerrainVAO, TerrainVBO; + unsigned int VAO, VBO, EBO; int row_num, col_num; public: - unsigned int dep, tex1, tex2; + unsigned int dep, tex; Terrain(int rows = 200, int cols = 200); void render(); unsigned int loadTexture(std::string path); diff --git a/terrain/123.jpg b/terrain/123.jpg new file mode 100644 index 0000000..ec4cdf8 Binary files /dev/null and b/terrain/123.jpg differ diff --git a/terrain/heightmap.png b/terrain/456.png similarity index 100% rename from terrain/heightmap.png rename to terrain/456.png diff --git a/terrain/789.jpg b/terrain/789.jpg new file mode 100644 index 0000000..b1ec2da Binary files /dev/null and b/terrain/789.jpg differ diff --git a/terrain/heightmap.jpg b/terrain/heightmap.jpg new file mode 100644 index 0000000..689c30c Binary files /dev/null and b/terrain/heightmap.jpg differ diff --git a/terrain/rock.jpg b/terrain/rock.jpg index ec4cdf8..f892884 100644 Binary files a/terrain/rock.jpg and b/terrain/rock.jpg differ diff --git a/terrain/water.jpg b/terrain/water.jpg index b1ec2da..cd551aa 100644 Binary files a/terrain/water.jpg and b/terrain/water.jpg differ