Commit 70d13df1 authored by Ruben Jesus Garcia Hernandez's avatar Ruben Jesus Garcia Hernandez
Browse files

Android: support spheres. support gamepad.

parent eb2d0670
...@@ -125,5 +125,10 @@ JNI_METHOD(void, nativeLoadConfigFile) ...@@ -125,5 +125,10 @@ JNI_METHOD(void, nativeLoadConfigFile)
native(native_treasure_hunt)->loadConfigFile(); native(native_treasure_hunt)->loadConfigFile();
} }
JNI_METHOD(void, nativekeyPress)
(JNIEnv *env, jobject obj, jlong native_treasure_hunt, jint key) {
int k=key;
native(native_treasure_hunt)->keyPress(k);
}
} // extern "C" } // extern "C"
...@@ -341,6 +341,40 @@ static float VectorInnerProduct(const std::array<float, 4>& vect1, ...@@ -341,6 +341,40 @@ static float VectorInnerProduct(const std::array<float, 4>& vect1,
} }
} // anonymous namespace } // anonymous namespace
void TreasureHuntRenderer::keyPress (int k)
{ //X=99, Y=100, A=96, B=97
//UP=19, DOWN=20, LEFT=21, RIGHT=22,
//https://developer.android.com/reference/android/view/KeyEvent.html
switch (k) {
case 19:
currentSet++;
if (currentSet>TIMESTEPS-1)
currentSet=0;
break;
case 20:
currentSet--;
if (currentSet<0)
currentSet=TIMESTEPS-1;
break;
case 21:
currentIso++;
if (currentIso>ISOS)
currentIso=0;
break;
case 22:
currentIso--;
if (currentIso<0)
currentIso=ISOS;
break;
case 99:
animateTimesteps=!animateTimesteps;
break;
case 96:
animateMovement=!animateMovement;
}
}
void TreasureHuntRenderer::loadConfigFile(void) void TreasureHuntRenderer::loadConfigFile(void)
{ {
if (configPath!=nullptr) if (configPath!=nullptr)
...@@ -495,6 +529,7 @@ glGenTextures(2+ZLAYERS, textDepthPeeling); ...@@ -495,6 +529,7 @@ glGenTextures(2+ZLAYERS, textDepthPeeling);
//isosurfaces //isosurfaces
if (ISOS) { if (ISOS) {
currentIso=ISOS;
//rgh: FIXME Additive blending for android; do not need most of these textures //rgh: FIXME Additive blending for android; do not need most of these textures
//additive blending means white background makes everything white //additive blending means white background makes everything white
float m=BACKGROUND[0]; float m=BACKGROUND[0];
...@@ -668,8 +703,7 @@ void TreasureHuntRenderer::DrawFrame() { ...@@ -668,8 +703,7 @@ void TreasureHuntRenderer::DrawFrame() {
ProcessControllerInput(); ProcessControllerInput();
} }
// if (animateTimesteps) { if (animateTimesteps) {
if (animationspeed>1) if (animationspeed>1)
currentSet+=animationspeed; currentSet+=animationspeed;
else { else {
...@@ -682,7 +716,7 @@ void TreasureHuntRenderer::DrawFrame() { ...@@ -682,7 +716,7 @@ void TreasureHuntRenderer::DrawFrame() {
} }
if (currentSet>TIMESTEPS-1) if (currentSet>TIMESTEPS-1)
currentSet=0; currentSet=0;
// } }
PrepareFramebuffer(); PrepareFramebuffer();
gvr::Frame frame = swapchain_->AcquireFrame(); gvr::Frame frame = swapchain_->AcquireFrame();
...@@ -693,7 +727,7 @@ void TreasureHuntRenderer::DrawFrame() { ...@@ -693,7 +727,7 @@ void TreasureHuntRenderer::DrawFrame() {
head_view_ = gvr_api_->GetHeadSpaceFromStartSpaceRotation(target_time); head_view_ = gvr_api_->GetHeadSpaceFromStartSpaceRotation(target_time);
if (animateTimesteps) { if (animateMovement) {
const float speed=0.01; const float speed=0.01;
gvr::Mat4f inv=invert(head_view_); gvr::Mat4f inv=invert(head_view_);
//for (int i=0;i<4;i++) //for (int i=0;i<4;i++)
...@@ -708,8 +742,8 @@ if (animateTimesteps) { ...@@ -708,8 +742,8 @@ if (animateTimesteps) {
} }
LOGD("UT=%f, %f, %f\n", UserTranslation[0], UserTranslation[1], UserTranslation[2]); //LOGD("UT=%f, %f, %f\n", UserTranslation[0], UserTranslation[1], UserTranslation[2]);
LOGD("MovementSpeed=%f\n", movementspeed); //LOGD("MovementSpeed=%f\n", movementspeed);
gvr::Mat4f left_eye_matrix = gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE); gvr::Mat4f left_eye_matrix = gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE);
gvr::Mat4f right_eye_matrix = gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE); gvr::Mat4f right_eye_matrix = gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE);
...@@ -853,7 +887,7 @@ if (has_abc) { ...@@ -853,7 +887,7 @@ if (has_abc) {
} }
if (ISOS) if (ISOS)
RenderIsos(modelview_projection_cube_, ISOS); RenderIsos(modelview_projection_cube_, currentIso);
} }
...@@ -1045,8 +1079,7 @@ glBindVertexArray(AtomTVAO[0]); ...@@ -1045,8 +1079,7 @@ glBindVertexArray(AtomTVAO[0]);
eprintf("1 Gl error RenderAtomTrajectoriesUnitCell: %d\n", e); eprintf("1 Gl error RenderAtomTrajectoriesUnitCell: %d\n", e);
//glUseProgram(UnitCellP); //glUseProgram(UnitCellP);
//glUniformMatrix4fv(m_nUnitCellMatrixLocation, 1, GL_FALSE, matrix); //glUniformMatrix4fv(m_nUnitCellMatrixLocation, 1, GL_FALSE, matrix);
float color[4]={1,0,0,1}; glUniform4fv(UnitCellColourLoc, 1, atomtrajectorycoluor);
glUniform4fv(UnitCellColourLoc, 1, color);
if ((e = glGetError()) != GL_NO_ERROR) if ((e = glGetError()) != GL_NO_ERROR)
eprintf("Gl error after glUniform4fv 2 RenderAtomTrajectoriesUnitCell: %d\n", e); eprintf("Gl error after glUniform4fv 2 RenderAtomTrajectoriesUnitCell: %d\n", e);
//glEnableVertexAttribArray(0); //glEnableVertexAttribArray(0);
...@@ -1114,9 +1147,8 @@ void TreasureHuntRenderer::RenderUnitCell(const gvr::Mat4f eyeViewProjection) ...@@ -1114,9 +1147,8 @@ void TreasureHuntRenderer::RenderUnitCell(const gvr::Mat4f eyeViewProjection)
glUseProgram(UnitCellP); glUseProgram(UnitCellP);
glUniformMatrix4fv(UnitCellMatrixLoc, 1, GL_FALSE, t); glUniformMatrix4fv(UnitCellMatrixLoc, 1, GL_FALSE, t);
if ((e = glGetError()) != GL_NO_ERROR) if ((e = glGetError()) != GL_NO_ERROR)
eprintf("Gl error after glUniform4fv 1 RenderUnitCell: %d\n", e); eprintf("Gl error after glUniform4fv 1 RenderUnitCell: %d\n", e);
float color[4]={1,1,1,1}; glUniform4fv(UnitCellColourLoc, 1, unitcellcolour);
glUniform4fv(UnitCellColourLoc, 1, color);
if ((e = glGetError()) != GL_NO_ERROR) if ((e = glGetError()) != GL_NO_ERROR)
eprintf("Gl error after glUniform4fv 2 RenderUnitCell: %d\n", e); eprintf("Gl error after glUniform4fv 2 RenderUnitCell: %d\n", e);
glBindVertexArray(UnitCellVAO); glBindVertexArray(UnitCellVAO);
...@@ -1142,6 +1174,6 @@ void TreasureHuntRenderer::RenderUnitCell(const gvr::Mat4f eyeViewProjection) ...@@ -1142,6 +1174,6 @@ void TreasureHuntRenderer::RenderUnitCell(const gvr::Mat4f eyeViewProjection)
} }
void TreasureHuntRenderer::OnTriggerEvent() { void TreasureHuntRenderer::OnTriggerEvent() {
animateTimesteps=!animateTimesteps; animateMovement=!animateMovement;
} }
...@@ -45,6 +45,8 @@ class TreasureHuntRenderer { ...@@ -45,6 +45,8 @@ class TreasureHuntRenderer {
TreasureHuntRenderer(gvr_context* gvr_context, TreasureHuntRenderer(gvr_context* gvr_context,
std::unique_ptr<gvr::AudioApi> gvr_audio_api); std::unique_ptr<gvr::AudioApi> gvr_audio_api);
void keyPress (int k);
/** /**
* Destructor. * Destructor.
*/ */
...@@ -173,7 +175,9 @@ GLuint BlendVAO=0, BlendBuffer=0, BlendIndices=0; ...@@ -173,7 +175,9 @@ GLuint BlendVAO=0, BlendBuffer=0, BlendIndices=0;
int *numISOIndices=nullptr/*[ISOS*TIMESTEPS]*/; int *numISOIndices=nullptr/*[ISOS*TIMESTEPS]*/;
int currentSet=0; int currentSet=0;
bool animateTimesteps=false; bool animateTimesteps=true;
bool animateMovement=false;
int currentIso;
void RenderAtoms(const float *m); void RenderAtoms(const float *m);
void RenderUnitCell(const gvr::Mat4f eyeViewProjection); void RenderUnitCell(const gvr::Mat4f eyeViewProjection);
......
...@@ -25,14 +25,16 @@ public: ...@@ -25,14 +25,16 @@ public:
static const float Verts[]; static const float Verts[];
}; };
class Solid { class Solid {
public: public:
enum Type { enum Type {
Icosahedron, Icosahedron,
Octahedron, Octahedron,
Tetrahedron Tetrahedron,
Sphere
}; };
Solid(Type t) { Solid(Type t, int SphereFacets=15) {
switch (t) { switch (t) {
case Icosahedron: case Icosahedron:
nFaces=Icosahedron::nFaces; nFaces=Icosahedron::nFaces;
...@@ -45,17 +47,48 @@ public: ...@@ -45,17 +47,48 @@ public:
Faces=Octahedron::Faces; Faces=Octahedron::Faces;
nVerts=Octahedron::nVerts; nVerts=Octahedron::nVerts;
Verts=Octahedron::Verts; Verts=Octahedron::Verts;
break;
case Tetrahedron: case Tetrahedron:
nFaces=Tetrahedron::nFaces; nFaces=Tetrahedron::nFaces;
Faces=Tetrahedron::Faces; Faces=Tetrahedron::Faces;
nVerts=Tetrahedron::nVerts; nVerts=Tetrahedron::nVerts;
Verts=Tetrahedron::Verts; Verts=Tetrahedron::Verts;
} break;
} case Sphere:
//https://stackoverflow.com/questions/23143921/python-program-to-create-sphere-coordinates-not-working
//http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html For the future
nVerts=SphereFacets*SphereFacets;
float *tVerts=new float[nVerts*3];
for (int i=0;i<SphereFacets;i++)
for (int j=0;j<SphereFacets;j++) {
float t, f;
t=i*2*M_PI/(float)(SphereFacets-1);
f=j*M_PI/(float)(SphereFacets-1);
tVerts[i*SphereFacets*3 + j*3]=sin(f)*sin(t);
tVerts[i*SphereFacets*3 + j*3+1]=sin(f)*cos(t);
tVerts[i*SphereFacets*3 + j*3+2]=cos(f);
}
nFaces=2*(SphereFacets-1)*(SphereFacets-1);
int *tFaces=new int[3*nFaces];
for (int i=0;i<SphereFacets-1;i++)
for (int j=0;j<SphereFacets-1;j++) {
tFaces[i*(SphereFacets-1)*6 + j*6]=i*SphereFacets+j;
tFaces[i*(SphereFacets-1)*6 + j*6+1]=((i+1)%SphereFacets)*SphereFacets+j;
tFaces[i*(SphereFacets-1)*6 + j*6+2]=(i)*SphereFacets+(j+1)%SphereFacets;
tFaces[i*(SphereFacets-1)*6 + j*6+3]=((i+1)%SphereFacets)*SphereFacets+j;
tFaces[i*(SphereFacets-1)*6 + j*6+4]=(i)*SphereFacets+(j+1)%SphereFacets;
tFaces[i*(SphereFacets-1)*6 + j*6+5]=((i+1)%SphereFacets)*SphereFacets+(j+1)%SphereFacets;
}
Faces=tFaces; Verts=tVerts;
} // switch
} // Solid (constructor)
int nFaces; int nFaces;
const int *Faces; const int *Faces;
int nVerts; int nVerts;
const float *Verts; const float *Verts;
}; }; // class Solid
#endif // __POLYHEDRON_H #endif // __POLYHEDRON_H
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment