How to increase depth of window in OpenGL? - opengl

In the assignment, I'm working on, I have to create a huge box in an OpenGL window and be able to rotate it.
A window is being created by this code:
window = glfwCreateWindow(1000, 1000, "Window for Box", NULL, NULL);
This first 1000 in the above line corresponds to the width of the window where the second 1000 window corresponds to the height of the window.
Now, when I open the lid of the box I've created and rotated it, it looks like this:
The yellow coloured square is the lid and it is opened. You can see that it doesn't look like a box as the depth of the window is less and the box is cut with a white surface. How can I increase the window size to display box perfectly?

The window is 2D, it has no depth. What you're looking for is a projection matrix. If you have used glOrtho(), the last parameter defines how far from the camera a fragment can be, to still be visible. If you have defined your own projection matrix set the zFar value to be greater than the z coordinates of all your vertices.
Alternatively you can use depth clamping: https://www.khronos.org/opengl/wiki/Vertex_Post-Processing#Depth_clamping

Related

Place Gtkmm Window at the center of one display when multiple screens are present

I have multiscreen ubuntu machine which has screens arranged as follows
I need to place one Gtkmm window at the center of Screen 3.
For that, I calculate the center of the screen location and move the window in that location.
// let window size may (w,h) and Screen size may (W,H)
// let (X,Y) be the output
X = 0 + W/2 - w/2;
Y = 2160 + H/2 - h/2;
Here the window size (w,h) is predefined, sometimes the screen grows bigger than (w,h) because of bigger content. At that time window won't be at the center since we calculated with (w,h) and screen size is changed to (w', h') now.
I tried to get the updated size of the window by
this->signal_size_allocate().connect(sigc::mem_fun(*this, &BaseWindow::windowShown));
But the window is shown at the wrong location and moved to the correct location immediately. This won't be an issue but sometimes there are a lot of window updates and looks like the window is moving back and forth.
So I would like to know what is the correct way to put a window at the center of a specified location (here screen 3) in Gtkmm way so that even though the window grows bigger, it should be at the center.

c++ win32 Relative position to desktop

How will i get a application x y position relative to the client screen?
I tried but all unsucessful so can anyone help.
RECT pta;
GetWindowRect(hWnd,&pta);
POINT Rpt = { pta.left, pta.top };
ScreenToClient(hWnd, &Rpt);
But this doesn't work.
I want to set my cursor position to middle in the window of my app
If I understand right, you want to call the SetCursorPos() windows API call to center the mouse cursor to your window. That function takes screen coordinates.
GetWindowRect() returns the window top and left coordinates already in the screen coordinates, so no transform is necessary.
To get to your window's center coordinates, you just need to add half of your window's width and height to the top-left point's coordinates. Then you can call SetCursorPos().

Cut a window with a triangle shape from top most right corner

We have two windows out of which one window is a small triangular shaped window used to display with a certain size in triangular format on top most right corner of desktop.
The 2nd window is a full screen mode window (Maximized window).
So, my requirement says that the 2nd window should be displayed in full screen mode such that it does not hide the 1st triangular window. I have the liberty to cut the full screen window size up to the size of triangular window.
PS: I can not use the TOP_MOST window property to always display triangular window on top of screen.
Attached: An expected window layout:

QGraphicsScene image pos pixmap

