opengl 도형옮기기 질문.

cey0116의 이미지

opengl초본대요.. 고수님들이라면 바로 보면 아실거라고 생각되네요.
마우스를 클릭하면 삼각형이나 사각형이 그려지고 다시 그려진 도형들을 클릭해서 드래깅하면 옮겨져야 합니다. 셀렉트나 피킹쓰지 않고 자리를 옮기도록 해야하는데 제 생각에 배열에 들어가있는 값들을 새로운 자리의 배열로 옮기면 된다고 생각하는데.. 어쨋든 잘 안돼네요. 좀 도와주세요~~~ 절박합니다 낼까지 인데..ㅠ_ㅠ

#include "stdafx.h"
#include
#include
#include
#include // Must come first to avoid redef error
#include
#define NUMBER_OF_POINT 1000
#include
//using namespace std;

// Constants and enums
enum buttonTypes {SQUARE_BUTTON_ID = 0, TRIANGLE_BUTTON_ID=1} buttonA;
enum colors { RED =0, GREEN=1, BLUE=2} buttonB;
const int SQUARE_WIDTH = 10;
const int TRIANGLE_WIDTH = 10;
const int WIN_WIDTH = 500;
const int WIN_HEIGHT = 500;

// These are the live variables controlled/modified by the GUI
int main_window;
int red = 0;
int green = 0;
int blue = 0;
GLUI *glui;
//GLint xa;
//GLint ya;
GLfloat myVertex[100][2];
GLfloat myVertexCol[100][3];
GLint count=0;
void buttonCB(int control)
{
switch(control)
{
case SQUARE_BUTTON_ID:
printf ("Square\n");
buttonA = SQUARE_BUTTON_ID;
break;
case TRIANGLE_BUTTON_ID:
printf("Triangle\n");
buttonA = TRIANGLE_BUTTON_ID;
break;
}
// This causes opengl to redraw
glutPostRedisplay();
}

