OpenGL Aspect Ratio Issue - opengl

I am using Qt + OpenGl for 2D rendering.
I am a beginner at OpenGL and for the life of me i am not able to figure out this aspect ratio issue. Everytime i think i have understood glOrtho and gViewPort, but very next time i am into another issue with them. While if coordinates are symmetric like between -1 and 1, my code works else it doesn't. I really want to get through these for once and all. All the suggestions i have searched and applied have gone fruitless for me.
My Problem Statement:
I am rendering a square and a triangle and i switch between them with keystroke "R". I am also zooming in and out. While square is maintaining aspect ratio, triangle is not. Coordinates for shapes are:
Square: (-10, -250), (500, -250), (500, -260), (-10, -260);
Triangle: (250, 0), (310, 0), (280, 30)
Basically I am not able to render above triangle. Here is code for same:
My Code
#include <QtGui/QMouseEvent>
#include <qdebug.h>
#include "GLWidget.h"
#include "stdio.h"
#include "qgl.h"
#include "qimage.h"
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
setMouseTracking(true);
}
void GLWidget::initializeGL()
{
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(1, 1, 1, 0);
glEnable( GL_POINT_SMOOTH ); // For Circular Points
}
void GLWidget::resizeGL(int w, int h)
{
canvas_width = (double)w;
canvas_height = (double)h;
aspect_ratio = canvas_width/canvas_height;
left_plane = 250;
right_plane = 310;
bottom_plane = 0;
top_plane = 60;
z_near_plane = 1;
z_far_plane = -1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if( canvas_width > canvas_height ){
glOrtho(left_plane*aspect_ratio, right_plane*aspect_ratio, bottom_plane, top_plane, z_near_plane, z_far_plane);
}else{
glOrtho(left_plane, right_plane, bottom_plane/aspect_ratio, top_plane/aspect_ratio, z_near_plane, z_far_plane);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0); // red
glBegin(GL_POLYGON);
//glVertex2f(-30,0);
//glVertex2f(30,0);
//glVertex2f(0,60);
glVertex2f(250,0);
glVertex2f(310,0);
glVertex2f(280,60);
glEnd();
}
I do not see any traingle because left_plane*aspect_ratio clips my drawing (250 is minimum X, 250*1.4 > 310, 310 is maximum X).
I hope i have made myself clear.
I will try to place images as well (i guess i will have to upload images to some other site and link them here?).

Your problem is that the bounding box (the values assigned to left/right/top/bottom_plane) doesn't have the same aspect ratio as the viewport. If you have the bounding box for the object, you need to find viewport coordinates with the correct aspect ratio (w/h). The viewport needs to be both centered on the bounding box center, and be big enough to fit the bounding box. But the aspect ratio of the viewport has nothing to do with the size of the bounding box.
In general you have a 3D bounding box (8 corner points). You would project each corner onto the screen, then use min/max to get a rectangle that needs to be centered on screen. You then check the aspect ratio ar = wr/hr of that rectangle against the aspect ratio of the viewport a=w/r. If a < ar, you need to fit wr to w, otherwise fit hr to h.
void GLWidget::resizeGL(int w, int h)
{
// First set up the projection.
double canvas_width = (double)w;
double canvas_height = (double)h;
double a = canvas_width / canvas_height;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-a, +a, -1, +1, -1, +1);
// Now set up the view matrix.
double leftBoundingRectangle = 250;
double rightBoundingRectangle = 310;
double bottomBoundingRectangle = 0;
double topBoundingRectangle = 60;
double widthBoundingRectangle = rightBoundingRectangle - leftBoundingRectangle;
double heightBoundingRectangle = topBoundingRectangle - bottomBoundingRectangle;
double ar = widthBoundingRectangle / heightBoundingRectangle;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Center on bounding rectangle center.
double tx = (leftBoundingRectangle + rightBoundingRectangle)/2.0, ty = (topBoundingRectangle + bottomBoundingRectangle)/2.0;
glTranslated(tx, ty, 0.0); // or is it -tx, -ty?
// Scale to fit bounding box.
double s;
if (ar > a)
{
s = ... // sorry, but you have to figure this one out for yourself. :)
}
else
{
s = ...
}
glScaled(s,s,s);
}

