Advertisement
Guest User

Untitled

a guest
Sep 10th, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. #ifndef POLYEDITOR_H
  2. #define POLYEDITOR_H
  3.  
  4. #define MAXPOLYGONS 32
  5. #define LENOFLINE 64
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <GL/freeglut.h>
  10.  
  11. typedef struct eVector2d{
  12. double a;
  13. double b;
  14. } eVector2d_t;
  15.  
  16. typedef struct ePolygon{
  17. eVector2d_t v[3];
  18. } ePolygon_t;
  19.  
  20. typedef struct eObject{
  21. int count;
  22. int brightness;
  23. ePolygon_t polygons[MAXPOLYGONS];
  24. } eObject_t;
  25.  
  26. eObject_t *gobject;
  27. int *gsize;
  28.  
  29. void addVert(int x, int y, short clear){
  30. static int currVert = 0;
  31. if(clear){
  32. currVert = 0;
  33. return;
  34. }
  35. gobject->polygons[gobject->count].v[currVert].a = ((double)x)/((double)glutGet(GLUT_WINDOW_WIDTH));
  36. gobject->polygons[gobject->count].v[currVert].b = ((double)y)/((double)glutGet(GLUT_WINDOW_HEIGHT));
  37. currVert+=1;
  38. if(currVert>=3){
  39. currVert = 0;
  40. gobject->count+=1;
  41. }
  42. }
  43.  
  44. int removePoly(){
  45. gobject->count -= 1;
  46. if(gobject->count < 0)
  47. gobject->count = 0;
  48. return gobject->count;
  49. }
  50.  
  51. void getWinSize(){
  52. }
  53.  
  54. void mouseClicks(int button, int state, int x, int y){
  55. if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
  56. addVert(x, y, 0);
  57. }else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){
  58. removePoly();
  59. }
  60. }
  61.  
  62. void keypress(unsigned char key, int x, int y){
  63. switch(key){
  64. case('q'):
  65. glutDestroyWindow(glutGetWindow());
  66. break;
  67. case('r'):
  68. while(removePoly());
  69. addVert(x, y, 1);
  70. break;
  71. case('h'):
  72. printf("Object Generator Help Page\n\tThis tool is to assist in the creation of a 2D object\n\nControls:\n\tLMB-Places a new vertex, takes 3 vertecies to create a polygon\n\tRMB-Delete last polygon\n\tQ-Save and quit the software\n\tR-Reset all polygons\n\tH-Display this page\n");
  73. break;
  74. default:
  75. printf("Unrecognized keypress (\'%c\').\nPress H to show the help page\n", key);
  76. }
  77. }
  78.  
  79. void idle(){
  80. glutPostRedisplay();
  81. }
  82.  
  83. void display(){
  84. gsize[0] = glutGet(GLUT_WINDOW_WIDTH);
  85. gsize[1] = glutGet(GLUT_WINDOW_HEIGHT);
  86. glClear(GL_COLOR_BUFFER_BIT);
  87. for(int i = 0; i<gobject->count; i++){
  88. float stdBrightness = ((float)gobject->brightness)/5.0f;
  89. glColor3f(stdBrightness, stdBrightness, stdBrightness);
  90. glBegin(GL_POLYGON);
  91. glVertex3f(gobject->polygons[i].v[0].a*2-1, -(gobject->polygons[i].v[0].b*2-1), 0);
  92. glVertex3f(gobject->polygons[i].v[1].a*2-1, -(gobject->polygons[i].v[1].b*2-1), 0);
  93. glVertex3f(gobject->polygons[i].v[2].a*2-1, -(gobject->polygons[i].v[2].b*2-1), 0);
  94. glEnd();
  95. }
  96. glFlush();
  97. }
  98.  
  99. //char *polyEditor(char *name, int brightness){
  100. int main(){
  101. int brightness = 1;
  102. char intToCharBrightness[5] = {'.','<','|','X','$'};
  103. gobject = calloc(1,sizeof(eObject_t));
  104. gobject->brightness = brightness;
  105. gsize = malloc(2*sizeof(int));
  106. //make up the args for glutInit
  107. int x = 1;
  108. char **j = malloc(sizeof(char*));
  109. j[0] = malloc(2);
  110. j[0][0] = 'a';
  111. j[0][1] = 0;
  112. //start opengl
  113. glutInit(&x, j);
  114. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  115. glutInitWindowPosition(80, 80);
  116. glutInitWindowSize(400, 300);
  117. glutCreateWindow("Object Creator | h for Help");
  118. glClear(GL_COLOR_BUFFER_BIT);
  119. glutDisplayFunc(display);
  120. glutIdleFunc(idle);
  121. glutKeyboardFunc(keypress);
  122. glutMouseFunc(mouseClicks);
  123. glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
  124. glutMainLoop();
  125. free(j[0]);
  126. free(j);
  127. //formulate response
  128. char *content = malloc(gobject->count*LENOFLINE);
  129. for(int i = 0; i < gobject->count; i++){
  130. printf("%f %f %f %f %f %f %c\n", gobject->polygons[i].v[0].a, gobject->polygons[i].v[0].b, gobject->polygons[i].v[1].a, gobject->polygons[i].v[1].b, gobject->polygons[i].v[2].a, gobject->polygons[i].v[2].b, intToCharBrightness[gobject->brightness-1]);
  131. }
  132. free(content);
  133. free(gsize);
  134. free(gobject);
  135. // return content;
  136. }
  137.  
  138. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement