2012-07-30 3 views
1

OpenGL을 사용하여 점의 방향으로 2D 객체를 이동하는 방법 (GL_POINTS이 아니라 좌표)?OpenGL의 점으로 2D 객체 이동


내 코드의 더 나은 이해를 위해

: 나는 다른 소스 코드에 내 코드의 대부분을 splited 한

, 그러나 이것은 실제로 장면을 모양을 생성하고 설정하는 하나입니다

void setupScene(int clearColor[]) { 
    glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); 
    //glClearColor(250, 250, 250, 1.0); // Set the cleared screen colour to black. 
    glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // This sets up the viewport so that the coordinates (0, 0) are at the top left of the window. 

    // Set up the orthographic projection so that coordinates (0, 0) are in the top left. 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -10, 10); 

    // Back to the modelview so we can draw stuff. 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth buffer. 
} 

void drawScene() { 
    setupScene((int[]){250, 250, 250, 1}); 

    triangle(210, WINDOW_WIDTH, WINDOW_HEIGHT); 

    glBegin(GL_QUADS); 
    glColor3f(RGB(80), RGB(80), RGB(80)); 

    glPushMatrix(); 
    glTranslatef(400, 400, 0); 
    glVertex2d(200, 100); 
    glVertex2d(100, 100); 
    glVertex2d(100, 200); 
    glVertex2d(200, 200); 
    glPopMatrix(); 
    glEnd(); 

    glutSwapBuffers(); // Send the scene to the screen. 
} 

void update(int value) { 
    glutPostRedisplay(); // Tell GLUT that the display has changed. 
    glutTimerFunc(25, update, 0); // Tell GLUT to call update again in 25 milliseconds. 
} 

추신 : 죄송 합니다만 멍청한 소리가 들리지만 Java에서 C++로 바뀌고 있습니다. 따라서 플랫폼 간 게임을 할 수 있습니다. 또한 게임을 개발 한 적이 없으며 일부 Android, iOS 및 BlackBerry 앱도 개발하지 않았습니다.


솔루션

감사 @paddy, 나는 glTranslatef의 사용을 이해하려고 노력하고, 솔루션과 함께 제공되었다. 여기에 작업 코드, 그것은 100 × 100에 사각형을 생성하고 400x200까지 확장 될 때까지 이동합니다 :

void setupScene(int clearColor[]) { 
    glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); 
    //glClearColor(250, 250, 250, 1.0); // Set the cleared screen colour to black. 
    glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // This sets up the viewport so that the coordinates (0, 0) are at the top left of the window. 

    // Set up the orthographic projection so that coordinates (0, 0) are in the top left. 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -10, 10); 

    // Back to the modelview so we can draw stuff. 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth buffer. 
} 

int a = 100; 
int b = 200; 
int x = 100; 
int y = 100; 

void drawScene() { 
    setupScene((int[]){250, 250, 250, 1}); 

    triangle(210, WINDOW_WIDTH, WINDOW_HEIGHT); 

    glPushMatrix(); 
    glTranslatef(x, y, 0); 

    glBegin(GL_QUADS); 
    glColor3f(RGB(80), RGB(80), RGB(80)); 

    glVertex2d(b, a); 
    glVertex2d(a, a); 
    glVertex2d(a, b); 
    glVertex2d(b, b); 

    glEnd(); 

    glPopMatrix(); 

    glutSwapBuffers(); // Send the scene to the screen. 
} 

void update(int value) { 
    if (x != 400 && y != 200) { 
     x += 4; 
     y += 2; 
    } 
    glutPostRedisplay(); // Tell GLUT that the display has changed. 
    glutTimerFunc(25, update, 0); // Tell GLUT to call update again in 25 milliseconds. 
} 

OpenGL은 더 많은 내가 생각했던 것보다 쉽다. C/C++에 대한 두려움이 있었기 때문에 자바와 자바 스크립트에 익숙했기 때문에 정말 멋지고 간단하고 우아합니다. 도와 주셔서 감사합니다 @ 패디. :

+0

[무엇을 시도해 봤습니까?] (http://www.whathaveyoutried.com) –

+0

@ Code-Guru : 약간의 도움을 받으면 효과가 있습니다. 감사. –

+0

푸시 앤 팝 매트릭스 호출이 실제로 필요 없다는 것에 유의하십시오. 거기에는 조작을 분리하거나 worldspace에서 상대 변환을 빌드 할 수 있습니다. 월드 스페이스에서 항상 작업하고 있다면 행렬을 대신 덮어 씁니다. – paddy

답변

3

modelview 매트릭스를 변환해야합니다. 당신은 이미 모델 뷰 모드에있어 가정하면 :

glPushMatrix(); 
glTranslatef(x, y, z); 
// Draw your shape 
glPopMatrix(); 

[편집]

@paddy : 이런 식으로 뭔가를? 나는 이것을 시도했다. 그러나 정방형은 움직이지 않고있다. pastebin.com/2PCsy5kC

명시 적으로 modelview 매트릭스를 선택하십시오. 귀하의 예는 현재의 어떤 모드 알려하지 않습니다 보통의 시작 부분에

glSetMatrixMode(GL_MODELVIEW); 
glPushMatrix(); 
glTranslatef(x, y, z); 
// Draw your shape 
glPopMatrix(); 

하여 재설정하고 카메라를 설정 glLoadIdentity() 전화, 당신은 ... 모든 것을 다시 그래서 당신은 GL_PROJECTION 모드로 들어갑니다 렌더링, GL_MODELVIEW 행렬에 대해서도 이렇게하십시오.

+0

이것은 @ Nathan이 즉각적인 모드를 사용한다고 가정합니다. 그렇지 않습니까? –

+0

@TimCooper : 아니요, 이것은 비 즉각적 모드라고 부르는 모든 기능을합니다. –

+0

@paddy :이게 뭐니? 나는 이것을 시도했다. 그러나 정방형은 움직이지 않고있다. http://pastebin.com/2PCsy5kC –