PDA

View Full Version : Window resize, image not showing



Pedrosanchez87
02-16-2017, 01:32 PM
I have only recently started to learn openGL. But i am trying to create a window which displays a circle, which can then be resized. I can get the circle to display fine, until i put in the code to resize the window.

So this is the code i have

main.cpp




#include<gl/glut.h>
#include"shape.h"
#include<cmath>

using namespace std;
float width, height;
float aspect;

class Circle : public Shape
{
public:
Circle(float X, float Y, float Radius);


void show();
private:
float x, y, radius;
};

Circle::Circle(float ix, float iy, float iRadius) :Shape(ix, iy)
{
radius = iRadius;
};

void Circle::show() {
glBegin(GL_LINE_LOOP);
const int NPOINTS = 25;
const float TWOPI = 2 * 3.1415927;
const float STEP = TWOPI / NPOINTS;
for (float angle = 0; angle<TWOPI; angle += STEP)
glVertex2f(x + radius*cos(angle), y + radius*sin(angle));
glEnd();
};

Circle C(0.1, 0.5, 0.6);



void display()
{
glClear(GL_COLOR_BUFFER_BIT);
C.setColour(1, 0, 0);

C.show();
glFlush();
}

void reshape(int x, int y)
{
width = x;
height = x / aspect;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);

}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
width = 900;
height = 900;
aspect = width / height;
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glutCreateWindow("Simple Shapes");
glClearColor(0.9, 0.3, 0,0);
glutDisplayFunc(display);


glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

shape.h


#pragma once
#ifndef SHAPE_H
#define SHAPE_H
#include<gl/glut.h>
class Shape
{
public:
Shape(float ix, float iy) { x = ix; y = iy; }
virtual void show() { glColor3fv(col); }
void setColour(float red, float green, float blue) {
col[0] = red; col[1] = green; col[2] = blue;
}
void move(float dx, float dy) { x += dx; y += dy; }
protected:
float x, y;
float col[3];
};
#endif


So what happens is, If i dont use the reshape function the circle is drawn, but when i include the reshape function the circle is not drawn, and i cannot figure out why.
If anyone has any ideas it would be appreciated :)

Silence
02-17-2017, 01:46 AM
You're most certainly looking for glutPostRedisplay (https://www.opengl.org/resources/libraries/glut/spec3/node20.html). Call it at the end of your reshape function.

Pedrosanchez87
02-17-2017, 09:06 AM
You're most certainly looking for glutPostRedisplay (https://www.opengl.org/resources/libraries/glut/spec3/node20.html). Call it at the end of your reshape function.


Like this?




void reshape(int x, int y)
{
width = x;
height = x / aspect;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glutPostRedisplay();

}

paul_g_griffiths
02-18-2017, 06:47 PM
Your using old code, deprecated.

Check learnopengl.com (learnopengl.com)and start learning.

Silence
02-19-2017, 12:48 AM
Like this?




void reshape(int x, int y)
{
width = x;
height = x / aspect;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glutPostRedisplay();

}



Yes, this should do the thing. If not, tell us in what way.

Dark Photon
02-19-2017, 06:16 AM
Like this?

void reshape(int x, int y)
{
width = x;
height = x / aspect;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glutPostRedisplay();

}


Yes.


Your using old code, deprecated.

That's not really helpful. It's just fine for folks to use the compatibility APIs (especially when just getting started). That makes it easier to get going. However, they need to realize they are more likely to fall into slow paths in the driver.

Pedrosanchez87
02-19-2017, 07:39 PM
Your using old code, deprecated.

Check learnopengl.com (learnopengl.com)and start learning.

mm im learning it in uni. So hearing that isnt very reassuring :D

Pedrosanchez87
02-19-2017, 07:39 PM
Yes, this should do the thing. If not, tell us in what way.

I seem to just be having the same problem as the original problem. The circle is still not displaying, but when i remove the redisplay function it does show

Silence
02-19-2017, 11:07 PM
I seem to just be having the same problem as the original problem. The circle is still not displaying, but when i remove the redisplay function it does show

There's also something wrong with your aspect. You calculate it once for all when creating the window. Then when the window gets resized, one would expect this ratio to change. Always calculate the ratio from the new window size (aspact = (float)width/height) in the resize.

You might also consider to use glutPostRedisplay at the end of your display function.

Here (https://www.opengl.org/archives/resources/code/samples/glut_examples/examples/examples.html) is a set of glut examples.

Silence
02-19-2017, 11:09 PM
mm im learning it in uni. So hearing that isnt very reassuring :D

Well, and as Dark Photon said, that's not that bad. It is even very good to start.