Moving a drawing around in openGL with mouse - opengl

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
...

Related

Calibrate coordinates on OpenGL?

I have a problem regarding the gluOrtho2D function. I want to resize the window size and calibrate its coordinates, but i have problems with the function. I think that the coordinates calculated
The problem is that if i click somewhere on the window, the point isn't on the cursor, but somewhere near it ( ~20 pixels). If i resize the window, i want the window points to be converted to the new window size, that's why the gluOrtho2D function had those parameters. Maybe i didn't figure it out the good way.
GLdouble mouseX = ( double )(2* widthValue * x / windowWidthSize) - widthSize ;
GLdouble mouseY = -( double )(2 * heightValue * y / windowHeightSize) + heightValue;
are good as i want them to take values from -30 to +30 (both x and y), but the
gluOrtho2D( -width / widthValue , width / widthValue , -height / heightValue , height / heightValue);
function doesn't convert them as i want. Any ideas to fix it?
#include<windows.h>
#include<GL/Glut.h> //includes the opengl, glu, and glut header files
#include<stdlib.h> //includes the standard library header file
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
vector<pair<GLdouble,GLdouble>>v;
GLdouble heightValue = 30.0; // the points X and Y coordinates would be [-30,30]
GLdouble widthValue = 30.0; // the points X and Y coordinates would be [-30,30]
void initGL()
{
glClearColor(1,1,1,0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5.0);
glBegin(GL_POINTS);
glColor3f(1.0f, 1.0f, 0.0f);
// prints the points
for(auto i : v)
glVertex2d(i.first, i.second);
glEnd();
glFlush();
}
void reshape(GLsizei width, GLsizei height) {
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset the projection matrix
// here is the problem with the coordinates. Any help??
gluOrtho2D(-width/ widthValue, width/ widthValue,-height / heightValue ,height / heightValue);
}
void mouseClick(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) // left button is pressed
{
double windowHeightSize = glutGet(GLUT_WINDOW_HEIGHT); // gets window's size
double windowWidthSize = glutGet(GLUT_WINDOW_WIDTH); // gets window's size
// converts the click coordinate on the screen to the coordinate on the window
GLdouble mouseX = (double)(60.0 * x / windowWidthSize) - 30.0;
GLdouble mouseY = -(double)(60.0 * y / windowHeightSize) + 30.0;
v.push_back(make_pair(mouseX,mouseY) ); // insert the point's coordinates in the vector
display();
}
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitWindowSize(800, 600);
glutInitWindowPosition(50, 50);//sets the position of the window in pixels from top left corner
glutCreateWindow("Program Find Convex Hull");
glutDisplayFunc(display);
glutReshapeFunc(reshape); // functia este apelata cand redimensionam fereastra
glutMouseFunc(mouseClick);
// initGL();
glutMainLoop();//loops the current event
return 0;
}
If the Orthographic projection is set by:
gluOrtho2D(-ortho_x, ortho_x, -ortho_y, ortho_y);
then the transformation of the moue position (window coordinates) to view coordinates is:
GLdouble mouseX = (double)(x/windowWidthSize * 2*ortho_x) - ortho_x;
GLdouble mouseY = ortho_y - (double)(y/windowHeightSize * 2*ortho_y);
Apply that to your example:
GLdouble ortho_x = 30.0;
GLdouble ortho_y = -30.0;
void reshape(GLsizei width, GLsizei height) {
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset the projection matrix
ortho_x = width / widthValue;
ortho_y = height / heightValue;
gluOrtho2D(-ortho_x, ortho_x, -ortho_y, ortho_y);
}
void mouseClick(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) // left button is pressed
{
double windowHeightSize = glutGet(GLUT_WINDOW_HEIGHT); // gets window's size
double windowWidthSize = glutGet(GLUT_WINDOW_WIDTH); // gets window's size
// converts the click coordinate on the screen to the coordinate on the window
GLdouble mouseX = (double)(x/windowWidthSize * 2*ortho_x) - ortho_x;
GLdouble mouseY = ortho_y - (double)(y/windowHeightSize * 2*ortho_y);
v.push_back(make_pair(mouseX,mouseY) ); // insert the point's coordinates in the vector
glutPostRedisplay();
}
}
Note, it is not necessary to change (ortho_x, ortho_y) in reshape. Since you mentioned in your question "i want them to take values from -30 to +30", it is even possible to keep (30.0, 30.0).

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.

