Created
May 26, 2020 16:34
-
-
Save wowotek/94e93a1b52b8f4b2738ae5495288edc6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <GL/glut.h> | |
#include <iostream> | |
#include <vector> | |
// DEFINITIONS // | |
template <typename T> | |
struct Vec2 { | |
T x; | |
T y; | |
inline Vec2<T>(T initX, T initY) : x(initX), y(initY) {} | |
}; | |
typedef Vec2<int> Point; | |
template <typename T> | |
struct Vec3 { | |
T r; | |
T g; | |
T b; | |
inline Vec3<T>(T iR, T iG, T iB) : r(iR), g(iG), b(iB) {} | |
inline Vec3<T>(T iS) : r(iS), g(iS), b(iS) {} | |
inline Vec3<T>() : r(0), g(0), b(0) {} | |
}; | |
typedef Vec3<unsigned char> Color; | |
#define colorUnpack(c) c.r, c.g, c.b | |
typedef struct tShapeProperties { | |
Color color; | |
unsigned int mode; | |
tShapeProperties(Color initColor, unsigned int initMode): color(initColor), mode(initMode) {} | |
} ShapeProperties; | |
struct Template { | |
ShapeProperties kotak_kiri; | |
ShapeProperties segitiga; | |
ShapeProperties kotak_kanan; | |
}; | |
//-------------// | |
#define TEMPLATE_NORMAL 0; | |
#define TEMPLATE_WHITE 1; | |
#define TEMPLATE_LINES 2; | |
Template shapeTemplate[3] = { | |
Template{ | |
ShapeProperties(Color(0, 0, 255), GL_POLYGON), | |
ShapeProperties(Color(255, 0, 0), GL_POLYGON), | |
ShapeProperties(Color(0, 255, 0), GL_POLYGON) | |
}, | |
Template{ | |
ShapeProperties(Color(255, 255, 255), GL_POLYGON), | |
ShapeProperties(Color(255, 255, 255), GL_POLYGON), | |
ShapeProperties(Color(255, 255, 255), GL_POLYGON) | |
}, | |
Template{ | |
ShapeProperties(Color(255, 255, 255), GL_LINE_LOOP), | |
ShapeProperties(Color(255, 255, 255), GL_LINE_LOOP), | |
ShapeProperties(Color(255, 255, 255), GL_LINE_LOOP) | |
}, | |
}; | |
unsigned int currentTemplate = TEMPLATE_NORMAL; | |
std::vector<Point> *points = new std::vector<Point>; | |
void render(void) { | |
glClear(GL_COLOR_BUFFER_BIT); | |
// Gambar Kotak Kiri | |
glColor3f(colorUnpack(shapeTemplate[currentTemplate].kotak_kiri.color)); | |
glBegin(shapeTemplate[currentTemplate].kotak_kiri.mode); | |
glVertex2f(40, 175); | |
glVertex2f(40, 300); | |
glVertex2f(125, 300); | |
glVertex2f(125, 175); | |
glEnd(); | |
// Segitiga | |
glColor3f(colorUnpack(shapeTemplate[currentTemplate].segitiga.color)); | |
glBegin(shapeTemplate[currentTemplate].segitiga.mode); | |
glVertex2f(170, 300); | |
glVertex2f(300, 300); | |
glVertex2f(230, 175); | |
glEnd(); | |
// Gambar Kotak Kanan | |
glColor3f(colorUnpack(shapeTemplate[currentTemplate].kotak_kanan.color)); | |
glBegin(shapeTemplate[currentTemplate].kotak_kanan.mode); | |
glVertex2f(40 + 310, 175); | |
glVertex2f(40 + 310, 300); | |
glVertex2f(125 + 310, 300); | |
glVertex2f(125 + 310, 175); | |
glEnd(); | |
// Gambar Titik | |
glColor3f(1, 1, 1); | |
glPointSize(5); | |
glBegin(GL_POINTS); | |
for (unsigned int i = 0; i < points->size(); i++) { | |
glVertex2f(points->at(i).x, points->at(i).y); | |
} | |
glEnd(); | |
glFlush(); | |
} | |
void onKeyboardDownEvent(unsigned char key, int mouseX, int mouseY) { | |
if (key == 'a' || key == 'A') { // Normal | |
currentTemplate = TEMPLATE_NORMAL; | |
} | |
else if (key == 's' || key == 'S') { // White | |
currentTemplate = TEMPLATE_WHITE; | |
} | |
else if (key == 'd' || key == 'D') { // LINES | |
currentTemplate = TEMPLATE_LINES; | |
} | |
glutPostRedisplay(); | |
} | |
void onMouseClick(int button, int buttonState, int mouseX, int mouseY) { | |
if (button == 0 && buttonState == 0) { | |
points->push_back(Point(mouseX, mouseY)); | |
} | |
glutPostRedisplay(); | |
} | |
int main(int argc, char** argv) { | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); | |
glutInitWindowSize(500, 500); | |
glutCreateWindow("Coba Title"); | |
glutDisplayFunc(render); | |
glutKeyboardFunc(onKeyboardDownEvent); | |
glutMouseFunc(onMouseClick); | |
gluOrtho2D(0, 500, 500, 0); | |
glClearColor(0, 0, 0, 1); | |
glEnable(GL_POINT_SMOOTH); | |
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment