和深度測試一樣,對模板緩衝應該通過還是失敗,以及它應該如何影響模板緩衝,有一定控制。一共有兩個函數能夠用來配置模板測試: glStencilFunc和glStencilOp
glStencilFunc(GLenum func, GLint ref,Gluint mask)一共包含三個參數:
func:設置模板測試函數。用於比較已儲存的模板值上和glStencilFunc函數的ref值上。可用的選項有:GL_NEVER,GL_LESS,GL_LEQUAL,GL_GREATER,GL_GEQUAL,GL_EQUAL,GL_NOTEQUAL和GL_ALWAYS。
ref:設置了模板測試的參考值。模板緩衝的內容將會與這個值進行比較。
mask:設置一個掩碼,它將會與參考值和儲存的模板值在測試比較它們之前進行與(AND)運算。初始情況下所有位都爲1.
指示如何更新緩衝,需要glStencilOp函數:
glStencilOp(GLenum sfail,GLenum dpfail,GLenum dppass)一共包含三個選項,
sfail:模板測試失敗時採取的行爲
dpfail:模板測試通過,但深度測試失敗時採取的行爲。
dppass:模板測試和深度測試都通過時採取的行爲。
模板測試小節代碼實現:
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <fstream>
#include <sstream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//#include <boost/filesystem.cpp>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include "Shader.h"
#include "Camera.h"
#define _USE_MATH_DEFINES
#include<math.h>
void framebuffer_size_callback(GLFWwindow* windows, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void processInput(GLFWwindow *window);
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
unsigned int loadTexture(const char *path);
void DrawTwoContainers(Shader &normalShader);
void DrawTwoScaledUpContainers(Shader &shaderSingleColor);
//settings
const unsigned int SCR_WIDTH = 1280;
const unsigned int SCR_HEIGHT = 720;
#define ambient_light 0.1 //定義四個點光源的環境光影響
#define specular_light 0.05 // 定義四個點光源鏡面反射的影響
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = (float)SCR_HEIGHT/ 2.0f;
float lastY = (float)SCR_WIDTH / 2.0f;
bool firstMouse = true;
unsigned int cubeVAO, cubeVBO;
unsigned int cubeTexture;
unsigned int floorTexture;
//timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;
int main()
{
//glfw:initialize and configure
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "model_loading", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glEnable(GL_STENCIL_TEST);
glDepthFunc(GL_LESS);
//Model ourModel("D:/opengltask/LearnOpenGL-master/resources/objects/nanosuit/nanosuit.obj");
float cubeVertices[] = {
// positions // texture Coords
-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, 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
};
float planeVertices[] = {
// positions // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat)
5.0f, -0.5f, 5.0f, 2.0f, 0.0f,
-5.0f, -0.5f, 5.0f, 0.0f, 0.0f,
-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,
5.0f, -0.5f, 5.0f, 2.0f, 0.0f,
-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,
5.0f, -0.5f, -5.0f, 2.0f, 2.0f
};
//對於立方體的VAO
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &cubeVBO);
glBindVertexArray(cubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glBindVertexArray(0);
//對於平面的VAO
unsigned int planeVAO, planeVBO;
glGenVertexArrays(1, &planeVAO);
glGenBuffers(1, &planeVBO);
glBindVertexArray(planeVAO);
glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), &planeVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glBindVertexArray(0);
cubeTexture= loadTexture("D:/opengltask/LearnOpenGL-master/resources/textures/marble.jpg");
floorTexture = loadTexture("D:/opengltask/LearnOpenGL-master/resources/textures/metal.png");
Shader normalShader=Shader("1.1.depth_testing.vs", "1.1.depth_testing.fs");
Shader shaderSingleColor=Shader("1.1.depth_testing.vs", "shaderSingleColor.fs");
//加載紋理
normalShader.use();
//normalShader.setInt("texture1", 0);
while (!glfwWindowShouldClose(window))
{
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
//處理鍵盤輸入
processInput(window);
//渲染
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); //將模板緩衝清楚爲0
normalShader.use();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = camera.GetViewMatrix();
normalShader.setMat4("projection", projection);
normalShader.setMat4("view", view);
shaderSingleColor.use();
shaderSingleColor.setMat4("projection", projection);
shaderSingleColor.setMat4("view", view);
normalShader.use();
//畫地板
glStencilMask(0x00); // 保證在繪製地板的時候不會更新模板緩衝
glBindVertexArray(planeVAO);
glBindTexture(GL_TEXTURE_2D, floorTexture);
normalShader.setMat4("model", glm::mat4(1.0f));
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
glEnable(GL_DEPTH_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glStencilFunc(GL_ALWAYS, 1, 0xFF); //用於已儲存的模板值和ref之間進行比較,所有的片段都應該更新模板緩衝
glStencilMask(0xFF);//啓用模板緩衝寫入
normalShader.use();
DrawTwoContainers(normalShader);
glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
glStencilMask(0x00); //禁止模板緩衝的寫入
glDisable(GL_DEPTH_TEST);
shaderSingleColor.use();
DrawTwoScaledUpContainers(shaderSingleColor);
glStencilMask(0xFF);
glEnable(GL_DEPTH_TEST);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &cubeVAO);
glDeleteVertexArrays(1, &planeVAO);
glDeleteBuffers(1, &cubeVBO);
glDeleteBuffers(1, &planeVBO);
glfwTerminate();
return 0;
}
void DrawTwoContainers(Shader &normalShader)
{
//畫第一個立方體
glBindVertexArray(cubeVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, cubeTexture);
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
normalShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
//畫第二個立方體
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
normalShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
void DrawTwoScaledUpContainers(Shader &shaderSingleColor)
{
//畫第一個立方體
glBindVertexArray(cubeVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, cubeTexture);
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
model = glm::scale(model, glm::vec3(1.1f, 1.1f, 1.1f));
shaderSingleColor.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
//畫第二個立方體
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
model = glm::scale(model, glm::vec3(1.1f, 1.1f, 1.1f));
shaderSingleColor.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) //視口根據窗口大小調整的函數,兩個整數表示窗口的新維度
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window) //輸入的控制
{
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
/*if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS)
model = glm::rotate(model, (float)(M_PI / 360.0), glm::vec3(0.0f, 1.0f, 0.0f));
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
model = glm::rotate(model, (float)(-M_PI / 360.0), glm::vec3(0.0f, 1.0f, 0.0f));*/
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) // glfwGetKey函數需要一個窗口以及一個按鍵作爲輸入,函數將會返回這個按鍵是否正在被按下
glfwSetWindowShouldClose(window, true);
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos) //鼠標回調函數
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos; //相反的,因爲y座標是從底部往頂部依次增大的
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) //鼠標滾輪的回調函數
{
camera.ProcessMouseScroll(yoffset);
}
unsigned int loadTexture(char const *path)
{
unsigned int texture_id;
glGenTextures(1, &texture_id);
int width, height, nrChannels;
unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0);
if (data)
{
GLenum format; //unsigned int 類型的別稱
if (nrChannels == 1)
format = GL_RED;
else if (nrChannels == 3)
format = GL_RGB;
else if (nrChannels == 4)
format = GL_RGBA;
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data);
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_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
}
else
{
std::cout << "Failed to load texture at path:" <<path<< std::endl;
}
return texture_id;
}