C++ 在OpenGL中绘制对象的轨迹
我想制作代码,当我点击屏幕上的任何位置时,在对象移动时绘制轨迹 我将初始中心点设置为起点。我想用鼠标点击另一个点(目标点)作为变量,但我不知道怎么做C++ 在OpenGL中绘制对象的轨迹,c++,c,opengl,C++,C,Opengl,我想制作代码,当我点击屏幕上的任何位置时,在对象移动时绘制轨迹 我将初始中心点设置为起点。我想用鼠标点击另一个点(目标点)作为变量,但我不知道怎么做 float v1[3] = { -35.0f, 22.5f, 0.0f }; float v2[3] = { -35.0f, -22.5f, 0.0f }; float v3[3] = { 0.0f, 42.5f, 0.0f }; float v4[3] = { 0.0f, -42.5f, 0.0f }; float v5[3] = { 35.
float v1[3] = { -35.0f, 22.5f, 0.0f };
float v2[3] = { -35.0f, -22.5f, 0.0f };
float v3[3] = { 0.0f, 42.5f, 0.0f };
float v4[3] = { 0.0f, -42.5f, 0.0f };
float v5[3] = { 35.0f, 22.5f, 0.0f };
float v6[3] = { 35.0f, -22.5f, 0.0f };
这是对象的初始位置。(带2个三角形的6点星)
center\u s
是起点,center\u d
是带有变量px
,py
的终点
void lines(void) {
glColor3f(1.0, 0.0, 0.0);
glPointSize(5);
glBegin(GL_POINTS);
glVertex3fv(center_s);
glLineWidth(1);
glBegin(GL_LINES);
glVertex3fv(center_s);
glVertex3fv(center_d);
}
此函数用红线从中心点
到中心点
绘制轨迹。它还绘制了一个中心点
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
x = mx;
y = glutGet(GLUT_WINDOW_HEIGHT) - my;
px = x + 35.0;
py = y + 42.5;
glutIdleFunc(lines);
}
glutPostRedisplay();
break;
问题就在这里。按下鼠标左键时,星星必须移动到单击的位置,并计算该位置的中心点并绘制线。但是如果我运行这些代码,运动轨迹就不会画出来
请告诉我是什么问题。另外,星星必须以恒定的速度移动到单击的位置。(不是远程传送)
以下是完整代码:
#include <stdlib.h>
#include <GL/glut.h>
float v1[3] = { -35.0f, 22.5f, 0.0f };
float v2[3] = { -35.0f, -22.5f, 0.0f };
float v3[3] = { 0.0f, 42.5f, 0.0f };
float v4[3] = { 0.0f, -42.5f, 0.0f };
float v5[3] = { 35.0f, 22.5f, 0.0f };
float v6[3] = { 35.0f, -22.5f, 0.0f };
float px, py;
float center_s[3] = { 0.0f, 0.0f, 0.0f };
float center_d[3] = { px, py, 0.0f };
static GLfloat spin = 0.0;
float x = 400.0f, y = 442.5f;
float color1[3] = { 1.0f, 1.0f, 1.0f };
float color2[3] = { 1.0f, 1.0f, 1.0f };
int mode = 1;
int rotate = 1;
void init(void);
void triangle_1(void);
void triangle_2(void);
void lines(void);
void display(void);
void spinDisplay_1(void);
void spinDisplay_2(void);
void reshape(int, int);
void changeColor(int);
void mouse(int, int, int, int);
////////////////////////////////////////////////////////////////////
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(300, 300);
glutCreateWindow("6-Point Star");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
////////////////////////////////////////////////////////////////////
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void triangle_1(void) {
glColor3fv(color1);
glBegin(GL_TRIANGLE_FAN);
glVertex3fv(v1);
glVertex3fv(v4);
glVertex3fv(v5);
glEnd();
}
void triangle_2(void) {
glColor3fv(color2);
glBegin(GL_TRIANGLE_FAN);
glVertex3fv(v2);
glVertex3fv(v3);
glVertex3fv(v6);
glEnd();
}
void lines(void) {
glColor3f(1.0, 0.0, 0.0);
glPointSize(5);
glBegin(GL_POINTS);
glVertex3fv(center_s);
glLineWidth(1);
glBegin(GL_LINES);
glVertex3fv(center_s);
glVertex3fv(center_d);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(x, y, 0.0f);
glRotatef(spin, 0.0, 0.0, 1.0);
triangle_1();
triangle_2();
glPopMatrix();
glutSwapBuffers();
}
void spinDisplay_1(void) {
spin = spin + 2.0;
if (spin > 360.0) {
spin = spin - 360.0;
}
glutPostRedisplay();
}
void spinDisplay_2(void) {
spin = spin - 2.0;
if (spin < 360.0) {
spin = spin + 360.0;
}
glutPostRedisplay();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void changeColor(int n) {
if (n == 1) {
color1[0] = 0.0f, color1[1] = 0.0f, color1[2] = 1.0f;
color2[0] = 0.0f, color2[1] = 1.0f, color2[2] = 0.0f;
}
else if (n == 2) {
color1[0] = 1.0f, color1[1] = 1.0f, color1[2] = 1.0f;
color2[0] = 1.0f, color2[1] = 1.0f, color2[2] = 1.0f;
}
}
void mouse(int button, int state, int mx, int my) {
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
x = mx;
y = glutGet(GLUT_WINDOW_HEIGHT) - my;
px = x + 35.0;
py = y + 42.5;
glutIdleFunc(lines);
}
glutPostRedisplay();
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN) {
if (mode == 1) {
changeColor(mode);
mode = 2;
}
else if (mode == 2) {
changeColor(mode);
mode = 1;
}
}
glutPostRedisplay();
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
if (rotate == 1) {
glutIdleFunc(spinDisplay_1);
rotate = 2;
}
else if (rotate == 2) {
glutIdleFunc(spinDisplay_2);
rotate = 1;
}
break;
default:
break;
}
}
#包括
#包括
浮动v1[3]={-35.0f,22.5f,0.0f};
浮点数v2[3]={-35.0f,-22.5f,0.0f};
浮点v3[3]={0.0f,42.5f,0.0f};
浮点数v4[3]={0.0f,-42.5f,0.0f};
浮点数v5[3]={35.0f,22.5f,0.0f};
浮动v6[3]={35.0f,-22.5f,0.0f};
浮动px,py;
浮心_s[3]={0.0f,0.0f,0.0f};
浮心_d[3]={px,py,0.0f};
静态GLfloat自旋=0.0;
浮子x=400.0f,y=442.5f;
float color1[3]={1.0f,1.0f,1.0f};
float color2[3]={1.0f,1.0f,1.0f};
int模式=1;
int=1;
void init(void);
空心三角形_1(空心);
空心三角形_2(空心);
虚线(void);
作废显示(作废);
void spinu 1(void);
void spinu 2(void);
空洞重塑(int,int);
无效颜色(int);
无效鼠标(int,int,int,int);
////////////////////////////////////////////////////////////////////
int main(int argc,字符**argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500500);
位置(300300);
窗口(“6点星”);
init();
glutDisplayFunc(显示器);
GLUTREFORUNC(重塑);
glutMouseFunc(小鼠);
glutMainLoop();
返回0;
}
////////////////////////////////////////////////////////////////////
void init(void){
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GLU平面);
}
空心三角形_1(空心){
glColor3fv(color1);
glBegin(GLU三角形风扇);
glVertex3fv(v1);
glVertex3fv(v4);
glVertex3fv(v5);
格伦德();
}
空心三角形_2(空心){
glColor3fv(color2);
glBegin(GLU三角形风扇);
glVertex3fv(v2);
glVertex3fv(v3);
glVertex3fv(v6);
格伦德();
}
空心线(空心线){
GL3F(1.0,0.0,0.0);
gl点大小(5);
glBegin(总分);
glVertex3fv(中心);
线宽(1);
glBegin(GL_行);
glVertex3fv(中心);
glVertex3fv(中心d);
}
作废显示(作废){
glClear(GLU颜色缓冲位);
glPushMatrix();
glTranslatef(x,y,0.0f);
glRotatef(自旋,0.0,0.0,1.0);
三角形_1();
三角形_2();
glPopMatrix();
glutSwapBuffers();
}
void spin显示_1(void){
自旋=自旋+2.0;
如果(旋转>360.0){
自旋=自旋-360.0;
}
再发现();
}
void spin显示_2(void){
自旋=自旋-2.0;
如果(旋转<360.0){
自旋=自旋+360.0;
}
再发现();
}
空洞重塑(整数w,整数h){
GLVIEW(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_投影);
glLoadIdentity();
gluOrtho2D(0.0500.0,0.0500.0);
glMatrixMode(GLU模型视图);
glLoadIdentity();
}
void changeColor(int n){
如果(n==1){
颜色1[0]=0.0f,颜色1[1]=0.0f,颜色1[2]=1.0f;
color2[0]=0.0f,color2[1]=1.0f,color2[2]=0.0f;
}
else如果(n==2){
颜色1[0]=1.0f,颜色1[1]=1.0f,颜色1[2]=1.0f;
color2[0]=1.0f,color2[1]=1.0f,color2[2]=1.0f;
}
}
无效鼠标(int按钮、int状态、int mx、int my){
开关(按钮){
案例过量左按钮:
如果(状态==GLUT\U DOWN){
x=mx;
y=glutGet(GLUT\U窗口\U高度)-我的;
px=x+35.0;
py=y+42.5;
glutIdleFunc(行);
}
再发现();
打破
机箱GLUT_中间按钮:
如果(状态==GLUT\U DOWN){
如果(模式==1){
改变颜色(模式);
模式=2;
}
否则如果(模式==2){
改变颜色(模式);
模式=1;
}
}
再发现();
打破
案例过量右按钮:
如果(状态==GLUT\U DOWN)
如果(旋转==1){
glutIdleFunc(spinDisplay_1);
旋转=2;
}
否则如果(旋转==2){
glutIdleFunc(spinDisplay_2);
旋转=1;
}
打破
违约:
打破
}
}
您缺少一些glEnd()
,除非您对center\u s
和center\u d
有其他计划,否则您不需要它们。Asx
和y
是center\u s
,而px
和py
是center\u d
因此,首先在mouse()
中,只需将鼠标位置分配给px
和py
,而不是x
和y
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
px = mx;
py = glutGet(GLUT_WINDOW_HEIGHT) - my;
}
现在,接下来您需要一种获取增量时间的方法。Delta time是自上一帧以来经过的时间。为了方便起见,我在display()
的顶部添加了以下代码
记住声明int timeLastFrame=0代码>在全局变量中
现在(仍在display()
)我们可以计算行驶路径的方向。我们通过计算两点之间的差值来实现这一点。然后计算长度和法线i
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
px = mx;
py = glutGet(GLUT_WINDOW_HEIGHT) - my;
}
int timeNow = glutGet(GLUT_ELAPSED_TIME);
float delta = (float)(timeNow - timeLastFrame) / 1000.0f;
timeLastFrame = timeNow;
float dx = px - x;
float dy = py - y;
float length = sqrt(dx * dx + dy * dy);
dx /= length;
dy /= length;
if (length > 1.0f) {
x += dx * speed * delta;
y += dy * speed * delta;
}
void display(void) {
int timeNow = glutGet(GLUT_ELAPSED_TIME);
float delta = (float)(timeNow - timeLastFrame) / 1000.0f;
timeLastFrame = timeNow;
float dx = px - x;
float dy = py - y;
float length = sqrt(dx * dx + dy * dy);
dx /= length;
dy /= length;
if (length > 1.0f) {
x += dx * speed * delta;
y += dy * speed * delta;
}
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(x, y, 0.0f);
glRotatef(spin, 0.0, 0.0, 1.0);
triangle_1();
triangle_2();
glPopMatrix();
lines();
glutSwapBuffers();
glutPostRedisplay();
}
void lines(void) {
glColor3f(1.0, 0.0, 0.0);
glPointSize(5);
glBegin(GL_POINTS);
glVertex2f(px, py);
glEnd();
glLineWidth(1);
glBegin(GL_LINES);
glVertex2f(x, y);
glVertex2f(px, py);
glEnd();
}