Commit ad069ccf authored by bbguimaraes's avatar bbguimaraes
Browse files

solar

parent 128c8168
......@@ -10,6 +10,8 @@ for details/demos/screenshots.
- parser: a top-down parser for a made-up programming language.
- sockets: an implementation of the game Jungle ("Dou Shou Qi", in Chinese)
with graphics and network communication.
- solar: graphical simulation of the solar system with embedded internet
searches.
- sudoku: Sudoku solver library/GUI using genetic algorithms.
- tablut: ANSI C / ncurses implementation of the game Tablut.
- transactions: two-phase locking (2PL) simulation TUI/GUI.
Simulation of the solar system with an augmented interface. When clicking on a
planet, several internet searches are performed and the results shown next to
the main display.
#ifndef CAMERA_H
#define CAMERA_H
#include "Vector.h"
class Camera {
Vector m_center;
float m_distance, m_x, m_y, m_z;
public:
Vector center() const {return this->m_center;}
float distance() const {return this->m_distance;}
float x_angle() const {return this->m_x;}
float y_angle() const {return this->m_y;}
float z_angle() const {return this->m_z;}
void set_distance(float d) {this->m_distance = d;}
void change_distance(float d) {this->m_distance += d;}
void set_x_angle(float a) {this->m_x = a;}
void set_y_angle(float a) {this->m_y = a;}
void set_z_angle(float a) {this->m_z = a;}
void move_x(float d) {this->m_center += Vector(d, 0, 0);}
void move_y(float d) {this->m_center += Vector(0, d, 0);}
void move_z(float d) {this->m_center += Vector(0, 0, d);}
void rotate(float x, float y, float z) {
this->rotate_x(x);
this->rotate_y(y);
this->rotate_z(z);
}
void rotate_x(float d) {this->m_x += d;}
void rotate_y(float d) {this->m_y += d;}
void rotate_z(float d) {this->m_z += d;}
};
#endif // CAMERA_H
#ifndef GL_DISPLAY_H
#define GL_DISPLAY_H
#include <set>
#include <QGLWidget>
#include <QTime>
#include "Camera.h"
#include "Vector.h"
class QKeyEvent;
class QMouseEvent;
class QWheelEvent;
class OpenGLTexture;
class SolarSystem;
class Planet;
class GLDisplay : public QGLWidget {
Q_OBJECT
public:
GLDisplay(QWidget * parent = nullptr);
Camera * camera() {return &this->m_camera;}
bool paused() const {return this->m_paused;}
bool slow() const {return this->m_slow;}
bool ambient() const {return this->m_ambient;}
bool diffuse() const {return this->m_diffuse;}
bool specular() const {return this->m_specular;}
int shininess() const {return this->m_shininess;}
bool local_viewer() const {return this->m_local_viewer;}
bool perspective_corr() const {return this->m_perspective_corr;}
bool texture() const {return this->m_texture;}
void set_solar_system(SolarSystem * ss) {this->m_solar_system = ss;}
public slots:
virtual void update();
void set_update_rate(float ur) {this->m_update_rate = ur;}
void set_selected(Planet * p) {this->m_selected = p;}
void toggle_paused() {this->m_paused = !m_paused;}
void toggle_slow() {this->m_slow = !m_slow;}
void toggle_ambient();
void toggle_diffuse();
void toggle_specular();
void set_shininess(int shininess);
void toggle_local_viewer() {this->m_local_viewer = !m_local_viewer;}
void toggle_line_smooth();
void toggle_perspective_corr();
void toggle_texture() {this->m_texture = !this->m_texture;}
signals:
void planet_highlighted(Planet * planet);
void planet_selected(Planet * planet);
void opengl_error(GLenum error);
protected:
virtual void initializeGL();
virtual void resizeGL(int w, int h);
virtual void paintGL();
virtual void keyPressEvent(QKeyEvent * event);
virtual void keyReleaseEvent(QKeyEvent * event);
virtual void mousePressEvent(QMouseEvent * event);
virtual void mouseReleaseEvent(QMouseEvent * event);
virtual void mouseMoveEvent(QMouseEvent * event);
virtual void wheelEvent(QWheelEvent * event);
private:
GLDisplay(GLDisplay &) = delete;
GLDisplay & operator =(GLDisplay &) = delete;
static GLuint create_planet_list(int slices);
bool key_pressed(int key) const;
void check_keys();
void draw_background() const;
void draw_system(GLenum mode) const;
void pick(const Vector & pos);
void set_camera();
float m_update_rate;
float m_zoom_speed;
OpenGLTexture * m_background;
SolarSystem * m_solar_system;
Planet * m_selected;
Camera m_camera;
std::set<int> m_keys_pressed;
GLuint m_planet_list;
GLuint m_planet_select_list;
Vector m_last_position;
bool m_paused;
bool m_slow;
bool m_ambient;
bool m_diffuse;
bool m_specular;
int m_shininess;
bool m_local_viewer;
bool m_line_smooth;
bool m_perspective_corr;
bool m_texture;
QTime m_last_frame;
unsigned int m_frame_count;
};
#endif // GL_DISPLAY_H
#ifndef MAIN_WINDOW_H
#define MAIN_WINDOW_H
#include <QMainWindow>
#include <QTimer>
#include <vector>
#include <string>
#include <SolarSystem.h>
#include <Planet.h>
class QLabel;
class QPushButton;
class QSlider;
class QSpinBox;
class GLDisplay;
class WebSearchWidget;
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget * parent = nullptr);
public slots:
void set_fps(int fps);
void set_shininess(int shininess);
void highlight_panel(Planet * planet);
void show_planet(Planet * planet);
void center_camera();
void load_started();
void load_finished(bool ok);
void load_solar_system_file();
void load_solar_system_file(const std::string & filename);
void opengl_error(GLenum error);
private:
void init();
static OpenGLTexture * load_texture(const std::string & filename);
std::vector<Planet> m_planets;
SolarSystem m_solar_system;
GLDisplay * m_display;
QTimer m_display_timer;
QLabel * m_url_label;
std::vector<WebSearchWidget *> m_websearch_widgets;
QDockWidget * m_controls_dock;
QDockWidget * m_options_dock;
QDockWidget * m_content_dock;
QPushButton * m_pause_button;
QPushButton * m_slow_button;
QPushButton * m_center_camera_button;
QPushButton * m_ambient_button;
QPushButton * m_diffuse_button;
QPushButton * m_specular_button;
QPushButton * m_local_viewer_button;
QPushButton * m_line_smooth_button;
QPushButton * m_perspective_corr_button;
QPushButton * m_texture_button;
QSlider * m_shininess_slider;
QSpinBox * m_fps_spin;
};
#endif // MAIN_WINDOW_H
#ifndef OPENGL_TEXTURE_H
#define OPENGL_TEXTURE_H
#include <GL/gl.h>
class Vector;
struct OpenGLTexture {
GLuint m_id;
public:
OpenGLTexture(
const unsigned char * data,
unsigned int format,
unsigned int width,
unsigned int height);
void bind() const;
};
#endif // OPENGL_TEXTURE_H
#ifndef PLANET_H
#define PLANET_H
#include <ostream>
#include <string>
#include <GL/glu.h>
class OpenGLTexture;
class Planet {
public:
static const float AU_TO_KM;
Planet(
std::string name,
float distance,
float speed = 0.0f,
float radius = 1.0f,
OpenGLTexture * texture = nullptr,
bool star = false);
void set_name(std::string name) {this->m_name = name;}
void set_distance(float distance) {this->m_distance = distance;}
void set_speed(float speed) {this->m_speed = speed;}
void set_radius(float radius) {this->m_radius = radius;}
void set_texture(OpenGLTexture * texture) {this->m_texture = texture;}
void set_star(bool star) {this->m_star = star;}
std::string name() const {return this->m_name;}
float distance() const {return this->m_distance;}
float speed() const {return this->m_speed;}
float angle() const {return this->m_angle;}
float radius() const {return this->m_radius;}
const OpenGLTexture * texture() const {return this->m_texture;}
bool star() const {return this->m_star;}
void update(float time);
void draw(GLUquadric * quadric, unsigned int slices) const;
private:
std::string m_name;
float m_distance;
float m_speed;
float m_angle;
float m_radius;
OpenGLTexture * m_texture;
bool m_star;
};
std::ostream & operator<<(std::ostream & os, const Planet & p);
#endif // PLANET_H
#ifndef SOLAR_SYSTEM_H
#define SOLAR_SYSTEM_H
#include <vector>
class Planet;
class SolarSystem {
std::vector<Planet *> m_planets;
public:
SolarSystem() {}
SolarSystem(std::vector<Planet *> planets) : m_planets(planets) {}
void set_planets(std::vector<Planet *> planets)
{this->m_planets = planets;}
std::vector<Planet *> * planets() { return &this->m_planets; }
const std::vector<Planet *> * planets() const
{return &this->m_planets;}
};
#endif // SOLAR_SYSTEM_H
#ifndef TILE_H
#define TILE_H
class OpenGLTexture;
class Vector;
class Tile {
const OpenGLTexture * m_texture;
public:
Tile(const OpenGLTexture * t) : m_texture(t) {}
void draw(const Vector & bl, const Vector & tr) const;
};
#endif // TILE_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment