Objectives

Setup

#include "libopengl.h"

void renderScene(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glColor3f(1.0f, 0.0f, 0.0f);
  glutSolidSphere(10.0f, 15, 15);

  glutSwapBuffers();
}

void setupRC()
{
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  glEnable(GL_DEPTH_TEST);
  glFrontFace(GL_CCW);
  glEnable(GL_CULL_FACE);

  glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho (-100.0f, 100.0f, -100.0f, 100.0f, -100.0f, 100.0f);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void timerFunc(int value)
{
  glutPostRedisplay();
  glutTimerFunc(50, timerFunc, 1);
}

int main(int argc, char* argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

  glutInitWindowSize(400,400);
  glutCreateWindow("Lab 05b");
  glutDisplayFunc(renderScene);
  setupRC();
  timerFunc(50);
  glutMainLoop();

  return 0;
}
#pragma once

struct Color
{
  float R;
  float G;
  float B;
  float A;

  static Color White;
  static Color Yellow;
  static Color Red;
  static Color Magenta;
  static Color Cyan;
  static Color Green;
  static Color Black;
  static Color Blue;


  Color();
  Color(float r, float g, float b, float a=1.0f);
  Color(int r, int g, int b, int a=255);

  void render();
  void renderClear();
};
#include "libopengl.h"
#include "Color.h"

Color Color::Black   (0,     0,    0);
Color Color::Blue    (0,     0,  255);
Color Color::Green   (0,   255,    0);
Color Color::Cyan    (0,   255,  255);
Color Color::Red     (255,   0,    0);
Color Color::Magenta (255,   0,  255);
Color Color::Yellow  (255, 255,    0);
Color Color::White   (255, 255,  255);


Color::Color()
{
  R = G = B = A = 1.0f;
}

Color::Color(float r, float g, float b, float a)
{
  R = r;
  G = g;
  B = b;
  A = a;
}

Color::Color(int r, int g, int b, int a)
{
  R = (float) r / 255.0f;
  G = (float) g / 255.0f;
  B = (float) b / 255.0f;
  A = (float) a / 255.0f;
}

void Color::render()
{
  glColor4f(R,G,B,A);
}

void Color::renderClear()
{
  glClearColor(R,G,B, 1.0f);
}

Nucleus

  //glColor3f(1.0f, 0.0f, 0.0f);
  Color::Red.render();

  //glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  Color::Black.renderClear();
g++ -framework GLUT -framework OpenGL -o "lab09"  ./src/electron.o
Undefined symbols:
...
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [lab09] Error 1

Electron

  Color::Green.render();
  glRotatef(0.0f, 0.0f, 1.0f, 0.0f);
  glTranslatef(90.0f, 0.0f, 0.0f);
  glutSolidSphere(6.0f, 15, 15);
  glPushMatrix();
    Color::Green.render();
    glRotatef(0.0f, 0.0f, 1.0f, 0.0f);
    glTranslatef(90.0f, 0.0f, 0.0f);
    glutSolidSphere(6.0f, 15, 15);
  glPopMatrix();
  static int angle = 0;
    glRotatef(angle, 0.0f, 1.0f, 0.0f);
  angle = (angle + 10) % 360;

Exercise 1

Vector

struct Vector3
{
  float X;
  float Y;
  float Z;

  static Vector3 UnitX;
  static Vector3 UnitY;
  static Vector3 UnitZ;

  Vector3(float x, float y, float z);
  Vector3(float value);
  Vector3();
  Vector3(std::istream& is);

  void render();
};
#include "libopengl.h"
#include "vector3.h"
#include "utils.h"
using namespace std;

Vector3 Vector3::UnitX(1.0f, 0.0f, 0.0f);
Vector3 Vector3::UnitY(0.0f, 1.0f, 0.0f);
Vector3 Vector3::UnitZ(0.0f, 0.0f, 1.0f);

Vector3::Vector3(float x, float y, float z)
: X(x)
, Y(y)
, Z(z)
{}

Vector3::Vector3(float value)
: X(value)
, Y(value)
, Z(value)
{}

Vector3::Vector3()
: X(0)
, Y(0)
, Z(0)
{}

Vector3::Vector3(istream &is)
{
  skipComment(is);
  is >> X >> Y >> Z;
}

void Vector3::render()
{
  glVertex3f(X, Y, Z);
}
// Header
  void translate();
  void rotate (float angle);

// Implementation
void Vector3::translate()
{
  glTranslatef(X,Y,Z);
}
void Vector3::rotate (float angle)
{
  glRotatef(angle, X,Y,Z);
}
  glPushMatrix();
    Color::Green.render();
    Vector3::UnitY.rotate(angle);
    Vector3(90,0,0).translate();
    glutSolidSphere(6.0f, 15, 15);
  glPopMatrix();

renderElectron

   glPushMatrix();
     Color::Green.render();
     Vector3::UnitZ.rotate(10);
     Vector3::UnitY.rotate(angle);
     Vector3(90,0,0).translate();
     glutSolidSphere(6.0f, 15, 15);
   glPopMatrix();
void renderElectron(Color color,  float orbitRadius, float orbitAngle, float zSkew)
{
  color.render();
  glPushMatrix();
    Vector3::UnitZ.rotate(zSkew);
    Vector3::UnitY.rotate(orbitAngle);
    Vector3(orbitRadius,0,0).translate();
    glutSolidSphere(6.0f, 15, 15);
  glPopMatrix();
}
  renderElectron(Color::Green,  90,  angle, 40);
  renderElectron(Color::Green,  90,  angle, 40);
  renderElectron(Color::Cyan,   40,  angle, 20);
  static int angle2 = 0;
  //
  renderElectron(Color::Cyan,   40,  angle2, 20);
  //
  angle2 = (angle2 + 5) % 360;

Exercises

Further Electrons

Different orbit velocity

Model