LeeMcQueen / GameDemo

Demo
0 stars 0 forks source link

water FBOs #28

Closed LeeMcQueen closed 3 years ago

LeeMcQueen commented 3 years ago

unsigned int WaterFrameBuffers::createTextureAttachment(int width, int height){

unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0);

return texture;

}

LeeMcQueen commented 3 years ago

include "WaterFrameBuffers.h"

WaterFrameBuffers::WaterFrameBuffers(){

//加载反射buffer
initReflectionBuffer();
//加载折射buffer
initRefractionBuffer();

}

//WaterFrameBuffers::~WaterFrameBuffers(){}

void WaterFrameBuffers::initReflectionBuffer(){

//生成帧缓冲
reflectionFrameBuffer_ = createFrameBuffer();
//反射帧缓冲图像纹理
reflectionTexture_ = createTextureAttachment(REFLECTION_WIDTH, REFLECTION_HEIGHT);
//反射帧缓冲深度
reflectionDepthBuffer_ = createDepthBufferAttachment(REFLECTION_WIDTH, REFLECTION_HEIGHT);
unbindCurrentFrameBuffer();

}

void WaterFrameBuffers::initRefractionBuffer(){

//生成帧缓冲
refractionFrameBuffer_ = createFrameBuffer();
//折射帧缓冲图像纹理
refractionTexture_ = createTextureAttachment(REFRACTION_WIDTH, REFRACTION_HEIGHT);
//折射帧缓存深度纹理
refractionDepthTexture_ = createDepthTextureAttachment(REFRACTION_WIDTH, REFRACTION_HEIGHT);
unbindCurrentFrameBuffer();

}

//生成帧缓冲 unsigned int WaterFrameBuffers::createFrameBuffer(){

//生成帧缓冲对象
unsigned int frameBuffer;
glGenFramebuffers(1, &frameBuffer);
//绑定帧缓冲
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
//颜色缓冲设定
glDrawBuffer(GL_COLOR_ATTACHMENT0);
return frameBuffer;

}

//帧缓冲图像纹理 unsigned int WaterFrameBuffers::createTextureAttachment(int width, int height){

//生成帧缓冲的图像纹理
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//帧缓冲的图像纹理
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0);

return texture;

}

//帧缓冲深度纹理 unsigned int WaterFrameBuffers::createDepthTextureAttachment(int width, int height){

//生成帧缓冲的纹理
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//帧缓冲的深度纹理
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, texture, 0);

return texture;

}

unsigned int WaterFrameBuffers::createDepthBufferAttachment(int width, int height){

unsigned int depthBuffer;
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
//将渲染缓冲区挂载到当前帧缓冲区上
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
return depthBuffer;

}

void WaterFrameBuffers::bindFrameBuffer(unsigned int framebuffer, int width, int height){

glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glViewport(0, 0 , width, height);

}

void WaterFrameBuffers::unbindCurrentFrameBuffer() {

glBindTexture(GL_TEXTURE_2D, 0);
glViewport(0, 0, 1280, 720);

}

void WaterFrameBuffers::bindReflectionFrameBuffer() {

bindFrameBuffer(reflectionFrameBuffer_, REFLECTION_WIDTH, REFLECTION_HEIGHT);

}

void WaterFrameBuffers::bindRefractionFrameBuffer() {

bindFrameBuffer(refractionFrameBuffer_, REFRACTION_WIDTH, REFRACTION_HEIGHT);

}

void WaterFrameBuffers::cleanUp(){

glDeleteFramebuffers(1, &reflectionFrameBuffer_);
glDeleteTextures(1, &reflectionTexture_);
glDeleteRenderbuffers(1, &reflectionDepthBuffer_);
glDeleteFramebuffers(1, &refractionFrameBuffer_);
glDeleteTextures(1, &refractionTexture_);
glDeleteTextures(1, &refractionDepthTexture_);

}

LeeMcQueen commented 3 years ago

void WaterFrameBuffers::unbindCurrentFrameBuffer() {

glBindTexture(GL_FRAMEBUFFER, 0);
glViewport(0, 0, 1280, 720);

}

LeeMcQueen commented 3 years ago

reflection camera function

camera.h void setViewDirection(glm::vec3 viewDirection){ viewDirection_ = viewDirection; }

LeeMcQueen commented 3 years ago

第一个就是把camera的位置进行调整,看看y轴正整数的是时候会不会出现错误

skybox的top出现了花的问题,如何解决

LeeMcQueen commented 3 years ago

loader里面的loadCubeMap函数 设置breakpoint 看看top的图片取得有没有问题 是不是需要把参数改为RGBA

LeeMcQueen commented 3 years ago

直接把相机的lookat函数里面的参数修改一下,1.地面上看天空 2.地面下看天空 就可以知道是sky剥削的问题 还是反射的时候的Zbuffer的问题

LeeMcQueen commented 3 years ago

glDepthMask(GL_FALSE);

glDepthMask(GL_TRUE);

是不是没用禁用深度写入 加上看看

LeeMcQueen commented 3 years ago

水面折射可以不需要渲染树,草,人物啥的

LeeMcQueen commented 3 years ago

想太多了,就是top图片的质量太低,被opengl自动填充了,然后距离太远出现了走样

LeeMcQueen commented 3 years ago

关于水面只有x轴上面的偏移但是没有y轴上偏移的原因很有可能是地面的y值是z值 textureCoords = vec2(position.x / 2.0 + 0.5, position.y / 2.0 + 0.5) titing; textureCoords = vec2(position.x / 2.0 + 0.5, position.z / 2.0 + 0.5) titing;

LeeMcQueen commented 3 years ago

waterVertexShader 里面的 textureCoords 是通过 position的坐标强行算出来的,所以要根据自己的xyz坐标进行修改 不然就会出现波浪只有x轴偏移,没有y轴上的偏移

LeeMcQueen commented 3 years ago

水面不动的原因是应为我给波动的变量一个定值 正确的方法应该是给一个 glfwGetTime() 取得游戏已经经过的时间