我们希望通过某种方式对物体的每个片段单独设置漫反射颜色。有能够让我们根据片段在物体上的位置来获取颜色值得系统吗?
这可能听起来很熟悉,而且事实上这个系统我们已经使用很长时间了。这听起来很像在之前教程中详细讨论过的纹理,而这基本就是这样:一个纹理。我们仅仅是对同样的原理使用了不同的名字:其实都是使用一张覆盖物体的图像,让我们能够逐片段索引其独立的颜色值。在光照场景中,它通常叫做一个漫反射贴图(Diffuse Map)(3D艺术家通常都这么叫它),它是一个表现了物体所有的漫反射颜色的纹理图像。
为了演示漫反射贴图,我们将会使用下面的图片,它是一个有钢边框的木箱:
在着色器中使用漫反射贴图的方法和纹理教程中是完全一样的。但这次我们会将纹理储存为Material结构体中的一个sampler2D
。我们将之前定义的vec3
漫反射颜色向量替换为漫反射贴图。
注意sampler2D
是所谓的不透明类型(Opaque Type),也就是说我们不能将它实例化,只能通过uniform来定义它。如果我们使用除uniform以外的方法(比如函数的参数)实例化这个结构体,GLSL会抛出一些奇怪的错误。这同样也适用于任何封装了不透明类型的结构体。
我们也移除了环境光材质颜色向量,因为环境光颜色在几乎所有情况下都等于漫反射颜色,所以我们不需要将它们分开储存:
struct Material {
sampler2D diffuse;
vec3 specular;
float shininess;
};
...
in vec2 TexCoords;
如果你非常固执,仍想将环境光颜色设置为一个(漫反射值之外)不同的值,你也可以保留这个环境光的vec3
,但整个物体仍只能拥有一个环境光颜色。如果想要对不同片段有不同的环境光值,你需要对环境光值单独使用另外一个纹理。
注意我们将在片段着色器中再次需要纹理坐标,所以我们声明一个额外的输入变量。接下来我们只需要从纹理中采样片段的漫反射颜色值即可:
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
不要忘记将环境光得材质颜色设置为漫反射材质颜色同样的值。
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
这就是使用漫反射贴图的全部步骤了。你可以看到,这并不是什么新的东西,但这能够极大地提高视觉品质。为了让它正常工作,我们还需要使用纹理坐标更新顶点数据,将它们作为顶点属性传递到片段着色器,加载材质并绑定材质到合适的纹理单元。
更新后的顶点数据可以在这里找到。顶点数据现在包含了顶点位置、法向量和立方体顶点处的纹理坐标。让我们更新顶点着色器来以顶点属性的形式接受纹理坐标,并将它们传递到片段着色器中:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
...
out vec2 TexCoords;
void main()
{
...
TexCoords = aTexCoords;
}
记得去更新两个VAO的顶点属性指针来匹配新的顶点数据,并加载箱子图像为一个纹理。在绘制箱子之前,我们希望将要用的纹理单元赋值到material.diffuse这个uniform采样器,并绑定箱子的纹理到这个纹理单元:
lightingShader.setInt("material.diffuse", 0);
...
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, diffuseMap);
使用了漫反射贴图之后,细节再一次得到惊人的提升,这次箱子有了光照开始闪闪发光(字面意思也是)了。你的箱子看起来可能像这样:
你可以在这里找到程序的全部代码。
你可能会注意到,镜面高光看起来有些奇怪,因为我们的物体大部分都是木头,我们知道木头不应该有这么强的镜面高光的。我们可以将物体的镜面光材质设置为vec3(0.0)
来解决这个问题,但这也意味着箱子钢制的边框将不再能够显示镜面高光了,我们知道钢铁应该是有一些镜面高光的。所以,我们想要让物体的某些部分以不同的强度显示镜面高光。这个问题看起来和漫反射贴图非常相似。是巧合吗?我想不是。
我们同样可以使用一个专门用于镜面高光的纹理贴图。这也就意味着我们需要生成一个黑白的(如果你想得话也可以是彩色的)纹理,来定义物体每部分的镜面光强度。下面是一个镜面光贴图(Specular Map)的例子:
镜面高光的强度可以通过图像每个像素的亮度来获取。镜面光贴图上的每个像素都可以由一个颜色向量来表示,比如说黑色代表颜色向量vec3(0.0)
,灰色代表颜色向量vec3(0.5)
。在片段着色器中,我们接下来会取样对应的颜色值并将它乘以光源的镜面强度。一个像素越「白」,乘积就会越大,物体的镜面光分量就会越亮。
由于箱子大部分都由木头所组成,而且木头材质应该没有镜面高光,所以漫反射纹理的整个木头部分全部都转换成了黑色。箱子钢制边框的镜面光强度是有细微变化的,钢铁本身会比较容易受到镜面高光的影响,而裂缝则不会。
从实际角度来说,木头其实也有镜面高光,尽管它的反光度(Shininess)很小(更多的光被散射),影响也比较小,但是为了教学目的,我们可以假设木头不会对镜面光有任何反应。
使用Photoshop或Gimp之类的工具,将漫反射纹理转换为镜面光纹理还是比较容易的,只需要剪切掉一些部分,将图像转换为黑白的,并增加亮度/对比度就好了。
镜面光贴图和其它的纹理非常类似,所以代码也和漫反射贴图的代码很类似。记得要保证正确地加载图像并生成一个纹理对象。由于我们正在同一个片段着色器中使用另一个纹理采样器,我们必须要对镜面光贴图使用一个不同的纹理单元(见纹理),所以我们在渲染之前先把它绑定到合适的纹理单元上:
lightingShader.setInt("material.specular", 1);
...
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, specularMap);
接下来更新片段着色器的材质属性,让其接受一个sampler2D
而不是vec3
作为镜面光分量:
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess;
};
最后我们希望采样镜面光贴图,来获取片段所对应的镜面光强度:
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
FragColor = vec4(ambient + diffuse + specular, 1.0);
通过使用镜面光贴图我们可以可以对物体设置大量的细节,比如物体的哪些部分需要有闪闪发光的属性,我们甚至可以设置它们对应的强度。镜面光贴图能够在漫反射贴图之上给予我们更高一层的控制。
如果你想另辟蹊径,你也可以在镜面光贴图中使用真正的颜色,不仅设置每个片段的镜面光强度,还设置了镜面高光的颜色。从现实角度来说,镜面高光的颜色大部分(甚至全部)都是由光源本身所决定的,所以这样并不能生成非常真实的视觉效果(这也是为什么图像通常是黑白的,我们只关心强度)。
如果你现在运行程序的话,你可以清楚地看到箱子的材质现在和真实的钢制边框箱子非常类似了:
你可以在这里找到程序的全部源码。
通过使用漫反射和镜面光贴图,我们可以给相对简单的物体添加大量的细节。我们甚至可以使用法线/凹凸贴图(Normal/Bump Map)或者反射贴图(Reflection Map)给物体添加更多的细节,但这些将会留到之后的教程中。把你的箱子给你的朋友或者家人看看,并且坚信我们的箱子有一天会比现在更加漂亮!
看一下箱子的shader代码:
box.vs:
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 textCoord;
out vec3 color;
out vec3 normal_frag;
out vec3 fragPos;
out vec2 atextcoord;
uniform mat4 view;
uniform mat4 model;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(aPos,1.0);
fragPos = vec3(model * vec4(aPos,1.0));
normal_frag = mat3(transpose(inverse(model))) * normal;
atextcoord = textCoord;
}
box.fs:
#version 330 core
in vec3 color;
out vec4 fragColor;
uniform vec3 objectColor; // 物体颜色
uniform vec3 lightColor; // 光源颜色
uniform vec3 lightPos; // 光源位置
uniform vec3 viewPos; // 视角位置也就是相机的位置
in vec3 fragPos; // 片段位置
in vec3 normal_frag; // 片段法向量
in vec2 atextcoord; // 纹理坐标
struct Material {
sampler2D diffuse; // 漫反射贴图
sampler2D specular; // 镜面光贴图
sampler2D refTex; // 发射光贴图
float shininess;
};
struct Light {
vec3 ambient;
vec3 diffuse;
vec3 position;
vec3 specular;
};
uniform Material material;
uniform Light light;
// 计算漫反射的函数
vec3 diffLight(vec3 normalVector) {
float diff;
vec3 lightDir = normalize(lightPos - fragPos);
diff = max(dot(normalVector,lightDir),0.0);
vec3 res;
res = diff * vec3(texture(material.diffuse,atextcoord)) * lightColor * light.diffuse + vec3(texture(material.refTex,atextcoord));
return res;
}
// 镜面反射光计算
vec3 mirrorLight(vec3 normalVector) {
vec3 lightDir = normalize(lightPos - fragPos);
vec3 viewDir = normalize(viewPos - fragPos);
vec3 reflectDir = reflect(-lightDir,normalVector);
float spec = pow(max(dot(viewDir,reflectDir),0.0),material.shininess);
return spec * (vec3(texture(material.specular,atextcoord))) * lightColor * light.specular + vec3(texture(material.refTex,atextcoord));
}
// 环境光计算
vec3 ambientLight() {
vec3 res;
res = lightColor * light.ambient * vec3(texture(material.diffuse,atextcoord)) + vec3(texture(material.refTex,atextcoord));
return res;
}
void main() {
vec3 normalVector = normalize(normal_frag);
vec3 diff = diffLight(normalVector);
vec3 mirror = mirrorLight(normalVector);
vec3 phoneLight = ambientLight() + diff + mirror;
fragColor = vec4(phoneLight,1.0);
}
main.cpp:
#include
#include
#include
#include
#include
#include
#include
#include
#include "shader.h"
#include "stb_image.h"
using namespace std;
typedef unsigned int uint;
const uint SCR_WIDTH = 800;
const uint SCR_HEIGHT = 600;
void processInput(GLFWwindow *window);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
GLFWwindow* initGlfwWindow();
void renderTriangle();
void drawArrays();
void renderBox();
void drawBox(uint vao);
glm::mat4 createViewMatrix();
// 渲染灯光立方体
void renderLightBox();
void rotateLight();
void changeLightColor(Shader shaderProgram);
void loadImage(string url,uint index,string type);
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f,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, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
uint VAO, VBO,lightVAO;
// 摄像机的位置
glm::vec3 cameraPos = glm::vec3(-2.0f,0.0f,5.0f);
// 摄像机的方向
glm::vec3 cameraFront = glm::vec3(0.0f,0.0f,-1.0f);
// 摄像机的up
glm::vec3 cameraUp = glm::vec3(0.0f,1.0f,0.0f);
// 观察矩阵
glm::mat4 viewMatrix = glm::mat4(1.0f);
// 投影矩阵
glm::mat4 projectionMatrix = glm::mat4(1.0f);
float fov;
glm::vec3 lightPos = glm::vec3(-1.0f, 1.0f, 1.0f);
uint texture0,texture1,texture2;
int main() {
// 初始化窗口
GLFWwindow* window = initGlfwWindow();
// 启用深度检测
glEnable(GL_DEPTH_TEST);
// 投影矩阵
projectionMatrix = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
viewMatrix = createViewMatrix();
loadImage("container2.png",0,"png");
loadImage("container2_specular.png",1,"png");
loadImage("matrix.jpg",2,"jpg");
// 创建shader
Shader shaderProgram = Shader("box.vs","box.fs");
//ShaderTest shadertest = ShaderTest("box.vs","box.fs");
shaderProgram.use();
renderBox();
// 物体颜色
glm::vec3 objectColor = glm::vec3(1.0f, 0.4f, 0.0f);
glUniform3fv(glGetUniformLocation(shaderProgram.ID, "objectColor"), 1, glm::value_ptr(objectColor));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram.ID, "view"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram.ID, "projection"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
shaderProgram.setInt("material.diffuse", 0);
shaderProgram.setInt("material.specular", 1);
shaderProgram.setInt("material.refTex", 2);
shaderProgram.setFloat("material.shininess",32.0f);
shaderProgram.setVec3("lightColor", glm::vec3(1.0f, 0.5f, 0.8f));
shaderProgram.setVec3("light.ambient", glm::vec3(0.3f,0.3f,0.3f));
shaderProgram.setVec3("light.diffuse", glm::vec3(0.5f,0.5f,0.5f));
shaderProgram.setVec3("light.specular",glm::vec3(1.0f, 1.0f, 1.0f));
shaderProgram.setVec3("viewPos",cameraPos);
Shader lightShader = Shader("light.vs","light.fs");
lightShader.use();
//renderTriangle();
renderLightBox();
// 灯光uniform
glUniformMatrix4fv(glGetUniformLocation(lightShader.ID, "view"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(glGetUniformLocation(lightShader.ID, "projection"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
// 渲染循环
while (!glfwWindowShouldClose(window)) {
processInput(window);
glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 改变光照的位置
rotateLight();
/*
设置物体相关shader
*/
shaderProgram.use();
//changeLightColor(shaderProgram);
// 模型矩阵
glm::mat4 modelMatrix = glm::mat4(1.0f);
fov = (float)glfwGetTime() * 2;
modelMatrix = glm::translate(modelMatrix, glm::vec3(-1.0f,0.0f,1.0f));
modelMatrix = glm::rotate(modelMatrix, fov, glm::vec3(1.2f, 1.0f, 2.0f));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram.ID, "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniform3fv(glGetUniformLocation(shaderProgram.ID, "lightPos"), 1, glm::value_ptr(lightPos));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,texture0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,texture1);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D,texture2);
//shaderProgram.use();
//drawArrays();
drawBox(VAO);
/*
设置灯光相关shader
*/
lightShader.use();
glm::mat4 lightModelMatrix = glm::mat4(1.0f);
lightModelMatrix = glm::translate(lightModelMatrix, lightPos);
lightModelMatrix = glm::scale(lightModelMatrix, glm::vec3(0.5f));
glUniformMatrix4fv(glGetUniformLocation(lightShader.ID, "model"), 1, GL_FALSE, glm::value_ptr(lightModelMatrix));
drawBox(lightVAO);
//shaderProgram.setMa
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteBuffers(1,&VBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBindVertexArray(VAO);
glfwTerminate();
return 0;
}
//void setMatrix(glm::mat4 model,glm::mat4 view,glm::mat4 projection,Shader shaderProgram) {
//
//}
void changeLightColor(Shader shaderProgram) {
float time = (float)glfwGetTime();
// 灯光颜色
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
lightColor.x = sin(time * 2.0f);
lightColor.y = sin(time * 1.0f);
lightColor.z = sin(time * 2.8f);
glUniform3fv(glGetUniformLocation(shaderProgram.ID, "lightColor"), 1, glm::value_ptr(lightColor));
glm::vec3 lightDiff = lightColor * glm::vec3(0.5f);
glm::vec3 lightAmbient = lightDiff * glm::vec3(0.2f);
//vec3 lightAmbient = ;
shaderProgram.setVec3("light.ambient", lightAmbient);
shaderProgram.setVec3("light.diffuse", lightDiff);
}
// 加载漫反射贴图
void loadImage(string url,uint index,string type) {
if (index == 0) {
glActiveTexture(GL_TEXTURE0);
glGenTextures(1,&texture0);
glBindTexture(GL_TEXTURE_2D,texture0);
}
else if (index == 1) {
glActiveTexture(GL_TEXTURE1);
glGenTextures(1,&texture1);
glBindTexture(GL_TEXTURE_2D,texture1);
}
else if (index == 2) {
glActiveTexture(GL_TEXTURE2);
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_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, channel;
unsigned char *data = stbi_load(url.c_str(),&width,&height,&channel,0);
if (data) {
if (type == "png") {
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,data);
}
else {
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,data);
}
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
cout << "load image error" << endl;
}
// 释放纹理占用的内存
stbi_image_free(data);
}
void rotateLight() {
float radius = 5.0f;
float time = (float)glfwGetTime();
float temx = sin(time) * radius;
float temz = cos(time) * radius;
lightPos.x = temx;
lightPos.z = temz;
}
glm::mat4 createViewMatrix() {
glm::mat4 view = glm::lookAt(cameraPos,cameraFront,cameraUp);
return view;
}
void renderTriangle() {
/*glGenBuffers(1, &VBO);*/
// 讲顶点数据送往显卡
glGenVertexArrays(1, &VAO);
glGenBuffers(1,&VBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(firstVertices),firstVertices,GL_STATIC_DRAW);
// 配置属性指针
glBindVertexArray(VAO);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,8 * sizeof(float),(void *)(0 * sizeof(float)));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,8 * sizeof(float),(void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,8 * sizeof(float),(void *)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);
}
void renderBox() {
/*glGenBuffers(1, &VBO);*/
// 讲顶点数据送往显卡
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(lightTextVertices), lightTextVertices, GL_STATIC_DRAW);
// 配置属性指针
glBindVertexArray(VAO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(0 * sizeof(float)));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void renderLightBox() {
glGenVertexArrays(1,&lightVAO);
if (!VBO) {
glGenBuffers(1,&VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(lightTextVertices), lightTextVertices,GL_STATIC_DRAW);
}
glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(0 * sizeof(float)));
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER,0);
}
void drawBox(uint vao) {
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES,0,36);
}
void drawArrays() {
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,0,3);
}
GLFWwindow* initGlfwWindow() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT,"opengl game", NULL, NULL);
if (window == NULL) {
glfwTerminate();
return nullptr;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
cout << "failed to load glfw" << endl;
return nullptr;
}
return window;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
这里最后用到了放射光贴图:效果像这样: