Hi i was given an assignment that ask me to create a simple object viewer.
These are the requirements:
1. Display simple objects such as cubes, cylinders, pyramids, etc.
2. Objects should be able to rotate around all 3 dimensions.
3. There should be a parent object class from which all other specific
object classes derive.
4. The parent object class should keep track of and handle changes to all
generic attributes such as position, orientation, rate of rotation, etc.
5. Each face of each object should use a different colour.
6. There should be multiple instantiation of objects from the same class.
7. Data abstraction should be applied; for example, instead of keeping
position data as 3 separate entities x, y, z, using a struct makes more
sense.
8. There should be a linked list linking all the objects.
9. The linked list should be traversed, and for each object, the draw()
function called. The draw() function should do the following:
9.1 move the object to its position
9.2 rotate the object to its current orientation
9.3 call a pure virtual function render() that is implemented at
the child class to draw the object
So far i did the classes and the part where openGL is needed.
Okay, so these are my codes. I am currently stuck on how to use the linked list to link all the objects
My main.cpp:
#include<iostream>
#include "freeglut.h"
#include "main_class.h"
#include "cube.h"
#include "pyramid.h"
#include "cuboid.h"
using namespace std;
int speed;
void init (void) {
glClearColor (0.0, 0.0, 0.0, 0.0); // set background colour to black
glEnable (GL_CULL_FACE);
glShadeModel (GL_FLAT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (50.0, 1.45, 1.0, 1000.0);
glMatrixMode (GL_MODELVIEW);
gluLookAt (100.0, 100.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void rescale (GLsizei w, GLsizei h) {
glViewport (0, 0, w, h);
}
void simpleAxes (void) {
glBegin (GL_LINES);
glColor3f (1.0, 0.0, 0.0);
glVertex3f (-100.0, 0.0, 0.0);
glVertex3f (100.0, 0.0, 0.0);
glColor3f (0.0, 1.0, 0.0);
glVertex3f (0.0, -100.0, 0.0);
glVertex3f (0.0, 100.0, 0.0);
glColor3f (0.0, 0.0, 1.0);
glVertex3f (0.0, 0.0, -100.0);
glVertex3f (0.0, 0.0, 100.0);
glEnd ();
}
void MyDisplay (void) {
cube box;
pyramid tri;
cuboid rect;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
simpleAxes ();
//pyramid
glPushMatrix();
glRotatef(speed, 0, 1, 0);
glTranslatef(-10.0, 0.0, 5.0);
tri.render();
speed ++;
glPopMatrix();
//cube
glPushMatrix();
glTranslatef(30.0, 10.0, 0.0);
glRotatef(speed, 0, 1, 0);
box.render ();
speed ++;
glPopMatrix();
//cuboid
glPushMatrix();
glTranslatef(-40.0, 10.0, 0.0);
glRotatef(speed, 0, 1, 0);
rect.render();
speed++;
glPopMatrix();
glutSwapBuffers ();
glutPostRedisplay ();
}
void main (int argc, char * argv[]) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (800, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow ("My First OpenGL Window");
glutDisplayFunc (MyDisplay);
glutReshapeFunc (rescale); // to be called when window size has changed
init ();
glutMainLoop ();
}
My cube.h:
class cube : public shape
{
public:
void render(void);
};
void cube :: render (void)
{
glBegin (GL_QUADS);
//red
glColor3f (1.0, 0.0, 0.0);
glVertex3f (-10.0, 10.0, 10.0);
glVertex3f (-10.0, -10.0, 10.0);
glVertex3f (10.0, -10.0, 10.0);
glVertex3f (10.0, 10.0, 10.0);
//green
glColor3f (0.0, 1.0, 0.0);
glVertex3f (10.0, 10.0,10.0);
glVertex3f (10.0, -10.0, 10.0);
glVertex3f (10.0, -10.0, -10.0);
glVertex3f (10.0, 10.0, -10.0);
//blue
glColor3f (0.0, 0.0, 1.0);
glVertex3f (-10.0, 10.0, 10.0);
glVertex3f (10.0, 10.0, 10.0);
glVertex3f (10.0, 10.0, -10.0);
glVertex3f (-10.0, 10.0, -10.0);
//yellow
glColor3f (1.0, 1.0, 0.0);
glVertex3f (-10.0, 10.0, -10.0);
glVertex3f (10.0, 10.0, -10.0);
glVertex3f (10.0, -10.0, -10.0);
glVertex3f (-10.0, -10.0, -10.0);
glColor3f (0.0, 1.0, 1.0);
glVertex3f (-10.0, -10.0, -10.0);
glVertex3f (10.0, -10.0, -10.0);
glVertex3f (10.0, -10.0, 10.0);
glVertex3f (-10.0, -10.0, 10.0);
glColor3f (1.0, 0.0, 1.0);
glVertex3f (-10.0, 10.0, 10.0);
glVertex3f (-10.0, 10.0, -10.0);
glVertex3f (-10.0, -10.0, -10.0);
glVertex3f (-10.0, -10.0, 10.0);
glEnd ();
}
My cuboid.h:
class cuboid : public shape
{
public:
void render(void);
};
void cuboid::render(void)
{
glBegin (GL_QUADS);
//red
glColor3f (1.0, 0.0, 0.0);
glVertex3f (-10.0, 10.0, 20.0);
glVertex3f (-10.0, -10.0, 20.0);
glVertex3f (10.0, -10.0, 20.0);
glVertex3f (10.0, 10.0, 20.0);
//green
glColor3f (0.0, 1.0, 0.0);
glVertex3f (10.0, 10.0,20.0);
glVertex3f (10.0, -10.0, 20.0);
glVertex3f (10.0, -10.0, -10.0);
glVertex3f (10.0, 10.0, -10.0);
//blue
glColor3f (0.0, 0.0, 1.0);
glVertex3f (-10.0, 10.0, 20.0);
glVertex3f (10.0, 10.0, 20.0);
glVertex3f (10.0, 10.0, -10.0);
glVertex3f (-10.0, 10.0, -10.0);
//yellow
glColor3f (1.0, 1.0, 0.0);
glVertex3f (-10.0, 10.0, -10.0);
glVertex3f (10.0, 10.0, -10.0);
glVertex3f (10.0, -10.0, -10.0);
glVertex3f (-10.0, -10.0, -10.0);
//cyan
glColor3f (0.0, 1.0, 1.0);
glVertex3f (-10.0, -10.0, -10.0);
glVertex3f (10.0, -10.0, -10.0);
glVertex3f (10.0, -10.0, 20.0);
glVertex3f (-10.0, -10.0, 20.0);
//purple
glColor3f (1.0, 0.0, 1.0);
glVertex3f (-10.0, 10.0, 20.0);
glVertex3f (-10.0, 10.0, -10.0);
glVertex3f (-10.0, -10.0, -10.0);
glVertex3f (-10.0, -10.0, 20.0);
glEnd ();
}
My pyramid.h:
class pyramid : public shape
{
public:
void render(void);
};
void pyramid::render(void)
{
glBegin (GL_TRIANGLES);
//red
glColor3f (1.0, 0.0, 0.0);
glVertex3f (5.0, 0.0, 0.0);
glVertex3f(15.0, 0.0, 0.0);
glVertex3f(10.0, 10.0, -5.0);
//green
glColor3f(0.0, 1.0, 0.0);
glVertex3f(10.0, 10.0, -5.0);
glVertex3f (15.0, 0.0, 0.0);
glVertex3f(15.0, 0.0, -10.0);
//blue
glColor3f(0.0, 0.0, 1.0);
glVertex3f(10.0, 10.0, -5.0);
glVertex3f(15.0, 0.0, -10.0);
glVertex3f(5.0, 0.0, -10.0);
//yellow
glColor3f(1.0, 1.0, 0.0);
glVertex3f(10.0, 10.0, -5.0);
glVertex3f(5.0, 0.0, -10.0);
glVertex3f(5.0, 0.0, 0.0);
glEnd ();
glBegin(GL_QUADS);
//white
glColor3f(1.0, 1.0, 1.0);
glVertex3f(5.0, 0.0, -10.0);
glVertex3f(15.0, 0.0, -10.0);
glVertex3f(15.0, 0.0, 0.0);
glVertex3f(5.0, 0.0, 0.0);
glEnd();
}
My main_class.h which is basically my base class:
typedef struct position
{
float x, y, z;
}
position, rot;
class shape
{
private:
position pos;
float orientation;
float rate;
shape * next;
public:
void draw (void);
shape();
shape(float o, float r);
};
shape::shape()
{
orientation = 0.0;
rate = 0.0;
}
shape::shape(float o, float r)
{
orientation = o;
rate = r;
}
/*
void shape::draw (void)
{
glPushMatrix();
glTranslatef (pos.x, pos.y, pos.z);
glRotatef(orientation, orientation.x, orientation.y, orientation.z);
render();
glPopMatrix();
orientation += rate;
if (orientation >= 360)
{
orientation -= 360;
}
}
*/
i am still not very sure what the draw function does because this draw function is given by my teacher.
Lastly, my link_list.h:
class nodetype
{
public:
int data;
public:
nodetype *link;
nodetype();
~nodetype();
};
class list
{
nodetype *head;
nodetype *last;
nodetype *current;
public:
add();
delete();
traverse();
};
nodetype::nodetype()
{
data = 0;
link = 0;
}
nodetype::~nodetype()
{
data = 0;
link = 0;
}
list::add(nodetype *newnode)
{
if (head == NULL)
{
head = last = newnode;
}
else
newnode->link;
}