learnOpenGL-深度测试

news/2024/11/24 8:04:43/

深度测试:OpenGL将一个片段的深度值与深度缓冲的内容进行对比。执行一个深度测试,测试通过则深度缓冲将会更新为新的深度值。测试失败则片段被丢弃。
深度测试片段着色器及模版测试之后执行。
片段着色器中内置变量gl_FragCoord的z值即为深度值。

提前深度测试:允许深度测试在片段着色器之前运行。片段着色器通常开销很大。使用提前深度测试时,片段着色器不可写入深度值,因为OpenGl不能提前知道深度值。

默认禁用,glEnable(GL_DEPTH_TEST);启用

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);清楚上一帧深度缓冲

实际运用的深度缓冲是非线性的

深度冲突:两个平面或三角形z值一样,两个形状会不断切换前后顺序

防止深度冲突:
1.不要把多个物体放的太近,导致它们的一些三角形重叠
2.将进平面设置远一些,因为精度在靠近进平面时较高。(注意,进平面太远会导致近处的一些物体被裁减掉)
3.牺牲一些性能,使用更高精度的深度缓冲。大部分深度缓冲的精度都是24位,但现在大部分显卡支持32位

main.cpp

#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "shader.h"
#include "stb_image.h"
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>
#include "Camera.h"
#include "Model.h"Camera* myCamera = new Camera(glm::vec3(0.0f, 0.0f, 3.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f));void mouse_callback(GLFWwindow* window, double xpos, double ypos) {myCamera->mouseCb(xpos, ypos);
};void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{myCamera->scrollCb(xoffset, yoffset);
}GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;void processInput(GLFWwindow* window) {GLfloat currentFrame = glfwGetTime();deltaTime = currentFrame - lastFrame;lastFrame = currentFrame;if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)glfwSetWindowShouldClose(window, true);if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {myCamera->pressKeyW(deltaTime);}if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {myCamera->pressKeyS(deltaTime);}if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {myCamera->pressKeyA(deltaTime);}if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {myCamera->pressKeyD(deltaTime);}
}glm::vec3 pointLightPositions[] = {glm::vec3(2.0f,  10.2f,  2.0f),glm::vec3(-2.3f, 7.3f, -4.0f)
};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
};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(800, 600, "test", nullptr, nullptr);if (window == nullptr){std::cout << "Failed to create GLFW window" << std::endl;glfwTerminate();return -1;}glfwMakeContextCurrent(window);//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);glfwSetCursorPosCallback(window, mouse_callback);glfwSetScrollCallback(window, scroll_callback);glewExperimental = GL_TRUE;if (glewInit() != GLEW_OK){std::cout << "Failed to initialize GLEW" << std::endl;glfwTerminate();return -1;}glViewport(0, 0, 800, 600);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);glEnableVertexAttribArray(0);glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));glEnableVertexAttribArray(1);glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));glEnableVertexAttribArray(2);glEnableVertexAttribArray(0);glBindVertexArray(0);GLuint cubeVAO, cubeVBO;glGenVertexArrays(1, &cubeVAO);glBindVertexArray(cubeVAO);glGenBuffers(1, &cubeVBO);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);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);GLuint tex;glGenTextures(1, &tex);int width, height, nrComponents;unsigned char* image = stbi_load("container.jpg", &width, &height, &nrComponents, 0);if (image) {GLenum format;if (nrComponents == 1)format = GL_RED;else if (nrComponents == 3)format = GL_RGB;else if (nrComponents == 4)format = GL_RGBA;glBindTexture(GL_TEXTURE_2D, tex);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, image);glGenerateMipmap(GL_TEXTURE_2D);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);stbi_image_free(image);}glm::mat4 model;glm::mat4 view;glm::mat4 projection;glEnable(GL_DEPTH_TEST);//深度测试函数glDepthFunc(GL_LESS);/** GL_ALWAYS	永远通过深度测试* GL_NEVER	永远不通过深度测试* GL_LESS	在片段深度值小于缓冲的深度值时通过测试* GL_EQUAL	在片段深度值等于缓冲区的深度值时通过测试* GL_LEQUAL	在片段深度值小于等于缓冲区的深度值时通过测试* GL_GREATER	在片段深度值大于缓冲区的深度值时通过测试* GL_NOTEQUAL	在片段深度值不等于缓冲区的深度值时通过测试* GL_GEQUAL	在片段深度值大于等于缓冲区的深度值时通过测试*/Shader* testShader = new Shader("test.vert", "test.frag");Shader* lightShader = new Shader("test.vert", "light.frag");while (!glfwWindowShouldClose(window)){processInput(window);glfwPollEvents();glClearColor(0.1f, 0.1f, 0.1f, 1.0f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);testShader->Use();glUniform1i(glGetUniformLocation(testShader->getProgram(), "texture1"), 0);glBindVertexArray(cubeVAO);glActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE_2D, tex);view = myCamera->getViewMat4();glUniformMatrix4fv(glGetUniformLocation(testShader->getProgram(), "view"), 1, GL_FALSE, glm::value_ptr(view));projection = glm::perspective(glm::radians(myCamera->getFov()), 800.0f / 600.0f, 0.1f, 100.0f);glUniformMatrix4fv(glGetUniformLocation(testShader->getProgram(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));glUniformMatrix4fv(glGetUniformLocation(testShader->getProgram(), "model"), 1, GL_FALSE, glm::value_ptr(model));glDrawArrays(GL_TRIANGLES, 0, 36);model = glm::mat4(1.0f);model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));glUniformMatrix4fv(glGetUniformLocation(testShader->getProgram(), "model"), 1, GL_FALSE, glm::value_ptr(model));glDrawArrays(GL_TRIANGLES, 0, 36);glBindVertexArray(planeVAO);glUniformMatrix4fv(glGetUniformLocation(testShader->getProgram(), "model"), 1, GL_FALSE, glm::value_ptr(glm::mat4(1.0f)));glDrawArrays(GL_TRIANGLES, 0, 6);glBindVertexArray(0);glfwSwapBuffers(window);}glfwTerminate();return 0;
}

