本文介绍了为什么纹理不显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨。
我写了一个代码,但它只显示没有纹理的立方体
Hi.
I wrote a code but it''s just show a cube without texture
#include "Texture.h"
int main(int argc,char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640,480,32,SDL_OPENGL);
glEnable(GL_DEPTH_TEST);
glClearColor(0,0,0,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,800/600,1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(7,7,7,
0,0,0,
0,1,0);
Texture* textre=new Texture((IMG_Load("Texture.png")));
int rot=0;
while(1)
{
glLoadIdentity();
gluLookAt(7,7,7,
0,0,0,
0,1,0);
glRotatef(rot,0,1,0);
glBindTexture(GL_TEXTURE_2D,textre->textureID);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(-.5f , -.5f , .5f);
glTexCoord2f(1,0);
glVertex3f(.5f , -.5f , .5f);
glTexCoord2f(1,1);
glVertex3f(.5f , .5f, .5f);
glTexCoord2f(0,1);
glVertex3f(-.5f , .5f , .5f);
glTexCoord2f(0,0);
glVertex3f(-.5f , -.5f , -.5f);
glTexCoord2f(1,0);
glVertex3f(.5f , -.5f , -.5f);
glTexCoord2f(1,1);
glVertex3f(.5f , .5f , -.5f);
glTexCoord2f(0,1);
glVertex3f(-.5f , .5f , -.5f);
glVertex3f(-.5f , .5f , -.5f);
glVertex3f(.5f , .5f , -.5f);
glVertex3f(.5f , .5f , .5f);
glVertex3f(-.5f , .5f , .5f);
glVertex3f(.5f , -.5f , -.5f);
glVertex3f(.5f , .5f ,-.5f);
glVertex3f(.5f , .5f , .5f);
glVertex3f(.5f , -.5f , .5f);
glVertex3f(-.5f , .5f , .5f);
glVertex3f(-.5f , -.5f ,.5f);
glVertex3f(-.5f , -.5f , -.5f);
glVertex3f(-.5f , .5f , -.5f);
glVertex3f(.5f , -.5f , .5f);
glVertex3f(-.5f , -.5f , .5f);
glVertex3f(-.5f , -.5f , -.5f);
glVertex3f(.5f , -.5f , -.5f);
glEnd();
SDL_GL_SwapBuffers();
rot++;
SDL_Delay(16);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
return 0;
}
------质地:
------ texture :
#pragma once
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_opengl.h>
class Texture
{
public:
Texture(SDL_Surface* surface)
{
unsigned int nOfColor=surface->format->BytesPerPixel;
unsigned int textureFormat;
if(nOfColor==4)
if(surface->format->Rmask==0x000000ff)
textureFormat=GL_RGBA;
else
textureFormat=GL_BGRA;
else
if (surface->format->Rmask==0x000000ff)
textureFormat=GL_RGB;
else
textureFormat=GL_BGR;
glGenTextures(1,&textureID);
glBindTexture(GL_TEXTURE_2D,textureID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,nOfColor,surface->w,surface->h,0,textureFormat,GL_UNSIGNED_BYTE,surface->pixels);
SDL_FreeSurface(surface);
}
unsigned int textureID;
};
推荐答案
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
2.请在纹理映射之前启用GL_TEXTURE_2D。在Texture类中,您可以通过调用
2. Please enable GL_TEXTURE_2D before texture mapping. Inside Texture class you can enable this by calling
glEnable( GL_TEXTURE_2D )
这篇关于为什么纹理不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!