This commit is contained in:
Saturneic 2018-12-26 17:32:18 +08:00
parent d3cb156a33
commit 1f3652bd49
2 changed files with 151 additions and 7 deletions

View File

@ -1,2 +1,5 @@
# 设计内容(图形部分)
```
```

View File

@ -3,7 +3,9 @@
#include <type.hpp>
//计算函数执行时间间隔
#define DRAW_TIMER 1
//绘图函数执行时间间隔
#define FRESH_TIMER 20
typedef std::pair<double, double> Size;
@ -19,13 +21,21 @@ using std::map;
class Field;
//绘图函数声明
unsigned long fresh_screen(int signal);
//计算函数声明
void draw_fields(int signal);
/************************************************
***/
class Color{
public:
// 颜色的RGB值范围从0到1
double red, green, blue;
Color():red(1.0),blue(1.0),green(1.0){
}
Color(RawColor color){
red = color[0]/255;
@ -37,16 +47,27 @@ public:
blue = color.blue;
green = color.green;
}
/**
@param color RawColor颜色数据
*/
void setColor(RawColor color){
red = color[0]/255;
green = color[1]/255;
blue = color[2]/255;
}
/**
@param color Color颜色数据
*/
void setColor(Color color){
red = color.red;
blue = color.blue;
green = color.green;
}
// 将颜色按照相关比例变亮
void brighter(void){
red = sqrt(red);
blue = sqrt(blue);
@ -54,12 +75,26 @@ public:
}
};
/************************************************
***/
class Shape{
public:
// 图形形状
GLenum type;
// 点大小和画笔大小
GLfloat point_size, line_size;
// 图形顶点的坐标
vector<Point3> points;
// 图形颜色
Color color;
Shape():type(GL_POINTS),point_size(2.0f),line_size(2.0f),color({255,255,255}){
}
/**
glut函数库中的函数进行绘图前的初始化操作
*/
void flesh(void){
if(point_size != 1.0f)
glPointSize(point_size);
@ -67,21 +102,41 @@ public:
glLineWidth(line_size);
glColor3f(color.red, color.blue, color.green);
}
/**
@param color RawColor颜色数据
*/
void setColor(RawColor color){
this->color.setColor(color);
}
/**
@param color Color颜色数据
*/
void setColor(Color color){
this->color.setColor(color);
}
Shape():type(GL_POINTS),point_size(2.0f),line_size(2.0f),color({255,255,255}){
}
/**
@param pos 线
*/
void setRetangle(const pair<Point3,Point3> pos){
double fx = pos.first[0],fy = pos.first[1];
double sx = pos.second[0], sy = pos.second[1];
setPolygon({{fx,fy,0},{sx,fy,0},{sx,sy,0},{fx,sy,0}});
}
/**
@param pos
*/
void setPolygon(const vector<Point3> pos){
type = GL_POLYGON;
for(auto i = pos.begin(); i != pos.end(); i++){
@ -91,7 +146,9 @@ public:
};
/************************************************
***/
class View2D {
public:
Point pos;
@ -99,7 +156,9 @@ public:
};
/************************************************
***/
class Bar:public View2D {
public:
Bar(Point, GLint width){
@ -107,22 +166,53 @@ public:
}
};
/************************************************
***/
class Window {
public:
// 储存指向窗体中要包含的图形对象的指针
static list<Shape *> shapes;
// 储存指向窗体中要包含的控件对象的指针
static map<string,View2D *> menus;
Window(int argc, char **argv){
glutInit(&argc, argv);
}
/**
@param Mode GLUT函数库相关说明即可
*/
void playMode(int Mode){
glutInitDisplayMode(Mode);
}
/**
@param sizexy Size数据
*/
void size(Size sizexy){
glutInitWindowSize(static_cast<int>(sizexy.first), static_cast<int>(sizexy.second));
}
/**
@param positionxy Point数据
*/
void position(Point positionxy){
glutPositionWindow(positionxy.first, positionxy.second);
}
/**
@param name
@param func
*/
void create(string name = "No Name",void (*func)() = Window::displayEngine){
glutCreateWindow(name.data());
glutDisplayFunc(func);
@ -131,13 +221,28 @@ public:
glutTimerFunc(FRESH_TIMER, rendEngine, 1);
}
/**
@param color Color数据
*/
void setColor(Color color){
glClearColor(color.red, color.blue, color.green, 1.0f);
}
/**
*/
void show(void){
glutMainLoop();
}
/**
@param shape
*/
static void rendShape(Shape *shape){
if(shape->point_size != 1.0f)
glPointSize(shape->point_size);
@ -151,6 +256,9 @@ public:
glEnd();
}
/**
*/
static void displayEngine(void){
glClear(GL_COLOR_BUFFER_BIT);
for(auto i = shapes.begin(); i != shapes.end(); i++)
@ -159,9 +267,18 @@ public:
//rendShape((*i).second);
glutSwapBuffers();
}
/**
*/
static void mouseListener(int button,int state,int x,int y){
}
/**
@param value
*/
static void rendEngine(int value){
unsigned long ms = fresh_screen(0);
unsigned int now_fresh = (unsigned int)(FRESH_TIMER - ms);
@ -169,6 +286,9 @@ public:
glutTimerFunc(now_fresh, rendEngine, 1);
}
/**
*/
void draw_triangle(vector<Point3> pos, RawColor color={255, 255, 255}){
Shape *p_shape = new Shape;
p_shape->type = GL_TRIANGLES;
@ -179,6 +299,9 @@ public:
draw_shape(p_shape);
}
/**
*/
void draw_point(Point3 pos, GLfloat size = 1.0f, RawColor color={255, 255, 255}){
Shape *p_shape = new Shape;
p_shape->type = GL_POINTS;
@ -188,6 +311,10 @@ public:
draw_shape(p_shape);
}
/**
*/
void draw_polygon(vector<Point3> pos, RawColor color={255,255,255}){
Shape *p_shape = new Shape;
p_shape->type = GL_POLYGON;
@ -196,6 +323,9 @@ public:
draw_shape(p_shape);
}
/**
线
*/
void draw_line(pair<Point3,Point3> pos, GLfloat size = 1.0f, RawColor color={255,255,255}){
Shape *p_shape = new Shape;
p_shape->type = GL_LINES;
@ -206,22 +336,33 @@ public:
draw_shape(p_shape);
}
/**
*/
void draw_retangle(pair<Point3,Point3> pos, RawColor color={255,255,255}){
double fx = pos.first[0],fy = pos.first[1];
double sx = pos.second[0], sy = pos.second[1];
draw_polygon({{fx,fy},{sx,fy},{sx,sy},{fx,sy}},color);
}
/**
@param shape
*/
void draw_shape(Shape *shape){
shapes.insert(shapes.end(),shape);
}
/**
@param name
@param view
*/
void add_menu(string name, View2D *view){
menus.insert({name,view});
}
};
#endif /* graphs_h */