计算机图形学课程设计报告书
一、选题
海军风格场景渲染
二、需求分析
用Visual Studio 2017 + OpenGL + GLSL环境,实现模型导入、光照、阴影、纹理、模型导入,实现泛光或其他特效,实现交互功能。
三、系统设计
1、算法和程序流程:
四、界面设计
采用图形界面,通过鼠标和按键WASD交互。
五、系统实现
核心代码:
/
// 程序名称:海军风格场景渲染
// 编译环境:VS2017+OpenGL+GLSL
// 作 者:CSDN——哆啦一泓
// 最后修改:2018-11-2
/
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
Shader shader("6.1.cubemaps.vs", "6.1.cubemaps.fs");
Shader skyboxShader("6.1.skybox.vs", "6.1.skybox.fs");
Shader ourShader("1.model_loading.vs", "1.model_loading.fs");
Shader ourShader0("model.vs", "model.fs");
Model ourModel(FileSystem::getPath("resources/objects/Cruisership 2012/Cruiser 2012.obj"));
unsigned int skyboxVAO, skyboxVBO;
glGenVertexArrays(1, &skyboxVAO);
glGenBuffers(1, &skyboxVBO);
glBindVertexArray(skyboxVAO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
unsigned int cubemapTexture = loadCubemap(faces);
shader.use();
shader.setInt("texture1", 0);
skyboxShader.use();
skyboxShader.setInt("skybox", 0);
while (!glfwWindowShouldClose(window))
{
ourShader.use();
shader.use();
model = glm::scale(model, glm::vec3(0.1f));
model = glm::translate(model, glm::vec3(0.0f, -600.0f, -500.0f));
shader.setMat4("model", model);
shader.setMat4("view", view);
shader.setMat4("projection", projection);
glm::mat4 model0;
model0 = glm::translate(model0, glm::vec3(0.0f, -1.75f, -1000.0f)); // translate it down so it's at the center of the scene
model0 = glm::scale(model, glm::vec3(0.2f, 0.2f, 0.2f)); // it's a bit too big for our scene, so scale it down
ourShader.setMat4("model0", model0);
ourModel.Draw(ourShader);
ourShader.setMat4("model", model);
glDepthFunc(GL_LEQUAL); // change depth function so depth test passes when values are equal to depth buffer's content
skyboxShader.use();
view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
skyboxShader.setMat4("view", view);
skyboxShader.setMat4("projection", projection);
// skybox cube
glBindVertexArray(skyboxVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthFunc(GL_LESS); // set depth function back to default
// we now draw as many light bulbs as we have point lights.
glBindVertexArray(lightVAO);
for (unsigned int i = 0; i < 4; i++)
{
model = glm::mat4();
model = glm::translate(model, pointLightPositions[i]);
model = glm::scale(model, glm::vec3(0.2f)); // Make it a smaller cube
glDrawArrays(GL_TRIANGLES, 0, 36);
}
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &skyboxVAO);
glDeleteBuffers(1, &skyboxVAO);
glfwTerminate();
return 0;
}
六、心得与体会
此次课程设计,我深刻体会到了编程知识积累的重要性。在课设中我遇到了不少难题,但是经过仔细研究,得到解决。2周的课设结束了,我的收获颇丰,同时也更深刻地认识到要做一个合格的程序员最重要的是严谨。计算机图形学的课程设计使我学到了很多东西,为我以后的学习做了引导,点明了方向。
这次课设,我更多学到的是不懂就要问和尽全力尝试,哪怕失败,也要竭尽全力,最后在自己的努力下,取得成功,这种感觉美不可言,成就感十足。相信在不久的将来,我会开创自己的一片天空。