I am using Qt Graphics Framework for displaying an image. I have opened a raw image in subclassed QGraphicsScene in QGraphicsView using addPixmap(). I have added zoom feature by using scale function and drag mode is set as scroll hand drag. Now, I need to get the pixel coordinates within the scene on mouse hover such that the x and y value show the pixel in the image (drawn by pixmap) the mouse is currently pointing to. I tried using pos() but it didn't work.
Here is the code from Widget.cpp:
img = openImage(dirPath2.toLocal8Bit().data(),
sizeX,sizeY,file_heade,scan_heade,bpp,sign);
QPixmap x = QPixmap(sizeX,sizeY);
x.convertFromImage(img,Qt::AutoColor);
scene->addPixmap(x);
ui->disp_img->setDragMode(QGraphicsView::ScrollHandDrag);
GraphicsScene.h:
class GraphicsScene : public QGraphicsScene {
public:
GraphicsScene(QWidget *parent) : QGraphicsScene(parent){}
};
(preferably the pixmap coordinates but even that doesn't happen and if the values change when zoomed I will use scale factor to get the original values)
I suggest you start by reading about Qt's Graphics Coordinate System.
There are various layers of coordinate systems and you need to think about those with which you dealing with. At the top layer is the screen (or view), which is where the mouse coordinates reside.
The next layer from the view is the graphics scene. Graphics items, such as the QGraphicsPixmapItem which you added with addPixmap, reside here. The graphics scene can be visualised as a world of items, each with there own position and orientation.
Moving to the last coordinate system is an item's local coordinate system. If, for example, we take a rectangle, it may have local coordinates of (-5, -5, 10, 10) for (x, y, w, h). This item is then placed in the scene at some position. If its position is the origin of the scene (0,0), then the item's local coordinates would read the same as its scene coordinates.
However, if we move the rectangle +5 units in x-axis, its local coordinates are the same, but its scene coordinates would now be (0, -5, 10, 10).
Likewise, the view (QGraphicsScene) is a window into the scene and can be looking at the whole scene, or just part of it. As the view's top left coordinate is (0,0), it may map onto (0,0) of the scene, or may not, depending on what area of the scene the view is looking at.
So, by getting a mouse position you're starting in the view's coordinates and need to convert to the scene's coordinate system. Fortunately, Qt provides lots of useful functions for this at every level.
To convert the mouse coordinates from the view to the scene, you can use the view's mapToScene function.
Using the scene coordinates you can then get an item and map that to the local coordinate's of the item with the item's mapFromScene.

How to resize the OpenGL view larger than the size passed to the initial SDL_SetVideoMode?

I use OpenGL with the SDL and I want to keep the scene adjusted to the window when the latter is resized. For an unknown reason the scene is cropped at the initial window size when the window becomes larger.
The function used to adjust the view in response to a SDL_VIDEORESIZE is the following, where m_scene_{width,height} is the size of the view as seen by the game.
void screen::resize_view( width, height )
{
// t will force the scene to be rendered with the top left corner matching the
// one of the window.
int t;
if ( height < m_size.y )
t = m_size.y - height;
else
t = 0;
glViewport( 0, t, width, height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0, m_scene_width, 0, m_scene_height, -1, 0 );
glMatrixMode(GL_MODELVIEW);
}
To initialize the window, I call
SDL_SetVideoMode( m_scene_width, m_scene_height 32, SDL_OPENGL | SDL_RESIZABLE );
resize_view( m_scene_width, m_scene_height );
With this code, I have an initial window whose size matches the size of the view. Then if I reduce the size of the window on one or both dimension, the scene is effectively stretched to fit the window, with the top-left corner staying fixed.
But, if I enlarge the window on one or both dimensions, the scene becomes cropped on the right and the bottom as soon as the window become larger than the initial size passed to SDL_SetVideoMode(). Even if the content of the scene is effectively stretched to the expected size, these parts of the screen remain black.
Note that if I set glClearColor() to red, these parts are still black.
The only solution I have now is to call SDL_SetVideoMode() with the new size each time the window is resized, which is not acceptable since it invalidates the GL context on Windows and invalidates all the textures.
Edit: After some experiments I think that the black area is the result of the SDL_Surface initially created, that does not follow the size of the window. Thus, if I can resize this surface without destroying the GL context, I suppose it would solve my problem.
It's a newbie mistake, so common that I write it in about every 6th answer: The code you have in resize_view is code setting drawing state and hence belongs in the display/drawing function where you draw your scene as initial drawing setup. Use SDL_GetWindowSize to retrieve the window size and set the viewport and projection just before drawing.
Keep in mind, that this is the only reasonable way to do it, because later on, you very likely may want to add some HUD, or a minimap or some UI controls and when those are added you must re-set the viewport and projection several times throughout rendering a single frame. So all the turorials setting the viewport and projection in some window resize handler effectively set up sort of a "mental roadblock", which gets people struggling with implementing such things. Your problems with the viewport not following the window size is similar.