PDA

View Full Version : I want to copy or instance my cube every time I move it with my WASD keys.



CJohnD
12-27-2016, 05:11 AM
Hello,
I made a quick program, and I got my cube to move with my WASD keys however I want to make it so when I move it I leave behind a copy.
It would look better if I only left the bottom plane behind.
Thank you!
Here is what I have so far.



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

GLfloat d[6] = {0.1, 0.1, 0.1, 0.1, 0.1, 0.1};
GLfloat deplasareX = 0.0, deplasareY = 0.0, deplasareZ = 0.0;
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;



void Keyboard (unsigned char key, int x, int y)
{
switch (key) {

case 'd' : d[0] += 0.1; break;
case 'a' : d[1] -= 0.1; break;
case 'w' : d[2] += 0.1; break;
case 's' : d[3] -= 0.1; break;
case 't' : d[4] += 0.1; break;
case 'y' : d[5] -= 0.1; break;


case 'z' : xAngle += 0.5; break;
case 'x' : yAngle += 0.5; break;
case 'c' : zAngle += 0.5; break;

default: printf (" Keyboard %c == %d\n", key, key);
}

glutPostRedisplay();
}

void Cube (void)
{
//cubul
glBegin (GL_QUADS);

glColor3f ( 0.0, 0.7, 0.1); // Front - green
glVertex3f (-1.0, 1.0, 1.0);
glVertex3f ( 1.0, 1.0, 1.0);
glVertex3f ( 1.0, -1.0, 1.0);
glVertex3f (-1.0, -1.0, 1.0);

glColor3f ( 0.9, 1.0, 0.0); // Back - yellow
glVertex3f (-1.0, 1.0, -1.0);
glVertex3f ( 1.0, 1.0, -1.0);
glVertex3f ( 1.0, -1.0, -1.0);
glVertex3f (-1.0, -1.0, -1.0);

glColor3f ( 0.2, 0.2, 1.0); // Top - blue
glVertex3f (-1.0, 1.0, 1.0);
glVertex3f ( 1.0, 1.0, 1.0);
glVertex3f ( 1.0, 1.0, -1.0);
glVertex3f (-1.0, 1.0, -1.0);

glColor3f ( 0.7, 0.0, 0.1); // Bottom - red
glVertex3f (-1.0, -1.0, 1.0);
glVertex3f ( 1.0, -1.0, 1.0);
glVertex3f ( 1.0, -1.0, -1.0);
glVertex3f (-1.0, -1.0, -1.0);

glEnd();
}
void redraw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_DEPTH_TEST);

glLoadIdentity ();

glDisable (GL_LIGHTING);

glPushMatrix ();
glTranslatef (d[0], d[2], d[4]); // Move box down X axis.
glTranslatef (d[1], d[3], d[5]); // Move box down X axis.
glScalef (0.2, 0.2, 0.2);
glRotatef (zAngle, 0,0,1);
glRotatef (yAngle, 0,1,0);
glRotatef (xAngle, 1,0,0);
Cube();
glPopMatrix ();

glutSwapBuffers();
}

int main(int argc, char **argv)
{
int width = 900;
int height = 600;

glutInit(&argc, argv);
glutInitWindowSize(width,height);
glutInitWindowPosition(300,300);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

glutCreateWindow("M");
glutDisplayFunc ( redraw );
glutKeyboardFunc ( Keyboard );

glClearColor (0.1, 0.0, 0.1, 1.0);
glMatrixMode (GL_PROJECTION);
gluPerspective (45, 1.5, 1, 10);
glMatrixMode (GL_MODELVIEW);
glutMainLoop();
return EXIT_SUCCESS;
}