Need help to complete a project
Hi, I have to complete a project by next friday that has literaly just been thrown at me. I had no experience with Opengl, and quite basic ideas of C++. Anyway, I am now stuck.
This is the source code that I have written so far:
/*
* course1.c/Mesa
* 2d Graphics Game, Fire missile at the targets
*/
/* Note that your project type should be a "Win32 Console Application", and within
Project -> Settings -> Link you should add "opengl32.lib glut32.lib glu32.lib"
to the "Object/Library modules" text box. */
#include
#include
#define X_CENTRE 0.0 /* centre point of square */
#define Y_CENTRE 0.0
#define LENGTH 1.0 /* lengths of sides of square */
GLfloat angle = 0.0; /* angle of rotation in degrees (global) */
/* reshape callback function
executed when window is moved or resized */
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* use orthographic (parallel) projection
use xmin = 0, xmax = 1
ymin = 0, ymax = 1
znear = -1, zfar = 1 - not relevant here (2D) */
glOrtho(0.0, 1.0, 0.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
}
/* display callback function
called whenever contents of window need to be re-displayed */
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT); /* clear window */
glColor3f(1.0, 1.0, 0.5); /* Yellow drawing objects */
/* Yellow Targets at top of screen*/
glBegin(GL_POLYGON);
glVertex2f(0.125, 0.9);
glVertex2f(0.125, 0.95);
glVertex2f(0.2, 0.95);
glVertex2f(0.2, 0.9);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.4625, 0.9);
glVertex2f(0.4625, 0.95);
glVertex2f(0.5375, 0.95);
glVertex2f(0.5375, 0.9);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.8, 0.9);
glVertex2f(0.8, 0.95);
glVertex2f(0.875, 0.95);
glVertex2f(0.875, 0.9 );
glEnd();
/* White Boundary Lines */
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2f(0.0, 0.25);
glVertex2f(0.12, 0.25);
glVertex2f(1.0, 0.25);
glVertex2f(0.88, 0.25);
glEnd();
/* White Missile */
//glLoadIdentity();
glTranslatef( 0.5, 0.125, 0.0 ); // moves origin back to centre of missile
glRotatef( angle, 0.0, 0.0, 1.0 ); // rotates about origin
glTranslatef( -0.5, -0.125, 0.0 ); // moves centre point to origin
// draw missile
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(0.5, 0.145); // top
glVertex2f(0.49, 0.135); // top left corner
glVertex2f(0.49, 0.105); // bottom left
glVertex2f(0.51, 0.105); // bottom right
glVertex2f(0.51, 0.135); // top right
glEnd();
glLoadIdentity();
glFlush();
}
/* graphics initialisation */
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0); /* window will be cleared to black */
}
/* Keyboard callback function, called when keyboard used */
void keyboard(unsigned char key, int x, int y)
{
switch ( key )
{
case ''r'': angle = angle - 5.0;
glutPostRedisplay();
break;
case ''l'': angle = angle + 5.0;
glutPostRedisplay();
break;
case ''e'': exit(0);
break;
default: break;
}
}
void mouse (int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN)
{
GLint viewpoint[4];
GLdouble mvmatrix[16], projmatrix[16];
GLdouble wx, wy, wz;
GLint realwy;
GLdouble objx, objy, objz;
glGetIntegerv (GL_VIEWPORT, viewpoint);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
realwy = viewpoint[3] - wy - 1;
gluUnProject (wx, realwy, wz, mvmatrix, projmatrix, viewpoint, &objx, &objy, &objz);
}
break;
//case GLUT_RIGHT_BUTTON: glutIdleFunc( idle );
case GLUT_RIGHT_BUTTON: exit(0); //for now exit, should be used to fire missile
break;
default: break;
}
}
int main(int argc, char** argv)
{
/* window management code ... */
/* initialises GLUT and processes any command line arguments */
glutInit(&argc, argv);
/* use single-buffered window and RGBA colour model */
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
/* window width = 400 pixels, height = 400 pixels */
glutInitWindowSize (400, 400);
/* window upper left corner at (100, 100) */
glutInitWindowPosition (100, 100);
/* creates an OpenGL window with command argument in its title bar */
glutCreateWindow (argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
/* register keyboard callback function */
glutKeyboardFunc(keyboard);
/* register mouse callback function */
glutMouseFunc(mouse);
glutMainLoop();
}
It is a small game where the user controls a missile, they can rotate the missile and fire it. After many hours I can now rotate the missile, but I am totally stuck on the moving bit and hitting a target (needs to use double buffering as well). Firstly can anyone help me to move the missile when I right click on the mouse? (it should just move in the direction it is pointing), and when it hits a target the target should disappear, and the missile should return to its previous position at the bottom of the screen.
Any help is welcome, I was required to use GLUT, it was not a personal choice. I am a beginner to C++ so this is not easy for me at all.
Thanks
Cos.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement