简体   繁体   中英

How to draw 2d object and 3d object on the same screen

I am trying to draw a button so that later I could click on it. But i am having some problems, namely when i UNcomment the selected part in the Draw2d function, the screen goes black and my 3d object disappears. Any ideas why and how to solve this? I want to add that with the commented part the button shows in a different area (left,top screen) and if i comment it just like its here, then its shown in the center of my 3d object and it doesnt have its borders. Only the label.

void Draw2D()
{
   glPushMatrix();
   glDisable(GL_DEPTH_TEST);
   //glMatrixMode(GL_PROJECTION);
   //glLoadIdentity();
   glOrtho(0, winw, winh, 0, -1, 1);
   //glMatrixMode(GL_MODELVIEW);
   //   glLoadIdentity();
   ButtonDraw(&MyButton);
   glPopMatrix();
   glEnable(GL_DEPTH_TEST);
}

and here's my source code: Source.cpp

I would also like to ask if anyone knows how to create an Input field? An easy way appreciated since i am still learning.

It it not clear how your draw loop looks, and where in it you are setting the matrices, drawing 3d stuff and drawing 2d stuff. But from the code shown so far, I can still explain why it might have an effect: There is a matrix stack per matrix type (modelview, projection, texture).

What your code does is pushing the currently selected matrix onto the stack (probably GL_MODELVIEW ). Then, you appy an ortho matrix to it. Since the matrix is unlikely to be identity here, the ortho will not have the results you expect, and the image is wrong. Also, the old projection matrix is still unchanged, which will further effect the transformarions. You then restore the original matrix and rendering goes on unchanged.

If you uncomment the code, you switch first push, then switch, set the ortho matrix as projection and set identity for modelview - but only the modelview matrix is to be restored, and the projection matrix will still have the ortho set.

What you really need to do is:

glMatrixMode(GL_PROJECTION);
glPushMatrix(); // save current projection matrix 
glLoadIdentity();
glOrtho(0, winw, winh, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix(); // save current modelview matrix
glLoadIdentity();
// ... draw
glMatrixMode(GL_PROJECTION);
glPopMatrix(); // restore PUSHed projection matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix(); // restore PUSHed modelview matrix

This assumes that intially when calling this, glMatrixMode(GL_MODELVIEW) is set, and also restores it to this. This can also be optimized to

// assume `GL_MODELVIEW` is the current matrix mode
glPushMatrix(); // save current modelview matrix
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix(); // save current projection matrix
glLoadIdentity();
glOrtho(0, winw, winh, 0, -1, 1);
// ... draw
glPopMatrix(); // restore PUSHed projection matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix(); // restore PUSHed modelview matrix
// now `GL_MODELVIEW` is selected again

which is slightly less calling overhead, but not as clear as the initial variant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM