下面的代码使用OpenGL ES2在2D屏幕空间中绘制一个矩形。如何在不修改矩形顶点的情况下,将矩形的绘图向右移动1像素?
具体来说,我要做的是把坐标0.5个像素移到右边。我以前必须用GLES1.x来完成这个任务,原因是我在正确的位置绘制线条时遇到了问题,除非我用0.5f做了一个glTranslate()。
我对下面代码中glm::translate()的用法感到困惑。
如果我尝试翻译0.5f,整个矩形从屏幕的左边移动到中间--大约200像素的跳跃。
无论是对模型还是视图矩阵进行glm::翻译,我都会得到相同的结果。
矩阵乘法的顺序是错误的吗?它应该是什么?
short g_RectFromTriIndices[] =
{
0, 1, 2,
0, 2, 3
}; // The order of vertex rendering.
GLfloat g_AspectRatio = 1.0f;
//--------------------------------------------------------------------------------------------
// LoadTwoTriangleVerticesForRect()
//--------------------------------------------------------------------------------------------
void LoadTwoTriangleVerticesForRect( GLfloat *pfRectVerts, float fLeft, float fTop, float fWidth, float fHeight )
{
pfRectVerts[ 0 ] = fLeft;
pfRectVerts[ 1 ] = fTop;
pfRectVerts[ 2 ] = 0.0;
pfRectVerts[ 3 ] = fLeft + fWidth;
pfRectVerts[ 4 ] = fTop;
pfRectVerts[ 5 ] = 0.0;
pfRectVerts[ 6 ] = fLeft + fWidth;
pfRectVerts[ 7 ] = fTop + fHeight;
pfRectVerts[ 8 ] = 0.0;
pfRectVerts[ 9 ] = fLeft;
pfRectVerts[ 10 ] = fTop + fHeight;
pfRectVerts[ 11 ] = 0.0;
}
//--------------------------------------------------------------------------------------------
// Draw()
//--------------------------------------------------------------------------------------------
void Draw( void )
{
GLfloat afRectVerts[ 12 ];
//LoadTwoTriangleVerticesForRect( afRectVerts, 0, 0, g_ScreenWidth, g_ScreenHeight );
LoadTwoTriangleVerticesForRect( afRectVerts, 50, 50, 100, 100 );
// Correct for aspect ratio so squares ARE squares and not rectangular stretchings..
g_AspectRatio = (GLfloat) g_ScreenWidth / (GLfloat) g_ScreenHeight;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
GLuint hPosition = glGetAttribLocation( g_SolidProgram, "vPosition" );
// PROJECTION
glm::mat4 Projection = glm::mat4(1.0);
// Projection = glm::perspective( 45.0f, g_AspectRatio, 0.1f, 100.0f );
// VIEW
glm::mat4 View = glm::mat4(1.0);
static GLfloat transValY = 0.5f;
static GLfloat transValX = 0.5f;
//View = glm::translate( View, glm::vec3( transValX, transValY, 0.0f ) );
// MODEL
glm::mat4 Model = glm::mat4(1.0);
// static GLfloat rot = 0.0f;
// rot += 0.001f;
// Model = glm::rotate( Model, rot, glm::vec3( 0.0f, 0.0f, 1.0f ) ); // where x, y, z is axis of rotation (e.g. 0 1 0)
glm::mat4 Ortho = glm::ortho( 0.0f, (GLfloat) g_ScreenWidth, (GLfloat) g_ScreenHeight, 0.0f, 0.0f, 1000.0f );
glm::mat4 MVP;
MVP = Projection * View * Model * Ortho;
GLuint hMVP;
hMVP = glGetUniformLocation( g_SolidProgram, "MVP" );
glUniformMatrix4fv( hMVP, 1, GL_FALSE, glm::value_ptr( MVP ) );
glEnableVertexAttribArray( hPosition );
// Prepare the triangle coordinate data
glVertexAttribPointer( hPosition, 3, GL_FLOAT, FALSE, 0, afRectVerts );
// Draw the rectangle using triangles
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, g_RectFromTriIndices );
glDisableVertexAttribArray( hPosition );
}以下是顶点着色器的来源:
attribute vec4 vPosition;
uniform mat4 MVP;
void main()
{
gl_Position = MVP * vPosition;
}更新:我发现下面的矩阵乘法给了我更好的结果。我不知道这是否“正确”,尽管如此:
MVP = Ortho * Model * View * Projection;发布于 2016-04-06 09:52:26
MVP对我来说很奇怪,你不需要4样东西就能得到你的MVP。你的投影矩阵应该是正交的,所以在这种情况下
MVP = Projection * View * Ortho;
但是我也可以看到,你的投影矩阵已经从角度上被评论了,所以我认为它现在做的并不多。
根据它的声音,既然你希望模型协调保持不变,而移动,你想移动你的相机对吗?所以(从外观上看,你的顶点使用的是每像素1单位的协调范围),完成0.5f到你的视图的转换就是在移动你的投影空间的一半。相反,您希望有一个类似于Camera类的东西,您可以通过使用相机的X和Y位置获得您的View。
然后你可以得到你的视图矩阵,使用相机的位置,它可以共享你使用的世界单位系统,也就是每像素一个单位。
glm::mat4 view;
view = glm::lookAt(glm::vec3(camX, camY, 0.0), glm::vec3(0.0, 0.0, 0.0),glm::vec3(0.0, 1.0, 0.0));我把这条直线(减去camZ for camY)从一个非常好的这里相机3d教程中撕下来,但是完全相同的概念可以应用于正交相机。
我知道这会增加一些开销,但是拥有一个cmaera类比手动使用glm::translate、旋转和缩放来控制你的视口要好得多(而且它可以让你在相机和模型协调点之间使用更多的obivous协调系统。)
https://stackoverflow.com/questions/36440062
复制相似问题