Sign in to follow this  
Hillman

GLUT/OpenGL Issues.

Recommended Posts

Hillman    100
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_ */



Share this post


Link to post
Share on other sites
Spynacker    106
Yeah you not only forgot to set the modelview matrix as current
but also you load the identity matrix within Plane::draw().
This will cause your glRotate command not to have any effect.
I am not sure what you want to do but your should use following
sequence:

void display() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(angle, 0.0f, 1.0f, 0.0f);
pl1->draw();
glutSwapBuffers();
}

void Plane::draw() {
glTranslatef(0.0f, 0.0f, -10.0f);
glColor3f(1.0f, 0.0f, 0.0f);

glBegin(GL_QUADS);

...
}



Otherwise your call to Plane::draw() would also result in
clearing your color and depth buffer and therefore erasing
all previous drawing operations on this buffer!
Hope that helps.

Share this post


Link to post
Share on other sites
japro    887
Im not 100% sure but i think at the end of the resizing operation you should insert a:

glMatrixMode(GL_MODELVIEW);

Otherwise all transformations are made on the projection matrix which will give rather unpredictable results.

Share this post


Link to post
Share on other sites
Hillman    100
Works perfectly, thanks guys. On a side note, any idea how to have the cube move left when the user presses the 'left' button?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this