#define _USE_MATH_DEFINES
#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
using namespace std;
GLdouble ox=0.0,oy=0.0,oz= 0.0;
int CursorX;
int CursorY;
float Xdevi;
float Ydevi;
float ang;
float enmX,enmY;
float a;
float angleofpoint2(float x1,float y1,float x2, float y2)
{
float b;
//b = atan2(y2 - y1, x2 - x1) * 180 / M_PI;
b = atan2(y2 - y1, x2 - x1) * 180 / 3.14159265;
return b;
}
void WinPosTo3DPos(int x, int y)
{
GLint viewport[4];
GLdouble modelview[16],projection[16];
GLfloat wx=x,wy,wz;
glGetIntegerv(GL_VIEWPORT,viewport);
y=viewport[3]-y;
wy=y;
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&wz);
gluUnProject(wx,wy,wz,modelview,projection,viewport,&ox,&oy,&oz);
}
void PosByAngle(float angle,float lenght,float x, float y)
{
enmX = (sin(angle) * lenght) + enmX;
enmY = (cos(angle) * lenght) + enmY;
}
void initRender(int w, int h)
{
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,
(double)w / (double)h,
1.0,
200.0);
WinPosTo3DPos(400,300);
}
void Render()
{
glDepthMask(true);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(0.8f,0.8f,0.6f);
glBegin(GL_TRIANGLE_STRIP);
glVertex3d(-50.0f,50.0f,-51.0f);
glVertex3d(-50.0f,-50.0f,-51.0f);
glVertex3d(50.0f,50.0f,-51.0f);
glVertex3d(50.0f,-50.0f,-51.0f);
glEnd();
glLoadIdentity();
a = angleofpoint2(enmX, enmY, CursorX , CursorY);
//ang = sin(
cout << a<< " ";
PosByAngle(a, 0.0f, enmX, enmY);
glTranslatef(enmX,enmY,-50.0f);
glColor3f(1.0f,0.0f,0.0f);
glutSolidSphere(1,8,8);
glLoadIdentity();
glDepthMask(false);
glColor3f(0.0f,1.0f,0.0f);
WinPosTo3DPos(CursorX,CursorY);
glTranslatef(ox,oy,oz);
glutSolidSphere(1,16,16);
glLoadIdentity();
glutSwapBuffers();
}
void MouseMang(int button, int state, int x, int y) // Mouse handle function
{
WinPosTo3DPos(x,y);
cout << "Done!";
}
void MouseMang_Move(int x, int y)
{
CursorX = x;
CursorY = y;
}
void Game(int vaule)
{
glutPostRedisplay();
glutTimerFunc(25, Game,0);
}
int main(int argc, char** argv)
{
enmX = 2.0f;
enmY = 0.0f;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("OpenGL");
initRender(800,600);
glutPassiveMotionFunc(MouseMang_Move);
glutMouseFunc(MouseMang);
glutDisplayFunc(Render);
glutTimerFunc(1000, Game,0);
glutMainLoop();
return 0;
}