Jump to content

  • Log In with Google      Sign In   
  • Create Account

Problem with texture mapping


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 aGuyWhoSucksAtOpenGL   Members   -  Reputation: 116

Like
0Likes
Like

Posted 28 May 2013 - 04:59 PM

Hi all, 

 

Today was my first step into the mighty land of OpenGL and so the first task I set myself- after playing around with shapes and colours- was to display an image on screen. So far, I've been attempting to texturise a quad for the past 5/6 hours with not much luck, well, none at all really. The rest of the code seems to work fine, I just can't get the texture to show. As a side note, my eyes are beginning to glaze over right now, so if anything I say doesn't make sense, just skip a few lines and see if that does the trick.

 

What choo doing? I'm using SOIL in my code, so the problem may lie with that if the rest of the code seems fine. If you lovely people could check my code for mistakes, I'd greatly appreciate it, thanks! As I said, I'm still a noob and so I can't figure out where I've gone wrong. As for my code, I'm writing this on a mac, so some functions are written in obj-c.

 

Where yo' code at?

-(void) loadTextures{
    
    GLuint textures[1];
    glGenTextures( 1, textures );
	
    int width, height;
    width = 128;
    height = 256;
    unsigned char *image;
    
    glBindTexture( GL_TEXTURE_2D, textures[0] );
    image = SOIL_load_image( "logo.png", &width, &height, 0, SOIL_LOAD_RGB );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image );
    SOIL_free_image_data( image );
    
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    
    glBegin(GL_QUADS);
    
    glTexCoord2d(0.0, 0.0); glVertex2d(-0.5, -0.5);
    glTexCoord2d(1.0, 0.0); glVertex2d(-0.5, 0.5);
    glTexCoord2d(1.0, 1.0); glVertex2d(0.5, 0.5);
    glTexCoord2d(0.0, 1.0); glVertex2d(0.5, -0.5);
    
    glEnd();
}


- (void)drawRect:(NSRect)dirtyRect
{
    glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0); // rgba ; Red, Green, Blue, Alpha (transparency)
    glClear(GL_COLOR_BUFFER_BIT); // if no buffer is present, glclear has no effect. glclear clears buffers to preset values.
    MyOpenGLView *GLView = [[MyOpenGLView alloc] init];
    [GLView loadTextures];
    glFlush(); // forces execution of GL commands.
}

 

 

I'm off to sleep now, this code -albeit small- has worn me out. Any responses would be fantastic, I've looked over and over my code, checked with what seems like a million other pieces of source code and yet I still can't figure out the issue, obviously there is, as the texture isn't loading! If I don't respond within a couple of minutes, odds are I'm asleep, but any responses would be much appreciated and I'll be sure to reply back in around 6 hours time.

 

Thanks!

 

Edit: Here's what I'm getting at the moment: http://i.imgur.com/JOFaYG6.png


Edited by aGuyWhoSucksAtOpenGL, 28 May 2013 - 05:01 PM.


Sponsor:

#2 BornToCode   Members   -  Reputation: 948

Like
1Likes
Like

Posted 29 May 2013 - 09:57 AM

Did you enable Texture Sampling by calling glEnable(GL_TEXTURE2D). By default GL_TEXTURE2D is not enabled. I bet that is your problem.


Edited by BornToCode, 29 May 2013 - 09:58 AM.


#3 Dave Hunt   Crossbones+   -  Reputation: 2478

Like
1Likes
Like

Posted 29 May 2013 - 10:49 AM

As an aside, you do NOT want to be generating a new texture and loading an image every time you draw. You should load the image and generate the texture once, during initialization (or level change), and then just set it on the device (with glBindTexuture) when you draw.



#4 aGuyWhoSucksAtOpenGL   Members   -  Reputation: 116

Like
0Likes
Like

Posted 30 May 2013 - 01:10 PM

Ah, that might be the problem, thanks, I'll give it a go. Will update in a few mins! Also, thanks for the tips Dave, much appreciated.

 

Edit: Yup, that did the trick, I can't thank you enough BornToCode! The funny thing is that I already had that line in my code, I'd just commented it out at some point- I'm an idiot. Thanks again!


Edited by aGuyWhoSucksAtOpenGL, 30 May 2013 - 01:30 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS