title: OpenGL/ImageMagick Failing to Render Texture (Sometimes)
body: <p>I have been having a very odd problem when trying to use OpenGL's C++ API. I am trying to load in a texture using ImageMagick, and then display it as a simple 2D textured square. I have a decent amount of experience with using OpenGL in Java, so I understand how to render a texture and bind it to a primitive. However, each time I attempt to draw it, the program either fails to render, or it renders it as a (properly sized) white square. I'm not entirely sure what is going on, but I believe it has to do with ImageMagick.</p>
  
  <p>I have been using Ubuntu's terminal for compiling, and I've learned just how painful it can be to have to install libraries manually. ImageMagick first refused to compile when used in my program, and when I finally got the program to compile, it would seg-fault each time it ran. I've finally got it "working", but now, whenever I attempt to load in the image, the program will run without rendering. I haven't found anything like this on Google.</p>
  
  <p><a href="http://imgur.com/C7yKwDK" rel="nofollow">http://imgur.com/C7yKwDK</a></p>
  
  <p>The odd thing is, very rarely, it will work correctly and render the square as expected. However, when I then try to rerun the program, it fails as shown above. I've determined that the line that causes it to fail to render is the same line the image is loaded, so that led me to believe that the image was just being loaded incorrectly, causing the program to fail. <strong>However</strong>, if I move the texture loading code before the creation of the GL window, the program will consistently render successfully, but the textured square appears only as white (though the size of the square is correct, so I know the image loading is working).</p>
  
  <p>Anyway, sorry for the long post. I've just given up solving this one on my own, and was hoping one of you could help me out.</p>
  
  <p><strong>OpenGL Initialization Code:</strong></p>
  
  <pre><code>Texture* tx;
  void GraphicsOGL :: initialize3D(int argc, char* argv[]) {
      Magick::InitializeMagick(*argv);    
      glutInit(&amp;argc, argv);
  
      //Loading Here ALWAYS Causes White Square
      /*glEnable(GL_TEXTURE_2D);
        tx = new Texture("Resources/Images/test.png");
        tx-&gt;load();*/
  
      glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA); 
      glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
      glutInitWindowPosition(100, 100);
      glutCreateWindow("OpenGL Game");    
      glViewport(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
      glOrtho(0,SCREEN_WIDTH,SCREEN_HEIGHT,0, -3,1000);
  
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_ALPHA_TEST);
      glEnable(GL_TEXTURE_2D);
  
      //Loading Here SOMETIMES Works, But Typically Fails
      tx = new Texture("Resources/Images/test.png");
      tx-&gt;load();
  
      glutDisplayFunc(displayCallback);
      glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  
      glutMainLoop();
  }
  </code></pre>
  
  <p><strong>Texture Loading Code:</strong></p>
  
  <pre><code>bool Texture::load() {
      try {
          m_image.read(m_fileName);        //This Line Causes it to Fail to Render
          m_image.write(&amp;m_blob, "RGBA");
      }
      catch (Magick::Error&amp; Error) {
          std::cout &lt;&lt; "Error loading texture '" &lt;&lt; m_fileName &lt;&lt; "': " &lt;&lt; Error.what() &lt;&lt; std::endl;
          return false;
      }
  
      width = m_image.columns();
      height = m_image.rows();
  
      glGenTextures(1, &amp;m_textureObj);
      glBindTexture(m_textureTarget, m_textureObj);
      //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
      //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
      glTexParameterf(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameterf(m_textureTarget, 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);
      glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); 
      glTexImage2D(m_textureTarget, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_blob.data());      
      //glBindTexture(m_textureTarget, 0);
  
      return true;
  }
  </code></pre>
  
  <p><strong>Texture Drawing Code:</strong></p>
  
  <pre><code>void GraphicsOGL :: drawTexture(float x, float y, Texture* tex) {
      glEnable(GL_TEXTURE_2D);
      tex-&gt;bind();
  
      float depth = 0, w, h;
      w = tex-&gt;getWidth();
      h = tex-&gt;getHeight();
  
      glBegin(GL_QUADS);
          glVertex3f(x, y+h, depth);  glTexCoord2f(1,0);
          glVertex3f(x+w, y+h, depth);    glTexCoord2f(1,1);
          glVertex3f(x+w, y, depth);  glTexCoord2f(0,1);
          glVertex3f(x, y, depth);    glTexCoord2f(0,0);
      glEnd();
  }
  </code></pre>