简体   繁体   English

如何使用矩阵在OpenGL中移动和旋转对象

[英]How to move and rotate an object in opengl using matrices

So im pretty new to opengl and i got an object to appear on the screen and i also have game loop but i cant get the triangle i made to move and if i do get it to move it doesn't redraw itself. 因此,我对opengl相当陌生,我有一个对象出现在屏幕上,我也有游戏循环,但我无法移动到我要移动的三角形,如果我移动了它,它就不会重绘。

PaintGl draws my triangle to the screen PaintGl将我的三角形绘制到屏幕上

void MeGlWindow::paintGL()
{
GLint TriPositionUniformLocation = glGetUniformLocation(programID, "TriPosition");
TriPosition = rot * tra;
glUniformMatrix3fv(TriPositionUniformLocation, 1, GL_FALSE, &TriPosition[0][0]);

//glClearColor(1, 0, 0, 1);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, width(), height());
//glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);

}

my update gets called every frame and im trying to use this to get keyboard input to know when to move an object. 我的更新被称为每一帧,而我试图使用它来获取键盘输入以知道何时移动对象。 right now im trying to translate if the u or down key is pressed and rotate if the left or right key is pressed. 现在,如果要按u或下键,则尝试翻译;如果按左键或右键,则尝试旋转。

void MeGlWindow::myUpdate()
{


short Left = GetAsyncKeyState(37);
short Up = GetAsyncKeyState(38);
short Right = GetAsyncKeyState(39);
short Down = GetAsyncKeyState(40);
float speed = .001f;
float angle = 0;


if (Left != 0)
{
    angle -= .5;
    rot = mat3(cos(angle), -sin(angle), rot[0][2], sin(angle), cos(angle), rot[1][2], rot[2][0], rot[2][1], rot[2][2]);
    cout << "moved" << endl;
}

if (Right != 0)
{
    angle += .5;
    rot = mat3(cos(angle), -sin(angle), rot[0][2], sin(angle), cos(angle), rot[1][2], rot[2][0], rot[2][1], rot[2][2]);
    cout << "moved" << endl;
}
if (Up != 0)
{
    tra = mat3(tra[0][0], tra[0][1], tra[0][2], tra[1][0], tra[1][1], tra[1][2] + speed, tra[2][0], tra[2][1], tra[2][2]);
    cout << "moved" << endl;
}
if (Down != 0)
{
    tra = mat3(tra[0][0], tra[0][1], tra[0][2], tra[1][0], tra[1][1], tra[1][2] + -speed, tra[2][0], tra[2][1], tra[2][2]);
    cout << "moved" << endl;
}
if (angle < 0) angle += 360;
if (angle > 360) angle -= 360;



paintGL();
}

my last piece of code is vertexshadercode that im using to draw the triangle aswell. 我的最后一段代码是vertexshadercode,我也用来绘制三角形。

#version 430

in layout(location=0) vec2 position;
in layout(location=1) vec3 vertexColor;

uniform mat3 TriPosition;

out vec3 theColor;

void main() 
{
vec4 v = vec4(position, 0.0, 1.0);
gl_Position.x = gl_Position.x + TriPosition[0][2];
gl_Position.y = gl_Position.y + TriPosition[1][2];
theColor = vertexColor;
};

Why is my triangle not moving or rotating. 为什么我的三角形不动或不旋转。 I was able to do this when i wasnt using opengl but now im lost. 当我不使用opengl时,我能够做到这一点,但现在我迷路了。

1. 1。

I'm not sure without access to your constructor of TriPosition, but it is possible that your matrix have been transposed in the process of converting from 'mat3' class to your 'TriPosition' class. 我不确定是否无法访问TriPosition的构造函数,但是在从'mat3'类转换为'TriPosition'类的过程中,可能已对矩阵进行了转置。

glUniformMatrix3fv(TriPositionUniformLocation, 1, GL_FALSE, &TriPosition[0][0]); glUniformMatrix3fv(TriPositionUniformLocation,1,GL_FALSE,&TriPosition [0] [0]);

Try to change the third argument to GL_TRUE, which will make your input transposed when passed to GPU. 尝试将第三个参数更改为GL_TRUE,这将使您的输入在传递给GPU时转置。 Test and see what happens. 测试,看看会发生什么。

glUniformMatrix3fv(TriPositionUniformLocation, 1, GL_TRUE, &TriPosition[0][0]); glUniformMatrix3fv(TriPositionUniformLocation,1,GL_TRUE,&TriPosition [0] [0]);

The reason why nothing happens is explained in the comment by Reto. Reto的评论中解释了什么也没发生的原因。

2. 2。

Although, I can see that your triangle is meant to live under 2D space, try to use mat4 instead of mat3 in vertex shader, so that you will only need to multiply matrix and vertex, instead of getting value out like what you are doing there. 虽然,我可以看到您的三角形是要在2D空间中生活的,但是请尝试在顶点着色器中使用mat4而不是mat3,这样您就只需要将矩阵和顶点相乘,而不用像在那做的那样获取价值。

 #version 430 in layout(location=0) vec2 position; in layout(location=1) vec3 vertexColor; uniform mat4 TriPosition; out vec3 theColor; void main() { vec4 v = vec4(position, 0.0, 1.0); gl_Posiiton = TriPosition * v; theColor = vertexColor; }; 

As you won't have any z translation or rotation, your matrix will look like the following where r0, r1, r2, r3 are rotation factors, tx, ty are translation factors: 由于没有z平移或旋转,因此矩阵如下所示,其中r0,r1,r2,r3是旋转因子,tx,ty是平移因子:

 r0 r1 0 tx r0 r1 0 0 1 0 0 tx r2 r3 0 ty = r2 r3 0 0 * 0 1 0 ty 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 TRIPOSITION = ROTATION * TRANSLATION 

Notice everything is same as 3x3 matrix if you take out thrid row and thrid column. 注意,如果取出第三行和第三列,则所有内容与3x3矩阵相同。

3. 3。

I'd say it is "Transform" rather than "TriPosition", because TriPosition sounds like only for translation. 我会说它是“ Transform”而不是“ TriPosition”,因为TriPosition听起来只喜欢翻译。

4. 4。

the class mat3 you have in c++. 您在C ++中拥有的mat3类。 Is that from glm? 是从glm吗? Then you might want to check this for easy transform matrix generation. 然后,您可能需要检查此内容以方便生成转换矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM