openGL增强表面细节----高度贴图
openGL系列文章目录
文章目录
openGL系列文章目录前言一、代码
主程序c++
效果
前言
一、代码
主程序c++
#include #include #include #include #include #include #include // glm::value_ptr#include // glm::translate, glm::rotate, glm::scale, glm::perspective#include "ImportedModel.h"#include "Utils.h"using namespace std;float toRadians(float degrees) { return (degrees * 2.0f * 3.14159f) / 360.0f; }#define numVAOs 1#define numVBOs 3float cameraX, cameraY, cameraZ;float gndLocX, gndLocY, gndLocZ;GLuint renderingProgram;GLuint vao[numVAOs];GLuint vbo[numVBOs];// variable allocation for displayGLuint mvLoc, projLoc;int width, height;float aspect;glm::mat4 pMat, vMat, mMat, mvMat;ImportedModel ground("grid.obj");int numGroundVertices;GLuint heightMap;GLuint heightTexture;void setupVertices(void) { numGroundVertices = ground.getNumVertices(); std::vector vert = ground.getVertices(); std::vector tex = ground.getTextureCoords(); std::vector norm = ground.getNormals(); std::vector pvalues; std::vector tvalues; std::vector nvalues; for (int i = 0; i < numGroundVertices; i++) { pvalues.push_back((vert[i]).x); pvalues.push_back((vert[i]).y); pvalues.push_back((vert[i]).z); tvalues.push_back((tex[i]).x); tvalues.push_back((tex[i]).y); nvalues.push_back((norm[i]).x); nvalues.push_back((norm[i]).y); nvalues.push_back((norm[i]).z); } glGenVertexArrays(1, vao); glBindVertexArray(vao[0]); glGenBuffers(numVBOs, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBufferData(GL_ARRAY_BUFFER, pvalues.size() * 4, &pvalues[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glBufferData(GL_ARRAY_BUFFER, tvalues.size() * 4, &tvalues[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); glBufferData(GL_ARRAY_BUFFER, nvalues.size() * 4, &nvalues[0], GL_STATIC_DRAW);}void init(GLFWwindow* window) { renderingProgram = Utils::createShaderProgram("vertShader.glsl", "fragShader.glsl"); cameraX = 0.03f; cameraY = 0.03f; cameraZ = 0.8f; gndLocX = 0.0f; gndLocY = 0.0f; gndLocZ = 0.0f; glfwGetFramebufferSize(window, &width, &height); aspect = (float)width / (float)height; pMat = glm::perspective(1.0472f, aspect, 0.1f, 1000.0f); setupVertices(); heightTexture = Utils::loadTexture("heightTexture.jpg"); heightMap = Utils::loadTexture("height.jpg");}void display(GLFWwindow* window, double currentTime) { glClear(GL_DEPTH_BUFFER_BIT); glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(renderingProgram); mvLoc = glGetUniformLocation(renderingProgram, "mv_matrix"); projLoc = glGetUniformLocation(renderingProgram, "proj_matrix"); vMat = glm::translate(glm::mat4(1.0f), glm::vec3(-cameraX, -cameraY, -cameraZ)); mMat = glm::translate(glm::mat4(1.0f), glm::vec3(gndLocX, gndLocY, gndLocZ)); mMat = glm::rotate(mMat, toRadians(15.0f), glm::vec3(1.0f, 0.0f, 0.0f)); mvMat = vMat * mMat; glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mvMat)); glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(pMat)); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(2); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, heightTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, heightMap); glEnable(GL_CULL_FACE); glFrontFace(GL_CCW); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glDrawArrays(GL_TRIANGLES, 0, numGroundVertices);}void window_size_callback(GLFWwindow* win, int newWidth, int newHeight) { aspect = (float)newWidth / (float)newHeight; glViewport(0, 0, newWidth, newHeight); pMat = glm::perspective(1.0472f, aspect, 0.1f, 1000.0f);}int main(void) { if (!glfwInit()) { exit(EXIT_FAILURE); } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); GLFWwindow* window = glfwCreateWindow(800, 800, "Chapter10 - program4a", NULL, NULL); glfwMakeContextCurrent(window); if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); } glfwSwapInterval(1); glfwSetWindowSizeCallback(window, window_size_callback); init(window); while (!glfwWindowShouldClose(window)) { display(window, glfwGetTime()); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS);}
## 2.着色器程序顶点着色器```cpp#version 430layout (location=0) in vec3 vertPos;layout (location=1) in vec2 texCoord;layout (location=2) in vec3 vertNormal;out vec2 tc;uniform mat4 mv_matrix;uniform mat4 proj_matrix;layout (binding=0) uniform sampler2D t; // for texturelayout (binding=1) uniform sampler2D h; // for height mapvoid main(void){ vec4 p = vec4(vertPos,1.0) + vec4((vertNormal*((texture(h, texCoord).r)/5.0f)),1.0f); tc = texCoord; gl_Position = proj_matrix * mv_matrix * p;}
2.片元着色器
#version 430in vec2 tc;out vec4 fragColor;uniform mat4 mv_matrix;uniform mat4 proj_matrix;layout (binding=0) uniform sampler2D t; // for texturelayout (binding=1) uniform sampler2D h; // for height mapvoid main(void){ fragColor = texture(t, tc);}
效果
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~