Skip to content

Some documents were added #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9d19eec
finish task2
CelesteOvO Nov 13, 2022
b2f2f56
finish task6
CelesteOvO Nov 13, 2022
6cde8c8
Update README.md
CelesteOvO Nov 13, 2022
e13b536
Update README.md
CelesteOvO Nov 13, 2022
cb00500
finish task7
CelesteOvO Nov 14, 2022
8768317
Merge remote-tracking branch 'origin/master'
CelesteOvO Nov 14, 2022
f4a1339
finish task9
CelesteOvO Nov 14, 2022
9b5303a
finish Transformation and coordinate system
CelesteOvO Nov 14, 2022
859b78b
finish the Getting Started section
CelesteOvO Nov 14, 2022
6a16bba
Starting light section
CelesteOvO Nov 14, 2022
fc8ae3b
finish basic light
CelesteOvO Nov 14, 2022
2ef2806
finish Material
CelesteOvO Nov 14, 2022
b6a6da5
finish task3
CelesteOvO Nov 14, 2022
5a597d1
add class object
CelesteOvO Feb 8, 2023
5984eb1
03task1
CelesteOvO Feb 22, 2023
ce98fba
03task2
CelesteOvO Feb 22, 2023
9f6c305
03task3
CelesteOvO Feb 22, 2023
b824bde
04DirectionLight
CelesteOvO Feb 22, 2023
abab3be
05AttenuationLight
CelesteOvO Feb 22, 2023
b1a2a27
06FlashLight
CelesteOvO Feb 23, 2023
c40a276
07MultipleLight(directional, point lights and an optional flashlight)
CelesteOvO Feb 23, 2023
33363d8
07task01desert
CelesteOvO Feb 23, 2023
78a25a8
Chapter3 Initial Submission
CelesteOvO Feb 23, 2023
c5a9073
Chapter4 01DepthTest
CelesteOvO Mar 23, 2023
3661bbf
Chapter4 02Visualization of depth buffer
CelesteOvO Mar 23, 2023
b24a2e4
Chapter4 03 object Outlining
CelesteOvO Mar 23, 2023
3f0e620
Chapter4 04 Blending
CelesteOvO Mar 23, 2023
7e46565
Chapter4 05 Render Translucent Texture
CelesteOvO Mar 23, 2023
5378713
Chapter4 06 initial go back school!!!
CelesteOvO Mar 23, 2023
e7680bf
chapter4 06 face culling
CelesteOvO Mar 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ add_subdirectory("${LEARN_OPENGL_DEPS_DIR}/assimp/" "${CMAKE_CURRENT_BINARY_DIR}
target_include_directories(glad PUBLIC ${LEARN_OPENGL_DEPS_DIR})

add_subdirectory(chapter1)
add_subdirectory(chapter2)
add_subdirectory(chapter2)
add_subdirectory(chapter3)
add_subdirectory(chapter4)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[![Cmake Build](https://github.com/BFU-Graphics/LearnOpenGL/actions/workflows/macos.yml/badge.svg)](https://github.com/BFU-Graphics/LearnOpenGL/actions/workflows/macos.yml)
[![Cmake Build](https://github.com/BFU-Graphics/LearnOpenGL/actions/workflows/windows.yml/badge.svg)](https://github.com/BFU-Graphics/LearnOpenGL/actions/workflows/windows.yml)

https://bronzed-lillipilli-ce9.notion.site/LearnOpenGL-e3f14b48e56143d185795ce62ebc1dac

## Get Started with OpenGL

This is a practice repo for mastering Modern OpenGL and learn how to startup a graphics project orgnized by Cmake from scratch.
Expand Down
20 changes: 20 additions & 0 deletions chapter1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,33 @@ set(SRCS
00BasicOpenGL
01InputAndResize
02HelloTriangle
02task1
02task2
02task3
03ShadersInFile
04PassValueToShader
05PassValueToLocation
06SetupShaderClass
06task1
06task2
06task3
07SetupTextureClass
07task0
07task1
07task2
07task3
07task4
07task5
08SetupRenderObjectClass
09IntroduceMVP
09task0
09task1
09task2
10Enter3D
11MoreCubes
11task1
12SetupCameraClass
12task1
)

foreach(SRC ${SRCS})
Expand Down
79 changes: 79 additions & 0 deletions chapter1/include/camera.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
#include "camera.h"

Camera::Camera(glm::vec3 position, glm::vec3 up, float yaw, float pitch):Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = position;
WorldUp = up;
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}

Camera::Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch): Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = glm::vec3(posX, posY, posZ);
WorldUp = glm::vec3(upX, upY, upZ);
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}

glm::mat4 Camera::GetViewMatrix() const {
return glm::lookAt(Position, Position + Front, Up);
}

void Camera::ProcessKeyboard(Camera_Movement direction, float deltaTime) {
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
Position += Front * velocity;
if (direction == BACKWARD)
Position -= Front * velocity;
if (direction == LEFT)
Position -= Right * velocity;
if (direction == RIGHT)
Position += Right * velocity;
}

void Camera::ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch) {
xoffset *= MouseSensitivity;
yoffset *= MouseSensitivity;

Yaw += xoffset;
Pitch += yoffset;

// make sure that when pitch is out of bounds, screen doesn't get flipped
if (constrainPitch)
{
if (Pitch > 89.0f)
Pitch = 89.0f;
if (Pitch < -89.0f)
Pitch = -89.0f;
}

// update Front, Right and Up Vectors using the updated Euler angles
updateCameraVectors();
}

void Camera::ProcessMouseScroll(float yoffset) {
Zoom -= (float)yoffset;
if (Zoom < 1.0f)
Zoom = 1.0f;
if (Zoom > 45.0f)
Zoom = 45.0f;
}

void Camera::updateCameraVectors() {
glm::vec3 front;
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
front.y = sin(glm::radians(Pitch));
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
Front = glm::normalize(front);
// also re-calculate the Right and Up vector
Right = glm::normalize(glm::cross(Front, WorldUp)); // normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
Up = glm::normalize(glm::cross(Right, Front));
}






43 changes: 43 additions & 0 deletions chapter1/include/camera.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
#ifndef LEARNOPENGL_CAMERA_H
#define LEARNOPENGL_CAMERA_H

#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include <vector>

enum Camera_Movement {
FORWARD,
BACKWARD,
LEFT,
RIGHT
};

const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;

class Camera
{
public:
// camera Attributes
glm::vec3 Position;
glm::vec3 Front;
glm::vec3 Up;
glm::vec3 Right;
glm::vec3 WorldUp;

// euler Angles
float Yaw;
float Pitch;

// camera options
float MovementSpeed;
float MouseSensitivity;
float Zoom;

Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH);
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch);
[[nodiscard]] glm::mat4 GetViewMatrix() const;
void ProcessKeyboard(Camera_Movement direction, float deltaTime);
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true);
void ProcessMouseScroll(float yoffset);
private:
void updateCameraVectors();
};

