【发布时间】:2016-07-27 11:15:38
【问题描述】:
我正在使用 OpenGL 创建一个 3D 游戏,我想在窗口顶部制作一个工具栏。为此,我尝试使用 SDL 绘制按钮并使用 OpenGL 绘制实际游戏。这是我的代码的相关部分:
void openMainWindow(){
SDL_Surface *screen;
SDL_Event event;
SDL_Rect position;
SDL_Init(SDL_INIT_VIDEO);
putenv("SDL_VIDEO_CENTERED=center");
SDL_WM_SetCaption("Example",NULL);
SDL_WM_SetIcon(IMG_Load("icon.png"),NULL);
screen = SDL_SetVideoMode(832,487,32,SDL_HWSURFACE | SDL_OPENGL);
glLoadIdentity();
gluPerspective(70,(double)832/487,1,1000);
//Some things to initialize the window
int continue = 1;
while(continue){
SDL_PollEvent(&event);
switch(event.type){
//Events
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
//Draw a square
glEnd();
//Draw more things the same way
glFlush();
SDL_GL_SwapBuffers();
SDL_Surface *button1 = SDL_CreateRGBSurface(SDL_HWSURFACE,50,50,32,0,0,0,0);
SDL_FillRect(button1,NULL,SDL_MapRGB(screen->format,50,50,50);
position.x = 8;
position.y = 8;
SDL_BlitSurface(button1,NULL,screen,&position);
SDL_Flip(screen);
}
SDL_Quit();
}
这样做的问题是,当调用这个函数时,进程结束并返回3(这意味着有错误)。所以我试着像这样用 OpenGL 绘制按钮:
void openMainWindow(){
//Everything before the while loop is the same as in the other code
int continue = 1;
while(continue){
SDL_PollEvent(&event);
switch(event.type){
//Events
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
//Draw a square
glEnd();
//Draw more things the same way
glDisable(GL_DEPTH_TEST);
glLoadIdentity();
glBegin(GL_QUADS); //Draw the button
glColor3ub(50,50,50);
glVertex2d(-0.5,-0.5);
glVertex2d(-0.5,0.5);
glVertex2d(0.5,0.5);
glVertex2d(0.5,-0.5);
glEnd();
glEnable(GL_DEPTH_TEST);
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
}
我知道第二个代码应该使窗口中的按钮居中,但我使用此代码只是为了测试它是否有效(它没有,这就是我发布这个问题的原因)。
使用第二个代码,3D 事物按应有的方式出现在窗口中,但我看不到任何按钮。如何在 3D OpenGL 窗口中放置 2D 按钮?
【问题讨论】:
-
您使用的是什么类型的投影?我会切换到 ui 的正字法。
-
@BDL 我真的不知道你所说的投影是什么意思,因为我是 OpenGL 的初学者,但你可能可以看到我在代码中使用的投影类型。如果没有,你可以告诉我如何找到。
-
有一个身份投影(透视项目是用
gluPerspective设置的,但随后立即用glLoadIdentity删除)。我在第二个代码中没有看到明显的错误(第一个是不同的故事 - 它不可能工作),但是由于 GL 是一个状态机,在给定代码之前可能会发生很多变化的状态并影响其执行(像背面剔除)。我建议发布最小的完整示例来说明您的问题。更好的解决方案是使用例如apitrace 来调试你的问题。