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

GoogleApiClient null on Device, works in Emulator
How to add buttons dynamically?
Prevent EditText from gaining focus instead of ImageView after returning from called activity
Set initial value for Location in Android
Breakdown of this interface
add a spinner in a fragment [duplicate]
How to generate productFlavors in parallel in gradle
How should proguard be configured with Google Play Services
EditText loses focus onTextChange
Draw rectangles in android application
EBS Payment integration - Android
How to restrict file chooser dialogue with Google Drive and DropBox?
Programmatically add views to LinearLayout with both gravity and weight
MPAndroidChart LineChart Double Tap Gesture Control
Horizontal ScrollView lagging after adding two or more horizontal scrollviews
“Warning: Policy violation” but the apps don't require any permission

Categories

HOME
phpword
assembly
localization
ng-idle
pyqt
yandex
rom
maxscript
nuget-package
lanczos
special-characters
32bit-64bit
nancy
dynatrace
cumulocity
microsoft-dynamics-nav
dimensional-modeling
google-data-studio
symfony-console
bourbon
swagger-2.0
charles
gmock
tiki-wiki
intersystems-cache
soot
user-defined-functions
autodesk-designautomation
login-script
hunspell
sharding
indy10
processwire
roblox
hivemq
sim-toolkit
openproject
dm-script
cd
flatmap
nohup
railstutorial.org
watchface
servermanager
mesosphere
nanoc
serde
appdomain
slackware
flex-monkey
spring-cloud-feign
windows-azure-queues
rspec2
jclouds
createprocess
state-restoration
custom-errors
opl
dynatree
cgi-bin
wso2greg
snoop
node-serialport
administration
metatrader5
cbind
strawberry-perl
mdichild
ignite-ui
cuba
reflector
functional-java
buffering
renderer
menubar
diem
sqlclr
joomla3.1
xcode3.2
subgraph
cgimage
user-tracking
fsevents
google-floodlight
webdev.webserver
drawtobitmap
twitterizer
ruby-1.8
metaweblog
hbm2java
zend-rest
cfwindow
web-application-project
todos
ifilter
lemmatization
xslcompiledtransform
idatareader

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