How can I apply a texture to a glutSolidSphere?
this what i have tried but it doesn’t work:
GLuint textureid;
glGenTextures(1, &textureid);
glBindTexture(GL_TEXTURE_2D, textureid);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, "stone.tga");
// Planet
glDisable(GL_LIGHTING);
glBindTexture(GL_TEXTURE_2D, textureid);
glutSolidSphere(35.0f, 30, 17);
glEnable(GL_LIGHTING);
can some one direct me on how to Load the texture and enable it.
i heard that glutSolidSphere emits texture coordinates already, so i don’t have to do anything special there.
Your issue is that you’re not actually creating a texture with
glTexImage2D. The last parameter should be abyte[]of pixel channel colors in the order Red, Green, Blue, Alpha (as specified by your other parameters –GL_UNSIGNED_BYTEandGL_RGBA), and not just the name of the file.OpenGL has no clue what a file is, how to read a file from a system-specific filesystem, or how to parse the TGA file format. It’s expecting raw color data, so you need to load and parse your image and pass the
byte[]to OpenGL (technically the pointer to the first element as avoid*). There are several guides that give you the minimum code necessary to parse a TGA file, but you can also use a library like FreeImage to handle loading your textures, and it works with much more than just TGA.