中文OpenGL学习网站:https://learnopengl-cn.github.io
一、学会使用矩阵变换
注:注意矩阵的结合顺序(从左往右读 位移->旋转->缩放)
1.1代码
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include "CShader.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
const unsigned int WIDTH = 800;
const unsigned int HEIGHT = 600;
float MixValue = 0.2f;
void callbackFramebufferSize(GLFWwindow *vWindow, int vWidth, int vHeight)
{
glViewport(0, 0, vWidth, vHeight);
}
void processInput(GLFWwindow *vWindow)
{
if (glfwGetKey(vWindow, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(vWindow, true);
}
if (glfwGetKey(vWindow, GLFW_KEY_UP) == GLFW_PRESS)
{
MixValue = MixValue + 0.001f;
if (MixValue >= 1.0f)
MixValue = 1.0f;
}
if (glfwGetKey(vWindow, GLFW_KEY_DOWN) == GLFW_PRESS)
{
MixValue = MixValue - 0.001f;
if (MixValue <= 0.0f)
MixValue = 0.0f;
}
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *Window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
if (Window == NULL)
{
std::cout << "failed to create glfw window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(Window);
glfwSetFramebufferSizeCallback(Window, callbackFramebufferSize);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "failed to initialize glad" << std::endl;
return -1;
}
CShader ShaderProgram("Shader/Vertex/VertexSource7.glsl", "Shader/Fragment/FragmentSource4.glsl");
//位置、颜色、纹理坐标
float Vertices[] =
{
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,0.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,1.0f
};
unsigned int Indexs[] =
{
0,1,3,
1,2,3
};
unsigned int VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indexs), Indexs, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
//创建应用纹理
unsigned int Texture1, Texture2;
glGenTextures(1, &Texture1);
glBindTexture(GL_TEXTURE_2D, Texture1);
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, NrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char *Data = stbi_load("container.jpg", &Width, &Height, &NrChannels, 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);
//第二张纹理
glGenTextures(1, &Texture2);
glBindTexture(GL_TEXTURE_2D, Texture2);
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);
stbi_set_flip_vertically_on_load(true);
Data = stbi_load("awesomeface.png", &Width, &Height, &NrChannels, 0);
if (Data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "failed to load texture" << std::endl;
}
stbi_image_free(Data);
ShaderProgram.useProgram();
ShaderProgram.setInt("texture1", 0);
ShaderProgram.setInt("texture2", 1);
//Trans = glm::scale(Trans, glm::vec3(0.5, 0.5, 0.5));
//Trans = glm::rotate(Trans, glm::radians(90.0f), glm::vec3(0.0, 0.0, 1.0));
//Trans = glm::translate(Trans, glm::vec3(0.5f, 0.5f, 0.0));
while (!glfwWindowShouldClose(Window))
{
processInput(Window);
ShaderProgram.useProgram();
ShaderProgram.setFloat("MixValue", MixValue);
glm::mat4 Trans = glm::mat4(1.0f);
Trans = glm::translate(Trans, glm::vec3(0.5f, -0.5f, 0.0f));
Trans = glm::rotate(Trans, (float)glfwGetTime(), glm::vec3(0.0, 0.0, 1.0f));
unsigned int TransLoc = glGetUniformLocation(ShaderProgram.m_ShaderId, "Transform");
glUniformMatrix4fv(TransLoc, 1, GL_FALSE, glm::value_ptr(Trans));
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, Texture2);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(Window);
glfwPollEvents();
}
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteVertexArrays(1, &VAO);
glfwTerminate();
return 0;
}
//顶点着色器
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aColor;
layout(location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
uniform mat4 Transform;
void main()
{
gl_Position = Transform*vec4(aPos, 1.0f);
ourColor = aColor;
TexCoord = aTexCoord;
}
//片段着色器
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform float MixValue;
void main()
{
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), MixValue);
}
1.2结果
箱子在右下角旋转
二、尝试再次调用glDrawElements画出第二个箱子,只使用变换将其摆放在不同的位置。让这个箱子被摆放在窗口的左上角,并且会不断的缩放(而不是旋转)
2.1代码
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include "CShader.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
const unsigned int WIDTH = 800;
const unsigned int HEIGHT = 600;
float MixValue = 0.2f;
void callbackFramebufferSize(GLFWwindow *vWindow, int vWidth, int vHeight)
{
glViewport(0, 0, vWidth, vHeight);
}
void processInput(GLFWwindow *vWindow)
{
if (glfwGetKey(vWindow, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(vWindow, true);
}
if (glfwGetKey(vWindow, GLFW_KEY_UP) == GLFW_PRESS)
{
MixValue = MixValue + 0.001f;
if (MixValue >= 1.0f)
MixValue = 1.0f;
}
if (glfwGetKey(vWindow, GLFW_KEY_DOWN) == GLFW_PRESS)
{
MixValue = MixValue - 0.001f;
if (MixValue <= 0.0f)
MixValue = 0.0f;
}
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *Window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
if (Window == NULL)
{
std::cout << "failed to create glfw window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(Window);
glfwSetFramebufferSizeCallback(Window, callbackFramebufferSize);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "failed to initialize glad" << std::endl;
return -1;
}
CShader ShaderProgram("Shader/Vertex/VertexSource7.glsl", "Shader/Fragment/FragmentSource4.glsl");
//位置、颜色、纹理坐标
float Vertices[] =
{
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,0.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,1.0f
};
unsigned int Indexs[] =
{
0,1,3,
1,2,3
};
unsigned int VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indexs), Indexs, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
//创建应用纹理
unsigned int Texture1, Texture2;
glGenTextures(1, &Texture1);
glBindTexture(GL_TEXTURE_2D, Texture1);
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, NrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char *Data = stbi_load("container.jpg", &Width, &Height, &NrChannels, 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);
//第二张纹理
glGenTextures(1, &Texture2);
glBindTexture(GL_TEXTURE_2D, Texture2);
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);
stbi_set_flip_vertically_on_load(true);
Data = stbi_load("awesomeface.png", &Width, &Height, &NrChannels, 0);
if (Data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "failed to load texture" << std::endl;
}
stbi_image_free(Data);
ShaderProgram.useProgram();
ShaderProgram.setInt("texture1", 0);
ShaderProgram.setInt("texture2", 1);
while (!glfwWindowShouldClose(Window))
{
processInput(Window);
ShaderProgram.useProgram();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, Texture2);
ShaderProgram.setFloat("MixValue", MixValue);
glm::mat4 Trans = glm::mat4(1.0f);
Trans = glm::translate(Trans, glm::vec3(0.5f, -0.5f, 0.0f));
Trans = glm::rotate(Trans, (float)glfwGetTime(), glm::vec3(0.0, 0.0, 1.0f));
unsigned int TransLoc = glGetUniformLocation(ShaderProgram.m_ShaderId, "Transform");
glUniformMatrix4fv(TransLoc, 1, GL_FALSE, glm::value_ptr(Trans));
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
Trans = glm::mat4(1.0f);
Trans = glm::translate(Trans, glm::vec3(-0.5f, 0.5f, 0.0f));
float Scale = sin(glfwGetTime());
Trans = glm::scale(Trans, glm::vec3(Scale, Scale, Scale));
glUniformMatrix4fv(TransLoc, 1, GL_FALSE, glm::value_ptr(Trans));
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(Window);
glfwPollEvents();
}
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteVertexArrays(1, &VAO);
glfwTerminate();
return 0;
}
//顶点着色器
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aColor;
layout(location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
uniform mat4 Transform;
void main()
{
gl_Position = Transform*vec4(aPos, 1.0f);
ourColor = aColor;
TexCoord = aTexCoord;
}
//片段着色器
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform float MixValue;
void main()
{
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), MixValue);
}
2.2结果