我正在尝试使用SDL库显示具有6种不同纹理(所有jpg和jpeg)的旋转立方体。但是该程序仅显示白色方块。任何人都可以解释一下这些纹理发生了什么吗?
每个功能的行为
opengl_init()
:初始化opengl设置
init()
:初始化SDL设置,包括opengl_init()
clean_up()
:在程序结束前运行
RenewTexture()
:在这里我想使用此功能映射纹理
main()
:关于绘制顶点并使用旋转功能播放每一帧
const int window_width = 1024;
const int window_height = 1024;
const int SCREEN_BPP = 32;
SDL_Event event;
SDL_Window* win = NULL;
SDL_Renderer* renderer = NULL;
GLuint texId[6];
SDL_GLContext context;
/****** Name Space for Game Variables ******/
namespace var
{
int frame_start_time = 0;
int frame_current_time = 0;
int frame_count = 0;
}
/****** Initializes Core OpenGL Features ******/
bool opengl_init()
{
glEnable(GL_DEpth_TEST);
glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glClearColor(0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(640.0 / 480.0),0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
std::cout << glGetError << std::endl;
if (glGetError() != GL_NO_ERROR)
{
return false;
}
return true;
}
/****** Initializes SDL,OpenGL and Video and Window ******/
bool init()
{
//if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
return false;
}
win = SDL_CreateWindow("WINDOW",100,window_width,window_height,SDL_WINDOW_OPENGL);
renderer = SDL_CreateRenderer(win,-1,SDL_RENDERER_accELERATED);
context = SDL_GL_CreateContext(win);
if (opengl_init() == false)
{
return false;
}
}
/* Removes objects before game closes */
void clean_up()
{
SDL_Quit();
}
void RenewTexture(SDL_Surface* tmpimg,int index) {
GLubyte* map = (GLubyte*)tmpimg->pixels;
glBindTexture(GL_TEXTURE_2D,texId[index]);
int w,h;
w = tmpimg->w;
h = tmpimg->h;
/*glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);*/
glTexImage2D(
GL_TEXTURE_2D,GL_RGB,w,h,GL_UNSIGNED_BYTE,map
);
glTexParameterf(GL_TEXTURE_2D,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_LINEAR);
// glBindTexture(GL_TEXTURE_2D,texId[index]);
}
int main(int argc,char* args[])
{
std::vector<SDL_Surface*> image;
GLfloat rotation = 0.0f;
bool quit = false;
if (init() == false)
{
return 1;
}
const char* ImageNames[6] = { "res/KGU-logo.jpg","res/Me.jpeg","res/beach-1838501.jpg","res/bench-560435.jpg","res/wolf-1341881_1920.jpg","res/spring-bird-2295434.jpg" };
for (int i = 0; i < 6; i++) {
SDL_RWops* tmprwop = SDL_RWFromFile(ImageNames[i],"rb");
SDL_Surface* tmpimg = IMG_LoadJPG_RW(tmprwop);
image.push_back(tmpimg);
}
for(int i=0;i<6;i++)
glGenTextures(1,&texId[i]);
/*SDL_Rect position;
position.x = 1;
position.y = 1;
position.h = 10;
position.w = 10;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,SDL_CreateTextureFromSurface(renderer,image.at(0)),NULL,&position);
SDL_RenderPresent(renderer);*/
/****** Main Game Loop ******/
while (quit == false)
{
/****** Get Most Current Time ******/
var::frame_start_time = SDL_GetTicks();
/****** Draw Rectangle ******/
glClear(GL_COLOR_BUFFER_BIT | GL_DEpth_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-6.0f);
glRotatef(rotation,1.0f,0.0f);
RenewTexture(image.at(0),0);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0); glVertex2f(0,0);
glTexCoord2f(1.0,0.0); glVertex2f(550,1.0); glVertex2f(550,550);
glTexCoord2f(0.0,1.0); glVertex2f(0,550);
glEnd();
RenewTexture(image.at(0),0);
glBegin(GL_QUADS);
/* Cube Top */
//glColor4f(1.0f,1.0f);
glVertex3f(-1.0f,-1.0f);
glVertex3f(1.0f,1.0f);
RenewTexture(image.at(1),1);
/* Cube Bottom */
//glColor4f(1.0f,0.5f,-1.0f,1.0f);
RenewTexture(image.at(2),2);
/* Cube Front */
//glColor4f(0.0f,1.0f);
glVertex3f(1.0f,1.0f);
RenewTexture(image.at(3),3);
/* Cube Back */
//glColor4f(0.0f,-1.0f);
glVertex3f(-1.0f,-1.0f);
RenewTexture(image.at(4),4);
/* Cube Left Side */
//glColor4f(0.5f,-1.0f);
RenewTexture(image.at(5),5);
/* Cube Right Side */
//glColor4f(0.15f,0.25f,0.75f,-1.0f);
glEnd();
rotation -= 0.5f;
/****** Check for Key & System input ******/
while (SDL_PollEvent(&event))
{
/****** Application Quit Event ******/
if (event.type == SDL_QUIT)
{
quit = true;
}
}
/****** Update Screen And Frame Counts ******/
SDL_GL_SwapWindow(win);
var::frame_count++;
var::frame_current_time = SDL_GetTicks();
/****** Frame Rate Handle ******/
if ((var::frame_current_time - var::frame_start_time) < (1000 / 60))
{
var::frame_count = 0;
SDL_Delay((1000 / 60) - (var::frame_current_time - var::frame_start_time));
}
}
clean_up();
return 0;
}
修改
通过将所有glTexCoord3f()
更改为glTexCoord2f()
来解决,并且有效...我的错:D
此外,我误解了glEnable(GL_TEXTURE_3D)
,因此更改为glEnable(GL_TEXTURE_2D)
,并通过错误检查功能检查了所有opengl错误
void errorcheck() {
int errormsg;
if ((errormsg = glGetError()) != GL_NO_ERROR)
{
std::cout << "glerror: " << errormsg << std::endl;
exit(0);
}
}