<p>This type of artifact is a tell-tale sign that you've messed up your texture image format at the byte level. It's hard to tell what exactly is wrong, but it's something along the lines of bad pitch or a component layout mismatch.</p>

<p>Have a read of <a href="https://www.opengl.org/wiki/Image_Format">this wiki page</a> and try again. <code>glTexImage2D</code> should work, keep trying until it does.</p>

CC BY-SA 3.0
2016-06-11T15:00:05.277Z
Thank you! I might as well try to reimplement some basic reader - but in the sample code that was given as example the calls didn't mess up things like this. I'm going to look at it again!
phagio
CC BY-SA 3.0
2016-06-11T16:29:33.877Z
I see you're using the SDL surface's pixel buffer directly. You could get the debug version of the SDL library and the source, and inspect what's happening inside SDL. The core of the problem is that OpenGL expects the pixels to be laid out in memory differently than what is actually in the buffer.
IneQuation
CC BY-SA 3.0
2016-06-17T11:55:14.220Z
I "surprisingly" solved by letting the code load a jpg rather than a png. I guess SDL likes MPEG codecs more than PNG.
phagio
CC BY-SA 3.0
2016-06-20T07:26:51.490Z
PNG supports alpha, JPEG doesn't. Have you tried uploading your PNG texture as GL_RGBA, not GL_RGB? Because you haven't solved your problem, you just sidestepped it.
IneQuation
CC BY-SA 3.0
2016-06-10T19:58:16.703Z
2016-06-10T19:58:16.703Z
IneQuation
<p>I know there are tons of questions about it, but I found nothing useful so far so, here I am.</p>

<p>I'm trying to render a texture which is attached to a wavefront .obj mesh. I continued to fail (somehow, it was like textures were not correctly loaded), so I wrote a simple example code to see where I was wrong. The sample code follows, and the texture is "correctly" loaded and rendered. However, there are some problems in the render itself: the image is incomplete / blurry / repeated / distorted. I will also post the expected result and the actual result.</p>

<p>Working on Linux Ubuntu 14.04, OpenGL 3.0 Mesa 10.5.9.
Any idea of what is happening?</p>

<p>Texture (copyright Angryfly @ Turbosquid) and result on the right:
<a href="https://i.sstatic.net/9VeLs.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/9VeLs.png" alt="enter image description here"></a></p>

<p>Sample code:</p>

<pre><code>#include &lt;math.h&gt;
#include &lt;GL/gl.h&gt;
#include &lt;GL/glu.h&gt;
#include &lt;SDL2/SDL.h&gt;
#include &lt;SDL2/SDL_image.h&gt;
#include&lt;iostream&gt;

int main(int argc, char* argv[]) {
    SDL_Init( SDL_INIT_VIDEO );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    // prepare the window
    SDL_Window *window = SDL_CreateWindow(argv[0], 0, 0,
            800, 600,
            SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
    SDL_GLContext context = SDL_GL_CreateContext(window);
    // enable textures and bind a name (say 50)
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 50);

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                     GL_LINEAR_MIPMAP_NEAREST );
    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 );
    // load image with SDL
    SDL_Surface *s = IMG_Load("texture.png");
    // the following doesn't work (white space)
    //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, s-&gt;w, s-&gt;h, 0, GL_RGB, GL_UNSIGNED_BYTE, s-&gt;pixels);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, s-&gt;w, s-&gt;h, GL_RGB, GL_UNSIGNED_BYTE, s-&gt;pixels);

    glViewport(0, 0, 800, 600);
    glClearColor(1,1,1,1);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70, 0.75, 0.2, 1000);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    gluLookAt(0, 10, 0, 0, 0, 0, 1, 0, 0);

    glActiveTexture(50);
    // display texture as 2 triangles.
    glBegin(GL_TRIANGLES);
    glTexCoord2d(0,0); glVertex3d(-5, 0, -5);
    glTexCoord2d(0,1); glVertex3d(-5, 0, 5);
    glTexCoord2d(1,0); glVertex3d(5, 0, -5);

    glTexCoord2d(0,1); glVertex3d(-5, 0, 5);
    glTexCoord2d(1,0); glVertex3d(5, 0, -5);
    glTexCoord2d(1,1); glVertex3d(5, 0, 5);

    glEnd();
    glFinish();
    SDL_GL_SwapWindow(window);
    // wait for quit procedure
    SDL_Quit();
    return 0;

}
</code></pre>

CC BY-SA 3.0
2016-06-10T14:06:58.027Z
Is there any reason why you use the old style openGL
joojaa
CC BY-SA 3.0
2016-06-11T14:58:30.990Z
It is a computer graphics project in OpenGL and SDL.
phagio
CC BY-SA 3.0
2016-06-11T15:07:44.933Z
that's not a reason its a circular definition.
joojaa
CC BY-SA 3.0
2016-06-12T20:19:48.513Z
Yeah, you're right, I didn't explain, sorry. I'm doing this for a computer graphics class which mainly focuses on OpenGL, SDL and many basic concepts of cg.
phagio
CC BY-SA 3.0
2016-06-12T21:57:10.490Z
Yes but why are you using obsolete and deprecated openGL calls?
joojaa
CC BY-SA 3.0
2016-06-14T07:51:27.473Z
Because the class taught this "dialect", mainly... Are there better ways to use OpenGL?
phagio
CC BY-SA 3.0
2016-06-14T08:03:27.687Z
Yes  that's what [deprecated means](http://stackoverflow.com/questions/8111774/deprecated-meaning), any new code should **not** use this method if possible (here its possible). Read its not very performant [why-do-vertex-buffer-objects-improve-performance](http://computergraphics.stackexchange.com/questions/32/why-do-vertex-buffer-objects-improve-performance). Nobody should be teaching you this stuff anymore.
joojaa
CC BY-SA 3.0
2016-06-14T08:13:06.377Z
also read http://stackoverflow.com/questions/14300569/opengl-glbegin-glend
joojaa
CC BY-SA 3.0
2016-10-05T23:12:46.950Z
I teach an OpenGL class about once per year. We **start** with 4.3 core and shaders. Despite claims the fixed pipeline being better for teaching, my (undergrad) students picked it up pretty quickly. And learned a useful skill.
3Dave
<opengl><texture><legacy-opengl>
Texture rendering in OpenGL
phagio
# Texture rendering in OpenGL