DirectX11 Mouse Control

I have a 3d program in DirectX and I want to give the mouse control to the camera. The problem is the mouse moves right off the screen ( In windowed mode ) then the camera doesn't turn anymore. I tried to use SetCusorPos to just lock it in place after the mouse is moved. That way I could get a dx and then set the mouse back to the center of the screen. I ended up getting a endless white screen. Here is my camera/mouse movement code so far. If you need any more information just ask.
void PhysicsApp::OnMouseMove(WPARAM btnState, int x, int y)
{
// Make each pixel correspond to a quarter of a degree.
float dx = XMConvertToRadians(0.25f*static_cast<float>(x - mLastMousePos.x));
float dy = XMConvertToRadians(0.25f*static_cast<float>(y - mLastMousePos.y));
// Update angles based on input to orbit camera around box.
mTheta += -dx;
mPhi += -dy;
// Update players direction to always face forward
playerRotation.y = -mTheta;
// Restrict the angle mPhi.
mPhi = MathHelper::Clamp(mPhi, 0.1f, MathHelper::Pi-0.1f);
if( (btnState & MK_RBUTTON) != 0 )
{
// Make each pixel correspond to 0.2 unit in the scene.
float dx = 0.05f*static_cast<float>(x - mLastMousePos.x);
float dy = 0.05f*static_cast<float>(y - mLastMousePos.y);
// Update the camera radius based on input.
mRadius += dx - dy;
// Restrict the radius.
mRadius = MathHelper::Clamp(mRadius, 5.0f, 50.0f);
}
mLastMousePos.x = x;
mLastMousePos.y = y;
}

Mouse movement accelerates code execution?