#endif //LEARNOPENGL_CAMERA_H
8 changes: 8 additions & 0 deletions chapter1/include/renderable_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ void RenderableObject::render(const Shader &shader)
glBindVertexArray(ID);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}

void RenderableObject::renderCube(const Shader &shader)
{
shader.use();
texture.bind();
glBindVertexArray(ID);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
}
1 change: 1 addition & 0 deletions chapter1/include/renderable_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class RenderableObject

public:
void render(const Shader &shader); // 使用这个shader来渲染这个物体
void renderCube(const Shader &shader);

public:
struct Vertex
Expand Down
88 changes: 86 additions & 2 deletions chapter1/include/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ Texture::Texture(const std::string &texture_path)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int width, height, nr_channels;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load(texture_path.c_str(), &width, &height, &nr_channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
GLenum format;
if (nr_channels == 1)
format = GL_RED;
else if (nr_channels == 3)
format = GL_RGB;
else if (nr_channels == 4)
format = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
stbi_image_free(data);

ID = texture_ID;
}
Expand All @@ -33,3 +41,79 @@ void Texture::bind()
{
glBindTexture(GL_TEXTURE_2D, ID);
}

Texture::Texture(const std::string &texture_path, int flag) {
if(flag == 1){
unsigned int texture_ID;
glGenTextures(1, &texture_ID);

glBindTexture(GL_TEXTURE_2D, texture_ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int width, height, nr_channels;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load(texture_path.c_str(), &width, &height, &nr_channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

ID = texture_ID;
}else if(flag == 2){
unsigned int texture_ID;
glGenTextures(1, &texture_ID);

glBindTexture(GL_TEXTURE_2D, texture_ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

int width, height, nr_channels;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load(texture_path.c_str(), &width, &height, &nr_channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

ID = texture_ID;
}else if(flag == 3){
unsigned int texture_ID;
glGenTextures(1, &texture_ID);

glBindTexture(GL_TEXTURE_2D, texture_ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

int width, height, nr_channels;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load(texture_path.c_str(), &width, &height, &nr_channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

ID = texture_ID;
}
}
1 change: 1 addition & 0 deletions chapter1/include/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Texture

public:
Texture(const std::string &texture_path);
Texture(const std::string &texture_path, int flag);
void bind();
};
#endif //LEARNOPENGL_TEXTURE_H
11 changes: 11 additions & 0 deletions chapter1/shader/06task1_shader_vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
out vec3 ourColor;

void main()
{
ourColor = aColor;
gl_Position = vec4(aPos.x, -aPos.y, aPos.z, 1.0);
}

13 changes: 13 additions & 0 deletions chapter1/shader/06task2_shader_vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;

out vec3 ourColor;

uniform float xOffset;

void main()
{
gl_Position = vec4(aPos.x + xOffset, aPos.y, aPos.z, 1.0); // 添加x偏移量
ourColor = aColor;
}
9 changes: 9 additions & 0 deletions chapter1/shader/06task3_shader_fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 330 core
out vec4 FragColor;
// in vec3 ourColor;
in vec3 ourPosition;

void main()
{
FragColor = vec4(ourPosition, 1.0);
}
15 changes: 15 additions & 0 deletions chapter1/shader/06task3_shader_vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;

///原本的样子是被注释的语句

// out vec3 ourColor;
out vec3 ourPosition;

void main()
{
gl_Position = vec4(aPos, 1.0);
// ourColor = aColor;
ourPosition = aPos;
}
12 changes: 12 additions & 0 deletions chapter1/shader/07task0_shader_fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;

// texture sampler
uniform sampler2D texture1;

void main()
{
FragColor = texture(texture1, TexCoord) * vec4(ourColor, 1.0);
}
Loading