2014-10-30 2 views
0

this post에서 사각형은 마우스를 통해 이동했습니다. 삼각형을 추가하고 마우스를 통해 직사각형처럼 움직이고 싶습니다.OpenGL에서 많은 도형 이동

삼각형 이런 기능 :

void drawTriangle(float x,float y,float size){ 
glPushMatrix(); 
glTranslatef(x, y, 0.0f); 
glScalef(size, size, 1.0f); 
glBegin(GL_TRIANGLES); 
glColor3ub(255, 255, 255); 
glVertex2f( -1, 1); 
glVertex2f( 1, -1); 
glVertex2f( 1, 1); 
glEnd(); 
glPopMatrix(); 

직사각형 삼각형 함께 이동. 그러나 나는 그것을 다르게 움직이고 싶다. 그럼 내 잘못은 뭐니?

+0

당신이 게시 코드는 단지 그립니다 'x'와'y' 좌표에 삼각형이 있습니다. 마우스 입력이나 삼각형이 나타나는 위치를 추적하는 것과 아무런 관련이 없습니다. – Wyzard

답변

1

당신은 모양 객체의 배열을 유지하고 마우스 충돌에 대한 각각의 테스트뿐만 아니라 모양은 드래그중인 를 추적해야합니다 :

#include <GL/glut.h> 
#include <vector> 

using namespace std; 

struct Shape 
{ 
    float mX, mY; 
    float mSize; 
    bool mIsRectangle; 

    bool PointInside(const float x, const float y) const 
    { 
     return 
      mX - mSize <= x && x <= mX + mSize 
      && 
      mY - mSize <= y && y <= mY + mSize; 
    } 
}; 

vector<Shape> objects; 
Shape* dragging = NULL; 
void mouse(int button, int state, int x, int y) 
{ 
    if(GLUT_DOWN == state) 
    { 
     dragging = NULL; 
     for(Shape& obj : objects) 
     { 
      if(obj.PointInside(x, y)) 
      { 
       dragging = &obj; 
       glutPostRedisplay(); 
       break; 
      } 
     } 
    } 
    else 
    { 
     dragging = NULL; 
    } 
} 

void motion(int x, int y) 
{ 
    if(dragging) 
    { 
     dragging->mX = x; 
     dragging->mY = y; 
     glutPostRedisplay(); 
    } 
} 

void drawRect(float x, float y, float size) 
{ 
    glPushMatrix(); 
    glTranslatef(x, y, 0.0f); 
    glScalef(size, size, 1.0f); 
    glBegin(GL_QUADS); 
    glColor3ub(255, 255, 255); 
    glVertex2f(-1, -1); 
    glVertex2f( 1, -1); 
    glVertex2f( 1, 1); 
    glVertex2f(-1, 1); 
    glEnd(); 
    glPopMatrix(); 
} 

void drawTriangle(float x, float y, float size) 
{ 
    glPushMatrix(); 
    glTranslatef(x, y, 0.0f); 
    glScalef(size, size, 1.0f); 
    glBegin(GL_TRIANGLES); 
    glColor3ub(255, 255, 255); 
    glVertex2f( -1, 1); 
    glVertex2f( 1, -1); 
    glVertex2f( 1, 1); 
    glEnd(); 
    glPopMatrix(); 
} 

void display() 
{ 
    glClearColor(0, 0, 0, 1); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    const double w = glutGet(GLUT_WINDOW_WIDTH); 
    const double h = glutGet(GLUT_WINDOW_HEIGHT); 
    glOrtho(0, w, h, 0, -1, 1); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    for(const Shape& obj : objects) 
    { 
     if(obj.mIsRectangle) 
      drawRect(obj.mX, obj.mY, obj.mSize); 
     else 
      drawTriangle(obj.mX, obj.mY, obj.mSize); 
    } 

    glutSwapBuffers(); 
} 

int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); 
    glutInitWindowSize(600, 600); 
    glutCreateWindow("GLUT"); 
    glutDisplayFunc(display); 
    glutMouseFunc(mouse); 
    glutMotionFunc(motion); 

    Shape temp; 
    temp.mSize = 50; 

    temp.mX = temp.mY = 100; 
    temp.mIsRectangle = true; 
    objects.push_back(temp); 

    temp.mX = temp.mY = 200; 
    temp.mIsRectangle = false; 
    objects.push_back(temp); 

    glutMainLoop(); 
    return 0; 
} 
+0

배열 또는 클래스입니다. –

+0

@GavinSimpson : 저는 * 계급이 어떻게 도움이되는지 모르겠습니다. 클래스는 여러 객체에 대한 정보를 보유하는 컨테이너가 아닙니다. 당신은 확실히 이것을위한 컨테이너를 원합니다. – datenwolf

+0

그러나 각 도형에 고유 한 클래스가 있으면 자체 코드도 포함 할 수 있습니다. –

관련 문제