I'm teaching myself how to use OpenGL to create graphics, and I've got a basic spiral script+rotation. The Y-Axis rotation is automatic based on a timer function, but I noticed that when I move my mouse inside the window, it seems to rotate faster than intended. Could someone please look over my script and tell me what is causing the acceleration of the timer function?
#include <Windows.h>
#include <glut.h>
#include <stdio.h>
#include <math.h>
// Change viewing volume and viewport. Called when window is resized
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat nRange = 100.0f;
//Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, buttom, top, near, far)
if (w<= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
//Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//Define a constant for pi
#define GL_PI 3.1415f
// This function does all the initialization
void SetupRC()
{
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
// Set drawing color to green
glColor3f(0.0f, 1.0f, 0.0f);
}
// Test declaration of rotation angle
GLfloat xRot = 0;
GLfloat yRot = 0;
// Modifiable variables for the eccentricity
GLfloat xMod = 50.0f;
GLfloat yMod = 50.0f;
// Called to draw scene
void RenderScene(void)
{
GLfloat x,y,z,angle; // Storage for coordinates and angles
GLfloat sizes[2]; // Store supported point size range
GLfloat step; // Store point size increments
GLfloat curSize; //Store current point size
// Get supported point size range and step size
glGetFloatv(GL_POINT_SIZE_RANGE, sizes);
glGetFloatv(GL_POINT_SIZE_GRANULARITY, &step);
//Set the initial point size
curSize = sizes[0];
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
// specify point size before primitive is specified
glPointSize(curSize);
//Call only once for remaining points
glBegin(GL_LINE_STRIP);
//Set beginning z coordinate
z = -50.0f;
//Loop around in a circle three times
for (angle = 0.0f; angle <= (2.0f*GL_PI)*3.0f; angle += 0.1f)
{
// Calculate x and y values on the circle (the major and minor axis)
x = xMod*sin(angle);
y = yMod*cos(angle);
// Specify the point and move the z value up a little
glVertex3f(x, y, z);
z += 0.5f;
}
// Done drawing points
glEnd();
// Restore transformations
glPopMatrix();
//Flush drawing commands
glFlush();
}
// Modifier Code
void CircleController (int key, int x, int y)
{
switch (key)
{
case 27 : break;
case 100 :
(yRot -= 5.0f); ; break;
case 102 :
(yRot += 5.0f); ; break;
case 101 :
(xRot -= 5.0f); ; break;
case 103 :
(xRot += 5.0f); ; break;
glutDisplayFunc(RenderScene);
}
}
void MouseHandler (int button, int state, int x, int y)
{
// Holder variable assigned to overcome printf limitation and prevent double- printing due to MouseUp function call
GLfloat Holder = xMod;
// Increases size, and decreases timer speed by increasing the amount of time needed.
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
xMod+= 5.0f;
}
// Decreases size, and increases timer speed by decreasing the amount of time needed.
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
xMod-= 5.0f ;
}
if (Holder != xMod)
printf("%d \n", Holder);
}
void TimerFunction(int value)
{
//Call the arrow key function
glutSpecialFunc(CircleController);
//Call the Mouseclick Modifier function
glutMouseFunc(MouseHandler);
if (xRot < 360)
(xRot += 1.0f);
else
(xRot = 0.0f);
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(1.6666f, TimerFunction, 1);
}
void main(void)
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Drawing Lines");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(1.6666f, TimerFunction, 1);
SetupRC();
glutMainLoop();
}
Eric Palace gave me the start
My personal theory was always that it had something to do with the window focus and which program is given more CPU time, but that's just pure speculation.
That makes sense to me. But wait, don't you only paint on a timer? Wouldn't that prevent additional CPU time from modifying movement speed? Yes you do. Sortof.
glutTimerFunc(1.6666f, TimerFunction, 1);
The doc's for glutTimerFunc say that the first parameter is an unsigned int, representing the timer in milliseconds. So you're telling glut "call this function every 1 millsecond." (Approx 1000FPS) And since it takes longer than one millisecond to execute, you're effectively telling it "run this function over and over as fast as you possibly can". Which it does. And so additional CPU time is making a difference.
To avoid situtations like this (aside from correcting the 1.6666f parameter), it's usually suggested to
update the "world" in separate functions from painting the screen. In fact, I would imagine it to be common to have two or more world update functions. One for stuff that needs updating with the paint: arrows and running characters, one for stuff that only changes once a second or so: mana regen and npc decisions, and one for really slow stuff: respawns.
During an "update", check how much time has passed since the last update, (maxing out at half a second or so), and make the world update that much. Then if updates run twice as often or half as often for any reason, the game doesn't appear to speed up or slow down, you just just more/fewer frames instead.
Here's what such an update might look like
radians xrot = 0; //use a units class
radians rot_per_second = .01;
void updateFrame(double time_passed) {
assert(time_passed>=0 && time_passed <= 1.0);
radians rotate_thistime = rot_per_second * time_passed;
xrot += rotate_thistime;
}
void do_updates() {
static clock_t lastupdatetime = clock()-1; //use openGL functions instead of C
clock_t thisupdatetime = clock();
double seconds = double(thisupdatetime-lastupdatetime)/CLOCKS_PER_SEC;
if (seconds > 0.5) //if something happened and haven't update in a long time
seconds = 0.5; //pretend only half a second passed.
//this can happen if
// computer is overloaded
// computer hibernates
// the process is debugged
// the clock changes
if (seconds <= 0.0) //computer is REALLY fast or clock changed
return; //skip this update, we'll do it when we have sane numbers
updateFrame(seconds);
lastupdatetime = thisupdatetime;
}

OpenGl - Object inverts after 180 degrees of rotation

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.