Skip to content

Commit 5cfd168

Browse files
committed
GLUT random spheres
1 parent ebc9594 commit 5cfd168

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

main.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,38 @@
66
#include <vector>
77
#include <cmath>
88

9-
#define USE_SHADERS 1
9+
#define USE_SHADERS 0
1010
GLuint prog_hdlr;
1111

12+
const int NATOMS = 10000;
1213
const int SCREEN_WIDTH = 1024;
1314
const int SCREEN_HEIGHT = 1024;
1415
const float camera[] = {.6,0,1};
1516
const float light0_position[4] = {1,1,1,0};
17+
std::vector<std::vector<float> > atoms;
18+
19+
float rand_minus_one_one() {
20+
return (float)rand()/(float)RAND_MAX*(rand()>RAND_MAX/2?1:-1);
21+
}
22+
23+
float rand_zero_one() {
24+
return (float)rand()/(float)RAND_MAX;
25+
}
26+
1627

1728
void render_scene(void) {
1829
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1930
glLoadIdentity();
2031
gluLookAt(camera[0], camera[1], camera[2], 0, 0, 0, 0, 1, 0);
21-
glColor3f(.8, 0., 0.);
22-
glutSolidTeapot(.7);
32+
33+
for (int i=0; i<NATOMS; i++) {
34+
glColor3f(atoms[i][4], atoms[i][5], atoms[i][6]);
35+
glPushMatrix();
36+
glTranslatef(atoms[i][0], atoms[i][1], atoms[i][2]);
37+
glutSolidSphere(atoms[i][3], 16, 16);
38+
glPopMatrix();
39+
}
40+
2341
glutSwapBuffers();
2442
}
2543

@@ -88,6 +106,18 @@ void setShaders(GLuint &prog_hdlr, const char *vsfile, const char *fsfile) {
88106

89107

90108
int main(int argc, char **argv) {
109+
for (int i=0; i<NATOMS; i++) {
110+
std::vector<float> tmp;
111+
for (int c=0; c<3; c++) {
112+
tmp.push_back(rand_minus_one_one()/2); // xyz
113+
}
114+
tmp.push_back(rand_zero_one()/8.0); // radius
115+
for (int c=0; c<3; c++) {
116+
tmp.push_back(rand_zero_one()); // rgb
117+
}
118+
atoms.push_back(tmp);
119+
}
120+
91121
glutInit(&argc, argv);
92122
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
93123
glutInitWindowPosition(100,100);

shaders/frag_shader.glsl

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
#version 120
22

3-
varying vec3 n;
4-
53
void main(void) {
6-
vec3 l = normalize(gl_LightSource[0].position.xyz);
7-
8-
float intensity = .2 + max(dot(l,normalize(n)), 0.0);
9-
10-
if (intensity > 0.95)
11-
intensity = 1;
12-
else if (intensity > 0.5)
13-
intensity = .6;
14-
else if (intensity > 0.25)
15-
intensity = .4;
16-
else
17-
intensity = .2;
18-
19-
gl_FragColor = gl_Color * intensity;
4+
gl_FragColor = gl_Color;
205
return;
216
}
227

shaders/vert_shader.glsl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#version 120
22

3-
varying vec3 n;
4-
53
void main() {
6-
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
7-
gl_FrontColor = gl_Color;
8-
n = gl_NormalMatrix * gl_Normal;
4+
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
5+
gl_FrontColor = gl_Color;
96
}

0 commit comments

Comments
 (0)