OpenGL - 纹理 glutSolidSphere?
如何将纹理应用到 glutSolidSphere?
这是我尝试过的,但它不起作用:
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);
有人可以指导我如何加载纹理并启用它。
我听说 glutSolidSphere 已经发出纹理坐标,所以我不必在那里做任何特别的事情。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您实际上并没有使用
glTexImage2D
创建纹理。最后一个参数应该是像素通道颜色的byte[]
,顺序为红、绿、蓝、Alpha(由其他参数指定 -GL_UNSIGNED_BYTE
和GL_RGBA
),而不仅仅是文件名。OpenGL 不知道文件是什么、如何从特定于系统的文件系统读取文件或如何解析 TGA 文件格式。它需要原始颜色数据,因此您需要加载并解析图像并将
byte[]
传递给 OpenGL(技术上讲,指向第一个元素的指针为void*
) 。有几个 指南,为您提供解析所需的最少代码TGA 文件,但您也可以使用像 FreeImage 这样的库来处理加载纹理,并且它适用于很多不仅仅是 TGA。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_BYTE
andGL_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.