OpenGL运动C ++ 2问题1问题. [英] OpenGL Movement C++ 2 problems 1 question.

查看:89
本文介绍了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();
}

推荐答案

我讨厌这样说,但是当您似乎根本不理解switch语句的含义时,尝试调试此代码似乎没有什么意义.做.掌握C ++书籍,搜索Google或查看 MSDN [ ^ ]有关使用C ++进行编码的有用信息.
I hate to say this but there seems little point in you trying to debug this code when you apparently do not even understand what a switch statement does. Get hold of a C++ book, search Google, or look in MSDN[^] for some useful information about coding in C++.


由于glOrtho使用不当而导致的第一个问题.
glOrtho函数将当前矩阵乘以正交矩阵.
第一次投影矩阵乘以正交投影.
接下来的渲染还会再次乘以先前的矩阵,这将导致问题.

只需调用glOrtho()即可解决此问题.
或在调用glOrtho之前调用glLoadIdendity().

First problem caused by improper usage of glOrtho.
The glOrtho function multiplies the current matrix by an orthographic matrix.
First time projection matrix is multipled for ortho projection.
Next renderings also multiplies previous matrix again and will cause issue.

This issue can be solved by calling glOrtho() once.
Or call glLoadIdendity() before calling glOrtho.

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问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