void colorCB(int id)
{

switch(id)
{
case RED:
printf("Changing Red\n");
glColor3f(1.0f, 0.0f, 0.0f);
buttonB = RED;
break;
case GREEN:
printf("Changing Green\n");
glColor3f(0.0f, 1.0f, 0.0f);
buttonB = GREEN;
break;
case BLUE:
printf("Changing Blue\n");
buttonB = BLUE;
break;
}

glutPostRedisplay();
}
void drawSquare(int xa, int ya)
{
GLint index;
myVertexCol[count][3]=(1.0,0.0,0.0);
glColor3f(1, 0, 0);

ya=WIN_HEIGHT-ya;

//for (index=0;index < count;index++){
// if (abs(xa-myVertex

    [0]) !=NULL && abs(ya-myVertex
      [1]) !=NULL){
      // printf("hh");
      // }else
      glBegin(GL_POLYGON);
      glVertex2f(xa+SQUARE_WIDTH, ya+SQUARE_WIDTH);
      glVertex2f(xa-SQUARE_WIDTH, ya+SQUARE_WIDTH);
      glVertex2f(xa-SQUARE_WIDTH, ya-SQUARE_WIDTH);
      glVertex2f(xa+SQUARE_WIDTH, ya-SQUARE_WIDTH);

      //}
      glEnd();

      glFlush();

      }

      void drawTriangle(int xa, int ya)
      {
      glColor3f(1, 0, 0);
      myVertexCol[count][3]=(1.0,0.0,0.0);
      ya=WIN_HEIGHT-ya;
      glBegin(GL_POLYGON);
      glVertex2f(xa, ya);
      glVertex2f(xa-SQUARE_WIDTH, ya-SQUARE_WIDTH);
      glVertex2f(xa+SQUARE_WIDTH, ya-SQUARE_WIDTH);
      //glVertex2f(xa+SQUARE_WIDTH, ya-SQUARE_WIDTH);
      glEnd();
      glFlush();
      }

      void drawLine()
      {
      // Code to draw a line
      }

      // The display function is called whenever OpenGL needs to refresh the image
      void myGlutDisplay(void)
      {

      glutSwapBuffers();
      }


      // This is for double buffering...it's necessary for interactive applications.
      // We'll learn more about it later

      //Code goes in here!

      void myGlutReshape(int x, int y)
      {
      // This will not allow them to resize the window! We'll change this
      // later down the road
      /*glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluOrtho2D(0.0, x, 0.0, y);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();

      glViewport(0,0,x,y);
      glClearColor(1.0, 1.0, 1.0, 1.0);
      glClear(GL_COLOR_BUFFER_BIT);
      glFlush();*/

      glutReshapeWindow(WIN_WIDTH, WIN_HEIGHT);

      }

      void myGlutMotion(int x, int y)
      {


      myGlutDisplay();
      }

      /*void myCol(int xa, int ya, int r, int g, int b)
      {
      GLint index, index1;
      myVertexCol[count][3]=(r,g,b);
      glColor3f(r+0.1,g,b);
      myVertexCol[count][3]=(r+0.2,g,b);
      ya=WIN_HEIGHT-ya;

      }*/
      void myGlutMouse (int button, int button_state, int x, int y)
      {

      //Callback for mouse presses
      if(button == GLUT_LEFT_BUTTON && button_state == GLUT_DOWN)
      {
      GLint index,index1,index2,index3,index4;
      myVertex[count][0]=x;
      myVertex[count][1]=y;


      for (index=0;index < count;index++){

      if (abs(myVertex

        [0]-x) > SQUARE_WIDTH && abs(myVertex
          [1]-y) > SQUARE_WIDTH)
          {
          switch (buttonA)
          {
          case SQUARE_BUTTON_ID:
          drawSquare(myVertex[count][0],myVertex[count][1]);
          break;

          case TRIANGLE_BUTTON_ID:
          drawTriangle(myVertex[count][0],myVertex[count][1]);
          break;
          }
          }
          if (abs(myVertex

            [0]-x) < SQUARE_WIDTH && abs(myVertex
              [1]-x) < SQUARE_WIDTH)
              {
              //int oldx=x;
              //int oldy=y;

              /*if(button == GLUT_LEFT_BUTTON && button_state == GLUT_UP){
              for(index1=oldx-5,index3=x-5;index1 for(index2=oldx-5,index4=x-5;index2 myVertex[index3][index4]=myVertex[oldx][oldy];
              }
              }*/

              switch(buttonB){

              case RED:
              // myCol(myVertex[index1][index2]);
              case BLUE:
              // myCol(count);
              case GREEN:
              break;
              // myCol(count);
              }

              }
              }


              count++;
              myGlutDisplay();
              }

              }

              void initScene()
              {
              ///All initialization stuff goes here
              /*glClear(GL_COLOR_BUFFER_BIT);
              glClearColor(1.0, 1.0, 1.0,1.0); //white background
              glutReshapeFunc(myGlutReshape);*/
              glMatrixMode(GL_PROJECTION);
              glLoadIdentity();
              gluOrtho2D(0.0, WIN_WIDTH, 0.0, WIN_HEIGHT);
              glMatrixMode(GL_MODELVIEW);
              glLoadIdentity();

              glViewport(0,0,WIN_WIDTH,WIN_HEIGHT);
              glClearColor(1.0, 1.0, 1.0, 1.0);
              glClear(GL_COLOR_BUFFER_BIT);
              glFlush();

              }

              int main(int argc, char **argv)
              {

              // setup glut
              glutInit(&argc, argv);
              glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE); //We'll discuss these later
              glutInitWindowPosition( 50, 50 );
              glutInitWindowSize( WIN_WIDTH , WIN_HEIGHT );

              glMatrixMode(GL_PROJECTION);
              //You'll need a handle for your main window for GLUI
              main_window = glutCreateWindow( "Graph Maker" );
              glutDisplayFunc( myGlutDisplay);
              glutReshapeFunc( myGlutReshape );
              glutMouseFunc( myGlutMouse );
              glutMotionFunc( myGlutMotion);

              // Initialize my Scene
              initScene();

              //Build the GUI
              glui = GLUI_Master.create_glui( "Graph Maker GUI", 0, 600, 50 );
              GLUI_Panel *componentPanel = glui->add_panel("Graph Components");
              glui->add_button_to_panel(componentPanel, "Square", SQUARE_BUTTON_ID, buttonCB);
              glui->add_button_to_panel(componentPanel, "Triangle", TRIANGLE_BUTTON_ID, buttonCB);
              glui->add_separator();

              GLUI_Panel *colorPanel = glui->add_panel("Color");
              // These could be done with floats but I ran into some issues
              GLUI_Spinner *redValue = glui->add_spinner_to_panel(colorPanel, "Red", 2, &red, RED, colorCB);
              redValue->set_int_limits(0, 255);
              GLUI_Spinner *greenValue = glui->add_spinner_to_panel(colorPanel, "Green", 2, &green,GREEN, colorCB);
              greenValue->set_int_limits(0,255);
              GLUI_Spinner *blueValue = glui->add_spinner_to_panel(colorPanel, "Blue", 2, &blue, BLUE, colorCB);
              blueValue->set_int_limits(0.0, 255);
              glui->set_main_gfx_window( main_window );

              // We register the idle callback with GLUI, *not* with GLUT
              //GLUI_Master.set_glutIdleFunc( myGlutIdle );
              GLUI_Master.set_glutIdleFunc( NULL );
              glutMainLoop();
              return EXIT_SUCCESS;
              }

              댓글 달기

              Filtered HTML

              • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
              • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
              • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
              • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

              BBCode

              • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
              • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
              • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
              • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

              Textile

              • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
              • You can use Textile markup to format text.
              • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

              Markdown

              • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
              • Quick Tips:
                • Two or more spaces at a line's end = Line break
                • Double returns = Paragraph
                • *Single asterisks* or _single underscores_ = Emphasis
                • **Double** or __double__ = Strong
                • This is [a link](http://the.link.example.com "The optional title text")
                For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
              • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
              • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

              Plain text

              • HTML 태그를 사용할 수 없습니다.
              • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
              • 줄과 단락은 자동으로 분리됩니다.
              댓글 첨부 파일
              이 댓글에 이미지나 파일을 업로드 합니다.
              파일 크기는 8 MB보다 작아야 합니다.
              허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
              CAPTCHA
              이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.