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:
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;
}