Window resize, image not showing

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 :)[QUOTE][/QUOTE]

You’re most certainly looking for glutPostRedisplay. 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();

}


Your using old code, deprecated.

Check learnopengl.comand start learning.

[QUOTE=Pedrosanchez87;1285976]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();

}


[/QUOTE]

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

[QUOTE=Pedrosanchez87;1285976]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();

}

[/QUOTE]

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.

[QUOTE=paul_g_griffiths;1285989]Your using old code, deprecated.

Check learnopengl.comand start learning.[/QUOTE]

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

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 is a set of glut examples.

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