开发者

Does glGenTexture only work in the main?

开发者 https://www.devze.com 2023-02-28 21:25 出处:网络
I have loaded textures into openGL before, worked fine. But this time I wanted to load a texture within a constructor of my rendering class. (im using MVC with abstract base classes as interfaces). I

I have loaded textures into openGL before, worked fine. But this time I wanted to load a texture within a constructor of my rendering class. (im using MVC with abstract base classes as interfaces). I was originally trying to load a png file rather than a bmp as I did before so i assumed it was something to do with that, but when i went back to my bmp code it st开发者_开发技巧ill didn't work. So a hunch I moved the bmp texture code into the main(of the same MVC project) and it works. The actual part that wasnt working was:

GLuint textureId;
    glGenTextures(1, &textureId); 
    glBindTexture(GL_TEXTURE_2D, textureId);
    //Map the image to the texture
    glTexImage2D(GL_TEXTURE_2D,//This bit!!               
                 0,                           
                 GL_RGB,                      
                 image->width, image->height,  
                 0,                            
                 GL_RGB, 
                 GL_UNSIGNED_BYTE,                                 
                 image->pixels);

if I stepped through the program I noticed that textureID stayed the same (didnt change to 1 becuase no texture was being mapped to it)

Why is this? and can it be fixed?


When is the constructor being called? You need a valid OpenGL context to make any GL call. A usual error is to create objects at the global scope, and that means the object is constructed at program start, before main() is called, and at that point there is no OpenGL context created yet.


Looks like you made textureId into a local variable, which disappears when the current function (the constructor) finishes. Try a class member variable if you want it to live longer.

0

精彩评论

暂无评论...
验证码 换一张
取 消