title: opengl: rendering to texture goes wrong
body: <p>I've written a program for testing rendering a texture with the framebuffer. In the function render_texture() I want to render a triangle on to a texture, but when I display the rendered texture in the display function, I get just a simple yellow square.</p>
  
  <pre><code>#include "GL/glew.h"
  #include "GL/glext.h"
  #include "GL/glu.h"
  #include "GL/freeglut.h"
  
  #include &lt;cstdio&gt;
  
  uint16_t tex_width = 75;
  uint16_t tex_height = 24;
  
  GLuint texture;
  
  
  int w1;
  int h1;
  
  void orthogonalStart (int w, int h) {
      glMatrixMode(GL_PROJECTION);
      glPushMatrix();
      glLoadIdentity();
      gluOrtho2D(0, w, 0, h);
      glScalef(1, -1, 1);
      glTranslatef(0, -h1, 0);
      glMatrixMode(GL_MODELVIEW);
  }
  
  void orthogonalEnd (void) {
      glMatrixMode(GL_PROJECTION);
      glPopMatrix();
      glMatrixMode(GL_MODELVIEW);
  }
  
  void display (void) {
      glClearColor (0.0,0.0,0.0,1.0);
      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();
  
      orthogonalStart(w1, h1);
  
      glBegin(GL_QUADS);
      glTexCoord2f(0, 0); glVertex2f(125, 125);
      glTexCoord2f(0, 1); glVertex2f(125, 125+tex_height);
      glTexCoord2f(1, 1); glVertex2f(125+tex_width, 125+tex_height);
      glTexCoord2f(1, 0); glVertex2f(125+tex_width, 125);
      glEnd();
  
      orthogonalEnd();
  
      glutSwapBuffers();
  }
  
  void reshape (int w, int h) {
      glViewport (0, 0, (GLsizei)w, (GLsizei)h);
      glMatrixMode (GL_PROJECTION);
      glLoadIdentity ();
      gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0);
      w1 = w;
      h1 = h;
      glMatrixMode (GL_MODELVIEW);
  }
  
  void render_texture()
  {
     GLuint framebuffer = 0;
  
     glGenFramebuffers(1, &amp;framebuffer);
     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  
     glGenTextures(1, &amp;texture);
  
     glBindTexture(GL_TEXTURE_2D, texture);
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA,
                  GL_UNSIGNED_BYTE, 0);
  
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  
     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
     GLenum draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
     glDrawBuffers(1, draw_buffers);
  
     if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
     {  
        fprintf(stderr, "[render_texture] Fehler im Framebuffer\n");
        exit(1);
     }
  
     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
     glViewport(0, 0, tex_width, tex_height);
  
     glClearColor (0.0,0.0,0.0,1.0);
  
     orthogonalStart(tex_width, tex_height);
  
     glColor4f(1, 1, 0, 0);
  
     glBegin(GL_TRIANGLES);
     glVertex2f(0.f,0.f);
     glVertex2f(tex_width, 0.0f);
     glVertex2f(tex_width, (GLfloat)tex_height/2.f);
     //glVertex2f(tex_width-(GLfloat)tex_height/2.f, tex_height);
     //glVertex2f(0, tex_height);
     glEnd();
  
     orthogonalEnd();
  
     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  }
  
  int main (int argc, char **argv) {
     glutInit (&amp;argc, argv);
     glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
     glutInitWindowSize (500, 500);
     glutInitWindowPosition (100, 100);
     glutCreateWindow ("");
     glutDisplayFunc (display);
     glutIdleFunc (display);
     glutReshapeFunc (reshape);
  
     glewExperimental=true;
     GLenum err=glewInit();
     if(err!=GLEW_OK)
     {
        //Problem: glewInit failed, something is seriously wrong.
        fprintf(stderr, "glewInit failed, aborting.\n");
     }
  
     render_texture();
  
     glutMainLoop ();
     return 0;
  }
  </code></pre>
  
  <p>Edit 1: I've updated the code, now I have just a blank black screen</p>
  
  <pre><code>#include "GL/glew.h"
  #include "GL/glext.h"
  #include "GL/glu.h"
  #include "GL/freeglut.h"
  
  #include &lt;cstdio&gt;
  
  uint16_t tex_width = 75;
  uint16_t tex_height = 24;
  
  GLuint texture;
  
  void orthogonalStart (int w, int h) {
      glMatrixMode(GL_PROJECTION);
      glPushMatrix();
      glLoadIdentity();
      gluOrtho2D(0, w, 0, h);
      glScalef(1, -1, 1);
      glTranslatef(0, -glutGet(GLUT_WINDOW_HEIGHT), 0);
      glMatrixMode(GL_MODELVIEW);
  }
  
  void orthogonalEnd (void) {
      glMatrixMode(GL_PROJECTION);
      glPopMatrix();
      glMatrixMode(GL_MODELVIEW);
  }
  
  void display (void) {
      glClearColor (0.0,0.0,0.0,1.0);
      glClear (GL_COLOR_BUFFER_BIT);
      glLoadIdentity();
  
      orthogonalStart(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
  
      glColor3f(1,1,1);
      glEnable(GL_TEXTURE_2D);
      glBindTexture(GL_TEXTURE_2D, texture);
  
      glBegin(GL_QUADS);
      glTexCoord2f(0, 0); glVertex2f(125, 125);
      glTexCoord2f(0, 1); glVertex2f(125, 125+tex_height);
      glTexCoord2f(1, 1); glVertex2f(125+tex_width, 125+tex_height);
      glTexCoord2f(1, 0); glVertex2f(125+tex_width, 125);
      glEnd();
  
      glDisable(GL_TEXTURE_2D);
  
      orthogonalEnd();
  
      glutSwapBuffers();
  }
  
  void reshape (int w, int h) {
      glViewport (0, 0, (GLsizei)w, (GLsizei)h);
      glMatrixMode (GL_PROJECTION);
      glLoadIdentity ();
      gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0);
      glMatrixMode (GL_MODELVIEW);
  }
  
  void render_texture()
  {
     GLuint framebuffer = 0;
  
     glGenFramebuffers(1, &amp;framebuffer);
     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  
     glGenTextures(1, &amp;texture);
  
     glBindTexture(GL_TEXTURE_2D, texture);
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA,
                  GL_UNSIGNED_BYTE, 0);
  
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  
     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
     GLenum draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
     glDrawBuffers(1, draw_buffers);
  
     if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
     {  
        fprintf(stderr, "[render_texture] Fehler im Framebuffer\n");
        exit(1);
     }
  
     glViewport(0, 0, tex_width, tex_height);
     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  
     orthogonalStart(tex_width, tex_height);
  
     glColor4f(1, 1, 0, 0);
  
     glBegin(GL_TRIANGLES);
     glVertex2f(0.f,0.f);
     glVertex2f(tex_width, 0.0f);
     glVertex2f(tex_width, (GLfloat)tex_height/2.f);
     //glVertex2f(tex_width-(GLfloat)tex_height/2.f, tex_height);
     //glVertex2f(0, tex_height);
     glEnd();
  
     orthogonalEnd();
  
     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  }
  
  int main (int argc, char **argv) {
     glutInit (&amp;argc, argv);
     glutInitContextVersion(3, 1);
     glutInitContextProfile(GLUT_CORE_PROFILE);
     glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
     glutInitWindowSize (500, 500);
     glutCreateWindow ("");
     glutDisplayFunc (display);
     glutIdleFunc (display);
     glutReshapeFunc (reshape);
  
     glewExperimental=true;
     GLenum err=glewInit();
     if(err!=GLEW_OK)
     {
        //Problem: glewInit failed, something is seriously wrong.
        fprintf(stderr, "glewInit failed, aborting.\n");
     }
  
     //glEnable(GL_TEXTURE_2D);
  
     render_texture();
  
     glutMainLoop ();
     return 0;
  }
  </code></pre>