Skip to content

Commit

Permalink
GLUT random spheres
Browse files Browse the repository at this point in the history
  • Loading branch information
ssloy committed Mar 23, 2015
1 parent ebc9594 commit 5cfd168
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
36 changes: 33 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,38 @@
#include <vector>
#include <cmath>

#define USE_SHADERS 1
#define USE_SHADERS 0
GLuint prog_hdlr;

const int NATOMS = 10000;
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 1024;
const float camera[] = {.6,0,1};
const float light0_position[4] = {1,1,1,0};
std::vector<std::vector<float> > atoms;

float rand_minus_one_one() {
return (float)rand()/(float)RAND_MAX*(rand()>RAND_MAX/2?1:-1);
}

float rand_zero_one() {
return (float)rand()/(float)RAND_MAX;
}


void render_scene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(camera[0], camera[1], camera[2], 0, 0, 0, 0, 1, 0);
glColor3f(.8, 0., 0.);
glutSolidTeapot(.7);

for (int i=0; i<NATOMS; i++) {
glColor3f(atoms[i][4], atoms[i][5], atoms[i][6]);
glPushMatrix();
glTranslatef(atoms[i][0], atoms[i][1], atoms[i][2]);
glutSolidSphere(atoms[i][3], 16, 16);
glPopMatrix();
}

glutSwapBuffers();
}

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


int main(int argc, char **argv) {
for (int i=0; i<NATOMS; i++) {
std::vector<float> tmp;
for (int c=0; c<3; c++) {
tmp.push_back(rand_minus_one_one()/2); // xyz
}
tmp.push_back(rand_zero_one()/8.0); // radius
for (int c=0; c<3; c++) {
tmp.push_back(rand_zero_one()); // rgb
}
atoms.push_back(tmp);
}

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
Expand Down
17 changes: 1 addition & 16 deletions shaders/frag_shader.glsl
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
#version 120

varying vec3 n;

void main(void) {
vec3 l = normalize(gl_LightSource[0].position.xyz);

float intensity = .2 + max(dot(l,normalize(n)), 0.0);

if (intensity > 0.95)
intensity = 1;
else if (intensity > 0.5)
intensity = .6;
else if (intensity > 0.25)
intensity = .4;
else
intensity = .2;

gl_FragColor = gl_Color * intensity;
gl_FragColor = gl_Color;
return;
}

7 changes: 2 additions & 5 deletions shaders/vert_shader.glsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#version 120

varying vec3 n;

void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
n = gl_NormalMatrix * gl_Normal;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
}

0 comments on commit 5cfd168

Please sign in to comment.