顶点着色器

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords;out vec2 TexCoords;uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;void main()
{TexCoords = aTexCoords;    gl_Position = projection * view * model * vec4(aPos, 1.0);
}

片段着色器

#version 330 core
out vec4 FragColor;in vec2 TexCoords;uniform sampler2D texture1;float near = 0.1; 
float far  = 100.0; float LinearizeDepth(float depth) 
{float z = depth * 2.0 - 1.0; // back to NDC [0,1]变换到[-1,1](标准化设备坐标/裁剪空间)return (2.0 * near * far) / (far + near - z * (far - near));    
}void main()
{    FragColor = texture(texture1, TexCoords);//FragColor = vec4(vec3(gl_FragCoord.z), 1.0);//float depth = LinearizeDepth(gl_FragCoord.z) / far; // 为了演示除以 far//FragColor = vec4(vec3(depth), 1.0);
}

http://www.ppmy.cn/news/99455.html

相关文章

Zephyr sem

文章目录 简介互斥同步 数据结构信号量初始化Z_SEM_INITIALIZERint k_sem_init (struct k_sem *sem, unsigned int initial_count, unsigned int limit) 获取信号量int k_sem_take(struct k_sem *sem, k_timeout_t timeout) 释放信号量void k_sem_give(struct k_sem *sem) 获取…

Java学习笔记20——常用API

常用API 常用APIMath类Math的常用方法 System类System类常用方法 Object类Object类常用方法 Arrays类Arrays常用方法 基本类型包装类Integer类的概述和使用int和String的相互转换自动装箱和拆箱 日期类Date类Date类的常用方法 SimpleDateFormat类SimpleDateFormat的构造方法Sim…

Java输入输出流

目录 一、数据流概念 1.输入输出的概念​ 2.流的概念 3.流的操作 二、常用的流分类 三、文件输入输出流 1.FileReader和FileWriter 2.FileInputStream和FileOutStream 四、复制文件 一、数据流概念 1.输入输出的概念​ 输入输出技术用于处理设备之间的数据传输&#x…

【数据库复习】第六章 关系数据理论 1

关系模式的设计 按照一定的原则从数量众多而又相互关联的数据中&#xff0c;构造出一组既能较好地反映现实世界&#xff0c;而又有良好的操作性能的关系模式 ●冗余度高 ●修改困难 ●插入问题 ●删除问题 ★产生问题的原因 属性间约束关系&#xff08;即数据间的依赖关系&…

dpdk ip分片报文重组处理

dpdk ip报文重组及分片API及处理逻辑介绍 DPDK的分片和重组实现零拷贝&#xff0c;详细介绍可以参阅DPDK分片与重组用户手则 相关数据结构 /** Fragmented packet to reassemble.* First two entries in the frags[] array are for the last and first fragments.*/ struct …

Doris----Rollup表分析及案例实现

ROLLUP 在多维分析中是“上卷”的意思&#xff0c;即将数据按某种指定的粒度进行进一步聚合。 之前的聚合模型: 用户id数据插入时间城市年龄性别最后一次访问的时间该用户的总消费额该用户的最大停留时长该用户的最小停留时长100002017/10/2北京1002017/10/02 08:00:00651521…

GPT-4发布!ChatGPT大升级!太太太太强了!

ChatGPT狂飙160天&#xff0c;世界已经不是之前的样子。 我新建了人工智能中文站https://ai.weoknow.com 每天给大家更新可用的国内可用chatGPT资源 一觉醒来&#xff0c;万众期待的GPT-4&#xff0c;它来了&#xff01; OpenAI老板Sam Altman直接开门见山地介绍说&#xff1a…

问chartgpt:python的编程规则?

一.问题描述 python的编程规则&#xff1f; 二.ChatGPT的解决方法【实测可行】 &#xff08;一&#xff09;Python编程有一些基本规则需要遵守&#xff0c;这些规则包括&#xff1a; 缩进&#xff1a;Python使用缩进来表示代码块&#xff0c;因此请确保使用一致的缩进方式&a…