OpenGl - Object inverts after 180 degrees of rotation - c++

I have an object rendered and I'm trying to rotate the object with the mouse. The object will rotate fine for 180 degrees but after that, the object inverts (if facing toward camera, switches to face away from camera) as does the expected movement of the mouse i.e. if dragging the mouse to the right rotates the object clockwise, then it will now rotate anti-clockwise. Once it reaches the next 180 degrees, it inverts again and normality is restored. I'm sure there must be something simple that I'm just not seeing?
Here is my code:
// Detect mouse state
void
mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
moving = 1;
beginX = x;
beginY = y;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{
moving = 0;
}
}
// Detect mouse movement
void motion(int x, int y)
{
if (moving)
{
angleX = (angleX + (x - beginX));
angleY = (angleY + (y - beginY));
beginX = x;
beginY = y;
newModel = 1;
glutPostRedisplay();
}
}
// Rotate object
void recalcModelView(void)
{
// Get object's centre
int hh = head->GetHeight() / 2;
int hw = head->GetWidth() / 2;
int hd = head->GetDepth() / 2;
glPopMatrix();
glPushMatrix();
// Rotate object based on mouse movement
glTranslatef(hw, hd, hh);
float temp1 = angleX / 5;
float temp2 = angleY / 5;
printf("TEMP1: %g\n", temp1);
printf("TEMP2: %g\n", temp2);
glRotatef(temp1, 0.0, 1.0, 0.0);
glRotatef(-temp2, 1.0, 0.0, 0.0);
glTranslatef(-hw, -hd, -hh);
newModel = 0;
}

Use something like Arcball to fix this.

Related

How to make 3d camera infinitely rotate around with SDL2 mouse motion?

I am trying to implement 3D camera mouse rotation. Right now what I can do is only rotate if my mouse is in the bound of the window. What it means is if my window is 1280 width I cannot rotate more if mouse coordinates rich 0 or 1279. I've read that relative mouse should help. It does not work for me though.
before the main loop
SDL_ShowCursor(SDL_DISABLE);
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_WarpMouseInWindow(window, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
The function that rotates xyz
glm::vec3 MouseMotion(float x, float y)
{
float lastX = SCREEN_WIDTH / 2, lastY = SCREEN_HEIGHT / 2;
float pitch = 0.0f, yaw = -90.0f;
float offsetX = x - lastX;
float offsetY = lastY - y;
lastX = x;
lastY = y;
float sensitivity = 0.3f;
yaw += offsetX * sensitivity;
pitch += offsetY * sensitivity;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 front;
front.x = std::cos(glm::radians(yaw)) * std::cos(glm::radians(pitch));
front.y = std::sin(glm::radians(pitch));
front.z = std::sin(glm::radians(yaw)) * std::cos(glm::radians(pitch));
//std::cout << "Mouse: " << offsetX << ", " << offsetY << std::endl;
//std::cout << "YAW AND PITCH: " << yaw << ", " << pitch << std::endl;
return glm::normalize(front);
}
and the event loop
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
exit(0);
}
if (event.type == SDL_MOUSEMOTION)
{
float mouseX = (float)event.motion.x;
float mouseY = (float)event.motion.y;
cameraFront = MouseMotion(mouseX, mouseY);
}
}
it is working, but only if my mouse is in the bound.
Changing to event.motion.xrel and event.motion.yrel do not fix this. It makes it more bugged because my camera shakes, i.e. going a little bit right and get back to center or left and back to center.
How to make my camera move fully, infinitely move around?
Following on from the comments, here is a list of problems:
You need to call SDL_WarpMouseInWindow every time after an SDL_MOUSEMOTION event to reset the mouse, or it will quickly hit the "bound" you were talking about.
pitch and yaw need to be higher in scope than the game loop, or they will be reset every time and you won't be able to achieve "infinite rotation".
sensitivity is far too high. A typical mouse movement of ~500 units translates to a ~180 degree turning.
Here is an attempt at fixing the above issues, with some code refactoring:
glm::vec3 polarVector(float p, float y)
{
// this form is already normalized
return glm::vec3(std::cos(y) * std::cos(p),
std::sin(p),
std::sin(y) * std::cos(p));
}
// clamp pitch to [-89, 89]
float clampPitch(float p)
{
return p > 89.0f ? 89.0f : (p < -89.0f ? -89.0f : p);
}
// clamp yaw to [-180, 180] to reduce floating point inaccuracy
float clampYaw(float y)
{
float temp = (y + 180.0f) / 360.0f;
return y - ((int)temp - (temp < 0.0f ? 1 : 0)) * 360.0f;
}
int main(int argc, char*args[])
{
// ...
// experiment with this
const float sensitivity = 0.001f;
#define CTR_X (SCREEN_WIDTH / 2)
#define CTR_Y (SCREEN_HEIGHT / 2)
#define RESET_MOUSE SDL_WarpMouseInWindow(window, CTR_X, CTR_Y)
// call once at the start
RESET_MOUSE;
// keep outside the loop
float pitch = 0.0f, yaw = 0.0f;
while (!quit)
{
// ...
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
exit(0);
}
if (event.type == SDL_MOUSEMOTION)
{
float deltaX = (float)event.motion.x - CTR_X;
float deltaY = (float)event.motion.y - CTR_Y;
yaw = clampYaw(yaw + sensitivity * deltaX);
pitch = clampPitch(pitch - sensitivity * deltaY);
// assumes radians input
cameraFront = polarVector(glm::radians(pitch), glm::radians(yaw));
// reset *every time*
RESET_MOUSE;
}
}
// ...
}
}

