/*
磁铁组绘制
*/
#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glut.h>
#include <stdio.h>
#include <math.h>
#include "ArcBall.h"
// 定义滚轮操作
#define GLUT_WHEEL_UP 3
#define GLUT_WHEEL_DOWN 4
// Rotation amounts
// 定义滚轮操作
#define GLUT_WHEEL_UP 3
#define GLUT_WHEEL_DOWN 4
double magnet_para[100][8];
int magnet_name[100];
double vBx[201];
double vBy[201];
double vBz[201];
double line[201];
int mouse_x_left = 0;
int mouse_x_right = 0;
int mouse_y_up = 0;
int mouse_y_down = 0;
double smax=8;
double shift_x = 0;
double shift_y = 0;
double shift_move_x = 0;
double shift_move_y = 0;
double scale = 1;
bool left_tag = false;
bool right_tag = false;
int mouse_x = 0;
int mouse_y = 0;
double nstart_x;
double nstart_y;
double nstart_z;
double nstop_x;
double nstop_y;
double nstop_z;
int item_num = 1;
float dx, dy, dz;
GLdouble posx, posy, posz;
GLfloat vertices[] = { -1.0,-1.0,-1.0,1.0,-1.0,-1.0,
1.0,1.0,-1.0, -1.0,1.0,-1.0, -1.0,-1.0,1.0,
1.0,-1.0,1.0, 1.0,1.0,1.0, -1.0,1.0,1.0 };
GLfloat colors[] = { 0.0,0.0,0.0,1.0,0.0,0.0,
1.0,1.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0,
1.0,0.0,1.0, 1.0,1.0,1.0, 0.0,1.0,1.0 };
GLubyte cubeIndices[] = { 0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4 };
// mouse control
Matrix4fT Transform = { 1.0f, 0.0f, 0.0f, 0.0f, // NEW: Final Transform
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
Matrix3fT LastRot = { 1.0f, 0.0f, 0.0f, // NEW: Last Rotation
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };
Matrix3fT ThisRot = { 1.0f, 0.0f, 0.0f, // NEW: This Rotation
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };
ArcBallT ArcBall(640.0f, 480.0f); // NEW: ArcBall Instance
Point2fT MousePt; // NEW: Current Mouse Point
bool isClicked = false; // NEW: Clicking The Mouse?
bool isDragging = false; // NEW: Dragging The Mouse?
GLfloat nRange;
GLfloat size_max = 10.0f;
void ChangeSize(GLsizei w, GLsizei h);
void DrawAxis(GLfloat axis_length);
void DrawLine(GLfloat start_x, GLfloat start_y, GLfloat start_z, GLfloat stop_x, GLfloat stop_y, GLfloat stop_z);
void DrawBox(double *cx, double *cy, double *cz, double *a, double *b, double *c, int *mag, int total);
void mouse(int btn, int state, int x, int y);
void mouseMotion(int x, int y);
void SetupRC();
void screen2GLPoint(float px, float py);
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix(); // NEW: Prepare Dynamic Transform
glRotatef(300.0f, 1.0f, 0.0f, 0.0f); //初始模型角度
glRotatef(15.0f, 0.0f, 0.0f, 1.0f); //初始模型角度
//缩放
glScalef(scale, scale, scale);
//矩阵实现旋转
glMultMatrixf(Transform.M); // NEW: Apply Dynamic Transform
//矩阵实现平移
extern GLdouble posx, posy, posz;
void screen2GLPoint(float px, float py);
screen2GLPoint(shift_x + shift_move_x, shift_y + shift_move_y);
dx = posx *0.1*smax;
dy = posy *0.1*smax;
dz = posz *0.1*smax;
Matrix4fT move_matrix = { 1.0f, 0.0f, 0.0f, 0.0f, // NEW: Final Transform
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
dx, dy, dz, 1.0f };
glMultMatrixf(move_matrix.M);
// 画坐标系
DrawAxis(1.2*size_max);
// 画磁铁
double cx[4] = { -3,-3,3,3 };
double cy[4] = { -4,4,4,-4 };
double cz[4] = { 0,0,0,0 };
double a[4] = { 2,2,2,2 };
double b[4] = { 3,3,3,3 };
double c[4] = { 2,2,2,2 };
int mag[4] = { 31,31,32,32 };
int total = 4;
DrawBox(cx, cy, cz, a, b, c, mag, total);
// 画作图线
DrawLine(-5.0f, 0.0f, 1.0f, 5.0f, 0.0f, 1.0f);
glPopMatrix();
glutSwapBuffers();
}
//========================================
void RenderScene()
{
//清空颜色缓冲区,填充的颜色由 glClearColor( 0, 0.0, 0.0, 1 ); 指定为黑色
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
//glRotatef(300.0f, 1.0f, 0.0f, 0.0f);
//glRotatef(15.0f, 0.0f, 0.0f, 1.0f);
//glRotatef((anger_x + anger_move_x) * 90, 1.0f, 0.0f, 0.0f);
//glRotatef((anger_y + anger_move_y) * 90, 0.0f, 1.0f, 0.0f);
//glTranslatef((shift_x + shift_move_x) * 0.02, 0.0f, 0.0f);
//glTranslatef(0.0f, (shift_y + shift_move_y) * 0.02, 0.0f);
//glScalef(scale, scale, scale);
// 画坐标系
DrawAxis(1.2*size_max);
// 画磁铁
double cx[4] = {-3,-3,3,3};
double cy[4] = {-4,4,4,-4};
double cz[4] = {0,0,0,0};
double a[4] = {2,2,2,2};
double b[4] = {3,3,3,3};
double c[4] = {2,2,2,2};
int mag[4] = {31,31,32,32};
int total = 4;
DrawBox(cx,cy,cz,a,b,c,mag,total);
// 画作图线
DrawLine(-5.0f, 0.0f, 1.0f, 5.0f, 0.0f, 1.0f);
//glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
//glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glMultMatrixf(Transform.M); // NEW: Apply Dynamic Transform
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("模型(鼠标旋转、缩放、移动)");
glutDisplayFunc(display);
glutIdleFunc(display); //glutIdleFunc 表示在CPU空闲的时间调用某一函数
glutReshapeFunc(ChangeSize);
glutMouseFunc(mouse);
glutMotionFunc(mouseMotion);
SetupRC();
glutMainLoop();
return 1;
}
void SetupRC()
{
glClearColor(0.7, 0.7, 0.9, 1);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);//被遮住的部分不绘制
//glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
//glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
}
void ChangeSize(GLsizei w, GLsizei h)
{
//作图范围
//GLfloat nRange = 12.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, bottom, top, near, far)
int nscale = 2;
if (w <= h)
glOrtho(-nscale * smax, nscale * smax, -nscale * smax*h / w, nscale * smax*h / w, -2 * nscale * smax, 6 * nscale * smax);
else
glOrtho(-nscale * smax*w / h, nscale * smax*w / h, -nscale * smax, nscale * smax, -2 * nscale * smax, 6 * nscale * smax);
// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void DrawAxis(GLfloat axis_length)
{
//绘制x、y、z坐标轴
glColor3f(1.0f, 0.0f, 0.0f);//指定线的颜色,红色
glBegin(GL_LINES);
{
// x-axis
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(axis_length, 0.0f, 0.0f);
// x-axis arrow
glVertex3f(axis_length, 0.0f, 0.0f);
glVertex3f(0.95*axis_length, 0.03*axis_length, 0.0f);
glVertex3f(axis_length, 0.0f, 0.0f);
glVertex3f(0.95*axis_length, -0.03*axis_length, 0.0f);
glVertex3f(axis_length, 0.0f, 0.0f);
glVertex3f(0.95*axis_length, 0.0f, 0.03*axis_length);
glVertex3f(axis_length, 0.0f, 0.0f);
glVertex3f(0.95*axis_length, 0.0f, -0.03*axis_length);
}
glEnd();
glColor3f(0.0f, 1.0f, 0.0f);//指定线的颜色,绿色
glBegin(GL_LINES);
{
// y-axis
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, axis_length, 0.0f);
// y-axis arrow
glVertex3f(0.0f, axis_length, 0.0f);
glVertex3f(0.03*axis_length, 0.95*axis_lengt