Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <GL/gl.h>
- #include <cmath>
- class checker {
- public:
- enum color {white, black};
- checker(color checker_color, const GLint x, const GLint y, const GLint radius, const GLint segments = 30)
- : checker_color(checker_color), x(x), y(y), radius(radius), segments(segments) {}
- inline bool is_white() {
- return checker_color == white;
- }
- inline bool is_black() {
- return checker_color == black;
- }
- void render() {
- if(is_black()) {
- glColor3f(0.0, 0.0, 0.0);
- } else {
- glColor3f(1.0, 1.0, 1.0);
- }
- glBegin(GL_TRIANGLE_FAN);
- glVertex2f(x, y);
- for(int n = 0; n <= segments; ++n) {
- float const t = 2 * M_PI * float(n) / float(segments);
- glVertex2f(x + sin(t) * radius, y + cos(t) * radius);
- }
- glEnd();
- if(is_black()) {
- glColor3f(1.0, 1.0, 1.0);
- } else {
- glColor3f(0.0, 0.0, 0.0);
- }
- glBegin(GL_LINE_LOOP);
- for(int n = 0; n <= segments; ++n) {
- float const t = 2 * M_PI * float(n) / float(segments);
- glVertex2f(x + sin(t) * radius, y + cos(t) * radius);
- }
- glEnd();
- glFlush();
- }
- private:
- const color checker_color;
- const GLint x;
- const GLint y;
- const GLint radius;
- const GLint segments;
- };
- class cell {
- public:
- cell(const checker _checker, const GLint x, const GLint y)
- : _checker(_checker), x(x), y(y) {}
- ~cell() {}
- GLint get_x() {
- return x;
- }
- GLint get_y() {
- return y;
- }
- private:
- const checker _checker;
- const GLint x;
- const GLint y;
- };
- class chessboard {
- public:
- chessboard(GLint left_corner_x=50, GLint left_corner_y=50, GLint polygon_size=50)
- : left_corner_x(left_corner_x), left_corner_y(left_corner_y), polygon_size(polygon_size)
- {
- }
- ~chessboard(){}
- void redner() {
- glColor3f(0.0, 0.0, 0.0);
- for(int i = 0; i < 8; ++i) {
- for(int j = 0; j < 8; ++j) {
- const GLint pos_x = left_corner_x + i * 50;
- const GLint pos_y = left_corner_y + j * 50;
- if((i + j) % 2 == 0) {
- black_box(pos_x, pos_y);
- } else {
- white_box(pos_x, pos_y);
- }
- }
- }
- }
- private:
- void white_box(int x, int y) {
- glBegin(GL_LINE_LOOP);
- glVertex2i(x, y);
- glVertex2i(x, y + polygon_size);
- glVertex2i(x + polygon_size, y + polygon_size);
- glVertex2i(x + polygon_size, y);
- glEnd();
- }
- void black_box(int x, int y) {
- glBegin(GL_POLYGON);
- glVertex2i(x, y);
- glVertex2i(x, y + polygon_size);
- glVertex2i(x + polygon_size, y + polygon_size);
- glVertex2i(x + polygon_size, y);
- glEnd();
- glFlush();
- }
- GLint left_corner_x;
- GLint left_corner_y;
- const GLint polygon_size;
- };
- void display_function() {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0, 0.0, 0.0);
- const GLint start_x = 100;
- const GLint start_y = 100;
- // chessboard cb(start_x, start_y);
- // cb.redner();
- checker(checker::color::black, 100, 100, 50).render();
- glFlush();
- }
- void init() {
- glClearColor(1.0, 1.0, 1.0, 0.0);
- glColor3f(0.0f, 0.0f, 0.0f);
- glPointSize(1.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0, 600.0, 0.0, 600.0);
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(600, 600);
- glutInitWindowPosition(100, 150);
- glutCreateWindow("Draw an 8X8 chess board using loop");
- glutDisplayFunc(display_function);
- init();
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment