完整的代码如下
#include <GL/glut.h>
#include <cmath>
#include <utility>
#include <vector>
using std::vector;
using std::pair;
vector<pair<int, int> > my_points(4);
vector<pair<int, int> > current_point(4);
my_points.push_back(make_pair(0, 0));
my_points.push_back(make_pair(50, 0));
my_points.push_back(make_pair(100, 0));
my_points.push_back(make_pair(150, 0));
current_point = my_points;
struct Degree {
Degree(int a, int b, int c) {
first = a;
second = b;
third = c;
}
int first;
int second;
int third;
};
map<int, Degree> position(4);
position[0] = position(0, 0, 0);
position[1] = position(90, 0, 0);
position[2] = position(45, -45, 45);
position[3] = position(-90, 0, 0);
void init();
void display();
void draw_axis();
void reshape(int w, int h);
void draw_frame(void);
void translate(Degree& posi);
void keyboard(unsigned char key, int x, int y);
float matrix_rotat[3][3] = { // 旋转矩阵
{0.0, -1.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, 0.0, 1.0}
};
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
void init(void) {
glClearColor(0.1, 0.2, 0.4, 0.0);
glShadeModel (GL_SMOOTH);
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if (w <= h)
gluOrtho2D(-200.0, 250.0, -100.0*(GLfloat)h/(GLfloat)w,
200.0*(GLfloat)h/(GLfloat)w);
else
gluOrtho2D(-200.0*(GLfloat)w/(GLfloat)h,
250.0*(GLfloat)w/(GLfloat)h, -50.0, 200.0);
glColor3f(1, 0.5, 0.4);
glMatrixMode(GL_MODELVIEW);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0.5, 0.4);
draw_axis();
glLoadIdentity();
// draw_frame();
glFlush ();
}
void draw_frame() {
glShadeModel(GL_SMOOTH);
glBegin (GL_LINE_STRIP);//draw frame
for (int i = 0; i < 4; i++) {
glColor3f(1.0, 0.0, 0.0);
glVertex2f(current_point[i].first, current_point[i].second);
}
glEnd();
}
void draw_axis() {
// draw coordinate axis
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0); // x axis
glVertex2f(-500.0, 40.0);
glVertex2f(500.0, 40.0);
glColor3f(1.0, 1.0, 1.0); // y axis
glVertex2f(0.0, 400.0);
glVertex2f(0.0, -400.0);
glEnd();
// draw scale
glBegin(GL_LINES);
for (int i = -50; i <= 50; i++) { // x axis
glColor3f(1.0, 1.0, 1.0);
glVertex2f(10.0*i, 40.0);
glVertex2f(10.0*i, 44.0);
}
for (int i = -40; i <= 40; i++) { // y axis
glColor3f(1.0, 1.0, 1.0);
glVertex2f(0.0, 10.0*i);
glVertex2f(4.0, 10.0*i);
}
glEnd();
}
void translate(Degree& posi) {
///// changed into matrix later
vector<vector<double> > trans_matrix(2);
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
trans_matrix[i].push_back(0);
double num1[] = {cos(posi.first), -sin(posi.first)};
//initialize the transform matrix
for (int i = 0; i < 2; i++)
trans_matrix[0][i] = num1[i];
double num2 = {sin(posi.first), cos(posi.first)};
for (int i = 0; i < 2; i++)
trans_matrix[1][i] = num2[i];
current_point[0].first = current_point[0].second = 0;
for (int i = 0; i < 2; i++) {
current_point[0].first += (trans_matrix[i][0]*my_points[0].first
+ trans_matrix[i][1]*my_points[1].second);
current_point[0].second += (trans_matrix[i][0]*my_points[0].first
+ trans_matrix[i][1]*my_points[0].second);
}
glBegin (GL_LINES);//draw frame
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0, 0);
glVertex2f(current_point[0].first, current_point[0].second);
glEnd();
}
/*
void rotate_frame(void) {
float (*tmp_frame)[7] = new float[3][7];
memset(tmp_frame, 0.0, sizeof(tmp_frame));
frame = tmp_frame;
glLoadIdentity();
draw_frame(); // draw the frame after transformation
glFlush();
}
*/
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'Q':
case 'q':
translate(position[0]);
break;
case 'W':
case 'w':
translate(position[1]);
break;
case 'E':
case 'e':
translate(position[2]);
break;
case 'R':
case 'r':
translate(position[3]);
break;
}
}
编译器报错>
arm.cpp:12:1: error: ‘my_points’ does not name a type
my_points.push_back(make_pair(0, 0));
^
arm.cpp:13:1: error: ‘my_points’ does not name a type
my_points.push_back(make_pair(50, 0));
^
arm.cpp:14:1: error: ‘my_points’ does not name a type
my_points.push_back(make_pair(100, 0));
^
arm.cpp:15:1: error: ‘my_points’ does not name a type
my_points.push_back(make_pair(150, 0));
^
arm.cpp:16:1: error: ‘current_point’ does not name a type
current_point = my_points;
^
arm.cpp:28:1: error: ‘map’ does not name a type
map position(4);
^
arm.cpp:30:1: error: ‘position’ does not name a type
position[0] = position(0, 0, 0);
^
arm.cpp:31:1: error: ‘position’ does not name a type
position[1] = position(90, 0, 0);
^
arm.cpp:32:1: error: ‘position’ does not name a type
position[2] = position(45, -45, 45);
^
arm.cpp:33:1: error: ‘position’ does not name a type
position[3] = position(-90, 0, 0);
^
不知道这里是什么问题,STL为什么不能用,是编译选项的问题吗?
编译命令g++ arm.cpp -o arm -lGL -lglut -lGLU