OpenGL - Why doesn't my ball move?

In OpenGL, I'm building a football game that allows you to shoot a ball by first moving height indicators left and right, before shooting based on the indicators when a button is pressed. Here's what it looks like:
Football Game Visual
When these indicators are moved, my ball needs to travel at the height of the vertical indicator (y), and left or right direction if the vertical one (x).
Firstly, here's the code that moves my indicators (which are just textures being drawn in my RenderScene() function)
void SpecialKeys(int key, int x, int y){
if (key == GLUT_KEY_RIGHT) { // moves the bottom indicator RIGHT
horizontalBarX += 5.0f;
}
if (key == GLUT_KEY_LEFT) {
horizontalBarX -= 5.0f; // moves the bottom indicator LEFT
}
if (key == GLUT_KEY_UP) { // moves the top indicator UP
verticalBarY += 5.0f;
verticalBarX += 1.0f;
}
if (key == GLUT_KEY_DOWN) { // moves the top indicator DOWN
verticalBarY -= 5.0f;
verticalBarX -= 1.0f;
}
}
Calculations for my football to move
Now to get my football to move after the indicators have be moved, I need to apply the following calculations to the x, y and z axis of the ball:
x = sin(theta) * cos (phi) y = cos(theta) * sin(phi) z = cos(theta)
where theta = angle in z-x, and phi = angle in z-y
So with this, I have attempted to get the values of both theta and phi angles first, by simply incrementing them depending on what height indicators you have pressed in the SpecialKeys() function:
void SpecialKeys(int key, int x, int y){
if (key == GLUT_KEY_RIGHT) { // moves the bottom indicator RIGHT
horizontalBarX += 5.0f;
theta += 5; // Increase theta angle by 5
}
if (key == GLUT_KEY_LEFT) {
horizontalBarX -= 5.0f; // moves the bottom indicator LEFT
theta -= 5; // Decrease theta angle by 5
}
if (key == GLUT_KEY_UP) { // moves the top indicator UP
verticalBarY += 5.0f;
verticalBarX += 1.0f;
phi += 5; // Increase theta angle by 5
}
if (key == GLUT_KEY_DOWN) { // moves the top indicator DOWN
verticalBarY -= 5.0f;
verticalBarX -= 1.0f;
phi -= 5; // Decrease phi angle by 5
}
}
Now that I have the angles, I want to plug in the calculated values into the drawFootball() parameters, which by the way is initially called in my RenderScene function as...
drawFootBall(0, 40, 500, 50); // x,.y, z, r
...and here's how I'm attempting to launch the ball with the calculations above:
void SpecialKeys(int key, int x, int y){
// indicator if statements just above this line
if (key == GLUT_KEY_F1) {
drawFootBall(sin(theta)*cos(phi), cos(theta)*sin(phi), cos(theta), 50);
}
}
But when I go to click the launch button F1, nothing happens at all. Where have I messed up?
EDIT:
If it helps, here's my drawFootball() function:
void drawFootBall(GLfloat x, GLfloat y, GLfloat z, GLfloat r)
{
glPushMatrix();
glFrontFace(GL_CCW);
glTranslatef(x,y,z);
//create ball texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_BALL]);
//glDisable(GL_LIGHTING);
glColor3f(0.5,0.5,0.5);
quadricFootball = gluNewQuadric();
gluQuadricDrawStyle(quadricFootball, GLU_FILL);
gluQuadricNormals(quadricFootball, GLU_SMOOTH);
gluQuadricOrientation(quadricFootball, GLU_OUTSIDE);
gluQuadricTexture(quadricFootball, GL_TRUE);
gluSphere(quadricFootball, r, 85, 50);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
Firstly make sure your theta and phi are in the right units i.e. Radians. If in degrees convert them to radians by using sin(theta * PI/180.0f) and so on, assuming PI is defined.
I believe what you are computing there is a direction vector for the Ball. The d(x,y,z) is the direction in which the ball should travel, (assuming there is no gravity or other forces). Its probably the direction in which the ball is kicked.
I think if you simply wanted to move your ball, you need to multiply this direction with a length. Since your ball has a radius of 50 units, try translating to 2 times this radius.
glTranslatef(2.0f*r*x,2.0f*r*y,2.0f*r*z);
This will move the ball to 2 times its radius in your desired direction.
However you probably want to have some physics to have a more realistic movement.

OpenGL - How can I move my printed lines?

I have set up a mouse function which is called in my main() like so:
struct point
{
int x;
int y;
};
std::vector <point> points;
point OnePoint;
void processMouse(int button, int state, int x, int y)
{
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
{
int yy;
yy = glutGet(GLUT_WINDOW_HEIGHT);
y = yy - y; /* In Glut, Y coordinate increases from top to bottom */
OnePoint.x = x;
OnePoint.y = y;
points.push_back(OnePoint);
}
glutPostRedisplay();
}
and to print a line through the vertices I have written some code in my display function that allows me to do just that:
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i < points.size();i++)
{
glVertex2i(points[i].x, points[i].y);
}
glEnd();
However now what I want to do is move all the lines either left or right when I click the correct arrow keys to do so, but I can't figure out how.
I'm aware that it could possibly be something like:
glVertex2i(points[i].x + 10, points[i].y); // moving points 10 along the x axis
However since i is out of the for loop, I get error messages
You should introduce a new variable:
std::vector <point> points;
point offset; // <- how much to offset points
Make sure to set it to zero during initialization.
Then in the drawing code you add that offset to each point:
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i < points.size();i++)
glVertex2i(points[i].x + offset.x, points[i].y + offset.y);
glEnd();
Or use a translation matrix to do that automagically:
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(offset.x, offset.y, 0);
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i < points.size();i++)
glVertex2i(points[i].x, points[i].y);
glEnd();
glPopMatrix();
On the key-press handler you just change the offset:
// left-arrow:
offset.x -= 1;
// right-arrow:
offset.x += 1;
...

