您的位置:首页 > 运维架构

Opengl_20 _复习变换矩阵+复习光源+两个模型使用不同的shader

2017-08-04 14:26 441 查看
1,
为了将坐标从一个坐标系变换到另一个坐标系,我们需要用到几个变换矩阵,最重要的几个分别是模型(Model)、观察(View)、投影(Projection)三个矩阵。



Vclip=Mprojection⋅Mview⋅Mmodel⋅Vlocal

//colors.vs
layout (location = 0) in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{gl_Position = projection * view * model * vec4(aPos, 1.0);}

2,

glm::mat4 _model;
glm::mat4 _view;
glm::mat4 _projection;
_model = glm::translate(_model, glm::vec3(1.2f, 1.0f, 2.0f));
_model = glm::scale(_model, glm::vec3(0.2f));
_model = glm::rotate(_model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
_view = glm::translate(_view, glm::vec3(0.0f, 0.0f, -3.0f));
_projection = glm::perspective(glm::radians(45.0f), screenWidth / screenHeight, 0.1f, 100.0f);
glUniformMatrix4fv(glGetUniformLocation(ID, "projection"), 1, GL_FALSE, &_projection[0][0]);
glUniformMatrix4fv(glGetUniformLocation(ID, "view"), 1, GL_FALSE, &_view[0][0]);
glUniformMatrix4fv(glGetUniformLocation(ID, "model"), 1, GL_FALSE, &_model[0][0]);

3,
//两个对象使用各自的片元着色器。
//使用同一个顶端缓存表示cube模型,在设置世界空间矩阵的时候各自调整scale、translate和rotate以放在不同位置。

unsigned int VBO ;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

unsigned int cubeVAO;
glGenVertexArrays(1, &cubeVAO);
glBindVertexArray(cubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

// second, configure the light's VAO (VBO stays the same; the vertices are the same for the light object which is also a 3D cube)
// we only need to bind to the VBO (to link it with glVertexAttribPointer), no need to fill it; the VBO's data already contains all we need (it's already bound, but we do it again for educational purposes)

unsigned int lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

4,

Shader lightingShader("colors.vs", "colors.fs");//cube shader
Shader lampShader("colors.vs", "lamp.fs");//光源shader

while (…)
{
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// be sure to activate shader when setting uniforms/drawing objects
lightingShader.use();
lightingShader.setVec3("objectColor", 1.0f, 0.5f, 0.31f);
lightingShader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
lightingShader.setMat4("projection", projection);
lightingShader.setMat4("view", view);
lightingShader.setMat4("model", model);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindVertexArray(cubeVAO); // render the cube
glDrawArrays(GL_TRIANGLES, 0, 36);

// also draw the lamp object
lampShader.use();
lampShader.setMat4("projection", projection);
lampShader.setMat4("view", view);
lampShader.setMat4("model", model);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindVertexArray(lightVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
//…
}

5,
我们在现实生活中看到某一物体的颜色并不是这个物体真正拥有的颜色,而是它所反射的(Reflected)颜色。换句话说,那些不能被物体所吸收(Absorb)的颜色(被拒绝的颜色)就是我们能够感知到的物体的颜色。
如果我们将白光照在一个蓝色的玩具上,这个蓝色的玩具会吸收白光中除了蓝色以外的所有子颜色,不被吸收的蓝色光被反射到我们的眼中,让这个玩具看起来是蓝色的。
我们可以定义物体的颜色为物体从一个光源反射各个颜色分量的大小
光色与物体颜色相乘即是反射的延伸至。
//colors.fs
out vec4 FragColor;
uniform vec3 objectColor;
uniform vec3 lightColor;
void main()
{
FragColor = vec4(lightColor * objectColor, 1.0);
}

//lamp.fs
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0); // set alle 4 vector values to 1.0
}



光源其实不可见的,这里专门在光源位置画个固定白色的方块表示光源。
model = glm::translate(model, lightPos);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: