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++에 대한 두려움이 있었기 때문에 자바와 자바 스크립트에 익숙했기 때문에 정말 멋지고 간단하고 우아합니다. 도와 주셔서 감사합니다 @ 패디. :
[무엇을 시도해 봤습니까?] (http://www.whathaveyoutried.com) –
@ Code-Guru : 약간의 도움을 받으면 효과가 있습니다. 감사. –
푸시 앤 팝 매트릭스 호출이 실제로 필요 없다는 것에 유의하십시오. 거기에는 조작을 분리하거나 worldspace에서 상대 변환을 빌드 할 수 있습니다. 월드 스페이스에서 항상 작업하고 있다면 행렬을 대신 덮어 씁니다. – paddy