Moving a drawing around in openGL with mouse

I am trying to move an image around in openGL while holding left mouse button.
i am NOT trying to drag an object around, just move the whole picture. Its a 2d drawing of a fractal and i was told that i can use gluortho2d but i can't find any info or similar tries on how to do it.
I am assuming something like
void mouse_callback_func(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
gluOrtho2D(x-250.0, x+250.0, y-250.0,y+250.);
glutPostRedisplay();
}
for a 500x500 window,but it's not working. The moment i left click the window goes blank.
Any ideas?
gluOrtho2D modifies the current matrix. It's designed to be used with glMatrixMode(GL_PROJECTION), for example:
glMatrixMode(GL_PROJECTION); //start editing the projection matrix
glLoadIdentity(); //remove current projection
gluOrtho2D(...); //create new one
glMatrixMode(GL_MODELVIEW); //back to editing the modelview matrix
It might be more simple to set up a camera concept...
float cameraX, cameraY;
int lastMouseX, lastMouseY;
void mouse_callback_func(int button, int state, int x, int y)
{
int dx = x - lastMouseX;
int dy = y - lastMouseY;
const float speed = 0.1f;
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
cameraX += dx * speed; //or -=, depending on which direction feels more natural to you
cameraY -= dy * speed; //-= as mouse origin is top left, so +y is moving down
glutPostRedisplay();
}
lastMouseX = x;
lastMouseX = y;
}
void display()
{
glLoadIdentity(); //remove transforms from previous display() call
glTranslatef(-cameraX, -cameraY, 0.0f); //move objects negative = move camera positive
...

OpenGL 3rd person camera

I've put together a 3rd person camera system using OpenGL and C++ from tutorials and such online, but I can't seem to figure out a specific problem. When I turn using mouse movement, my character is rotated around the camera rather than the camera around the character and the character turning on the spot. What should I do to have the character turn on the spot?
// variables ..
void checkMouse(){
if (mouseXPos > SCREEN_WIDTH/2){
// turn right
yrot += abs(mouseXPos - SCREEN_WIDTH/2) * .005;
} else if (mouseXPos < SCREEN_WIDTH/2){
// turn left
yrot -= abs(mouseXPos - SCREEN_WIDTH/2) * .005;
}
if (mouseYPos > SCREEN_HEIGHT/2){
// look up
xrot += abs(mouseYPos - SCREEN_HEIGHT/2) * .005;
} else if (mouseYPos < SCREEN_HEIGHT/2){
// look down
xrot -= abs(mouseYPos - SCREEN_HEIGHT/2) * .005;
}
}
void checkKeys(){
if(keys['t'] == true){
wireframe=!wireframe;
if(wireframe){
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if (keys['w'] == true){
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(yrotrad)) * 10 ;
zpos -= float(cos(yrotrad)) * 10 ;
}
if (keys['s'] == true){
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos -= float(sin(yrotrad)) * 10;
zpos += float(cos(yrotrad)) * 10;
}
if (keys['a'] == true){
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos -= float(cos(yrotrad)) * 10;
zpos -= float(sin(yrotrad)) * 10;
}
if (keys['d'] == true){
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos += float(cos(yrotrad)) * 10;
zpos += float(sin(yrotrad)) * 10;
}
}
void renderScene(){
// Clear framebuffer & depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset Modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set view position & direction
gluLookAt(0,0,5, 0,0,-1, 0,1,0);
checkKeys();
checkMouse();
// 3rd person object
// draw body
glPushMatrix();
glRotatef(xrot,1.0,0.0,0.0); // keeps object on ground level rather than always in front of camera
glTranslatef(0,-90,-400.0); // keep object 400 away from camera
glRotatef(-90,0.0,1.0,0.0);
glutSolidCube(20);
glPopMatrix();
// CAMERA
glRotatef(xrot,1.0,0.0,0.0); //rotate our camera on the x-axis (left and right)
glRotatef(yrot,0.0,1.0,0.0); //rotate our camera on the y-axis (up and down)
glTranslated(-xpos,-ypos-200,-zpos);
// rest of world
glPushMatrix();
glutSolidCube(30);
glPopMatrix();
// ..
glDisable(GL_TEXTURE_2D);
// Swap double buffer for flicker-free animation
glutSwapBuffers();
}
void updateScene(){
// Wait until at least 16ms passed since start of last frame
// Effectively caps framerate at ~60fps
while(timeGetTime()-lastTickCount<16);
lastTickCount=timeGetTime();
// Draw the next frame
glutPostRedisplay();
}
void keypress (unsigned char key, int x, int y) {
keys[key] = true;
// Test if user pressed ESCAPE (ascii 27)
// If so, exit the program
if(key==27){
exitScene();
}
}
void keypressup (unsigned char key, int x, int y) {
keys[key] = false;
wheel_turn = 0;
}
void mouseMovement(int x, int y) {
mouseXPos = x;
mouseYPos = y;
}
void mouseClick(int button, int state, int x, int y){
if (button == GLUT_LEFT_BUTTON){
if (state == GLUT_DOWN)
lButton = true;
else
lButton = false;
}
}
void setupScene(){
forwards = 0;
strafe = 0;
turn = 0;
std::cout<<"Initializing scene..."<<std::endl;
//Set up Lighting Stuff
glLightfv(GL_LIGHT0, GL_POSITION, left_light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, white_light);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
}
void exitScene(){
std::cout<<"Exiting scene..."<<std::endl;
// Close window
glutDestroyWindow(windowId);
// Free any allocated memory
// Exit program
exit(0);
}
void setViewport(int width, int height) {
// Work out window ratio, avoid divide-by-zero
if(height==0)height=1;
float ratio = float(width)/float(height);
// Reset projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Fill screen with viewport
glViewport(0, 0, width, height);
// Set a 45 degree perspective
gluPerspective(45, ratio, .1, 200000);
}
int main(int argc, char *argv[]){
// Initialise OpenGL
glutInit(&argc, argv);
// Set window position, size & create window
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(50,50);
glutInitWindowSize(SCREEN_WIDTH,SCREEN_HEIGHT);
windowId = glutCreateWindow("3rd person cam");
// Set GLUT callback functions
glutReshapeFunc(setViewport);
glutDisplayFunc(renderScene);
glutIdleFunc(updateScene);
glutKeyboardFunc(keypress);
glutKeyboardUpFunc(keypressup);
glutPassiveMotionFunc(mouseMovement); //check for mouse movement
glutMotionFunc(mouseMovement);
glutMouseFunc(mouseClick);
// Setup OpenGL state & scene resources (models, textures etc)
setupScene();
// Show window & start update loop
glutMainLoop();
return 0;
}
You're rotating the camera around itself — it's akin to you turning your head. You want to change the camera position, revolving around your object of interest.
1. Find your camera position
Look up 'spherical coordinates' for this
Your horizontal angle should vary between (0 and 2*PI) based on mouse x move
Your vertical angle should vary between (0 and PI) based on mouse y move
You can scale the found (x,y,z) position with a value to vary the distance between camera and object
Add the object position to this found position
You now have a valid camera position around your object
2. Find View matrix
There's a handy glut method called gluLookAt, just use that to find your final camera matrix. It needs (camera positoin, object position, and world up(0,1,0))