次のコードは、OpenGL ES2を使用して2Dスクリーンスペースに四角形を描きます。頂点を変更することなく、矩形の描画を1ピクセル右に移動するにはどうすればよいですか?OpenGL Orthographic ProjectionおよびTranslate
特に、私がしようとしているのは、座標を右に0.5ピクセル移動することです。 GLES1.xで以前にこれを行う必要がありました。なぜなら、0.5fでglTranslate()を実行しない限り、正しい場所に線を描くのに問題があったからです。
以下のコードでglm :: translate()の使用について混乱します。
翻訳をしようとすると、全体の長方形が画面の左側から中央に移動します。ジャンプは約200ピクセルです。
ModelまたはViewマトリックスでglm :: translateを実行しても同じ結果が得られます。
行列の乗算の順序は間違っていますか?
attribute vec4 vPosition;
uniform mat4 MVP;
void main()
{
gl_Position = MVP * vPosition;
}
UPDATE:ここ
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);
}
は、頂点シェーダ源である私は、以下の行列乗算は私より良い結果を与えている探しています。 Iこれはしかし、「正しい」かどうかわからない:
MVP = Ortho * Model * View * Projection;