Thread: OpenGL Textures Doing Weird Things?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    5

    OpenGL Textures Doing Weird Things?

    I'm trying to use textures on a cube for a project, and they aren't working at all. I can read them in successfully, but they always display weird.

    The cube currently looks something like this (the main face in the image being a 'textured' one, it's supposed to be a picture of a Pokemon so it is clearly wrong):

    What.png picture by snipy3 - Photobucket

    Here is my (relevant) code:
    Code:
    GLuint make_texture(const char * filename) {
    	GLuint text = 50;
    	int width, height;
    	GLubyte *data;
    	FILE * file;
    	
    	//Open texture data
    	file = fopen(filename, "rb");
    	if(file == NULL) {printf("File fail %s: %s\n", filename, strerror(errno)); return 0;}
    	
    	//Allocate buffer
    	data = read_image(filename);	
    	width = tex_w;
    	height = tex_h;
    
    	glEnable(GL_TEXTURE_2D);
    		
    	//Allocate a texture name
    	glGenTextures(1, &text);
    	//Select current texture
    	glBindTexture(GL_TEXTURE_2D, text);
    
    	//Select modulate to mix texture with color for shading
    	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    
    	//When texture area is small, bilinear filter the closest mipmap
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    	
    	//When large, bilinear filter the FIRST mipmap
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    	//If wrap is true, texture wraps over the edges, otherwise it ends at the edges
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    
    	//glBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
    	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    
    	//free buffer
    	free(data);
     
    	return text;
    }
    Code:
    void draw_polys() {
    	GLuint tex;
    	int i;
    	VECTOR *one, *two, *three;
    	for(i = 0; i < data.tCount; i++) {
    		one = &data.vArray[data.tArray[i].a - 1].pos;
    		two = &data.vArray[data.tArray[i].b - 1].pos;
    		three = &data.vArray[data.tArray[i].c - 1].pos;
    		tex = data.tArray[i].texture;		
    		if(tex == 0) { //No texture, then draw it normally
    			glBegin(GL_TRIANGLES);
    			glNormal3d(data.tArray[i].n.x, data.tArray[i].n.y, data.tArray[i].n.z);
    			glVertex3d(one->x, one->y, one->z);
    			glVertex3d(two->x, two->y, two->z);
    			glVertex3d(three->x, three->y, three->z);
    		}
    		else { //Texture exists, then texture it
    			glEnable(GL_TEXTURE_2D);
    			glBindTexture(GL_TEXTURE_2D, tex);
    			glBegin(GL_TRIANGLES);
    			glNormal3d(data.tArray[i].n.x, data.tArray[i].n.y, data.tArray[i].n.z);
    			//printf("a's s value is %lf, and t value is %lf\n", data.vArray[data.tArray[i].a - 1].s, data.vArray[data.tArray[i].a - 1].t);
    			glTexCoord2d(data.vArray[data.tArray[i].a - 1].s, data.vArray[data.tArray[i].a - 1].t);
    			glVertex3d(one->x, one->y, one->z);
    			
    			//printf("b's s value is %lf, and t value is %lf\n", data.vArray[data.tArray[i].b - 1].s, data.vArray[data.tArray[i].b - 1].t);
    			glTexCoord2d(data.vArray[data.tArray[i].b - 1].s, data.vArray[data.tArray[i].b - 1].t);
    			glVertex3d(two->x, two->y, two->z);
    			
    			//printf("c's s value is %lf, and t value is %lf\n", data.vArray[data.tArray[i].c - 1].s, data.vArray[data.tArray[i].c - 1].t);
    			glTexCoord2d(data.vArray[data.tArray[i].c - 1].s, data.vArray[data.tArray[i].c - 1].t);
    			glVertex3d(three->x, three->y, three->z);
    			
    		}
    		glEnd();
    		glDisable(GL_TEXTURE_2D);
    	}
    }
    I'm probably misusing a GL texture function somehow... any help would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    5
    Ignore the comments for the most part in my first segment of code, they are old and largely incorrect.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    From looking at the picture I would say you have the texcoords wrong somehow.
    Last edited by MK27; 05-05-2010 at 11:26 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    5
    The s and t values?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by snipy3 View Post
    The s and t values?
    Yeah.

    If this is the first time you are using textures, this is example is maybe too complicated. You should create as simple a demo for yourself as you can -- just draw a flat square and get a texture on it. Using that array for the vertex data may be screwing you up.

    Not that it will cause a problem, but what's the purpose of "=50" here?
    Code:
    	GLuint text = 50;
    Also, your function may only work with raw bitmap data (what does "read_image" do?). What's the format of the image/texture file?

    I believe you should close the file when done reading, too. Strangely, you open it, then pass the filename to another function and do nothing with the handle. You need to be more precise and less haphazard with your programming! OGL will screw you over otherwise.
    Last edited by MK27; 05-05-2010 at 11:38 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    5
    A few answers:
    GLuint text = 50 is actually old code from a while ago when it was believed that GLGenTextures wasn't generating unique values (in other words it doesn't need to be there).

    I will look into how I calculate s and t, that might actually be the problem. Thanks!

    If this seems rushed and poorly coded it is because it is a school project and it happens to be due in about 12 hours, so I've been working pretty much around the clock to get this functioning.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    5
    Thanks for pointing that out! It works now - I was passing in arbitrary values for s and t rather than calculating relevant ones.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  2. the effects of textures on my frame rate
    By DavidP in forum Game Programming
    Replies: 37
    Last Post: 10-03-2003, 11:24 AM
  3. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. OpenGL question
    By Malek in forum Windows Programming
    Replies: 1
    Last Post: 09-10-2001, 12:00 PM