在同一个OpenGL窗口中绘制2D和3D
问题描述:
我使用OpenGL创建3D游戏,并且想在窗口顶部制作一个工具栏。为此,我尝试使用SDL绘制按钮和OpenGL来绘制实际的游戏。这里是我的代码的相关部分:在同一个OpenGL窗口中绘制2D和3D
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按钮?
答
第二个代码的工作原理是绘制2D按钮之前加入以下权利:
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glDisable(GL_DEPTH_TEST);
glLoadIdentity();
和下面的代码绘制2D按钮后,右:
glEnable(GL_DEPTH_TEST);
gluPerspective(70,(double)640/480,0.5,INFINITE); //These parameters have to be the same as the ones used for gluPerspective when initializing the 3D
gluLookAt(0,0,3,1,0,3,0,0,0.01); //Where you want to position the camera and where you want to look at
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
下面是完整的代码,作品:
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
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
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);
gluPerspective(70,(double)640/480,0.5,INFINITE); //These parameters have to be the same as the ones used for gluPerspective when initializing the 3D
gluLookAt(0,0,3,1,0,3,0,0,0.01); //Where you want to position the camera and where you want to look at
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
}
你使用什么类型的投影?我会切换到用于ui的拼字。 – BDL
@BDL因为我是OpenGL的初学者,所以我不太清楚你的投影是什么意思,但是你可能会看到我在代码中使用了什么类型的投影。如果没有,你可以告诉我如何找出答案。 –
有一个身份投影(透视proj用'gluPerspective'设置,但随后立即用'glLoadIdentity'丢弃)。我在第二个代码中看不到任何明显的错误(第一个代码是另一回事 - 它没有办法工作),但是由于GL是一个状态机,在给定代码之前可能会发生很多状态改变并影响其执行(像背面剔除)。我建议发布说明你的问题的最小完整的例子。更好的解决方案是使用例如apitrace来调试你的问题。 – keltar