1)向量与向量的乘法: dot(a,b) cross(a,b)
2)矩阵与矩阵乘法: 矩阵行列乘法
3)向量与矩阵乘法:向量看作一维矩阵
4)例外:::::
GLSL的vect4* vec4是逐元乘法(component wise)
vect4 a=(1.0,2.0,3.0,4.0);
vect4 b=(0.1,0.2,0.3,0.4);
vect4 c=a*b; //vect4(0.1,0.4,0.9,1.6)
5)W分量为1 顶点向量 W分量为0 速度等向量
缩放矩阵:
位移矩阵:
旋转矩阵:
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "Shader.h"
/*图像载入数据 翻转*/
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
using namespace std;
//齐次坐标系 【-1,1】Normalized Device Coordinates(NDC)
//Vertex input
GLfloat 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 indices[] = { // note that we start from 0!
0, 1, 2, // first triangle
2,3, 0 // second triangle
};
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
int main(int argc, char* argv[])
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
if (window == nullptr)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
//////==========================
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return EXIT_FAILURE;
}
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
//线框模式(Wireframe Mode)
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
////三角形默认逆时针绘制--背面剔除
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
Shader* myShader = new Shader("vertexSource.txt", "fragmentSource.txt");
//VAO Vertex Array Object
// 1. bind Vertex Array Object 绑定顶点数组对象
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// vertex buffer objects (VBO) can store a large number of vertices in the GPU's memory.
// 2. copy our vertices array in a buffer for OpenGL to use
unsigned int VBO;
glGenBuffers(1, &VBO); //产生一个bufferID
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//3.copy our index array in a element buffer for OpenGL to use
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// 4. then set our vertex attributes pointers
// 位置属性
glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(6);
// 颜色属性
glVertexAttribPointer(7, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(7);
// 纹理属性
glVertexAttribPointer(8, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(8);
unsigned int TexBufferA;
glGenTextures(1, &TexBufferA);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, TexBufferA);
// set the texture wrapping/filtering options (on the currently bound texture object)
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_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load and generate the texture
int widthT, heightT, nrChannelsT;
unsigned char* data = stbi_load("container.jpg", &widthT, &heightT, &nrChannelsT, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, widthT, heightT, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D); //多级渐远纹理
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
unsigned int TexBufferB;
glGenTextures(1, &TexBufferB);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, TexBufferB);
stbi_set_flip_vertically_on_load(true);
unsigned char* data2 = stbi_load("awesomeface.png", &widthT, &heightT, &nrChannelsT, 0);
if (data2)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, widthT, heightT, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
glGenerateMipmap(GL_TEXTURE_2D); //多级渐远纹理
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data2);
//变换矩阵 先进行缩放操作,然后是旋转,最后才是位移
//矩阵相乘是右边乘,从右往左读
//glm::mat4 trans = glm::mat4(1.0f);
//trans = glm::rotate(trans, glm::radians(90.0f), glm::vec3(0.0, 0.0, 1.0));
//trans = glm::scale(trans, glm::vec3(0.5, 0.5, 0.5));
while (!glfwWindowShouldClose(window))
{
glm::mat4 trans = glm::mat4(1.0f);
trans = glm::translate(trans, glm::vec3(0.5, -0.5, 0));
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.0, 1.0));
//trans = glm::translate(trans, glm::vec3(-0.001f, 0, 0));
//trans = glm::rotate(trans, glm::radians(0.1f), glm::vec3(0.0, 0.0, 1.0));
// trans = glm::scale(trans, glm::vec3(1.002, 1.002, 1.002));
processInput(window);
glClearColor(0.2, 0.3, 0.3f, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// 4. draw the object
myShader->use();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, TexBufferA);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, TexBufferB);
glBindVertexArray(VAO);
/* //设置uniform
GLint vertexColorLocation = glGetUniformLocation(myShader->ID, "ourColor");
glUseProgram(shaderProgram);
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);*/
glUniform1i(glGetUniformLocation(myShader->ID, "ourTexture"), 0);
glUniform1i(glGetUniformLocation(myShader->ID, "ourFace"), 3);
glUniformMatrix4fv(glGetUniformLocation(myShader->ID, "transform"), 1, GL_FALSE, glm::value_ptr(trans));
// glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
//std::cin.get();
return 0;
}
#version 330 core
layout (location = 6) in vec3 aPos;
layout (location = 7) in vec3 aColor;
layout (location = 8) in vec2 aTexCoord;
out vec4 vertexColor;
out vec2 TexCoord;
uniform mat4 transform;
void main()
{
gl_Position =transform * vec4(aPos.x, aPos.y, aPos.z, 1.0);
vertexColor=vec4(aColor.x+0.5, aColor.y, aColor.z, 1.0);
TexCoord = aTexCoord;
}
#version 330 core
in vec4 vertexColor;
in vec2 TexCoord;
//uniform vec4 ourColor;
out vec4 FragColor;
uniform sampler2D ourTexture;
uniform sampler2D ourFace;
void main()
{
//FragColor = vertexColor;
//FragColor = texture(ourTexture, TexCoord) * vertexColor;
FragColor = mix(texture(ourTexture, TexCoord) , texture(ourFace, TexCoord),0.2);
}