(Asked by: username_0 on 2016-06-10)

I know there are tons of questions about it, but I found nothing useful so far so, here I am.

I'm trying to render a texture which is attached to a wavefront .obj mesh. I continued to fail (somehow, it was like textures were not correctly loaded), so I wrote a simple example code to see where I was wrong. The sample code follows, and the texture is "correctly" loaded and rendered. However, there are some problems in the render itself: the image is incomplete / blurry / repeated / distorted. I will also post the expected result and the actual result.

Working on Linux Ubuntu 14.04, OpenGL 3.0 Mesa 10.5.9.
Any idea of what is happening?

Texture (copyright Angryfly @ Turbosquid) and result on the right:
[![enter image description here](https://i.sstatic.net/9VeLs.png)](https://i.sstatic.net/9VeLs.png)

Sample code:

```
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include<iostream>

int main(int argc, char* argv[]) {
    SDL_Init( SDL_INIT_VIDEO );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    // prepare the window
    SDL_Window *window = SDL_CreateWindow(argv[0], 0, 0,
            800, 600,
            SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
    SDL_GLContext context = SDL_GL_CreateContext(window);
    // enable textures and bind a name (say 50)
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 50);

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                     GL_LINEAR_MIPMAP_NEAREST );
    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 );
    // load image with SDL
    SDL_Surface *s = IMG_Load("texture.png");
    // the following doesn't work (white space)
    //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, s->w, s->h, 0, GL_RGB, GL_UNSIGNED_BYTE, s->pixels);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, s->w, s->h, GL_RGB, GL_UNSIGNED_BYTE, s->pixels);

    glViewport(0, 0, 800, 600);
    glClearColor(1,1,1,1);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70, 0.75, 0.2, 1000);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    gluLookAt(0, 10, 0, 0, 0, 0, 1, 0, 0);

    glActiveTexture(50);
    // display texture as 2 triangles.
    glBegin(GL_TRIANGLES);
    glTexCoord2d(0,0); glVertex3d(-5, 0, -5);
    glTexCoord2d(0,1); glVertex3d(-5, 0, 5);
    glTexCoord2d(1,0); glVertex3d(5, 0, -5);

    glTexCoord2d(0,1); glVertex3d(-5, 0, 5);
    glTexCoord2d(1,0); glVertex3d(5, 0, -5);
    glTexCoord2d(1,1); glVertex3d(5, 0, 5);

    glEnd();
    glFinish();
    SDL_GL_SwapWindow(window);
    // wait for quit procedure
    SDL_Quit();
    return 0;

}

```

**Tags:** opengl, texture, legacy-opengl

> **Comments:**
> **username_1**: Is there any reason why you use the old style openGL  
> **username_0**: It is a computer graphics project in OpenGL and SDL.  
> **username_1**: that's not a reason its a circular definition.  
> **username_0**: Yeah, you're right, I didn't explain, sorry. I'm doing this for a computer graphics class which mainly focuses on OpenGL, SDL and many basic concepts of cg.  
> **username_1**: Yes but why are you using obsolete and deprecated openGL calls?  
> **username_0**: Because the class taught this "dialect", mainly... Are there better ways to use OpenGL?  
> **username_1**: Yes  that's what [deprecated means](http://stackoverflow.com/questions/8111774/deprecated-meaning), any new code should **not** use this method if possible (here its possible). Read its not very performant [why-do-vertex-buffer-objects-improve-performance](http://computergraphics.stackexchange.com/questions/32/why-do-vertex-buffer-objects-improve-performance). Nobody should be teaching you this stuff anymore.  
> **username_1**: also read http://stackoverflow.com/questions/14300569/opengl-glbegin-glend  
> **username_2**: I teach an OpenGL class about once per year. We **start** with 4.3 core and shaders. Despite claims the fixed pipeline being better for teaching, my (undergrad) students picked it up pretty quickly. And learned a useful skill.  

---

## 1 answers

### Answer by username_3 on 2016-06-10. Score: 7 | Accepted answer

This type of artifact is a tell-tale sign that you've messed up your texture image format at the byte level. It's hard to tell what exactly is wrong, but it's something along the lines of bad pitch or a component layout mismatch.

Have a read of [this wiki page](https://www.opengl.org/wiki/Image_Format) and try again. `glTexImage2D` should work, keep trying until it does.

> **Comments:**
> **username_0**: Thank you! I might as well try to reimplement some basic reader - but in the sample code that was given as example the calls didn't mess up things like this. I'm going to look at it again!  
> **username_3**: I see you're using the SDL surface's pixel buffer directly. You could get the debug version of the SDL library and the source, and inspect what's happening inside SDL. The core of the problem is that OpenGL expects the pixels to be laid out in memory differently than what is actually in the buffer.  
> **username_0**: I "surprisingly" solved by letting the code load a jpg rather than a png. I guess SDL likes MPEG codecs more than PNG.  
> **username_3**: PNG supports alpha, JPEG doesn't. Have you tried uploading your PNG texture as GL_RGBA, not GL_RGB? Because you haven't solved your problem, you just sidestepped it.  

---

