-2
我正在编写一个程序,以便在用户单击鼠标时绘制一个正方形,然后用户可以向上,向下,向左和向右移动它。用户移动对象时出现的问题是隐藏的三角形移动时从视图中消失
任何人都可以帮助我。
这是代码示例
#include <GL/glut.h>
#include<iostream.h>
#define ORTHO_SCALE 10.
GLint triangle_vertices[] = {15, 20,30, 20,10, 15};
GLint triangle_vertices2[] = {50, 70,80, 90,100, 130};
struct {
struct {
struct {
GLfloat x, y;
} pos;
GLfloat rot;
} triangle;
} sceneinfo;
void clearScreen()
{
glutSwapBuffers();
glColor3f (1.0, 1.0, 1.0);
int ver[]={38,0,1143,0,1143,800,38,1105};
glPushMatrix();
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_INT, 0, ver);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
exit(0);
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
glutSwapBuffers();
glPushMatrix();
glTranslatef(sceneinfo.triangle.pos.x, sceneinfo.triangle.pos.y, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_INT, 0, triangle_vertices2);
glDrawArrays(GL_TRIANGLES, 0, 3);
glPopMatrix();
glutSwapBuffers();
}
}
void display(void);
void special(int key, int x, int y);
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("test");
init();
glutDisplayFunc(display);
glutSpecialFunc(special);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
void display(void)
{
glColor3f(0.0, 0.0, 0.0);//set color of object
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(sceneinfo.triangle.pos.x, sceneinfo.triangle.pos.y, 0.);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_INT, 0, triangle_vertices);
glDrawArrays(GL_TRIANGLES, 0, 3);
glPopMatrix();
glutSwapBuffers();
}
void special(int key, int x, int y)
{
switch(key) {
case GLUT_KEY_LEFT:
sceneinfo.triangle.pos.x -= 0.2;
break;
case GLUT_KEY_RIGHT:
sceneinfo.triangle.pos.x += 0.2;
break;
case GLUT_KEY_UP:
sceneinfo.triangle.pos.y += 0.2;
break;
case GLUT_KEY_DOWN:
sceneinfo.triangle.pos.y -= 0.2;
break;
}
glutPostRedisplay();
}
问题不在sceneinfo.triangle.pos我的问题如何我可以重新绘制所有对象的新位置。 – Developer
我只是刚刚注意到你也在交换缓冲区并在'clearscreen()'中绘图,你也应该删除它。它只能用于清除渲染过程中使用的深度,渲染和任何其他缓冲区。 –
谢谢你帮助我 – Developer