移动相机角度
#include <stdio.h>
#include <gl/glut.h>
#include <math.h>
#include "gmath.h"
#include "gmatrix3d.h"
#include "gvector3d.h"
#include "gcamera.h"
bool gIsButtonDown = false;
int gMouseX = 0;
int gMouseY = 0;
GCamera gCamera;
void drawPlan()
{
glColor3f(0.6f,0.6f,0.6f);
glBegin(GL_LINES);
for(float u=-1.0f;u<1.01f;u+=0.2f)
{
glVertex3d(-1,0,u);
glVertex3d(1,0,u);
glVertex3d(u,0,-1);
glVertex3d(u,0,1);
}
glEnd();
}
void onDisplay()
{
glClearColor(0,0,0,1);
//清除缓存
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gCamera.lookAt();
glLineWidth(2);
drawPlan();
glPushMatrix();
glTranslatef(-0.3f,0,-0.3f);
glColor3f(1,0,0);
glutWireCube(0.4f);
glPopMatrix();
glPushMatrix();
glTranslatef(0.3f,0,-0.3f);
glColor3f(0,1,0);
glutWireSphere(0.2f,16,16);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0,0.3f);
glRotatef(-90,1,0,0);
glColor3f(1,1,0);
glutWireCone(0.2f,0.6f,16,8);
glPopMatrix();
//交换缓存
glutSwapBuffers();
}
void onReshape(int w,int h)
{
/*glViewport(0,0,w,h);
double aspect = (double)w/h;
glMatrixMode(GL_PROJECTION); //对接下来要做什么进行声明 GL_PROJECTION 投影, GL_MODELVIEW 模型视图, GL_TEXTURE 纹理.
glLoadIdentity(); //恢复初始坐标系
gluPerspective(60,aspect,0.1,10);
//glOrtho(-1,1,-1,1,-1,1);
glMatrixMode(GL_MODELVIEW);*/
gCamera.setScreenSize(w, h);
gCamera.project();
glutPostRedisplay();
}
GVector3d mousePt2SphereVec(int x, int y, int cw, int ch)
{
double lx, ly, lz,r;
lx = (2.0 * x - cw) / cw;
ly = (ch - 2.0 * y) / ch;
r = lx*lx + ly*ly;
if (r > 1