Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Build UI in OpenGL for my 3D game world?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Rizkay   Members   -  Reputation: 117

Like
0Likes
Like

Posted 26 December 2013 - 08:41 AM

i'm trying to create my own User Interface such as: Button, panel, window, radiobutton, checkbox, etc..

the word "create" here means:

-designing UI  with own style

-programming they all.

 

well, my problem is: what functions/codes that i must call to draw it all?

 

in this terms, maybe the points are:

-Drawing 2d texture on screen NOT in 3d world game.

so if we're doing translation camera in 3d world game, the UI texture won't  be move.

 

-determine xy coordinates based on screen NOT based in 3d world game.

of course before we set UI texture  first thing that we have to do is determine some textures.

 

to more cleary, take look at the picture

this is a sample User Interface that i've found in google. but this is XNA c#. 

Capture2_zps917ccb91.png

i've learned the source codes how they work. after i understood the concept (include how they draw many textures with structured coordinates)

 

ok so, the problem is: could you show me the point/codes that i should use in openGL graphic programming? include:

-Drawing 2d texture on screen NOT in 3d world game.

-determine xy coordinates in screen.


Edited by Rizkay, 26 December 2013 - 09:35 AM.


Sponsor:

#2 JTippetts   Moderators   -  Reputation: 8569

Like
2Likes
Like

Posted 26 December 2013 - 09:32 AM

What you're looking for is called an orthographic projection. This is a 3D projection that does not include any perspective "shrinking" as objects get further away. Older versions of GL included the now-deprecated glOrtho function to accomplish this, but you should be able to duplicate the effects easily enough using a matrix math library such as GLM.

Essentially, an orthographic projection maps the screen to some range of coordinates, X and Y, so that you can draw objects on the X/Y grid and have them appear in the correct location on-screen.

The typical drawing order is to draw the game view using the game camera and projection, then switch the projection to an orthographic matrix and draw the UI elements as a series of quads. These quads will technically be 3D, in that they are drawn using the same exact types of function calls as drawing the game objects (binding vertex buffers, shaders and textures, rendering triangles) but the geometry is configured so that the shapes are flat quads on the X/Y plane. The Z coordinate is typically set to 0; or, if desired, you can use the Z coordinate to determine the visible ordering of the objects as necessary.

#3 Mathimetric   Members   -  Reputation: 133

Like
0Likes
Like

Posted 27 December 2013 - 07:41 PM

*with mouse driver: you can draw your interface using photo shop,

*add animated buttons, similair to a web page design (hover, click ; textures),

then interact with your GUI using Test POS XY relative to screen pos (if not full screen) and by checking if your mouse button is

down or up. Then you have to support those handles for your buttons;

what will they do when clicked, or hovered over?.

 

example is coded using c/C++ (oop/structured)

//FILE MouseClass.h //
//windows mouse driver
//using windows library
//handles are usually updated via win main Loop, and passed data to windows handle
 
#include <windows.h>
#include <winuser.h>
#include <windef.h>
//simple example for winmouse class
//for using the windows mouse
//POINT  members { long x,y }
class WinMouse
{
private:
POINT CP;  //cursor position
public:
HWND windH;
RECT RectA;
long x, y;
long cx,cy;
int button1, button2;
WinMouse();
~WinMouse();
long CalcWindowX();
long CalcWindowY();
void ResetPos(long, long);
void GetRectPos(RECT );
void GetPos();
void SetPos(long x,long y);
void SetCenter();
void GetCenter();
void UpdateWindowCenter();
long GetX();
long GetY();
PosTest(int x, int y, int tx, int ty);
//note needed for relative mouse pos
//rect->left - GetX() , rect->top - GetY()
//GetWindowRect(HWND,RECT IN PTR)
//then Call
//this->GetRectPos(RECT );
};

////////////////////function definitions/////////////////////

WinMouse::WinMouse()
{
button1 = 0;
button2 = 0;
x=0;
y=0;
cx = 0;
cy = 0;
}
WinMouse::~WinMouse()
{
}
void WinMouse::UpdateWindowCenter()
{
this->GetPos();
 this->x = GetX() - this->cx;
 this->y = GetY() - this->cy;
}
WinMouse::PosTest(int x, int y, int tx, int ty)
{
 if(x < (GetX() -RectA.left ) && x + tx > (GetX() -RectA.left ))
 {
  if( y < (GetY() - RectA.top) && y + ty > (GetY() - RectA.top) )
  return 1;
  else
  return 0;
 }
 else
 return 0;
}
void WinMouse::GetCenter()
{
 x = (RectA.right + RectA.left)/2;
 y = (RectA.top + RectA.bottom)/2;
 
 cx = x;
 cy = y;
}
void WinMouse::SetCenter()
{
 x = RectA.left + RectA.right;
 y = RectA.top + RectA.bottom;  
 
 cx = x/2;
 cy = y/2;
 SetCursorPos(x/2,y/2);
}
 
void WinMouse::GetRectPos(RECT RectW)
{
 
 RectA.left = RectW.left;
 RectA.right = RectW.right;
 RectA.top = RectW.top;
 RectA.bottom = RectW.bottom;
}
long WinMouse::CalcWindowX()
{
 return this->RectA.right + this->RectA.left;
}
long WinMouse::CalcWindowY()
{
 return this->RectA.top + this->RectA.bottom;
}
 
