Sign in to follow this  
Rizky Aulia Rahman

OpenGL Build UI in OpenGL for my 3D game world?

Recommended Posts

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

Share this post


Link to post
Share on other sites
JTippetts    12970
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.

Share this post


Link to post
Share on other sites
Mathimetric    133

*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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
JTippetts    12970

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Mathimetric    133

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)

[attachment=19265:ex1.JPG]

Edited by Mathimetric

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now