3D shapes giving me a problem in openGL

So I'm making a program where these little cubes bounce around in a small wire frame cube (glutWireCube), but I've run into a strange problem when testing the functions to place cubes at will. Basically, the cubes are descending along the Z axis untill the point that I can't even see them anymore... I didn't program them to do that, but they just do it anyway. I've included the header, and cpp file that I am using for this project.

Also, I'm not sure how well I explain my problem... If you didn't understand, or didn't think you understood my problem, compile the source code I left you below. Thankyou!

BALL_FUNCS.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <Windows.h>
#include <cmath>
#include <string>
#include <glut.h>
#include <GL\GLU.h>

using namespace std;
const int MAX_CUBES = 10;

float posX[MAX_CUBES], posY[MAX_CUBES], posZ[MAX_CUBES];
float myR[MAX_CUBES], myG[MAX_CUBES], myB[MAX_CUBES];

bool keypressed = false;
int currBallImport = 0;
int currBall = 0;
bool type;
const int boundaries = 2;
float boxX = 0, boxY = 0, boxZ = 0;

enum walls{CEILING    =   boundaries/2,  // Y
		   FLOOR      =   boundaries/-2, // Y
		   LEFT_WALL  =   boundaries/-2, // X
		   RIGHT_WALL =   boundaries/2,  // X
		   FAR_WALL   =   boundaries/-2, // Z
		   CLOSE_WALL =   boundaries/2   // Z
}; 

bool filled[] ={false, false, false, false, false, false, false, false, false, false, false};
float ballSize        [MAX_CUBES];
/*
void checkColision(int ID){}
void wall_collision(float R, float G, float B, int wall){}
void eraseAllBalls(){}
*/


void importInfo_Random(int ID) {
	srand(time (NULL) );
	int x, y, z, R, G, B, size;
	x = rand() % 100 + 1;
	y = rand() % 100 + 1;
	z = rand() % 20+boundaries + 20-boundaries;
	R = rand() % 100 + 1;  
	G = rand() % 100 + 1;  
	B = rand() % 100 + 1;  
	size = rand() % 10 + 1; 
	posX[ID] = (float)x*.1;
	posY[ID] = (float)y*.1;
	posZ[ID] = -z;
	myR[ID] = R*.01;
	myG[ID] = G*.01;
	myB[ID] = B*.01;
	ballSize[ID]=(float)size/10;
}

void importInfo_userInput(int ID) {
	float x, y, z, R, G, B, size;
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
		cout << "LIMITS"                           << endl
			 << "---------"                        << endl
			 << "Positions must be between 1 - 10" << endl
			 << "colors must be between 1 - 100"   << endl
			 << "sizes must be between 13 - 30\n\n"<< endl;
		cout << "R: ";
		cin  >> R;
		cout << endl;
		cout << "G: ";
		cin  >> G;
		cout << endl;
		cout << "B: ";
		cin  >> B;
		cout << endl;
		cout << "size: ";
		cin  >> size;
		cout << endl;
		cout << "startX: ";
		cin  >> x;
		cout << endl;
		cout << "startY: ";
		cin  >> y;
		cout << endl;
		cout << "startZ: ";
		cin  >> z;
		posX[ID] = x;
		posY[ID] = y;
		posZ[ID] = z;
		myB[ID] = R;
		myG[ID] = G;
		myR[ID] = B;
		ballSize[ID] = size;
}


BALL_CAGE.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <Windows.h>
#include <cmath>
#include "ball_funcs.h"
#include <string>
#include <glut.h>
#include <GL\GLU.h>

using namespace std;



void checkColision(int ID){}
void wall_collision(float R, float G, float B, int wall){}
void eraseAllBalls(){}



void drawBalls(int i) {
	glTranslated(posX[i], posY[i], posZ[i]);
	glColor3d(myR[i], myG[i], myB[i]);
	glutSolidCube(ballSize[i]);			
}
		
void Reshape (int height, int width) {
	glViewport(0, 0, width, height);
	glClearColor(0, 0, 0, 1);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60, (float)height/(float)width, 1, 100);
	glMatrixMode(GL_MODELVIEW);
}
void Display(void) {
	glClear(GL_COLOR_BUFFER_BIT);
	glPushMatrix();
	for (int i = 0;i < 10; i++) {
		if (filled[i] == true) {
			drawBalls(i);
		}
	}
	glPopMatrix();



		system("cls");
		for (int i = 0; i < 10; i++) {
			cout << i <<  ". X: " << posX[i]   
                                   << "; Y: " << posY[i] 
                                   << "; Z: " << posZ[i] 
                                   << "; Size: " << ballSize[i] 
                                   << "; R: " << myR[i] 
                                   << "; G: " << myG[i] 
                                   << "; B: " << myB[i] << endl;
		}
	glPushMatrix();
	glTranslated(0+boxX, 0+boxY, -10+boxZ);
	glColor3d(0, 1, 0);
	glutWireCube(boundaries);
	glPopMatrix();

	glutSwapBuffers();
}

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

	switch(key){
		
		case'r': //r for random
			if (keypressed == false) {
				importInfo_Random(currBallImport);
				filled[currBallImport] = true;
				currBallImport++;
				keypressed = true;
				glutPostRedisplay();
			}
			keypressed = false;
		break;
		case'e':// e for entry (user input/entry)
			if (keypressed == false) {
				importInfo_userInput(currBallImport);
				filled[currBallImport] = true;
				currBallImport++;
				keypressed = true;
			}
			keypressed = false;
		break;
	}
}


const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
int main(int argc, char **argv) {
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInit(&argc, argv);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Ball cage!");
	glutReshapeFunc(Reshape);
	glutDisplayFunc(Display);
	glutKeyboardFunc(Keyboard);
	/*
	glEnable(GL_CULL_FACE);
	    glCullFace(GL_BACK);
	    glEnable(GL_DEPTH_TEST);
	    glDepthFunc(GL_LESS);
	    glEnable(GL_LIGHT0);
	    glEnable(GL_NORMALIZE);
	    glEnable(GL_COLOR_MATERIAL);
	    glEnable(GL_LIGHTING);
	    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
	    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
	    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
	    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
	    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
	    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
	    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
		*/
	glutMainLoop();
}
After you call glPushMatrix();, you should call glLoadIdentity();.

As it stands, in the first frame, you're telling the cubes to translate to their positions, which is what you want. But, in each subsequent frame, you're telling them to continue translating. Using glLoadIdentity() will reset their transformation matrices each frame, thus placing them in the correct location each time.

EDIT:

Actually, your drawBalls loop should look like this:

1
2
3
4
5
6
7
8
for (int i = 0;i < 10; i++) {
   if (filled[i] == true) {
      glPushMatrix();
      glLoadIdentity();
      drawBalls(i);
      glPopMatrix();
   }
}


and your draw wire cube should be this:

1
2
3
4
5
6
glPushMatrix();
glLoadIdentity();
glTranslated(0+boxX, 0+boxY, -10+boxZ);
glColor3d(0, 1, 0);
glutWireCube(boundaries);
glPopMatrix();


Last edited on
Omg thankyou so much. IT WORKS NOW :D
Good to hear. After 12 days no less ;)
Topic archived. No new replies allowed.