Hey guys,
I'm just starting out a little game I have to make using OpenGL and GLUT. I'm trying to get a cube to draw and rotate from it's own class. I know I could draw the cube in main.cpp but I plan to replace the cube with an md2 plane at a later point so I would like it drawn seperatly. Currently I can get an empty window to appear but no cube. It's most likely a silly mistake so I need a fresh set of eyes. Code is below.
main.cpp -
#include <iostream>
#include <stdlib.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include "gameobject.h"
#include "plane.h"
using namespace std;
float angle = 75.0f;
int w1 = 500;
int h1 = 500;
Plane * pl1 = new Plane;
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: exit(0);
case 38: h1 += 25;
case 37: w1 += 25;
}
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
}
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w/(double)h, 1.0, 200.0);
}
void display() {
glRotatef(angle, 0.0f, 1.0f, 0.0f);
pl1->draw();
glutSwapBuffers();
}
void refresh(int value) {
angle += 1.5f;
if (angle > 360) {
angle -= 360;
}
glutPostRedisplay();
glutTimerFunc(25, refresh, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(w1, h1);
glutCreateWindow("Welcome To AirDrop!");
initRendering();
glutDisplayFunc(display);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(25, refresh, 0);
glutMainLoop();
return 0;
}
plane.cpp -
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <iostream>
#include "gameobject.h"
#include "plane.h"
Plane::Plane() {
}
Plane::~Plane() {
}
void Plane::draw() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glEnd();
}
plane.h -
#include "gameobject.h"
#ifndef PLANE_H_
#define PLANE_H_
class Plane : public GameObject {
public:
Plane();
virtual ~Plane();
virtual void draw();
};
#endif /* PLANE_H_ */