3 Commits

Author SHA1 Message Date
Benjamin Petsch
6adef607f2 added a few missing lines 2016-09-08 15:39:12 +02:00
Benjamin Petsch
996564ed7d few changes? 2016-09-08 14:38:38 +02:00
Benjamin Petsch
08776360c2 added WriteTexture function to write rendered pixel into a texture 2016-09-07 13:17:27 +02:00
2 changed files with 44 additions and 1 deletions

View File

@@ -148,6 +148,48 @@ GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path
return ProgramID;
}
GLuint WriteTexture(int textw, int texth){
// The framebuffer, which regroups 0, 1, or more textures, and 0 or 1 depth buffer.
GLuint FramebufferName = 0;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
// The texture we're going to render to
GLuint renderedTexture;
glGenTextures(1, &renderedTexture);
// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, renderedTexture);
// Give an empty image to OpenGL ( the last "0" )
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, textw, texth, 0,GL_RGB, GL_UNSIGNED_BYTE, 0);
// Poor filtering. Needed !
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// The depth buffer -- OPTIONAL, REMOVE WHEN NOT NEEDED
GLuint depthrenderbuffer;
glGenRenderbuffers(1, &depthrenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1024, 768);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);
// Set "renderedTexture" as our colour attachement #0
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);
// Set the list of draw buffers.
GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
// Always check that our framebuffer is ok
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
return false;
// Render to our framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
glViewport(0, 0, textw, texth);
}
int main(int argc, char* args[]) {

View File

@@ -4,7 +4,8 @@
in vec2 UV;
// Ouput data
out vec3 color;
//out vec3 color;
layout(location = 0) out vec3 color;
// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;