void WinMouse::ResetPos(long Width, long Height)
{
 this->x = (long)(Width * (float).5);
 this->y = (long)(Height * (float).5);
SetCursorPos( this->x , this->y );
}
void WinMouse::SetPos(long x, long y)
{
SetCursorPos(x, y);
}
void WinMouse::GetPos()
{
GetCursorPos(&this->CP);
}
long WinMouse::GetX()
{
 return this->CP.x;
 
}
long WinMouse::GetY()
{
 return this->CP.y;
 
}
 
//EOF


#4 Rizkay   Members   -  Reputation: 117

Like
0Likes
Like

Posted 27 December 2013 - 10:44 PM

ok i got it.

here i got the things that must be understood before. there are some points to build User Interface in openGL.

  • firstly,  glOrtho help us to command "Orthographic Projection". although we're drawing 3d vertex no matter how far they are (in z coordinate). it will looks like 2D Image. but if we'd like to set our vertex Stay in front we should set z coordinate to 0.

          ok, lets try my code that i've made.

 void DrawImage(void) 
{
	int width = window.width;  //your window width
	int height = window.height;//your window height
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, width, height, 0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// here you will set xy coordinate of vertex on our screen
glBegin(GL_QUADS);
//there are reason why z must be set to 0
glVertex3f(0, 0, 0.0);
glVertex3f(0, 0.5f, 0.0);
glVertex3f(-1.0f, 0.5f, 0.0);
glVertex3f(-1.0f, 0, 0.0);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
  • second, set up our input/output for Keyboard and Mouse. that's why  it be called Graphic User Interface

Edited by Rizkay, 28 December 2013 - 02:02 AM.


#5 JTippetts   Moderators   -  Reputation: 8569

Like
0Likes
Like

Posted 27 December 2013 - 11:04 PM

Note that if you are using an orthographic projection that is windowwidth by windowheight in size, then drawing a quad as small as you are drawing will pretty much just draw a single pixel or two, since the viewport is now windowwidth units wide and windowheight units tall, and you are drawing a quad 1 unit wide and 1/2 unit tall. You need to draw your objects larger in order to see anything useful.


Edited by JTippetts, 27 December 2013 - 11:05 PM.


#6 Rizkay   Members   -  Reputation: 117

Like
0Likes
Like

Posted 28 December 2013 - 04:35 AM

Note that if you are using an orthographic projection that is windowwidth by windowheight in size, then drawing a quad as small as you are drawing will pretty much just draw a single pixel or two, since the viewport is now windowwidth units wide and windowheight units tall, and you are drawing a quad 1 unit wide and 1/2 unit tall. You need to draw your objects larger in order to see anything useful.

of course,, you've remind me.

when we build User Interface i think the best way to set the parameters of glOrtho like:

  • left : 0.0f
  • right: in accordance with our window width
  • top: 0.0f
  • bottom: in accordance with our window height
  • Znear Zfar: 0,1  -> i don't know why is this so match with UI graphic (these values make the plane always on foreground of the vieweport)

there is a reason why do i set the right and the bottom in accordance with our window size. whenever we resize the window Coordinate of glOrtho won't be screwed.

do you remember in desktop application programming we're like drawing on Quadrant 4 (270 - 360 degree) but y coordinate it's just positive value NOT negative. therefore i set it up in accordance with desktop programming, it wold be easy!

 

well, for drawing vertex actually we have to adjust the coordinate also where  the max x/y coordinate is in accordance with our window size. so if we would follow this way you must set your vertex like we're doing graphic programming on deskop programming. lets look at my vertex code:

::glTranslatef(0,0,0); //the position of the element
glBegin(GL_QUADS
//look actully we've created left sidebar
glVertex3f(200, 0, 0); 
glVertex3f(200, height, 0);
glVertex3f(0.0f, height, 0);
glVertex3f(0, 0, 0);
glEnd();

yeahh we've created a sidebar at viewport.

 

you see? x/y coordinate is set to large value (Something unusual). but in this term i've set before, with this command: glOrtho(0, window.width, window.height, 0, 0, 1);

the viewport will be fit to our window. even you resize your window size i think we're just build simple algorithm (something like anchor?) cz the viewport and the window are same!


Edited by Rizkay, 28 December 2013 - 04:47 AM.


#7 Mathimetric   Members   -  Reputation: 133

Like
0Likes
Like

Posted 30 December 2013 - 11:25 AM

Just so you understand that

Mouse coord' system only uses 2 deminsions X,Y

the coord starts from the Top zero and the left zero (going top to  (<, greater) down, and left to  (<, greater) right)

You dont need to use Ortho graphic projection to use a 2d (or 3D) GUI.

 

the default OpenGL matrix is 4 x 4 units,

if you do that math and a little guessing or debugging, it is simple enough to

match your mouse Position checks with your Cursor and buttons.

 

when the screen position or size is changed

 

code:

//inside MainWindowProc  (these are your handles for your window and GUI)

//(windows messege SIZE)

//(windows messege MOVE)

 

// dispatch messages
 switch (uMsg)

{

//...

 

case WM_SIZE:
  height = HIWORD(lParam);  // retrieve width and height
  width = LOWORD(lParam);
// ~(update mouse coords' center "relative")~ //

 

case WM_MOVE:

//~(update new mouse postions relative to new RECT screen position)~//
// Mouse_.GetRectPos(windowRect); //

 

Example thumb PIC (JPEG)

ex1.JPG


Edited by Mathimetric, 30 December 2013 - 11:48 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS