Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdlib.h>
- #if defined(__APPLE__)
- #include <OpenGL/gl.h>
- #include <OpenGL/glu.h>
- #include <GLUT/glut.h>
- #else
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
- #include <windows.h>
- #endif
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #endif
- // Globalis valtozo, hogy eppen rajzolni akarunk-e
- bool drawing = false;
- const int kScreenWidth = 600, kScreenHeight = 600;
- struct Vector {
- float x, y;
- Vector(float x = 0, float y = 0) : x(x), y(y) {}
- } last_mouse_pos;
- void onInitialization() {
- glClearColor(0.1, 0.2, 0.3, 1); // A hatterszin beallitasa.
- glClear(GL_COLOR_BUFFER_BIT); // A kepernyo torlese, az uj hatterszinnel.
- }
- void onDisplay() {
- glutSwapBuffers();
- }
- // A esemeny kezelesekor az operacios rendszer koordinatarendszereben kapunk ertekeket,
- // azaz azt, hogy a bal felso saroktol hany pixel tavolsagra volt a kattintas. De nekunk
- // normalizalt eszkoz koordinatak (NDC - Normalized Device Coordinates) kellenek.
- // pl: ha az x = 147 -> az a kepernyo kozepetol(x = 300) balra van, ezert negativ lesz,
- // es absz erteke pedig az, hogy milyen tavolvan az origotol, ami 153 / 300.
- // Az oprendszer koordinata rendszerebe az y lefele no, mig NDC-ben felfele.
- Vector convertToNdc(float x, float y) {
- Vector ret;
- ret.x = (x - kScreenWidth/2) / (kScreenWidth/2);
- ret.y = (kScreenHeight/2 - y) / (kScreenHeight/2);
- return ret;
- }
- void onMouse(int button, int state, int x, int y) {
- if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
- glClear(GL_COLOR_BUFFER_BIT); // Jobb klikkre toroljuk a kepernyot.
- glutPostRedisplay(); // Szolunk, hogy az ablak tartalma megvaltozott, kerjuk a GLUT-ot, hogy hívja meg az onDisplay-t.
- } else if(button == GLUT_LEFT_BUTTON) { // Ha a bal gomb allapota megvaltozott.
- if(state == GLUT_DOWN) {
- drawing = true; // Ha lenyomtuk akkor rajzolni akarunk.
- Vector pos = convertToNdc(x, y); // Atvaltjuk a pontot.
- glBegin(GL_POINTS); { // Kirajzoljuk.
- glVertex2f(pos.x, pos.y);
- } glEnd();
- last_mouse_pos = pos; // Elmentjuk, hogy az elso szakasz, majd ebbol a pontbol indul.
- glutPostRedisplay(); // Szolunk, hogy az ablak megvaltozott, kerjuk az ujrarajzolasat.
- } else if(state == GLUT_UP) {
- drawing = false; // Ha most engedtuk fel, akkor mar nem akarunk rajzolni.
- }
- }
- }
- void onMouseMotion(int x, int y) {
- if(drawing) {
- Vector pos = convertToNdc(x, y); // Kiszamoljuk az eger jelenlegi helyzetet NDC-ben.
- glBegin(GL_LINES); { // Kirajzolunk egy vonalat az elozo es a mostani helyzete koze.
- glVertex2f(last_mouse_pos.x, last_mouse_pos.y);
- glVertex2f(pos.x, pos.y);
- } glEnd();
- glutPostRedisplay(); // Szolunk, hogy az ablak megvaltozott, kerjuk az ujrarajzolasat.
- last_mouse_pos = pos; // Frissitjuk a elozo helyzetet.
- }
- }
- void onKeyboard(unsigned char, int, int) {
- }
- void onKeyboardUp(unsigned char, int, int) {
- }
- void onIdle() {
- // Erre azert van szukseg mert nehany ablakkezelo (foleg linux-os)
- // magatol nem hivja meg az onDislay()-t egyszer se, ha csak kulon
- // meg nem kerjuk erre. Windows-on erre a pár sorra nincs szükség.
- static bool first_call = true;
- if(first_call) {
- glutPostRedisplay();
- first_call = false;
- }
- }
- int main(int argc, char **argv) {
- glutInit(&argc, argv);
- glutInitWindowSize(600, 600);
- glutInitWindowPosition(100, 100);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Grafika pelda program");
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- onInitialization();
- glutDisplayFunc(onDisplay);
- glutMouseFunc(onMouse);
- glutIdleFunc(onIdle);
- glutKeyboardFunc(onKeyboard);
- glutKeyboardUpFunc(onKeyboardUp);
- glutMotionFunc(onMouseMotion);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement