Is there something wrong with this circle class?

here is the header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<glut.h> 
#include"shape.h" 

#include<cmath>

class circle: public shape
{
public:
	circle(float radius, float ix, float iy)
		:shape(ix, iy)
	{		
		radius = r;
	}	
	void show();
private:
	float r;
};


and the cpp file :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<glut.h> 
#include"circle.h"
#include<cmath>

void circle::show(){
	shape::show();
	glBegin(GL_LINE_LOOP);
	float x, y;
	x = 0;
	y = 0;
	const int NPOINTS=20;
	const double TWOPI = 2*3.14159268;
	double STEP = TWOPI/20;
	for(float ang=0; ang<TWOPI; ang+=STEP)
		glVertex2f(x+r*cos(ang), y+r*sin(ang));
 glEnd();
}


and the header file it inherits from

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef SHAPE_H 
#define SHAPE_H 
#include<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: //  note this keyword. 
float x, y; 
float col[3]; 
}; 
#endif 


The code runs with no errors but the circle is not drawn, im not sure why im just getting into this, any help again , appreciated guys :)
You haven't posted enough of your code. If you're problem is with rendering the image then you need to show us what Drawing Context you are using. For example Windows? SFML? SDL? What is providing the surface you are drawing to?
Sorry heres the rest of my code with my main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<glut.h> 
#include"shape.h" 
#include"triangle.h"
#include"circle.h"
#include<cmath>



	circle c1(0.4, 0.3, 0.3);
	triangle t(0, 0.3, 0.5, 0.4);

	void display()
	{ 
		glClear(GL_COLOR_BUFFER_BIT); 
		c1.setColour(0, 0, 1);
		c1.move(0.0001, 0.0001);
		c1.show();
		t.setColour(0, 0, 1); 
		t.move(0.0001, -0.0002); 
		t.show(); 
		glFlush(); 
	} 

	int main(int argc, char **argv) 
	{ 
		glutInit(&argc, argv); 
		glutCreateWindow("Simple Shapes"); 
		glClearColor(1, 0, 0, 0.5); 
		glutDisplayFunc(display); 
		glutIdleFunc(display); 
		glutMainLoop(); 
		return 0; 
	}
You can't use OpenGL straight like that*, you need something to provide what is called a Rendering Context. I suggest SFML, it is pretty simple and easy enough to get the basics down. It even provides a tutorial on how to get started using OpenGL with it: http://www.sfml-dev.org/tutorials/1.6/
NOTE: I linked you to the 1.6 tuts because 2.0 isn't official yet.


*: Remember that OpenGL is a standard to interface with hardware, it's not really a stand alone library like so many noobs try to say it is to sound smart.
Here is some code that I wrote I don't even know how long ago that shows some basic playing around with SFML and OpenGL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <GL/glut.h>

#include <SFML/Window.hpp>

sf::Window App; //App Is Only Global For Simplicity, This Is Bad Practice

float Angle = 0.0f;

float XAxis = 0.0f;
float YAxis = 0.0f;
float ZAxis = 0.0f;

void Display()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    //glLoadIdentity();
    
    /* glBegin(GL_POLYGON);
        glVertex2f(-0.0f, -0.0f);
        glVertex2f(0.5f, -0.0f);
        glVertex2f(0.5f, 0.5f);
        glVertex2f(-0.0f, 0.5f);
    glEnd(); */
    
    glBegin(GL_POLYGON);
        //FACE 1
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(0.0f, 0.0f, 0.0f);
        glVertex3f(0.0f, 0.5f, 0.0f);
        glVertex3f(0.5f, 0.5f, 0.0f);
        glVertex3f(0.5f, 0.0f, 0.0f);
        
        //FACE 2
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(0.0f, 0.0f, 0.0f);
        glVertex3f(0.0f, 0.0f, 0.5f);
        glVertex3f(0.0f, 0.5f, 0.5f);
        glVertex3f(0.0f, 0.5f, 0.0f);
        
        //FACE 3
        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(0.0f, 0.0f, 0.0f);
        glVertex3f(0.5f, 0.0f, 0.5f);
        glVertex3f(0.0f, 0.5f, 0.0f);
        
        
    glEnd();
    
    
    glRotatef(Angle, XAxis, YAxis, ZAxis);
    //glTranslatef(XAxis, YAxis, ZAxis);
    
    /* glColor3f(0.0f, 1.0f, 0.0f);
    glBegin(GL_POLYGON);
        glVertex2f(0.0f, 0.0f);
        glVertex2f(-0.5f, 0.0f);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(0.0f, -0.5f);
    glEnd(); */
        
    glFlush();
    App.Display();
}



void Events(sf::Event Event)
{
    const sf::Input& Input = App.GetInput();
    
    switch(Event.Type)
    {
        case sf::Event::Closed: App.Close(); return;
        case sf::Event::Resized: glViewport(0, 0, Event.Size.Width, Event.Size.Height); break;
    }
    
    switch(Event.Key.Code)
    {
        case sf::Key::Escape: App.Close(); return;
        
        case sf::Key::R: glLoadIdentity(); return;
    }
    
    if(Input.IsKeyDown(sf::Key::Left))
    {
        YAxis = -1.0f;
        Angle = 5.0f;
    }
    if(Input.IsKeyDown(sf::Key::Right))
    {
        YAxis = 1.0f;
        Angle = 5.0f;
    }
    if(Input.IsKeyDown(sf::Key::Up))
    {
        ZAxis = -1.0f;
        Angle = 5.0f;
    }
    if(Input.IsKeyDown(sf::Key::Down))
    {
        ZAxis = 1.0f;
        Angle = 5.0f;
    }
    
    if(Input.IsKeyDown(sf::Key::Num7))
    {
        XAxis = 1.0f;
        Angle = 5.0f;
    }
    
    if(!Input.IsKeyDown(sf::Key::Left) && !Input.IsKeyDown(sf::Key::Right) && !Input.IsKeyDown(sf::Key::Up) && !Input.IsKeyDown(sf::Key::Down))
    {
        ZAxis = 0.0f;
        XAxis = 0.0f;
        YAxis = 0.0f;
        Angle = 0.0f;   
    }
    
    
}



int main(int argc, char *argv[])
{
    sf::WindowSettings Settings;
        Settings.DepthBits = 24;
        Settings.StencilBits = 8;
        Settings.AntialiasingLevel = 2;
    
    //sf::Window App(sf::VideoMode(400, 300, 32), "SFML & OpenGL", sf::Style::Close, Settings);
    App.Create(sf::VideoMode(400, 300, 32), "SFML&OpenGL", sf::Style::Close, Settings);
        App.SetFramerateLimit(60);
        App.SetActive();
        
    sf::Event Event;
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    
    while(App.IsOpened())
    {
        while(App.GetEvent(Event))
        {
            Events(Event);
        }
        
        Display();
    }
    
    
    return EXIT_SUCCESS;
}


Use the arrows to rotate the random non-Euclidian shape on the screen. Play around with the colors and stuff. If you have any questions I'll try to check back tomorrow to answer them.

EDIT: Please notr that this code is written in C-style. You should focus your program design on integrating the global variables into an object to pass around as needed.
Last edited on
just one more quick question though i have a triangle in the exact same programme and i manage too get that too draw fine with the correct colour and moving with how i tell it ? just the circle one wont work?

Header for the triangle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<glut.h>  

class triangle: public shape 
{ 
public: 
 triangle(float ix, float iy, float iwidth, float iheight) 
          :shape(ix, iy) 
 { 
  width = iwidth;  height = iheight; 
 }
 void show(); // Defined below. 
private: 
float width, height; 
}; 


And the cpp for the triangle

1
2
3
4
5
6
7
8
9
10
11
12
#include<glut.h> 
#include"shape.h"
#include"triangle.h"

void triangle::show(){ 
 shape::show(); 
 glBegin(GL_LINE_LOOP); 
  glVertex2f(x, y); 
  glVertex2f(x+width, y); 
  glVertex2f(x+width/2, y+height); 
 glEnd(); 
} 


And that is all the code i currently have in my programme at the moment
still cant get it ?
1
2
3
4
5
6
public:
	circle(float radius, float ix, float iy)
		:shape(ix, iy)
	{		
		radius = r; /// should this be r = radius?
	}	

I'm not 100% sure too be honest , I am just new too this and it was a lab in university for the triangle class and it Walked me through that one but I thought id try a circle one too, and tried too do it in a similar format too the triangle one. So it may be completely wrong lol
Circle is displayed if you make the change above. (It was displayed before but since r is zero the radius was zero so you can't see it!)

The circle will not move unless you do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
void circle::show(){
	shape::show();
	glBegin(GL_LINE_LOOP);
	//float x, y;
	//x = 0;
	//y = 0;
	const int NPOINTS=20;
	const double TWOPI = 2*3.14159268;
	double STEP = TWOPI/20;
	for(float ang=0; ang<TWOPI; ang+=STEP)
		glVertex2f(x+r*cos(ang), y+r*sin(ang));
 glEnd();
}
Last edited on
Topic archived. No new replies allowed.