Skip to content

DBWGLX/-OpenGL-3D-Lighting-and-Shadow-Modeling

Repository files navigation

REPO SIZE CODE SIZE License

Final project of SZU Computer Graphics

Introduction

The project features lighting, Phong shading model, planar shadow rendering, and support for loading and displaying models in .obj and .off formats. The camera perspective is first-person, allowing you to freely move through the scene, similar to playing an FPS game.

In terms of implementation, the fragment shader supports both color and texture rendering, and it is compatible with reading quadrilateral faces from .obj files, with smooth groups handled. However, it does not yet support loading .obj files with multi-textures (.mtl).

GIF 2024-12-18 4

You can easily load models with this code and freely admire them!

Of course, you can also expand and implement other features based on this foundation.

Implementation effect

  1. 场景设计和显示
    学生可以通过层级建模( 实验补充1和2)的方式建立多个虚拟物体,由多个虚拟物体组成一个虚拟场景,要求在程序中显示该虚拟场景,场景可以是室内或者室外场景;场景应包含地面。层级建模的最深层次需要达到至少四层。
    总场景:
    image
    层级建模:
    机械臂。四层:底座-上臂-下臂-手掌
    image
  2. 添加纹理
    参考实验4.1,为场景中至少两个主要物体添加纹理贴图 。
    image
    图中四个物体,地板和后面的背景图片都是贴的纹理
  3. 添加光照、材质、阴影效果
    参考实验3.2,实验3.3和实验3.4,实现光照效果、材质、阴影等。
    image
    可以看到phong光照模型,有阴影。材质由光的效果和网格面片的法向共同实现。
  4. 用户交互实现视角切换完成对场景的任意角度浏览
    参考实验3.1,完成相机变换。
    以上图片已经体现出自由的相机视角
  5. 通过交互控制物体
    参考实验2.3,实现物体的变换,允许用户通过键盘或者鼠标实现场景中至少两个物体的控制(移动,旋转,缩放等等)。
    image
    可以对除层级建模的机械臂外所有网格旋转,移动,放缩
    image

Code Overview

主要是main,然后是三个类的实现:1.相机(Camera.h/Camera.cpp),2.三角网格(TriMesh.h/TriMesh.cpp),3.网格绘制器(MeshPainter.h/MeshPainter.cpp)

image

  1. Camera.cpp:管理摄像机的属性,投影的计算。(Camera.h的实现)
  2. glad.c:加载了 c头文件和OpenGL 函数的实现,以及平台特定的处理。确保程序能够在不同的平台上正确调用 OpenGL API。(GLAD 是一个 OpenGL 加载库,它帮助加载 OpenGL 扩展。)
  3. InitShader.cpp:GLSL着色器初始化工具。用于读取和编译 GLSL 着色器(顶点和片段着色器),然后将它们链接成一个 OpenGL 程序对象。
  4. main.cpp:程序的入口点,包含 main 函数。设置 OpenGL 上下文、窗口,定义键鼠交互逻辑,并调用模型加载,渲染循环等。
  5. TriMesh.cpp:定义了一个基于 OpenGL 的三角形网格类 TriMesh 和一个继承自它的光源类 Light,为 3D 图形渲染和光影处理提供支持。(TriMesh.h的实现)
  6. MeshPainter.cpp:负责使用 OpenGL 绘制 3D 网格对象。包含了将数据绑定到 OpenGL 缓冲区、设置着色器、纹理、光照与材质,以及绘制物体和阴影等功能的实现。(MeshPainter.h的实现)

/shaders 存放着色器

/assets 存放.off ,.obj ,textures

Environment Configuration

https://github.com/DBWGLX/SZU_Tetris/tree/environment

About

My final homework for the computer graphics course at Shenzhen University

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published