一、开发环境说明
- 操作系统:windows
- 开发软件:Visual Studio 2017
- 编程语言:基于控制台下的opengl
- 用到的库: glfw 、glew
- 底部提供代码下载
opengl环境配置可参照上一篇博客opengl环境配置+GLFW+GLEW+VS2017
二、运行效果展示如下
三、内容说明:
1 、功能介绍
- 实现三维场景,可以游览以及可以看到模型物体的不同状态。
- 三维场景有不同的模型组合,有房子,有雪人。
- 可以模拟真实的光照效果,路灯效果。
- 模拟粒子系统的,下雪效果。
- 房子有真实效果的模型和细节。
- 鼠标键盘交互实现漫游功能
2、操作说明
- 鼠标左键:按下拖动控制视角
- 按键 ‘W’ : 控制视角前进
- 按键 ‘S’ : 控制视角后退
3、程序目录截图
4、程序部分代码展示
void GraphicRenderer::DrawSprite(int modetype,glm::mat4 modelview, Texture2D texture, glm::vec3 position, glm::vec3 size, glm::vec3 rotate, glm::vec3 color)
{// Prepare transformationsthis->shader.Use();//绑定shaderglm::mat4 model = modelview;model = glm::translate(model, glm::vec3(position)); // 平移model = glm::rotate(model, rotate[0], glm::vec3(1.0f, 0.0f, 0.0f)); //绕X轴旋转model = glm::rotate(model, rotate[1], glm::vec3(0.0f, 1.0f, 0.0f)); // 绕Y轴旋转model = glm::rotate(model, rotate[2], glm::vec3(0.0f, 0.0f, 1.0f)); // 绕Z轴旋转model = glm::scale(model, glm::vec3(size)); //缩放this->shader.SetMatrix4("model", model);//模型矩阵this->shader.SetVector3f("spriteColor", color);//传入颜色glActiveTexture(GL_TEXTURE0);//绑定纹理texture.Bind();if (modetype == 0)//四边形{glBindVertexArray(this->quadVAO);glDrawArrays(GL_TRIANGLES, 0, 6);}if (modetype == 1)//正方体{glBindVertexArray(this->cubeVAO);glDrawArrays(GL_TRIANGLES, 0, 36);}if (modetype == 2)//球体{glBindVertexArray(this->shpereVAO);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shpereVEO);glDrawElements(GL_TRIANGLES, (40 - 1)*(30 - 1) * 6, GL_UNSIGNED_INT, (void *)0);}glActiveTexture(0);glBindVertexArray(0);glUseProgram(0);
}void Render()
{//shader 1 光照ResourceManager::GetShader("GraphicLight").Use();glm::mat4 view = camera.GetViewMatrix();ResourceManager::GetShader("GraphicLight").SetMatrix4("view", view);ResourceManager::GetShader("GraphicLight").SetVector3f("viewPos", camera.Position);glm::mat4 model;Renderer->SetShader(ResourceManager::GetShader("GraphicLight"));//绘制地面Renderer->DrawSprite(1, model, ResourceManager::GetTexture("smoke"), glm::vec3(0, -1.0, 0), glm::vec3(1000, 1, 1000.0), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));//绘制墙壁{glm::mat4 model1 = glm::translate(model, glm::vec3(60.0, 0.0, 0.0));model1 = glm::scale(model, glm::vec3(1.2, 1.0, 1.0));Renderer->DrawSprite(1, model1, ResourceManager::GetTexture("wall"), glm::vec3(0, 50, 0), glm::vec3(300, 100, 300.0), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));//门Renderer->DrawSprite(1, model, ResourceManager::GetTexture("door"), glm::vec3(160, 35, 150), glm::vec3(30, 70, 1.0), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));}//绘制雪人DrawXueren(model); shader 3 室内ResourceManager::GetShader("Graphic").Use();ResourceManager::GetShader("Graphic").SetMatrix4("view", view); Renderer->SetShader(ResourceManager::GetShader("Graphic"));model = glm::translate(model, glm::vec3(0, 0, 0));绘制路灯DrawRoadLight(model);//房间1;DrawRoom1(model);//房间2DrawRoom2(model);//房间3DrawRoom3(model);//shader 2 雪花ResourceManager::GetShader("Particle").Use();//ResourceManager::GetShader("Particle").SetMatrix4("projection", projection);ResourceManager::GetShader("Particle").SetMatrix4("view", view);//绘制雪花m_ParticleGenerator->Draw(model);
}
5、 运行效果展示
四、测试用例及代码下载
- 测试用例下载:点击exe文件,可以直接运行;
- 完整代码工程下载