Created
September 23, 2020 08:13
-
-
Save muhajirinlpu/cafabb9de0dd94534be77a9cb045e6c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <iostream> | |
#include <cmath> | |
#include <GL/glut.h> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
const int WIDTH = 640; | |
const int HEIGHT = 480; | |
class Color { | |
public: | |
double r, g, b; | |
// color use rgb range 0 - 255 | |
Color(int red, int green, int blue) | |
{ | |
r = (double (red)) / 255; | |
g = (double (green)) / 255; | |
b = (double (blue)) / 255; | |
} | |
void setColor() | |
{ | |
glColor3f(this->r, this->g, this->b); | |
} | |
}; | |
class Position { | |
public: | |
float x, y; | |
Position(float position_x, float position_y) | |
{ | |
x = position_x; | |
y = position_y; | |
} | |
}; | |
class Draw { | |
public: | |
static void base(Position positions[], int n, Color color, GLenum mode) | |
{ | |
glBegin(mode); | |
color.setColor(); | |
for (int i = 0; i < n; i++) { | |
// cout << positions[i].x << " " << positions[i].y << endl; | |
glVertex2f(positions[i].x, positions[i].y); | |
} | |
// exit(EXIT_SUCCESS); | |
glEnd(); | |
} | |
static void polyline(Position positions[], int n, Color color) | |
{ | |
base(positions, n, color, GL_LINE_STRIP); | |
} | |
static void polygon(Position positions[], int n, Color color) | |
{ | |
base(positions, n, color, GL_LINE_LOOP); | |
} | |
static void fillPolygon(Position positions[], int n, Color color) | |
{ | |
base(positions, n, color, GL_POLYGON); | |
} | |
}; | |
void userdraw(void) { | |
Color yellow = *new Color(230, 224, 62); | |
std::vector<Position> shape; | |
double srad, r; | |
for (int i = 0; i < 360; i++) { | |
srad = i * M_PI / 180; | |
r = sin(5 * srad); | |
shape.push_back(* new Position(r * cos(srad), r * sin(srad))); | |
} | |
Draw::fillPolygon(shape.data(), 360, yellow); | |
} | |
void display(void) { | |
glClear( GL_COLOR_BUFFER_BIT); | |
userdraw(); | |
glutSwapBuffers(); | |
} | |
int main(int argc, char **argv) { | |
glutInit(&argc,argv); | |
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB ); | |
glutInitWindowPosition(100,100); | |
glutInitWindowSize(WIDTH, HEIGHT); | |
glutCreateWindow("night view"); | |
glClearColor(0.0, 0.0, 0.0, 0.0); | |
// gluOrtho2D(0., WIDTH, 0., HEIGHT); | |
glutIdleFunc(display); | |
glutDisplayFunc(display); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment