Browse Source

创建fbo

master
blobt 4 years ago
parent
commit
04ed54ba38
  1. 4
      main.cpp
  2. 38
      misc.cpp
  3. 1
      misc.h

4
main.cpp

@ -133,6 +133,10 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
/*创建纹理*/
GLuint mainTexture = CreateTextureFromFile("res/image/niutou.bmp");
/*创建FBO*/
GLuint colorBuffer, depthBuffer;
GLuint fbo = CreateFramebufferObject(colorBuffer, depthBuffer, windowWidth, windowHeight);
GL_CALL(glClearColor(0.1f, 0.4f, 0.7f, 1.0f));
glEnable(GL_DEPTH_TEST);

38
misc.cpp

@ -26,6 +26,44 @@ GLuint CreateVAOWithVBOSettings(std::function<void()> settings)
return vao;
}
GLuint CreateFramebufferObject(GLuint & colorBuffer, GLuint & depthBuffer, int width, int height)
{
GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
//colorbuffer
glGenTextures(1, &colorBuffer);
glBindTexture(GL_TEXTURE_2D, colorBuffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorBuffer, 0);//°ó¶¨¸Õ²ÅÉú³ÉµÄtextureµ½GL_COLOR_ATTACHMENT0
//depthbuffer
glGenTextures(1, &depthBuffer);
glBindTexture(GL_TEXTURE_2D, depthBuffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_RGBA, GL_DEPTH_COMPONENT, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthBuffer, 0);
//check
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
printf("create framebuffer object fail\n");
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return GLuint();
}
char* LoadFileContent(const char *path) {
FILE* pFile = fopen(path, "rb");
if (pFile) {

1
misc.h

@ -4,6 +4,7 @@
GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void* data = nullptr);
GLuint CreateVAOWithVBOSettings(std::function<void()>settings);
GLuint CreateFramebufferObject(GLuint &colorBuffer, GLuint &depthBuffer, int width, int height);
char* LoadFileContent(const char *path);
GLuint CompileShader(GLenum shaderType, const char* shaderPath);

Loading…
Cancel
Save