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]