Tuesday, February 19, 2013

[c/c++] Open GL display list example

Notice that:

- The drawParticle() method is static
- That's why this method can be put onto the GL 'display list', it will be loaded once but run many times, if you put 'cout', for example, in drawParticle() method, it will only show output once
- The position of drawing the points are actually specified by assigning value to the member variable x, at the end of the caller to the drawParticle() method.


--Particle.h--

#pragma once

#include <GL\glut.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <limits.h>
#include <iostream>
#include "Vector3D.h"
#include "Constants.h"
using namespace std;


class Particle {

public: 
    /** Display list index. */
    static int PARTICLE_DISPLAY_LIST;

    /** Highlighted appearance if true, otherwise white. */
    bool highlight;

    /** If true, then particle is pinned in space. */
    bool pin;

    /** mass. */
    float m;

    /** Deformed Position. */
    Point3D x;

    /** Undeformed/material Position. */
    Point3D x0;

    /** Velocity. */
    Vector3D v;

    /** Force accumulator. */
    Vector3D f;

    /**
    * Draws a canonical circular particle.
    */
    static void drawParticle(Point3D p); 

public: 

    /**
    * Constructor: 
    */
    Particle() {
        setDefaultMemebers();
    }

    /**
    * Constructor: Constructs particle with the specified material/undeformed coordinate,
    * p0.
    */
    Particle(Point3D p_x0) {
        setDefaultMemebers();
        x0.Set(p_x0.x, p_x0.y, p_x0.z);
        x.Set(p_x0.x, p_x0.y, p_x0.z);
    }

    /** Set default values to some of the members */
    void setDefaultMemebers(){
        highlight = false;
        pin = false;
        m = PARTICLE_MASS; 
        PARTICLE_DISPLAY_LIST = -1;
    }

    // Setters

    /** Specifies whether particle should be drawn highlighted. */
    void setHighlight(bool p_highlight) {
        highlight = p_highlight;
    }
    /**
    * Specifies whether or not this particle is fixed in space via a pin
    * constraint. (Should probably be elsewhere in a generic constraint list).
    */
    void setPin(bool fix) { 
        pin = fix; 
    }

    // Getters 

    /** True if particle should be drawn highlighted. */
    bool getHighlight() { 
        return highlight; 
    }
    /** Returns true if currently pinned. */
    bool isPinned() {
        return pin;
    }

    /** Draws circular particle using a display list. */
    void display();
};


----Particle.cpp----

#include "Particle.h"

/**
* Simple particle implementation, with miscellaneous adornments.
* 
* @author James Wang, Feb, 2013
* based on code orginally developed by Doug James in Java with JOGL 1.x
*/

int Particle::PARTICLE_DISPLAY_LIST = -1;

/** Draws circular particle using a display list. */
void Particle::display() {
    //cout << "NEW PARTICLE FOR DISPLAY LIST " << PARTICLE_DISPLAY_LIST << "\n";
    if (PARTICLE_DISPLAY_LIST < 0) {// MAKE DISPLAY LIST:
        int displayListIndex = glGenLists(1);
        glNewList(displayListIndex, GL_COMPILE);
        drawParticle(Point3D(0.0f, 0.0f, 0.0f));// /particle at origin
        glEndList();
        std::cout<<"MADE LIST" << displayListIndex << " : "
            << glIsList(displayListIndex);
        PARTICLE_DISPLAY_LIST = displayListIndex;
    }

    // COLOR: DEFAULT WHITE; GREEN IF HIGHLIGHTED; ADD RED IF PINNED
    Point3D c (1.0f, 1.0f, 1.0f);// default: white
    if (pin) {
        c.x = 1.0f;// add red
        c.y *= 0.2f;
        c.z = 0;
    }
    if (highlight) {
        c.y = 1;
        c.z = 0;
    }

    glColor3f(c.x, c.y, c.z);

    // / DRAW ORIGIN-CIRCLE TRANSLATED TO "p":
    glPushMatrix();
    glTranslated(x.x, x.y, x.z);
    glCallList(PARTICLE_DISPLAY_LIST);
    glPopMatrix();
}

/**
* Draws a canonical circular particle.
*/
void Particle::drawParticle(Point3D p) {
    double radius = PARTICLE_RADIUS;

    double vectorX1 = p.x;
    double vectorY1 = p.y;
    double vectorZ1 = p.z;

    cout << "\n" << "MAKING COORDINATE SPOT AT " << p.x << " " << p.y << " "<< p.z << "\n";

    // Draw the particle as 3D spheres 
    glColor4f(1, 0, 0, 0); 
    glutSolidSphere(radius, 10, 10);

    glEnd();
}

No comments:

Post a Comment