feat(OpenGL-hello): 3D
This commit is contained in:
parent
56ca8c2766
commit
6721cec798
4 changed files with 66 additions and 31 deletions
|
@ -49,6 +49,9 @@ vpath %.cpp $(sort $(dir $(CPP_SRCS)))
|
||||||
$(OBJ_DIR)/%.o:%.cpp
|
$(OBJ_DIR)/%.o:%.cpp
|
||||||
$(CC) $(CPP_FLAGS) -c $< -o $@
|
$(CC) $(CPP_FLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
nv:$(OUT)
|
||||||
|
prime-run ./$(OUT)
|
||||||
|
|
||||||
run:$(OUT)
|
run:$(OUT)
|
||||||
./$(OUT)
|
./$(OUT)
|
||||||
|
|
||||||
|
|
|
@ -8,19 +8,61 @@
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
// 四边形顶点
|
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
// ---- 位置 ---- ---- 颜色 ---- - 纹理坐标 -
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||||
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // 右上
|
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
|
||||||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // 右下
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // 左下
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // 左上
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||||
|
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
|
||||||
|
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
|
||||||
};
|
};
|
||||||
|
|
||||||
// 索引
|
glm::vec3 cubePositions[] = {
|
||||||
unsigned int indices[] = {
|
glm::vec3( 0.0f, 0.0f, 0.0f),
|
||||||
0, 1, 3, // 第一个三角形
|
glm::vec3( 2.0f, 5.0f, -15.0f),
|
||||||
1, 2, 3 // 第二个三角形
|
glm::vec3(-1.5f, -2.2f, -2.5f),
|
||||||
|
glm::vec3(-3.8f, -2.0f, -12.3f),
|
||||||
|
glm::vec3( 2.4f, -0.4f, -3.5f),
|
||||||
|
glm::vec3(-1.7f, 3.0f, -7.5f),
|
||||||
|
glm::vec3( 1.3f, -2.0f, -2.5f),
|
||||||
|
glm::vec3( 1.5f, 2.0f, -2.5f),
|
||||||
|
glm::vec3( 1.5f, 0.2f, -1.5f),
|
||||||
|
glm::vec3(-1.3f, 1.0f, -1.5f)
|
||||||
};
|
};
|
||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||||
|
@ -71,23 +113,14 @@ int main() {
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
// 创建索引缓冲对象
|
|
||||||
glGenBuffers(1, &EBO);
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
|
|
||||||
GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
// 顶点属性
|
// 顶点属性
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
// 颜色属性
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
// 纹理坐标属性
|
||||||
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
|
||||||
(void *)(3 * sizeof(float)));
|
(void *)(3 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
// 纹理坐标属性
|
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
|
||||||
(void *)(6 * sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(2);
|
|
||||||
|
|
||||||
// 创建纹理
|
// 创建纹理
|
||||||
unsigned int texture1, texture2;
|
unsigned int texture1, texture2;
|
||||||
|
@ -161,6 +194,9 @@ int main() {
|
||||||
shader.setMat4("view", view);
|
shader.setMat4("view", view);
|
||||||
shader.setMat4("projection", projection);
|
shader.setMat4("projection", projection);
|
||||||
|
|
||||||
|
// 开启深度测试
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
// 循环渲染
|
// 循环渲染
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
// 处理输入
|
// 处理输入
|
||||||
|
@ -168,7 +204,7 @@ int main() {
|
||||||
|
|
||||||
// 设置为白色
|
// 设置为白色
|
||||||
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
// 绑定纹理
|
// 绑定纹理
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
@ -185,7 +221,7 @@ int main() {
|
||||||
0.0));
|
0.0));
|
||||||
shader.setMat4("trans", trans);
|
shader.setMat4("trans", trans);
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||||
|
|
||||||
// 检查并调用事件,交换缓冲
|
// 检查并调用事件,交换缓冲
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
in vec3 ourColor;
|
|
||||||
in vec2 TexCoord;
|
in vec2 TexCoord;
|
||||||
|
|
||||||
uniform sampler2D texture1;
|
uniform sampler2D texture1;
|
||||||
|
@ -9,5 +8,5 @@ uniform sampler2D texture2;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2) * vec4(ourColor, 1.0);
|
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
|
||||||
}
|
}
|
|
@ -1,9 +1,7 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
layout(location = 0) in vec3 aPos;
|
layout(location = 0) in vec3 aPos;
|
||||||
layout(location = 1) in vec3 aColor;
|
layout(location = 1) in vec2 aTexCoord;
|
||||||
layout(location = 2) in vec2 aTexCoord;
|
|
||||||
|
|
||||||
out vec3 ourColor;
|
|
||||||
out vec2 TexCoord;
|
out vec2 TexCoord;
|
||||||
|
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
|
@ -14,6 +12,5 @@ uniform mat4 trans;
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = projection * view * model * trans * vec4(aPos, 1.0);
|
gl_Position = projection * view * model * trans * vec4(aPos, 1.0);
|
||||||
// gl_Position = trans * vec4(aPos, 1.0);
|
// gl_Position = trans * vec4(aPos, 1.0);
|
||||||
ourColor = aColor;
|
|
||||||
TexCoord = aTexCoord;
|
TexCoord = aTexCoord;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue