本文介绍了OpenGL运动C ++ 2问题1问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我刚刚拿出这段代码来测试正方形的运动,但是我在结构上有两个问题和一个问题.前两个问题是每当帧渲染正方形变得更小直到在大约3个渲染中都看不到它时,第二个问题是移动功能似乎不起作用,当我从中拉出它的地方说它显然应该起作用时,带给我有关结构的问题,我在运动中写的是​​什么开关,案例,它上面有一个教程,因为它看起来很有趣而且很有用.




Okay well i just pulled out this code to test the movement of a square, but i have two problems and one question on structure. first two problems are every time the frame renders the square becomes smaller till it can''t be seen in about 3 renders, secondly the movement function doesn''t seem to work when the place i pulled it from says it apparently should work which brings me to my question about structure, What is the switch, case thing that i have written in movement, it there a tutorial on it because it seems interesting and useful.




#include <iostream>
#include <gl\glut.h>
using namespace std;
unsigned int Character(0);
float Posx(1);
float Posy(1);
int Test(0);
void DisplayListInit()
{
	Character = glGenLists(1);
	glNewList(Character, GL_COMPILE);
		glBegin(GL_QUADS);
			glColor3i(0, 0, 1);
			glVertex2f(-1, -1);
			glVertex2f(-1, 1 );
			glVertex2f( 1, 1 );
			glVertex2f( 1, -1);
		glEnd();
	glEndList();
	cout << "List Declared\n";
}
void Render()
{
	glClearColor(1, 1, 1, 1);
	glClearDepth(0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glOrtho(-10, 10, -10, 10, -1, 1);
	glPushMatrix();
		glTranslatef(Posx, Posy, 1);
		glCallList(Character);
	glPopMatrix();
	glutSwapBuffers();
	cout << "Render Complete\n";
	if(Test < 1){
		glutPostRedisplay();
		Test ++;
	}
}
void Movement(unsigned char key, int x, int y)

	switch (key){
		case ''a'':
			Posx -= 0.5;
			break;
		case ''s'':
			Posy -= 0.5;
			break;
		case ''d'':
			Posx += 0.5;
			break;
		case ''w'':
			Posy += 0.5;
			break;
		default:
			break;
	}
	glutPostRedisplay();
	cout << "****************Movement done\n";
}
int main (int argc, char **argv )
{
	glutInit(&argc, argv);
	glutInitWindowSize(800, 800);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutCreateWindow("Boxs");
	DisplayListInit();
	glutDisplayFunc(Render);
	glutKeyboardFunc(Movement);
	//glutIdleFunc(Render);
	glutMainLoop();
}

推荐答案


glClearColor(1, 1, 1, 1);
    glClearDepth(0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity(); // Reset your current matrix with idendity matrix.
    glOrtho(-10, 10, -10, 10, -1, 1);
    glPushMatrix();
    glTranslatef(Posx, Posy, 1);
    glCallList(Character);
    glPopMatrix();


这篇关于OpenGL运动C ++ 2问题1问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:33