title: Wrong colors when loading textures in OpenGL
body: <p>I'm trying to understand how to load a texture in OpenGL and I wrote this very simple code:</p>
  
  <pre><code>GLuint texture;
  
  void loadTexture() {
  
      GLubyte data[] = {  255,0,0,
                          0,255,0,
                          0,255,0,
                          255,0,0    };
  
      glGenTextures(1, &amp;texture);
  
      glBindTexture(GL_TEXTURE_2D, texture);          
  
      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);   
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);   
  
      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  
      int chk = gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB, 2, 2, GL_RGB, GL_UNSIGNED_BYTE, data );
  
      if (chk!=0) 
          printf("error code = %d\n",chk);
      else 
          printf("success\n");  
  
  }
  </code></pre>
  
  <p>I use <code>loadTexture()</code> to load the texture in memory. The texture, in this sample, is extremely simple, but at the moment it doesn't matter.</p>
  
  <pre><code>void drawTexturedSquare() {
  
      glEnable( GL_TEXTURE_2D );
      glBegin (GL_QUADS);
  
      glTexCoord2f (0.0, 0.0);
      glNormal3f(0, 0, 1);
      glVertex3f (0.0, 0.0, 0.0);
  
      glTexCoord2f (1.0, 0.0);
      glNormal3f(0, 0, 1);
      glVertex3f (10.0, 0.0, 0.0);
  
      glTexCoord2f (1.0, 1.0);
      glNormal3f(0, 0, 1);
      glVertex3f (10, 10, 0.0);
  
      glTexCoord2f (0.0, 1.0);
      glNormal3f(0, 0, 1);
      glVertex3f (0.0, 10, 0.0);
  
      glEnd ();
      glDisable( GL_TEXTURE_2D);
  
  }
  </code></pre>
  
  <p>I would like to apply this simple texture to a square. I call function <code>drawTexturedSquare()</code> from inside a <code>draw()</code> function where I already called </p>
  
  <pre><code>glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  </code></pre>
  
  <p>The problem is that this is the result I get</p>
  
  <p><img src="https://i.stack.imgur.com/F02UQ.png" alt="enter image description here"></p>
  
  <p>while I expected the square to be green in main diagonal (upper-left to lower-right) and red is secondary diagonal (upper-right to lower-left). May someone explain me why?</p>
  
  <p>Besides, every time I run the program I get a different result: </p>
  
  <p><img src="https://i.stack.imgur.com/WSwlS.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/50CYw.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/F2MXC.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/92pk9.png" alt="enter image description here"></p>
  
  <p>I do not understand where this blue comes out...May someone help me?</p>