OpenGL City Problem

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
#include <iostream>
#include <GL/freeglut.h>
#include <GL/glut.h>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;

int X = 0, Y = 1, Z = 0;
GLfloat spin = .0;
GLfloat ambient_color[] = { .6, .1, .0, 1.0 };
GLfloat specular_color[] = { 1.0, 1.0, .0, 1.0 };
GLfloat light_position[] = { .5, .75, -1.0, 1.0 };
GLfloat diffuse_color[] = { .3, .6, .0, 1.0 };
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
vector<GLfloat> storage;
int storage_count = 0;
int MAX_HEIGHT = 7, height = 0;
bool start = true;

void init(void) {
	glClearColor(0.0, 0.0, 0.0, 0.0);

	glShadeModel(GL_SMOOTH);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_color);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_DEPTH_TEST);

	cout << "\tP3 - Pointing Object Representation:\n";
	cout << "* Objects should be organized next to each other, "
		<< "9 objects per line.\n"
		<< "* Once clicked on, specified object should rotate, "
		<< "and upon another selection \nof an object, previous rotation stops\n"
		<< "and selected object rotates.";
}
void startup(void)
{
	GLfloat x = 0., y = .1, z = 0.;

	//Store coordinates for building
	srand(time(0));
	GLfloat min = -.80, max = .80;
	GLfloat range = (max - min);
	for (int i = 0; i < 30; i++) {
		height = 1 + rand() % (MAX_HEIGHT + 1);
		for (int j = 0; j < height; j++) {
			y += .1;
			storage.push_back(height);
			storage.push_back(x);
			storage.push_back(y);
			storage.push_back(z);
			storage_count++;
		}
		x = RAND_MAX / range;
		x = min + (rand() / x);
		z = RAND_MAX / range;
		z = min + (rand() / z);
		y = .1;
	}
}
void display(void) {
	//Create ground
	glPushMatrix();
	glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
	glTranslatef(.0, .0, .0);
	glScalef(2.5, .1, 2.5);
	glRotatef(.0, X, Y, Z);
	glutSolidCube(.75);
	glPopMatrix();

	int count = 0, k = 0, h = 0;
	while (count != storage_count) {
		h = storage[k];
		for (int i = 0; i < h; i++) {
			glPushMatrix();
			glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
			glTranslatef(storage[k + 1], storage[k + 2], storage[k + 3]);
			glutSolidCube(.1);
			k += 4;
			count++;
                        glPopMatrix();
		}
	}
	glutSwapBuffers();
}
void spinDisplay(void) {
	spin += .05;
	if (spin > 360)
		spin = 0.;
	glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 'x': glRotatef(1., 1., 0., 0.);  break;
	case 'y': glRotatef(1., 0., 1., 0.);  break;
	case 'z': glRotatef(1., 0., 0., 1.);  break;
	case 'X': glRotatef(-1., 1., 0., 0.);  break;
	case 'Y': glRotatef(-1., 0., 1., 0.);  break;
	case 'Z': glRotatef(-1., 0., 0., 1.);  break;
	}
	glutPostRedisplay();
}
void main(int argc, char **argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(1150, 650); glutInitWindowPosition(0, 0);
	glutCreateWindow("P3 - 18 Objects - Mustapha Olokun");
	init();
	if (start) {
		startup();
		start = false;
	}
	else
		glutDisplayFunc(display);
	glutKeyboardFunc(keyboard);
	glutIdleFunc(spinDisplay);
	glutMainLoop();
}


It gives me a white screen right now, I use the startup function to set coordinates for each building, and then the display function to render each building. There is a problem here with global and local variables I believe.
Last edited on
I don't believe you are actually calling the display function. Because start is initialized to true, you only perform lines 116 and 117 and you skip over line 120.
But when I set it to false, when the glutMainLoop() reiterates shouldn't start be false, and display execute?
Why check? You can initialize the data without the if (start) check. You essentially hand control over to glut, and your startup function won't be called again unless you tell it to.
It appear as doug4 states, you're either assigning the display callback or initializing your data, but not both.

glLightfv is some pretty old OpenGL. Have you checked out newer versions of OpenGL? My linking to this site may appear rude, but it is a fantastic resource for learning modern OpenGL: https://learnopengl.com/
I also recommend GLFW, since freeglut is old and not maintained anymore.
I will defer to JayhawkZombie on all OpenGL issues. It has been years since I looked at OpenGL, and I was never proficient in it.

However, I think you are missing something else:

But when I set it to false, when the glutMainLoop() reiterates shouldn't start be false, and display execute?


Your main() function calls glutMainLoop(). If that function ever returns, the program terminates (there is nothing in main() after glutMainLoop()). So, line 115 only executes the first time. If you are expecting things to happen inside the main loop, you need to put that code inside the main loop. Lines 115-120 are not inside the main loop.

Sorry, I don't remember how to add code to the OpenGL infrastructure. I will defer to JayhawkZombie or some other OpenGL expert to give guidance there.
Topic archived. No new replies allowed.