android


How can I draw two squares side by side?


Following the android OpenGL ES tutorial, I can draw a square to the screen.
I'd like to draw a second square to the screen, beside the initial square, so I've done as follows (only twice):
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, 3.0f, 0, 0, 0, 0, 1.0f, 0);
float[] modelMatrix = new float[16];
Matrix.setIdentityM(modelMatrix, 0);
Matrix.translateM(modelMatrix, 0, 2, 2, 0); //arbitrarily picking 2, the correctly drawn square uses 0's.
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, modelMatrix, 0)
Matrix.multiplyMM(mMVPMatrix, 0, mMVPMatrix, 0, mProjMatrix, 0);
and then proceed to draw it (link to google's code)
notably:
private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main() {" +
" gl_Position = vPosition * uMVPMatrix;" +
"}";
and
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
However, my second square is skewed like so:
Why is this the case? How can I simply draw a square beside this one, without modifying the initial coordinates?
My suspicion is it has something to do with Matrix.setLookAtM(), and that the projection is reshaping the square. I'm not sure how to go about fixing this however!
This thread solved my issue.
In the shader, I was multiplying by
vPosition * uMVPMatrix;"
but the multiplications should follow the ordering:
transformed vertex = projMatrix * viewMatrix * modelMatrix * input vertex
note that input vertex comes after the projection, view and model matrix (MVPMatrix)
So switching to
vPosition * uMVPMatrix;
and
float[] pvMatrix = new float[16];
//projection * view
Matrix.multiplyMM(pvMatrix, 0, mProjMatrix, 0, mViewMatrix, 0);
float[] mModelMatrix = new float[16];
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.scaleM(mModelMatrix,0, mScale[0],mScale[1], 1.0f);
Matrix.translateM(mModelMatrix, 0, mPosition[0],mPosition[1], mPosition[2]);
//projection * view * model
Matrix.multiplyMM(mMVPMatrix,0, pvMatrix, 0, mModelMatrix, 0);
Did the trick!

Related Links

Format first line of a multiline TextView in Android
How to post update on Facebook wall using SDK 4.0?
ImageView expanding container despite adjustViewBounds
Issue in generating FCM token
Connectivity MYSQL Databse to Firebase in Android [on hold]
SQLite Null Point Exception due to Insert [duplicate]
Two radio Buttons getting selected in android
Google Places Autocomplete Api in Tab layout Fragment
Picasso doesnot show placeholder, error and url image in imageview of fragment
Crashlytics.getInstance() returns null for logging custom exceptions
cannot resolve symbol 'editText'
How to connect to my http://localhost web server from Android Emulator in wampserver?
how to get RecyclerView item's padding in JellyBean
How to get Last Recent Height and Weight from Google Fit Api
Accessing main activity in Broadcast Reciever without using intent
FCM how to display data from notification click in another activity

Categories

HOME
search
oracle12c
checkstyle
string
ecmascript-6
redux-form
twig
character-encoding
hessian
package
vscode-extensions
opendj
hpc
arangodb
openstreetmap
commonjs
endeca
siddhi
redis-sentinel
renjin
enterprise-architect
3nf
selectize.js
cumulocity
docx
modbus-tcp
null-pointer
jersey-1.0
spring-async
visual-studio-extensions
hta
exploit
scriptella
tosca
xcode-ui-testing
definitelytyped
siri
fragment-backstack
progress-bar
wcag
ceylon
google-cast
fstream
github3.py
hibernate-search
fiware-cygnus
openalpr
git-rewrite-history
dm-script
release
railstutorial.org
unordered-map
signals-slots
contract
knime
file-sharing
show-hide
facebook-social-plugins
phpdbg
double-click
wcftestclient
chai-as-promised
fclose
twilio-click-to-call
custom-font
ngmaterial
restore
signalr.client
jtwig
qiime
kango-framework
libav
qbxml
libvlc
linklabel
java-money
dllimport
android-launcher
rpart
atoi
wso2greg
avahi
jquery-forms-plugin
fits
phpldapadmin
ejb-2.x
nodelist
mruby
deform
with-statement
build-script
ss7
personalization
jquery-slider
memset
authlogic
code-duplication
asplinkbutton
web-farm
fitbounds
node-redis
procedures
google-ajax-api
load-time
globals
jqueryform
zen
lalr
alsb
int64

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App