中小型网站建设教程学ps可以做网站策划吗

当前位置: 首页 > news >正文

中小型网站建设教程,学ps可以做网站策划吗,wordpress怎么设置跳站外链接,建设银行唐山分行网站OpenGL模板测试模板函数物体轮廓模板测试 当片段着色器处理完一个片段后#xff0c;模板测试就会开始执行。类似于深度测试#xff0c;模板测试也可能会丢弃片段。被保留的片段会进入深度测试#xff0c;可能会丢弃更多的片段。 模板测试是根据模板缓冲来进行的。一个模板缓… OpenGL模板测试模板函数物体轮廓模板测试 当片段着色器处理完一个片段后模板测试就会开始执行。类似于深度测试模板测试也可能会丢弃片段。被保留的片段会进入深度测试可能会丢弃更多的片段。 模板测试是根据模板缓冲来进行的。一个模板缓冲中通常每个模板值是8位。所以每个像素/片段一共能有256种不同的模板值。可以将模板值设置为想要的值当某一个片段有某一个模板值的时候就可以选择丢弃或者保留这个片段。 注意每个窗口库都需要配置一个模板缓冲。GLFW自动做了所以不需要告诉GLFW创建一个模板缓冲但是其他的窗口库不会默认创建一个模板库需要查看库文档。 举例如下 模板缓冲首先会被清除为0之后在模板缓冲中使用1填充了一个空心矩形。场景中的片段将会只在片段的模板值为1的时候会被渲染其它的都被丢弃了。 模板缓冲操作允许我们在渲染片段时将模板缓冲设定为一个特定的值。通过在渲染时修改模板缓冲的内容我们写入了模板缓冲。在同一个或者接下来的渲染迭代中我们可以读取这些值来决定丢弃还是保留某个片段。使用模板缓冲的时候你可以尽情发挥但大体的步骤如下 1.启用模板缓冲的写入。 2.渲染物体更新模板缓冲的内容。 3.禁用模板缓冲的写入。 4.渲染其它物体这次根据模板缓冲的内容丢弃特定的片段。 所以通过使用模板缓冲我们可以根据场景中已绘制的其它物体的片段来决定是否丢弃特定的片段。 你可以启用GL_STENCIL_TEST来启用模板测试。在这一行代码之后所有的渲染调用都会以某种方式影响着模板缓冲 glEnable(GL_STENCIL_TEST);和颜色和深度缓冲一样你也需要在每次迭代之前清除模板缓冲。 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);类似深度测试的glDepthMask函数一样模板缓冲也有一个类似的函数。glStencilMask允许我们设置一个位掩码(Bitmask)它会与将要写入缓冲的模板值进行与(AND)运算。默认情况下设置的位掩码所有位都为1不影响输出但如果我们将它设置为0x00写入缓冲的所有模板值最后都会变成0.这与深度测试中的glDepthMask(GL_FALSE)是等价的。 glStencilMask(0xFF); // 每一位写入模板缓冲时都保持原样 glStencilMask(0x00); // 每一位在写入模板缓冲时都会变成0禁用写入模板函数 和深度测试类似对模板缓冲应该 通过还是失败以及应该如何影响模板缓冲也是有一定控制的。一个有两个函数能够用来配置模板测试 glStencilFunc和glStencilOp glStencilFunc(GLenum func, GLint ref, GLuint mask)一共包含三个参数 func设置模板测试函数(Stencil Test Function)。这个测试函数将会应用到已储存的模板值上和glStencilFunc函数的ref值上。可用的选项有GL_NEVER、GL_LESS、GL_LEQUAL、GL_GREATER、GL_GEQUAL、GL_EQUAL、GL_NOTEQUAL和GL_ALWAYS。它们的语义和深度缓冲的函数类似。 ref设置了模板测试的参考值(Reference Value)。模板缓冲的内容将会与这个值进行比较。 mask设置一个掩码它将会与参考值ref和储存的模板值在测试比较它们之前进行与(AND)运算。初始情况下所有位都为1。 举例如下 glStencilFunc(GL_EQUAL, 1, 0xFF)解释只要一个片段的模板值等于(GL_EQUAL)参考值1片段将会通过测试并被绘制否则会被丢弃。 但是glStencilFunc仅仅描述了OpenGL应该对模板缓冲内容做什么而不是我们应该如何更新缓冲。这就需要glStencilOp这个函数了。 glStencilOp(GLenum sfail, GLenum dpfail, GLenum dppass)一共包含三个选项我们能够设定每个选项应该采取的行为 sfail模板测试失败时采取的行为。 dpfail模板测试通过但深度测试失败时采取的行为。 dppass模板测试和深度测试都通过时采取的行为。 每个选项都可以选用以下的其中一种行为 GL_KEEP 保持当前储存的模板值 GL_ZERO 将模板值设置为0 GL_REPLACE 将模板值设置为glStencilFunc函数设置的ref值 GL_INCR 如果模板值小于最大值则将模板值加1 GL_INCR_WRAP 与GL_INCR一样但如果模板值超过了最大值则归零 GL_DECR 如果模板值大于最小值则将模板值减1 GL_DECR_WRAP 与GL_DECR一样但如果模板值小于0则将其设置为最大值 GL_INVERT 按位翻转当前的模板缓冲值 默认情况下glStencilOp是设置为(GL_KEEP, GL_KEEP, GL_KEEP) 的所以不论任何测试的结果是如何模板缓冲都会保留它的值。默认的行为不会更新模板缓冲所以如果你想写入模板缓冲的话你需要至少对其中一个选项设置不同的值。 综上通过使用glStencilFunc和glStencilOp我们可以精确地指定更新模板缓冲的时机与行为了我们也可以指定什么时候该让模板缓冲通过即什么时候片段需要被丢弃。 物体轮廓 使用模板测试可以完成的有用特性叫做物体轮廓 会为每个或者一个物体在它的周围创建一个很小的有色边框。当你想要在策略游戏中选中一个单位进行操作的想要告诉玩家选中的是哪个单位的时候这个效果就非常有用了。为物体创建轮廓的步骤如下 在绘制需要添加轮廓的物体之前将模板函数设置GL_ALWAYS每当物体的片段被渲染时将模板缓冲更新为1。渲染物体。禁用模板写入以及深度测试。将每个物体缩放一点点。使用一个不同的片段着色器输出一个单独的边框颜色。再次绘制物体但只在它们片段的模板值不等于1时才绘制。再次启用模板写入和深度测试。 创建一个很简单的片段着色器它会输出一个边框颜色。我们简单地给它设置一个硬编码的颜色值将这个着色器命名为shaderSingleColor void main() {FragColor vec4(0.04, 0.28, 0.26, 1.0); }首先绘制地板再绘制两个箱子并写入模板缓冲之后绘制放大的箱子并丢弃覆盖了之前绘制的箱子片段的那些片段。 先启用模板测试并设置测试通过或失败的行为 glEnable(GL_STENCIL_TEST); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);如果其中的一个测试失败了我们什么都不做我们仅仅保留当前储存在模板缓冲中的值。如果模板测试和深度测试都通过了那么我们希望将储存的模板值设置为参考值参考值能够通过glStencilFunc来设置我们之后会设置为1。 我们将模板缓冲清除为0对箱子中所有绘制的片段将模板值更新为1 glStencilFunc(GL_ALWAYS, 1, 0xFF); // 所有的片段都应该更新模板缓冲 glStencilMask(0xFF); // 启用模板缓冲写入 normalShader.use(); DrawTwoContainers();通过使用GL_ALWAYS模板测试函数我们保证了箱子的每个片段都会将模板缓冲的模板值更新为1。因为片段永远会通过模板测试在绘制片段的地方模板缓冲会被更新为参考值。 现在模板缓冲在箱子被绘制的地方都更新为1了我们将要绘制放大的箱子但这次要禁用模板缓冲的写入 glStencilFunc(GL_NOTEQUAL, 1, 0xFF); glStencilMask(0x00); // 禁止模板缓冲的写入 glDisable(GL_DEPTH_TEST); shaderSingleColor.use(); DrawTwoScaledUpContainers();我们将模板函数设置为GL_NOTEQUAL它会保证我们只绘制箱子上模板值不为1的部分即只绘制箱子在之前绘制的箱子之外的部分。注意我们也禁用了深度测试让放大的箱子即边框不会被地板所覆盖。 在完成之后重新启用深度缓冲。 完整步骤如下 glEnable(GL_DEPTH_TEST); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glStencilMask(0x00); // 记得保证我们在绘制地板的时候不会更新模板缓冲 normalShader.use(); DrawFloor() glStencilFunc(GL_ALWAYS, 1, 0xFF); glStencilMask(0xFF); DrawTwoContainers();glStencilFunc(GL_NOTEQUAL, 1, 0xFF); glStencilMask(0x00); glDisable(GL_DEPTH_TEST); shaderSingleColor.use(); DrawTwoScaledUpContainers(); glStencilMask(0xFF); glEnable(GL_DEPTH_TEST); 综上代码如下放大立方体画边框地方需要仔细学习 #include glad/glad.h #include GLFW/glfw3.h #include stb_image.h#include glm/glm.hpp #include glm/gtc/matrix_transform.hpp #include glm/gtc/type_ptr.hpp#include learnopengl/shader_m.h #include learnopengl/camera.h #include learnopengl/model.h#include iostreamvoid framebuffer_size_callback(GLFWwindow* window, int width, int height); void mouse_callback(GLFWwindow* window, double xpos, double ypos); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); unsigned int loadTexture(const char path);// settings const unsigned int SCR_WIDTH 800; const unsigned int SCR_HEIGHT 600;// camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); float lastX (float)SCR_WIDTH / 2.0; float lastY (float)SCR_HEIGHT / 2.0; bool firstMouse true;// 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);#ifdef APPLEglfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); #endif// glfw window creation// ——————–GLFWwindow window glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, LearnOpenGL, 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);// tell GLFW to capture our mouseglfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);// glad: load all OpenGL function pointers// —————————————if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){std::cout Failed to initialize GLAD std::endl;return -1;}// configure global opengl state// —————————–glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LESS);glEnable(GL_STENCIL_TEST);glStencilFunc(GL_NOTEQUAL, 1, 0xFF);glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);// build and compile shaders// ————————-Shader shader(2.stencil_testing.vs, 2.stencil_testing.fs);Shader shaderSingleColor(2.stencil_testing.vs, 2.stencil_single_color.fs);// set up vertex data (and buffer(s)) and configure vertex attributes// ——————————————————————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};// cube VAOunsigned int cubeVAO, cubeVBO;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);// plane VAOunsigned 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);// load textures// ————-unsigned int cubeTexture loadTexture(FileSystem::getPath(resources/textures/marble.jpg).c_str());unsigned int floorTexture loadTexture(FileSystem::getPath(resources/textures/metal.png).c_str());// shader configuration// ——————–shader.use();shader.setInt(texture1, 0);// render loop// ———–while (!glfwWindowShouldClose(window)){// per-frame time logic// ——————–float currentFrame static_castfloat(glfwGetTime());deltaTime currentFrame - lastFrame;lastFrame currentFrame;// input// —–processInput(window);// render// ——glClearColor(0.1f, 0.1f, 0.1f, 1.0f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // dont forget to clear the stencil buffer!// set uniformsshaderSingleColor.use();glm::mat4 model glm::mat4(1.0f);glm::mat4 view camera.GetViewMatrix();glm::mat4 projection glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);shaderSingleColor.setMat4(view, view);shaderSingleColor.setMat4(projection, projection);shader.use();shader.setMat4(view, view);shader.setMat4(projection, projection);// draw floor as normal, but dont write the floor to the stencil buffer, we only care about the containers. We set its mask to 0x00 to not write to the stencil buffer.glStencilMask(0x00);// floorglBindVertexArray(planeVAO);glBindTexture(GL_TEXTURE_2D, floorTexture);shader.setMat4(model, glm::mat4(1.0f));glDrawArrays(GL_TRIANGLES, 0, 6);glBindVertexArray(0);// 1st. render pass, draw objects as normal, writing to the stencil buffer// ——————————————————————–glStencilFunc(GL_ALWAYS, 1, 0xFF);glStencilMask(0xFF);// cubesglBindVertexArray(cubeVAO);glActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE_2D, cubeTexture);model glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));shader.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));shader.setMat4(model, model);glDrawArrays(GL_TRIANGLES, 0, 36);// 2nd. render pass: now draw slightly scaled versions of the objects, this time disabling stencil writing.// Because the stencil buffer is now filled with several 1s. The parts of the buffer that are 1 are not drawn, thus only drawing // the objects size differences, making it look like borders.// —————————————————————————————————————————–glStencilFunc(GL_NOTEQUAL, 1, 0xFF);glStencilMask(0x00);glDisable(GL_DEPTH_TEST);shaderSingleColor.use();float scale 1.1f;// cubesglBindVertexArray(cubeVAO);glBindTexture(GL_TEXTURE_2D, cubeTexture);model glm::mat4(1.0f);model glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));model glm::scale(model, glm::vec3(scale, scale, scale));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(scale, scale, scale));shaderSingleColor.setMat4(model, model);glDrawArrays(GL_TRIANGLES, 0, 36);glBindVertexArray(0);glStencilMask(0xFF);glStencilFunc(GL_ALWAYS, 0, 0xFF);glEnable(GL_DEPTH_TEST);// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)// ——————————————————————————-glfwSwapBuffers(window);glfwPollEvents();}// optional: de-allocate all resources once theyve outlived their purpose:// ————————————————————————glDeleteVertexArrays(1, cubeVAO);glDeleteVertexArrays(1, planeVAO);glDeleteBuffers(1, cubeVBO);glDeleteBuffers(1, planeVBO);glfwTerminate();return 0; }// 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);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); }// 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); }// glfw: whenever the mouse moves, this callback is called // ——————————————————- void mouse_callback(GLFWwindow* window, double xposIn, double yposIn) {float xpos static_castfloat(xposIn);float ypos static_castfloat(yposIn);if (firstMouse){lastX xpos;lastY ypos;firstMouse false;}float xoffset xpos - lastX;float yoffset lastY - ypos; // reversed since y-coordinates go from bottom to toplastX xpos;lastY ypos;camera.ProcessMouseMovement(xoffset, yoffset); }// glfw: whenever the mouse scroll wheel scrolls, this callback is called // ———————————————————————- void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {camera.ProcessMouseScroll(static_castfloat(yoffset)); }// utility function for loading a 2D texture from file // ————————————————— unsigned int loadTexture(char const * path) {unsigned int textureID;glGenTextures(1, textureID);int width, height, nrComponents;unsigned char *data stbi_load(path, width, height, nrComponents, 0);if (data){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, textureID);glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);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(data);}else{std::cout Texture failed to load at path: path std::endl;stbi_image_free(data);}return textureID; }