Related

How to stop stretching in opengl

I created Lines and when I'm rotate the line. Line will be stretch. How can I stop stretch at rotation time. When I change height in Ortho it will be not displaying properly. When Line is going left or right it will be start strtching but when it will be reach in main point it will come in real position.
#include<fstream>
#include<iostream>
#include<stdlib.h>
#include<glut.h>
using namespace std;
float yr = 0;
void introscreen();
void screen();
void screen1();
void PitchLadder();
int width = 1268;
int height = 720;
float translate = 0.0f;
GLfloat angle = 0.0f;
void display(void) {
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-300, 300, -10, 25, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
static int center_x = 0;
static int center_y = 0;
}
void specialKey(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP:
translate += 1.0f;
break;
case GLUT_KEY_DOWN:
translate -= 1.0f;
break;
case GLUT_KEY_LEFT:
angle += 1.0f;
break;
case GLUT_KEY_RIGHT:
angle -= 1.0f;
break;
}
glutPostRedisplay();
}
void Rolling(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 1, 0);
glPushMatrix();
glRotatef(-angle, 0, 0, 1);
glTranslatef(-10, translate,0);
PitchLadder();
glPopMatrix();
glFlush();
}
void PitchLadder() {
GLfloat y;
GLfloat y2;
GLfloat fSize[5];
GLfloat fCurrSize;
fCurrSize = fSize[2];
for (y2 = -90.0f ; y2 <= 90.0f ; y2 += 10.0f) {
glLineWidth(fCurrSize);
glBegin(GL_LINES);
glVertex3f(-50.0f , y2 , 0);
glVertex3f(50.0f , y2 , 0);
glEnd();
fCurrSize += 1.0f;
screen();
screen1();
}
}
void renderbitmap1(float x3, float y3, void *font1, char *string1) {
char *c1;
glRasterPos2f(x3, y3);
for (c1=string1; *c1 != '\0'; c1++) {
glutBitmapCharacter(font1, *c1);
}
}
void screen(void) {
glColor3f(0, 1, 0);
char buf1[20] = { '\0' };
for (int row1 = -90.0f; row1 <= 90 + yr; row1 +=10.0f) {
sprintf_s(buf1,"%i", row1);
renderbitmap1(70 , (yr+row1), GLUT_BITMAP_TIMES_ROMAN_24, buf1);
}
}
void renderbitmap2(float x4, float y4, void *font2, char *string2) {
char *c1;
glRasterPos2f(x4, y4);
for (c1=string2; *c1 != '\0'; c1++) {
glutBitmapCharacter(font2, *c1);
}
}
void screen1(void) {
glColor3f(0, 1, 0);
char buf1[20] = { '\0' };
for (int row1 = -90.0f; row1 <= 90 + yr; row1 +=10.0f) {
sprintf_s(buf1,"%i", row1);
renderbitmap2(-70 , (yr+row1), GLUT_BITMAP_TIMES_ROMAN_24, buf1);
}
}
int main(int arg, char** argv) {
glutInit(&arg, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutInitWindowPosition(50, 100);
glutCreateWindow("HUD Lines");
display();
glutDisplayFunc(Rolling);
glutSpecialFunc(specialKey);
glutMainLoop();
return 0;
}
At Orthographic Projection, the view space coordinates are linearly mapped to the clip space coordinates respectively normalized device coordinates. The normlaized device space is a cube with a minimum of (-1, -1, -1) and a maximum of (1, 1, 1).
Finally the coordinates in normalized device space are mapped to the rectangular viewport.
If the viewport is rectangular then the aspect ratio has to be considered, when the view space coordinates are transformed to clip space.
The mapping of the normalized device coordinates to the viewport distorted the geometry by the reciprocal aspect ration of the viewport. This distortion has to be compensated by the orthographic projection.
When the orthographic projection is set by glOrtho(left, right, bottom, top, near, far), then the cuboid volume is defined, which maps (left, bottom, near) to (-1, -1, -1) and (right, top, far) to (1, 1, 1).
It is not necessary that the x and y range of the orthographic projection is equal the view port rectangle, bit the ration (left-right)/(top-bottom)hast to be equal the ration of the viewport rectangle else the geometry will be distored.
double size = 200.0f;
double aspect = (double)width / (double)height;
glOrtho(-aspect*size/2.0, aspect*size/2.0, -size/2.0, size/2.0, -1.0, 1.0);
Your window size and orthographic "view" do not have the same aspect ratio:
// This creates a window that's 1268 x 720 (a wide rectangle)
int width = 1268;
int height = 720;
glutInitWindowSize(width, height);
// This creates a "view" that's 300 x 300 (a square)
glOrtho(-300, 300, -10, 25, 0, 1);
The "view" will be stretched to fill the viewport (window). You are seeing a 300 x 300 image being stretched to 1268x720, which definitely makes horizontal lines appear longer than vertical lines even though they're the same length in the code.
You should call glOrtho using the width and height variables of your window:
glOrtho(0, width, 0, height, 0, 1);
Notice that I have changed the arguments to (left = 0, right = width, bottom = 0, top = height, ...). This allows you to work with a screen coordinate space that is similar to 2D rendering but the bottom-left corner is (0,0) and the top-right is (width,height).

Polyline only renders completely after resizing the window

I am making a 3d project in OpenGL which contain a ground (drawn as line loops). The issue I have is when the project starts only a single line is drawn as shown in the next image:
When I resize or maximize the window then the actual ground gets displayed like this:
Any idea how to resolve this issue? I'm a beginner in OpenGL programming.
Here is the code :
void drawHook(void);
void timer(int);
void drawFlorr();
float L = 100;
const int screenWidth = 1000; // width of screen window in pixels
const int screenHeight = 1000; // height of screen window in pixels
float ww = 800;
float wh = 800;
float f = 520, n = 10.0;
static GLdouble ort1[] = { -200, 200, -33, 140 };
static GLdouble viewer[] = { 525, 25, -180 };
static GLdouble objec[] = { 525.0, 25, -350 };
float x, y = 0.0, z, z1;
float xmax = screenWidth - 200.0;
float zmax = screenWidth - 200.0;
float xmin, zmin;
float step = 5.0;
float fov = 80;
void myInit(void)
{
glClearColor(0.0,0.0,0.0,0.0); // background color is white
glPointSize(2.0); // a 'dot' is 2 by 2 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, screenWidth, 0.0, screenHeight);//dino window
glViewport(0, 0, screenWidth, screenHeight);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0], viewer[1], viewer[2], objec[0], objec[1], objec[2], 0, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, 1.333, n, f);
glPointSize(2.0);
glMatrixMode(GL_MODELVIEW);
drawFlorr();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // set display mode
glutInitWindowSize(screenWidth, screenHeight); // set window size
glutInitWindowPosition(10, 10); // set window position on screen
glutCreateWindow("Dino Line Drawing"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
myInit();
//glutTimerFunc(1,timer,1);
glutMainLoop(); // go into a perpetual loop
return 1;
}
void drawFlorr()
{
xmin = -100;
zmin = -100;
for (x = xmin; x < xmax; x += step)
{
for (z = zmin; z < zmax; z += step)
{
z1 = -z;
glBegin(GL_LINE_LOOP);
glVertex3f(x, y, z1);
glVertex3f(x, y, z1-step+1.0);
glVertex3f(x + step - 1.0, y, z1 - step + 1.0);
glVertex3f(x+step-1.0, y, z1);
glEnd();
}
}
}
Your code is broken in many ways:
Your myDisplay function uses whatever the current matrix mode is to set the view matrix on.
Initially, you leave the matrix mode as GL_PROJECTION in myInit()
These two together mean that for the first frame, you just use identity as MODELVIEW matrix, and just overwrite the projection matrix twice. After a resize, the frame ais drawn again, and your code does waht you probably intented to do.
However, there is more:
You do not have any resize handler, so your viewport will not change when you resize the window.
You are setting an ortho matrix initailly for the projection, although you are not planning to use it at all.
and, the most import point:
All of your code depends on deprecated functionality which is not even available in modern OpenGL at all. You should really not use this in 2016, but learn modern OpenGL instead (with "modern" meaning "only a decade old" here).

