1
\$\begingroup\$

Hi guys I am really new in OpenGL-ES and I am developing a simple android application with NDK, and I just want to ask why the image is not fit after I set the width and height of it.

this is my width and height

const int FIXED_WIDTH = 320;
const int FIXED_HEIGHT = 568;

and this is how I set my Matrix

glViewport(0, 0, width, height);

// Create a new perspective projection matrix. The height will stay the same
// while the width will vary as per aspect ratio.
float left = FIXED_WIDTH;
float right = 0.0f;
float bottom = FIXED_HEIGHT;
float top = 0.0f;
float near = 1.0f;
float far = 50.0f;

mProjectionMatrix = Matrix::newFrustum(left, right, bottom, top, near, far);

// Position the eye in front of the origin.
float eyeX = 0.0f;
float eyeY = 0.0f;
float eyeZ = 1.0f;

// We are looking at the origin
float centerX = 0.0f;
float centerY = 0.0f;
float centerZ = 0.0f;

// Set our up vector.
float upX = 0.0f;
float upY = 1.0f;
float upZ = 0.0f;

// Set the view matrix.
mViewMatrix = Matrix::newLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);

// model * view * projection
mMVPMatrix->multiply(*mProjectionMatrix, *mViewMatrix);

After setting the Matrix I thought that my point 0.0 will start at the top right corner of the device (This is my understanding of matrix). as you shown in the image.

enter image description here

and this is how I setup my texture

TextureGenerator* textureGenerator = new TextureGenerator();
textureGenerator->setSize(320.0f, 568.0f); // width and height
textureGenerator->setTranslate(0.0f, 0.0f); // X and Y
textureGenerator->setTextureId(GLUtils::loadTexture("bg.jpg"));
mTextureCollection.push_back(textureGenerator);

then after I add some texture this is the result.

enter image description here

This is the actual picture, size 320x568.

enter image description here

as you can see to the result of my doing, I don't know if my point 0.0 is starting at the center. Please someone help and explain to me why the texture is not fit, and if my matrix is correctly set or not. Thank you.

\$\endgroup\$
1
\$\begingroup\$

You are very right about your guess that the "point 0.0 is starting at the center" - simply looking at the screenshot you can see it is the bottom left corner of your texture.
The provided code example only confirms the suspicion:

// We are looking at the origin
float centerX = 0.0f;
float centerY = 0.0f;
float centerZ = 0.0f;

which is setting the view center to [0,0,0]. Adjusting either the quad position or the eye+center position by [-w/2, h/2] should do the trick. Suggested further reading:
Understanding glm::lookAt()
Getting started with OpenGL coordinate system
Viewing and Transformations

\$\endgroup\$
  • \$\begingroup\$ thanks for answering, but what do you mean by this [-w/2, h/2]? Is this how I declare my textures? because this is how I declare my texture. mTextureSize[0] = -(width / 2.0f); // Left mTextureSize[1] = height / 2.0f; // Top mTextureSize[2] = width / 2.0f; // Right mTextureSize[3] = -(height / 2.0f); // Bottom Is this also the reason why my images display to the upper right of the screen instead of fitting it to the screen? \$\endgroup\$ – dotGitignore Apr 21 '18 at 11:28
  • 1
    \$\begingroup\$ @Jer the texture is cropped by screen because most of it lies outside rendered area. You need to move either the texture quad (model matrix) or the camera (view matrix). The former could be trivially done by textureGenerator->setTranslate(-w/2, -h/2); latter is done by offsetting eye's and center's corresponding coordinates (e.g. centerX += -(w / 2.0f)). You can learn that and much more from the linked resources. \$\endgroup\$ – wondra Apr 21 '18 at 12:38
  • \$\begingroup\$ Ohh.. you are right... Thank you, now my texture is rendering on the 0.0 point now. what I've done is mTextureSize[0] = mWidth; // Left mTextureSize[1] = mHeight; // Top mTextureSize[2] = 0.0f; // Right mTextureSize[3] = 0.0f; // Bottom Instead of setting a negative number of texture I set some part of it to 0.0 and the other part is the entered width and height of it. \$\endgroup\$ – dotGitignore Apr 21 '18 at 13:15
  • \$\begingroup\$ though I see your comment more useful rather than your answer, I still mark it as an accepted because you put some useful links, Thanks again. \$\endgroup\$ – dotGitignore Apr 21 '18 at 13:21

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.