生成纹理时加载alpha通道
注意透明纹理边缘采用的纹理,更过环绕方式
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
启用混合
glEnable(GL_BLEND);
设置源因子和目标因子
例如:使用源颜色向量的alpha作为源因子,使用1−alpha作为目标因子
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
注意深度测试可能抛弃在后面的物体,因此绘制透明物体混合时,要按最远到近排序,再按顺序渲染
流程:
1.绘制所有不透明物体
2.对所有透明物体排序
3.按顺序绘制透明物体
main.cpp
#define GLEW_STATIC
#include
#include
#include
#include "shader.h"
#include "stb_image.h"
#include
#include
#include
#include "Camera.h"
#include "Model.h"
#include
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)
};
GLfloat vertices[] = {
// positions // normals // texture coords
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.5f, 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
};
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
};
float grassVertices[] = {
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 1.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);
GLuint lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
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, format, 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);
}
GLuint grassVAO, grassVBO;
glGenVertexArrays(1, &grassVAO);
glGenBuffers(1, &grassVBO);
glBindVertexArray(grassVAO);
glBindBuffer(GL_ARRAY_BUFFER, grassVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(grassVertices), &grassVertices, 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 grassTex;
glGenTextures(1, &grassTex);
//image = stbi_load("grass.png", &width, &height, &nrComponents, 0);
image = stbi_load("blending_transparent_window.png", &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, grassTex);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
//如果是GL_REPEAT。纹理图像顶部会和底部边缘的纯色值进行插值,导致出现一个半透的有色边缘框
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
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;
Shader* frameShader = new Shader("test.vert", "frame.frag");
Shader* testShader = new Shader("test.vert", "test.frag");
Shader* lightShader = new Shader("test.vert", "light.frag");
//Model* testModel = new Model("ganyu/tex/甘雨.fbx");
vector<glm::vec3> vegetation;
vegetation.push_back(glm::vec3(-1.0f, 0.0f, -0.48f));
vegetation.push_back(glm::vec3(2.0f, 0.0f, 0.51f));
vegetation.push_back(glm::vec3(0.5f, 0.0f, 0.7f));
vegetation.push_back(glm::vec3(0.2f, 0.0f, -2.3f));
vegetation.push_back(glm::vec3(1.0f, 0.0f, -0.6f));
std::map<float, glm::vec3> sorted;
for (unsigned int i = 0; i < vegetation.size(); i++)
{
float distance = glm::length(myCamera->getCameraPos() - vegetation[i]);
sorted[distance] = vegetation[i];
}
while (!glfwWindowShouldClose(window))
{
processInput(window);
glfwPollEvents();
glEnable(GL_BLEND);//启用混合,渲染半透明图像
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/*
源颜色向量。这是源自纹理的颜色向量
目标颜色向量。这是当前储存在颜色缓冲中的颜色向量
源因子值。指定了alpha值对源颜色的影响
目标因子值。指定了alpha值对目标颜色的影响
可通过glBlendColor设置一个常数颜色向量
glBlendFunc:参数1:源因子,2.目标因子
GL_ZERO 因子等于0
GL_ONE 因子等于1
GL_SRC_COLOR 因子等于源颜色向量
GL_ONE_MINUS_SRC_COLOR 因子等于1?源色向量
GL_DST_COLOR 因子等于目标颜色向量
GL_ONE_MINUS_DST_COLOR 因子等于1-目标颜色向量
GL_SRC_ALPHA 因子等于源色向量的alpha分量
GL_ONE_MINUS_SRC_ALPHA 因子等于1-源色向量的alpha分量
GL_DST_ALPHA 因子等于目标向量的alpha分量
GL_ONE_MINUS_DST_ALPHA 因子等于1-目标向量的alpha分量
GL_CONSTANT_COLOR 因子等于常数颜色向量
GL_ONE_MINUS_CONSTANT_COLOR 因子等于1-常数颜色向量
GL_CONSTANT_ALPHA 因子等于常数颜色向量的alpha分量
GL_ONE_MINUS_CONSTANT_ALPHA 因子等于1-常数颜色向量的alpha分量
*/
//glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
//为RGB和alpha通道分别设置不同选项
//glBlendEquation(GLenum mode)设置源和目标的运算符。GL_FUNC_ADD:默认,相加。GL_FUNC_SUBTRACT:源向量减目标向量。 GL_FUNC_REVERSE_SUBTRACT:目标向量减源向量
glEnable(GL_DEPTH_TEST);
//深度测试函数
glDepthFunc(GL_LESS);
//glEnable(GL_STENCIL_TEST); //启用模板缓冲
//glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
参数:1.模板测试失败时的行为,2.模板测试通过,但深度测试失败时的行为,3.模板测试和深度测试都通过的行为
///*
//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 按位翻转当前的模板缓冲值
//*/
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
testShader->Use();
//glStencilMask(0x00); //绘制底板时不更新缓冲
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(glGetUniformLocation(testShader->getProgram(), "texture1"), 0);
glBindVertexArray(planeVAO);
glUniformMatrix4fv(glGetUniformLocation(testShader->getProgram(), "model"), 1, GL_FALSE, glm::value_ptr(glm::mat4(1.0f)));
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
//glStencilMask(0xFF); // 设置一个位编码,与将要写入缓冲的模板值进行与(AND)运算。默认情况下设置的位掩码所有位都为1,不影响输出
glStencilMask(0x00); 每一位在写入模板缓冲时都会变成0(禁用写入) 与深度测试中的glDepthMask(GL_FALSE)是等价的
//glStencilFunc(GL_ALWAYS, 1, 0xFF);
参数1:模板测试函数。该函数将应用到已储存的模板值和第二个参数上。:GL_NEVER、GL_LESS、GL_LEQUAL、GL_GREATER、GL_GEQUAL、GL_EQUAL、GL_NOTEQUAL和GL_ALWAYS。与深度缓冲类似
2:模板测试的参考值,模板缓存的内容会与该值比较,3.掩码,在参考值与模板值测试比较前与他们进行AND运算。
glBindVertexArray(cubeVAO);
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::mat4(1.0f);
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(grassVAO);
glBindTexture(GL_TEXTURE_2D, grassTex);
glUniform1i(glGetUniformLocation(testShader->getProgram(), "texture1"), 0);
/*for (unsigned int i = 0; i < vegetation.size(); i++)
{
model = glm::mat4(1.0f);
model = glm::translate(model, vegetation[i]);
glUniformMatrix4fv(glGetUniformLocation(testShader->getProgram(), "model"), 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 6);
}*/
for (std::map<float, glm::vec3>::reverse_iterator it = sorted.rbegin(); it != sorted.rend(); ++it)
{
model = glm::mat4();
model = glm::translate(model, it->second);
glUniformMatrix4fv(glGetUniformLocation(testShader->getProgram(), "model"), 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 6);
}
//glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
//glStencilMask(0x00); // 禁止模板缓冲的写入
//glDisable(GL_DEPTH_TEST);
//frameShader->Use();
//float scale = 1.1f;
//glBindVertexArray(cubeVAO);
//glActiveTexture(GL_TEXTURE0);
//glBindTexture(GL_TEXTURE_2D, tex);
//view = myCamera->getViewMat4();
//glUniformMatrix4fv(glGetUniformLocation(frameShader->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(frameShader->getProgram(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
//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));
//glUniformMatrix4fv(glGetUniformLocation(frameShader->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));
//model = glm::scale(model, glm::vec3(scale, scale, scale));
//glUniformMatrix4fv(glGetUniformLocation(frameShader->getProgram(), "model"), 1, GL_FALSE, glm::value_ptr(model));
//glDrawArrays(GL_TRIANGLES, 0, 36);
//glStencilMask(0xFF);
//glStencilFunc(GL_ALWAYS, 0, 0xFF);
//glEnable(GL_DEPTH_TEST);
//testModel->Draw(*testShader);
//glBindVertexArray(lightVAO);
//lightShader->Use();
//glUniformMatrix4fv(glGetUniformLocation(lightShader->getProgram(), "view"), 1, GL_FALSE, glm::value_ptr(view));
//glUniformMatrix4fv(glGetUniformLocation(lightShader->getProgram(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
//for (GLuint i = 0; i < 2; i++)
//{
// model = glm::mat4();
// model = glm::translate(model, pointLightPositions[i]);
// model = glm::scale(model, glm::vec3(0.2f));
// glUniformMatrix4fv(glGetUniformLocation(lightShader->getProgram(), "model"), 1, GL_FALSE, glm::value_ptr(model));
// // 绘制灯立方体对象
// glDrawArrays(GL_TRIANGLES, 0, 36);
//}
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}