opengl: Trouble setting up viewport and glortho

I am trying to setup my OpenGL views for some texture rendering. Following some advice on the forum, I set up my viewport and ortho matrix as follows:
First I try to compute the screen width and height that I can use while maintaining the aspect ratio of my image:
void resize(int w, int h)
{
float target_aspect_ratio = image_width / image_height;
width = w;
height = (int)(width / target_aspect_ratio + 0.5f);
if (height > h) {
height = h;
width = (int)(height * target_aspect_ratio + 0.5f);
}
off_x = (w - width)/2.f;
off_y = (h - height)/2;
// I want to center my image. So I have these offsets
glViewport(off_x, off_y, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, 0.0f, 1.0f);
Now when I want to render my texture I do:
void paint()
{
// texture binding etc.
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(width, 0);
glTexCoord2f(1, 1); glVertex2f(width, height);
glTexCoord2f(0, 1); glVertex2f(0, height);
}
However, this does not show the image as expected. It does not maintain the aspect ratio as I size the screen. It is almost like the glViewport has no effect and I can verify this function gets called every time my window is resized.
Update:
It is strange. Almost as if these calls have no effect. I even did something as:
_off_x = _off_y = 0;
_width = 500;
_height = 500;
So I expected the viewport to be lower left box of my screen but the image is being drawn as before basically using the whole screen as the viewport.
Update 2:
Ok, so if I call
glViewport(_off_x, _off_y, _width, _height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, _width, 0, _height, 0, 1);
in my paint events, it works as expected! However, I thought it was enough to put this in the resize event handler.
Before start drawing, you need to switch your Matrix mode to GL_MODELVIEW. You don't need to set your projection matrix inside your render function at each frame.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Here is a detailed analysis that I wrote about glMatrixMode() function modes :
OpenGL glMatrixMode(GL_PROJECTION) vs glMatrixMode(GL_MODELVIEW)

Keeping aspect ratio when resizing in OpenGL

I implemented the following code:
void TestGlPlot::resizeGL(int width, int height)
{
setupViewport(width, height);
}
void TestGlPlot::setupViewport(int width, int height)
{
/* Prevent divide by zero --------------------------------------------------------------------*/
if (height == 0) height = 1;
/* Calculate aspect ratio --------------------------------------------------------------------*/
float aspectRatio = (float)width / (float)height;
/* Set viewport to cover the window ----------------------------------------------------------*/
glViewport(0, 0, width, height);
/* Set aspect ratio --------------------------------------------------------------------------*/
glMatrixMode(GL_PROJECTION); /* switch to projection matrix */
glLoadIdentity();
/*
if (width >= height)
{
gluOrtho2D(-0.5*aspectRatio, 0.5*aspectRatio, 0.0, 1.0);
}
else
{
gluOrtho2D(-0.5, 0.5, 0.0*aspectRatio, 1.0*aspectRatio);
}
glMatrixMode(GL_MODELVIEW);
*/
gluOrtho2D(-1, 1, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void TestGlPlot::paintEvent(QPaintEvent *event) {
makeCurrent();
setupViewport(width(), height());
glMatrixMode(GL_MODELVIEW);
/* Set white background ----------------------------------------------------------------------*/
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(255,255,255,0);
/* Paint OpenGL events -----------------------------------------------------------------------*/
glColor4f(1.0, 0.0, 0.0, 1.0);
/* light grey */
glColor4f(0.0, 0.0, 0.0, 0.3);
gluPartialDisk(plainQuad, 0.1, 1, 20, 4, -60, 120);
/* Paint QPainter events ---------------------------------------------------------------------*/
QPainter painter(this);
/* Draw grey border around plot --------------------------------------------------------------*/
painter.setPen(QColor("#808080"));
painter.drawRect(0, 0, width()-1, height()-1);
painter.setFont(font);
/* Translate coordinate system to (0,0) center -----------------------------------------------*/
QMatrix m;
m.translate(width()*0.5, height()*0.5);
painter.setMatrix(m);
/* Left side descriptions for radius ---------------------------------------------------------*/
painter.drawText(-0.17*width(), 0.38*height(), tr("100m"));
painter.drawText(-0.27*width(), 0.28*height(), tr("200m"));
painter.drawText(-0.37*width(), 0.18*height(), tr("300m"));
painter.drawText(-0.47*width(), 0.08*height(), tr("400m"));
painter.drawText(0.45*width(), -0.01*height(), tr("60°"));
painter.drawText(0.26*width(), -0.38*height(), tr("30°"));
painter.drawText(-0.47*width(), -0.01*height(), tr("300°"));
painter.drawText(-0.28*width(), -0.38*height(), tr("330°"));
painter.end();
}
i tried different methods for resize handling (keeping the shape of the partialDisk object without stretching it) but every method failed. I also want to keep the coordinate handling of the unit-circle (so i can normalize my measurements and draw them into a polar-plot).
To keep the aspect ratio, you have several options in general:
Always scale to one dimension. For example, you just define that you want always see the horizontal range [-0.5,0.5]. In this case, you have to correct the vertical range by the factor (1/aspect_viewport).
Use some equivalent of letterboxing. So you define a "region of interest" which you always want to see completely, but you might see more in width or height, depending on the window aspect (that would be basically equivalent to the black bars when watching letterboxed movies). There are two cases to consider: the aspect of the viewport is greater than the aspect of your region, so it is wider. In that case, you should map the full height and increase the horitonal range by a factor of (aspect_viewport/aspect_region). Otherwise, the aspect of the window is lower than aspect of your region, so you should use the full width and scale up the vertical range by (aspect_region/aspect_viewport). Note that in both cases, the factor is >= 1.
In your code, you almost have implemented method 2, but you got the aspectRatio < 1 case wrong, it should be
if (width >= height)
gluOrtho2D(-0.5f*aspectRatio, 0.5f*aspectRatio, 0.0f, 1.0f);
else
gluOrtho2D(-0.5f, 0.5f, 0.0, 1.0/aspectRatio);

How do you adjust an opengl viewport size without scaling its contents

When my window is resized, i don't want the contents to scale but just to increase the view port size. I found this while searching on stackoverflow (http://stackoverflow.com/questions/5894866/resize-viewport-crop-scene) which is pretty much the same as my problem. However I'm confused as to what to set the Zoom to and where, i tried it with 1.0f but then nothing was shown at all :s
This is resize function code at the moment which does scaling:
void GLRenderer::resize() {
RECT rect;
int width, height;
GLfloat aspect;
GetClientRect(hWnd, &rect);
width = rect.right;
height = rect.bottom;
if (height == 0) {
height = 1;
}
aspect = (GLfloat) width / height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, aspect, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
And my function to render a simple triangle:
void GLRenderer::render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslated(0, 0, -20);
glBegin(GL_TRIANGLES);
glColor3d(1, 0, 0);
glVertex3d(0, 1, 0);
glVertex3d(1, -1, 0);
glVertex3d(-1, -1, 0);
glEnd();
SwapBuffers(hDC);
}
You can change the zoom in y (height) with the "field of view" parameter to gluPerspective. The one that is 45 degrees in your code. As it is currently always 45 degrees, you will always get the same view angle (in y). How to change this value as a function of the height of the window is not obvious. A linear relation would fail for big values (180 degrees and up). I would try to use arctan(height/k), where 'k' is something like 500.
Notice also that when you extend the window in x, you will already get what you want (the way your source code currently is). That is, you get a wider field of view. That is because you change the aspect (second argument) to a value depending on the ratio between x and y.
Height and Width is measured in pixels, so a value of 1 is not good.
Notice that you are using deprecated legacy OpenGL. See Legacy OpenGL for more information.