【发布时间】:2014-11-16 14:41:38
【问题描述】:
如果没有 OpenGL 矩阵函数,我不知道该怎么做。
通过将每个顶点位置乘以 2D 来变换三角形 仿射缩放、旋转或平移矩阵,如下所示 通过键盘输入。
键盘输入:
's' key, scale by 1.1 units in X and Y 'r' key, rotate clockwise (in XY plane) by 2.0 degrees 't' key, translate 0.1 unit in X and Y 'q' key, quit program您不得使用 OpenGL 矩阵函数(例如 glScale、glTranslate 或 glRotate)来执行转换。
这是我到目前为止所拥有的......当我构建程序时,三角形不会移动,但在按下一些键后我会得到随机数,所以我知道它正在做某事:
#include <iostream>
using namespace std;
#include <math.h>
#include <GLUT/glut.h>
void myInit();
void myDraw();
void keyboard( unsigned char, int, int );
float sum;
float theta = 3.14;
int k = 0;
float cVert[3][2]=
{
-1.0, -1.0,
1.0, -1.0,
0.0, 1.0
};
float triVert[3][2] =
{
-1.0, -1.0,
1.0, -1.0,
0.0, 1.0
};
float rotVert[3][3] =
{
1,0,0,
0, cos(theta), -sin(theta),
0, sin(theta), cos(theta)
};
float scaleVert[3][2] =
{
1.1, 0.0,
0.0, 1.1,
0.0, 0.0
};
int main( int argc, char *argv[] )
{
// Initialize window system
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "Lab 4" );
// Initialize graphics
myInit();
// Callbacks
glutDisplayFunc( myDraw );
glutKeyboardFunc( keyboard );
// Event loop
glutMainLoop();
}
// Initialize drawing
void myInit()
{
// Background color
glClearColor(0.0, 0.0, 0.0, 1.0);
// 2D world projection
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( -10.0, 10.0, -10.0, 10.0 );
}
// Display callback
void myDraw()
{
// Clear the screen
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT );
// // Draw triangle in initial position
// glColor3f( 1.0, 0.0, 0.0 );
// glBegin( GL_LINE_LOOP );
// glVertex2f( triVert[0][0], triVert[0][1] );
// glVertex2f( triVert[1][0], triVert[1][1] );
// glVertex2f( triVert[2][0], triVert[2][1] );
// glEnd();
// Draw triangle in new position here
glBegin( GL_LINE_LOOP );
glColor3f( 0.0, 1.0, 0.0 );
glVertex2f(cVert[0][0], cVert[0][1]);
glVertex2f(cVert[1][0], cVert[1][1]);
glVertex2f(cVert[2][0], cVert[2][1]);
cout << cVert[3][2] << " ";
glEnd();
// Execute draw commands
glFlush();
}
// Keyboard callback
void keyboard( unsigned char key, int x, int y )
{
// Process keys
switch (key)
{
case 's': //scale by 1.1 units in X and Y
for(k=0;k<3;++k)
{
cVert[3][2] = cVert[3][2]+(scaleVert[3][2]*triVert[3][2]);
cout << cVert[3][2]<<" ";
}
break;
case 'r': //rotate clockwise (in XY plane) by 2.0 degrees
for(k=0;k<3;++k)
{
cVert[3][2] = cVert[3][2]+(rotVert[3][3]*triVert[3][2]);
theta = theta + 2.0;
cout << cVert[3][2]<<" ";
cout << theta << " ";
}
break;
case 't': //translate 0.1 unit in X and Y
break;
case 'q': // exit program
exit(1);
break;
}
// Redraw the scene
glutPostRedisplay();
}
【问题讨论】:
-
这个作业告诉你该做什么。您必须构建相应的转换矩阵并将您的顶点与它们相乘。编写一个函数来进行 3×3 矩阵乘法和一些帮助程序来构建正确的变换矩阵。然后使用那些。
-
我不是很清楚,我认为这就是我当时正在做的事情,将矩阵乘以表示变换或旋转或缩放的矩阵。
-
我看到一些循环遍历变量 k,但该变量从未用作矩阵的索引。此外,您不应该逐字粘贴矩阵乘法。编写函数
mat3x3_mul(mat3x3 out, mat3x3 a, mat3x3b)和mat3x3_mul_vec3(vec3 out, mat3x34 M, vec3 v)并使用它们。你可能想看看我的 linmath.h 如何做到这一点:github.com/datenwolf/linmath.h/blob/master/linmath.h - 不包含 mat3x3 代码,但无论矩阵的维度如何,乘法规则都是相同的。