C++ Project NEED HELP

Good Afternoon all,

I am in C Programming and for one of our final projects we are working with a 3D object and I need to figure out how to tie the keyboard controls to each key. W is in and has already been defined with:

case 'w':
camRadius -= camRDelta;
break;

I just do no know how to tie the other keys to their respective motion. Here is the whole program:


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

#define MINIMUM_SIZE 10.0 // Minimum size of cube

// Camera controls
float camAngle = 0; // The rotation of the camera about the Z axis
float camADelta = 5; // Angular velocity, degrees
float camRadius = 20.0; // The distance of the camera from the origin
float camRDelta = 0.5; // Radial velocity
float camY = 4.0; // Vertical position
float camYDelta = 0.5; // Vertical velocity

// Colors
// Colors are represented by arrays of 3 intensities for Red, Green, Blue
// GLfloat is a float that is controlled by OpenGL.

// ambientLight is the all-pervasive light that seems to come from everywhere
GLfloat ambientLight[] = {0.3f, 0.3f, 0.3f, 1.0};

// lightColor is the light that comes from a point source, like a bright lightbulb
GLfloat lightColor[] = {0.7f, 0.7f, 0.7f, 1.0};

// lightPos tells where the light source is located.
GLfloat lightPos[] = {-8.0, 8.0, 8.0, 1.0};

// Define the container and its morphing rate
typedef struct {
float x;
float y;
float z;
} Point3D;

// Number of sides on the base polygon
#define baseSides 4

// Sides must be in right-hand rule order; i.e., wrapping your right
// hand around the base in sequence shoud leave the thumb pointing up.
// This array holds the 3D coordinates for each corner on the base polygon
// The sides are rectangles that rise from the sides of the base polygon
const float radius = 8.0;
Point3D vertices[baseSides]; // Initialized in main()

// This function initializes the base coordinates. Adapt it to the shape you
// wish. You must define "baseSides" above. This version produces a regular
// polygon of "baseSides" sides. Make your own shape as you wish
void initBase()
{
int i;
for (i = 0; i < baseSides; i++)
{
vertices[i].x = radius * cos(2 * M_PI * i / baseSides);
vertices[i].y = -4.0;
vertices[i].z = radius * sin(2 * M_PI * i / baseSides);
printf("(x, y, z) = (%f, %f, %f)\n",
vertices[i].x, vertices[i].y, vertices[i].z);
}
}

// Normals are perpendicular to the sides; they are calculated when needed.
Point3D normals[baseSides];

// Stop the vertical height at this limit
#define MAX_HEIGHT 10
float height = 0;

// Morphing rate
int timeInterval = 100; // milliseconds
int pause = 0;

// Keyboard controls:
// A/D are around the center
// W/S are in and out.
// Q/Z are up and down
// R resets
// Spacebar toggles pausing
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
case 'w':
// The w key is given for an example.
camRadius -= camRDelta;
break;
case ' ': // Pause and resume with the spacebar
pause = !pause;
break;
default:
break;
}
}

Up to this point is all we need to understand for this project.
Firstly, when posting code, please use code tags to make your code more readable.

With the example that you've been given, it must be obvious to you how you create the cases for the other keys. So presumably the task for you is to figure out what manipulations you need to make to the object in each case?

Have you thought about it at all? Having been shown already what to do on 'w', it can't be too hard to work out what to do for 's'.

Have a go working out what you might need to for each of the other key presses, and then try running it to see whether it's doing what you expect. If there are any problems, post what you've done and we can help.
Last edited on
I'm sorry first time posting here... fairly new to programming. I noticed the example and I tried including the cases for the controls here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        case 'w':
            camRadius -= camRDelta;
            break;
        case 's':
            camRadius += camRDelta;
            break;
        case 'a':
            camAngle -= camADelta;
            break;
        case 'd':
            camAngle += camADelta;
            break;
        case 'q':
            camY -= camYDelta;
            break;
        case 'z':
            camY += camYDelta;
            break;
        case ' ':  // Pause and resume with the spacebar
            pause = !pause;
            break;  


The issue I'm having is first whether my syntax is even right and two if using += and -= is really going to help me pan around the object. I wish I could say we went over this but I can't... I wrote all these cases, but none of them are working on my keyboard except 'w' and spacebar.
Never mind I got it working. Of course I would over think it and not compile it before running.
Cool! Glad you managed to get it working :)
Topic archived. No new replies allowed.