
|
Camera movement using the stylus. |
Author :
sapphiron
Post Date :
2005-09-28 22:02
Posts :
5
|
First, please sorry about my english.
Second, here we go.
AS you may notice, the stylus, is exaclty the same as a mouse, and when you hit the PDA screen you are just "left clicking" a position on the screen.
With this basic concept I`ll try to explain you how to use the stylus for camera rotation.
Part 1 - The camera Vector.
OPENGL ES uses a function to move the camera ( ugluLookAtf() ), if you have learned what grant wrote, you should know that those vectors are:
-> Camera Position (the position of the camera in x,y,z)
-> Target Position (where you want to look at in x,y,z)
-> Normal vector (we are not using this right now).
We will set a default value for the first 2 vectors.
code:
//camera x,y,z values
float xc = 1.0f;
float yc = 1.0f;
float zc = 0.0f;
//target x,y,z values
float xt=0.0f;
float xy=0.0f;
float xz=0.0f;
: end of code
Part 2 - the camera function
Now we are initialice a function that will create the camera using those values.
code:
void camera(float xc, float yc, float zc) //we are just using the camera values
{
ugluLookAtf(
xc, yc, zc,
xt, yt, zt,
0.0f, 1.0f, 0.0f);
}
: end of code
As you may notice, there are many ways you can set up the values, I will just change the global vars, but you can pass the float values to the function if you like (ex. void camera (float xc, flota yc, float zc, float xt, float yt, float zt) )
PART 3 : the pointer function
the pointer function will take these vars:
void pointer (UGWindow uwin, int button, int state, int x, int y)
we need to convert the x and y position to float values, a fast way to do so, is just multiply:
code:
float x1 = x*1.0f;
float y1 = y*1.0f;
:end of code
as you see, the x and y values are not changin.
Now lets thing logically, if you have set up your screen to 250x250, you should divide that in 4 rectangles. One for the top left part of the screen, one for the top right, another for the lower left and the last one for the lower right.
Now we are going to create an algoritm that recognizes what part of the screen we are touching:
code:
void pointer(UGWindow uwin, int button, int state, int x, int y)
{
float x1 = x*1.0f;
float y1 = y*1.0f;
switch (button)
{
case UG_BUT_LEFT :
if(y1<125 && x1<125){ //top left rectangle
xc=xc--;
yc=yc--;
reshape(uwin, w, h); //in case we have the reshape function
ugPostRedisplay(uwin);
break;
}else if (y1>125 && x1<125){ //boton left rectangle
xc=xc++;
yc=yc--;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
}else if(y1>125 && x1>125){ //upper right
xc=xc++;
yc=yc++;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
} else if(y1<125 && x1>125){
xc=xc++;
yc=yc--;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
}
}
}
:end of code
FINAL PART - the main function
Well, now you just have to call the pointer function and thats all
code:
ugPointerFunc(uwin, pointer);
:end of code
LAST THING:
Please, beware that this code is VERY buggy, it does work, but it obviosly needs a bunch of code to be perfect, but its just the basics.
If you want the whole example, Ill put it in the next post, Im using the same example that Grant wrote for the perspective tutorial. REMEMBER ITS NOT PERFECT |
Author :
sapphiron
Post Date :
2005-09-28 22:03
Posts :
5
|
full example code:
/*************************************************
* Zeus CMD - OpenGL ES Tutorial 12 : Perspective *
* By Grant James (ZEUS) *
*http://www.zeuscmd.com *
* small camera modification by sapphiron *
*************************************************/
#pragma comment(lib, "libGLES_CM.lib")
#pragma comment(lib, "ug.lib")
#include "ug.h"
int w = 0;
int h = 0;
float xc = 0.0f;
float yc = 0.0f;
float zc = 2.0f;
float cc1=0.0f;
float cc2=0.0f;
float cc3=0.0f;
float ccc1=0.0f;
float ccc2=1.0f;
float ccc3=0.0f;
bool perspective = true;
void init()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepthf(1.0f);
}
//camara
void camara(float xc, float yc, float zc)
{
ugluLookAtf(
xc, yc, zc,
cc1, cc2, cc3,
ccc1, ccc2, ccc3);
}
void display(UGWindow uwin)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
camara(xc,yc,zc);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glTranslatef(0.25f, 0.0f, 0.0f);
ugSolidCubef(0.5f);
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
glTranslatef(-0.25f, 0.0f, -1.0f);
ugSolidCubef(0.5f);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glTranslatef(-0.25f, 0.0f, -1.0f);
ugSolidCubef(0.5f);
glFlush ();
ugSwapBuffers(uwin);
}
void reshape(UGWindow, int, int);
void keyboard(UGWindow uwin, int key, int x, int y)
{
switch(key)
{
case 'q' : exit(0); break;
case UG_KEY_UP :
cc2=cc2+0.033f;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
case UG_KEY_DOWN :
cc2=cc2-0.033f;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
case UG_KEY_LEFT :
cc1=cc1-0.033f;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
case UG_KEY_RIGHT :
cc1=cc1+0.033f;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
case 'p' :
perspective = !perspective;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
case 'w' :
zc=zc-0.1f;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
case 's' :
zc=zc+0.1f;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
case 'a' :
xc=xc+0.1f;
zc=zc+0.1f;
//c2=c2+0.1f;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
case 'd' :
xc=xc-0.1f;
zc=zc-0.1f;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
}
}
void reshape(UGWindow uwin, int width, int height)
{
w = width;
h = height;
if (!height)
height = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width, height);
if (perspective)
ugluPerspectivef(45.0f, 1.0f * width / height, 1.0f, 100.0f);
else
glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 20.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void pointer(UGWindow uwin, int button, int state, int x, int y)
{
float x1 = x*1.0f;
float y1 = y*1.0f;
switch (button)
{
case UG_BUT_LEFT :
if(y1<125 && x1<125){ //top left rectangle
xc=xc--;
yc=yc--;
reshape(uwin, w, h); //in case we have the reshape function
ugPostRedisplay(uwin);
break;
}else if (y1>125 && x1<125){ //boton left rectangle
xc=xc++;
yc=yc--;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
}else if(y1>125 && x1>125){ //upper right
xc=xc++;
yc=yc++;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
} else if(y1<125 && x1>125){
xc=xc++;
yc=yc--;
reshape(uwin, w, h);
ugPostRedisplay(uwin);
break;
}
}
}
int main()
{
UGCtx ug = ugInit();
UGWindow uwin = ugCreateWindow(ug, "UG_DEPTH", "12 - Perspective", 250, 250, 100, 100);
init();
ugDisplayFunc(uwin, display);
ugReshapeFunc(uwin, reshape);
ugKeyboardFunc(uwin, keyboard);
ugPointerFunc(uwin, pointer);
ugMainLoop(ug);
return 0;
}
|
Author :
grant
Post Date :
2005-09-29 06:47
Posts :
117
|
Hi,
Thank you for posting your findings.
Also, for those interested, one of the next few OpenGL ES tutorials will concentrate on camera movement.
Regards,
Grant |
All Rights Reserved, © Zeus Communications, Multimedia & Development 2004-2005
Read the Disclaimer
|
What tutorial would you like to see next?
|
Useful Books :
|
Link to ZeusCMD
|
|