OpenGL:问题使图标旋转
OpenGL: Issues Making Icons Rotate
我已经成功地画出了一个有一条线穿过它的正方形,一个有一条线穿过它的梯形,以及一个在梯形上面有一条线穿过它的正方形。在这一点上,我只是试图让所有的图标在屏幕上慢慢旋转,但不知道我错在哪里。
我在之前编写的程序之后建模了我的程序,该程序使多边形在屏幕上行走并跳起来并进行翻转,但由于某种原因,我无法从任何图标中获得任何移动。我的TimerFunction
是否不像我期望的那样?
我知道这是乏味的阅读,但我包括我的整个代码,因为我不确定问题可能在哪里…
void init(void); //function that initializes the window clear color
void DrawsAllIcons(float x[], float y[], int ndraws, int pointsperdraw [], int drawtype[], float colorr[], float colorg[], float colorb[], float rotate, float scalex, float scaley, float transx, float transy); //function to draw the functions in the opened window
void SetupRC(void);
void RenderScene(void);
void settrans2 (float rotate, float scalex, float scaley, float transx, float transy);//function that sets the clear color used to set the state of the OpenGL system
void TimerFunction(int );
//square
float rotate = 00.0;
float xCoords [7] = {1.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0};
float yCoords [7] = {1.0,-1.0,-1.0,1.0,1.0,2.0,-2.0};
int numberofDraws = 2;
int pointsPerDraw[2] = {5, 2};
int typeOfDraw[2] = {2,1};
float colorR[3] = {1.0,0.0,0.0};
float colorg[3] = {0.0,1.0,0.0};
float colorb[3] = {0.0,0.0,1.0};
float transx = 5.0;
float transy = 5.0;
float scalex = 2.0;
float scaley = 2.0;
//trapezoid
float rotate2 = 00.0;
float xCoords2 [6] = {1.0, 1.5, -1.5, -1.0, 0.0, 0.0};
float yCoords2 [6] = {1.0,-1.0,-1.0,1.0,2.0,-2.0};
int numberofDraws2 = 2;
int pointsPerDraw2[2] = {4, 2};
int typeOfDraw2[2] = {3,1};
float colorR2[3] = {0.0,0.0,0.0};
float colorg2[3] = {1.0,1.0,0.0};
float colorb2[3] = {0.0,0.0,1.0};
float transx2 = -5.0;
float transy2 = -5.0;
float scalex2 = 2.0;
float scaley2 = 2.0;
//Square on trapezoid
float rotate3 = 00.0;
float xCoords3 [6] = {1.0, 1.0, -1.0, -1.0, 0.0,0.0};
float yCoords3 [6] = {1.0,0.0,0.0,1.0,3.0,-3.0};
int numberofDraws3 = 2;
int pointsPerDraw3[2] = {4, 2};
int typeOfDraw3[2] = {3,1};
float colorR3[3] = {1.0,0.0,0.0};
float colorg3[3] = {0.0,1.0,0.0}; //go down dont overthink
float colorb3[3] = {0.0,0.0,0.0};
float transx3 = 0.0;
float transy3 = 0.0;
float scalex3 = 1.0;
float scaley3 = 1.0;
//trapezoid under square
float rotate4 = 00.0;
float xCoords4 [6] = {1.5, 2.0, -2.0, -1.5, 0.0, 0.0};
float yCoords4 [6] = {0.0,-1.0,-1.0,0.0,3.0,-3.0};
int numberofDraws4 = 2;
int pointsPerDraw4[2] = {4, 2};
int typeOfDraw4[2] = {3,1};
float colorR4[3] = {0.0,0.0,0.0};
float colorg4[3] = {1.0,1.0,0.0};
float colorb4[3] = {0.0,0.0,1.0};
float transx4 = 0.0;
float transy4 = 0.0;
float scalex4 = 1.0;
float scaley4 = 1.0;
int main(int argc, char* *argv)
{
char header[]="This Bad Boy'll Draw any Icon you can think of"; //set up window title
glutInit(&argc, argv); // initialize glopen utility toolkit
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // Set up the display mode with a buffer and colors **
glutInitWindowSize(560,440); //window size and position
glutInitWindowPosition(140,20);
SetupRC();
glutCreateWindow(header); // Open and label the window
glutDisplayFunc(RenderScene); //points to the function that will be drawing the item // Set the state of the rendering machine
glutTimerFunc(30, TimerFunction, 1);
glutMainLoop(); // Call and activate the main
return 0;
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);//note clear color was set in SetupRC
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,540,440); //set viewpoint to dimensions
glOrtho(-20.0,20.0,-20.0,20.0,1.0,-1.0);
glClear(GL_COLOR_BUFFER_BIT);
DrawsAllIcons(xCoords, yCoords, numberofDraws, pointsPerDraw,typeOfDraw,colorR,colorg,colorb, rotate, transx,transy,scalex,scaley); //used GL_LINE_STRIP for the square to show that that case worked
DrawsAllIcons(xCoords2, yCoords2, numberofDraws2, pointsPerDraw2,typeOfDraw2,colorR2,colorg2,colorb2, rotate2, transx2,transy2,scalex2,scaley2); //used GL_POLYGON and GL_LINE for rest
DrawsAllIcons(xCoords3, yCoords3, numberofDraws3, pointsPerDraw3,typeOfDraw3,colorR3,colorg3,colorb3, rotate3, transx3,transy3,scalex3,scaley3);
DrawsAllIcons(xCoords4, yCoords4, numberofDraws4, pointsPerDraw4,typeOfDraw4,colorR4,colorg4,colorb4, rotate4, transx4,transy4,scalex4,scaley4);
glEnd();
glutSwapBuffers(); //
}
void DrawsAllIcons (float x[], float y[], int ndraws, int pointsperdraw [], int drawtype[], float colorr[], float colorg[], float colorb[], float rotateD, float transxD, float transyD, float scalexD, float scaleyD)
{
settrans2(rotateD,scalexD,scaleyD,transxD, transyD);
int k=0; //index for arrays
int drawTooIndex = 0;
ndraws=ndraws-1;
for (int j=0; j<=ndraws; j++) //runs through
{
int whatCase = drawtype[j]; //sees what type of draw
drawTooIndex +=pointsperdraw[j];
switch (whatCase)
{
case 1:
{
glColor3f(colorr[j],colorg[j],colorb[j]);
glBegin(GL_LINES);
glVertex2f(x[k], y[k]); //sets vertex at the first point at k in the point arrays
int i = k+1;
k++;
for (i; i <drawTooIndex; i++)
{
glVertex2f(x[i], y[i]);
k++;
}
glEnd();
glFlush();
}
break;
case 2:
{
glColor3f(colorr[j], colorg[j], colorb[j]);
glBegin(GL_LINE_STRIP);
glVertex2f(x[k], y[k]);
int m = k+1;
k++;
for (m; m <drawTooIndex; m++)
{
glVertex2f(x[m], y[m]);
k++;
}
glEnd();
glFlush();
}
break;
case 3:
{
glColor3f(colorr[j], colorg[j], colorb[j]);
glShadeModel(GL_FLAT);
glBegin(GL_POLYGON);
glVertex2f(x[k], y[k]);
int n = k+1; //gets index of where to start drawing in the x and y arrays
k++;
for (n; n <drawTooIndex; n++)
{
glVertex2f(x[n], y[n]);
k++;
}
glEnd();
glFlush();
}
break;
}
}
}
void SetupRC(void)
{ // function sets the clear color of an open window, and then clears the open window
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set clear color to pale green
return;
}//end of SetupRC
void settrans2(float rotateDD, float scalexDD, float scaleyDD, float transxDD, float transyDD)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(transxDD,transyDD,0.0);
glRotatef(rotateDD, 0.0, 0.0, 1.0); // where to put this in the program?
glScalef(scalexDD, scaleyDD, 1.0);
return;
}
void TimerFunction(int value)
{
rotate+=5.0;
rotate2+=5.0;
rotate3+=5.0;
rotate4+=5.0;
}
glutTimerFunc()
只触发一次,您需要在回调中重新武装它们以获得另一个,以及用新值重新绘制GLUT:
void TimerFunction(int value)
{
rotate+=5.0;
rotate2+=5.0;
rotate3+=5.0;
rotate4+=5.0;
glutPostRedisplay();
glutTimerFunc( 30, TimerFunction, 1 );
}
一起:
#include <GL/glut.h>
//square
float rotate = 00.0;
float xCoords [7] = {1.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0};
float yCoords [7] = {1.0,-1.0,-1.0,1.0,1.0,2.0,-2.0};
int numberofDraws = 2;
int pointsPerDraw[2] = {5, 2};
int typeOfDraw[2] = {2,1};
float colorR[3] = {1.0,0.0,0.0};
float colorg[3] = {0.0,1.0,0.0};
float colorb[3] = {0.0,0.0,1.0};
float transx = 5.0;
float transy = 5.0;
float scalex = 2.0;
float scaley = 2.0;
//trapezoid
float rotate2 = 00.0;
float xCoords2 [6] = {1.0, 1.5, -1.5, -1.0, 0.0, 0.0};
float yCoords2 [6] = {1.0,-1.0,-1.0,1.0,2.0,-2.0};
int numberofDraws2 = 2;
int pointsPerDraw2[2] = {4, 2};
int typeOfDraw2[2] = {3,1};
float colorR2[3] = {0.0,0.0,0.0};
float colorg2[3] = {1.0,1.0,0.0};
float colorb2[3] = {0.0,0.0,1.0};
float transx2 = -5.0;
float transy2 = -5.0;
float scalex2 = 2.0;
float scaley2 = 2.0;
//Square on trapezoid
float rotate3 = 00.0;
float xCoords3 [6] = {1.0, 1.0, -1.0, -1.0, 0.0,0.0};
float yCoords3 [6] = {1.0,0.0,0.0,1.0,3.0,-3.0};
int numberofDraws3 = 2;
int pointsPerDraw3[2] = {4, 2};
int typeOfDraw3[2] = {3,1};
float colorR3[3] = {1.0,0.0,0.0};
float colorg3[3] = {0.0,1.0,0.0}; //go down dont overthink
float colorb3[3] = {0.0,0.0,0.0};
float transx3 = 0.0;
float transy3 = 0.0;
float scalex3 = 1.0;
float scaley3 = 1.0;
//trapezoid under square
float rotate4 = 00.0;
float xCoords4 [6] = {1.5, 2.0, -2.0, -1.5, 0.0, 0.0};
float yCoords4 [6] = {0.0,-1.0,-1.0,0.0,3.0,-3.0};
int numberofDraws4 = 2;
int pointsPerDraw4[2] = {4, 2};
int typeOfDraw4[2] = {3,1};
float colorR4[3] = {0.0,0.0,0.0};
float colorg4[3] = {1.0,1.0,0.0};
float colorb4[3] = {0.0,0.0,1.0};
float transx4 = 0.0;
float transy4 = 0.0;
float scalex4 = 1.0;
float scaley4 = 1.0;
void settrans2(float rotateDD, float scalexDD, float scaleyDD, float transxDD, float transyDD)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(transxDD,transyDD,0.0);
glRotatef(rotateDD, 0.0, 0.0, 1.0); // where to put this in the program?
glScalef(scalexDD, scaleyDD, 1.0);
return;
}
void DrawsAllIcons( float x[], float y[], int ndraws, int pointsperdraw [], int drawtype[], float colorr[], float colorg[], float colorb[], float rotateD, float transxD, float transyD, float scalexD, float scaleyD)
{
settrans2(rotateD,scalexD,scaleyD,transxD, transyD);
int k=0; //index for arrays
int drawTooIndex = 0;
ndraws=ndraws-1;
for (int j=0; j<=ndraws; j++) //runs through
{
int whatCase = drawtype[j]; //sees what type of draw
drawTooIndex +=pointsperdraw[j];
switch (whatCase)
{
case 1:
{
glColor3f(colorr[j],colorg[j],colorb[j]);
glBegin(GL_LINES);
glVertex2f(x[k], y[k]); //sets vertex at the first point at k in the point arrays
int i = k+1;
k++;
for (i; i <drawTooIndex; i++)
{
glVertex2f(x[i], y[i]);
k++;
}
glEnd();
glFlush();
}
break;
case 2:
{
glColor3f(colorr[j], colorg[j], colorb[j]);
glBegin(GL_LINE_STRIP);
glVertex2f(x[k], y[k]);
int m = k+1;
k++;
for (m; m <drawTooIndex; m++)
{
glVertex2f(x[m], y[m]);
k++;
}
glEnd();
glFlush();
}
break;
case 3:
{
glColor3f(colorr[j], colorg[j], colorb[j]);
glShadeModel(GL_FLAT);
glBegin(GL_POLYGON);
glVertex2f(x[k], y[k]);
int n = k+1; //gets index of where to start drawing in the x and y arrays
k++;
for (n; n <drawTooIndex; n++)
{
glVertex2f(x[n], y[n]);
k++;
}
glEnd();
glFlush();
}
break;
}
}
}
void RenderScene(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set clear color to pale green
glClear(GL_COLOR_BUFFER_BIT);//note clear color was set in SetupRC
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,540,440); //set viewpoint to demensions
glOrtho(-20.0,20.0,-20.0,20.0,1.0,-1.0);
glClear(GL_COLOR_BUFFER_BIT);
DrawsAllIcons(xCoords, yCoords, numberofDraws, pointsPerDraw,typeOfDraw,colorR,colorg,colorb, rotate, transx,transy,scalex,scaley); //used GL_LINE_STRIP for the square to show that that case worked
DrawsAllIcons(xCoords2, yCoords2, numberofDraws2, pointsPerDraw2,typeOfDraw2,colorR2,colorg2,colorb2, rotate2, transx2,transy2,scalex2,scaley2); //used GL_POLYGON and GL_LINE for rest
DrawsAllIcons(xCoords3, yCoords3, numberofDraws3, pointsPerDraw3,typeOfDraw3,colorR3,colorg3,colorb3, rotate3, transx3,transy3,scalex3,scaley3);
DrawsAllIcons(xCoords4, yCoords4, numberofDraws4, pointsPerDraw4,typeOfDraw4,colorR4,colorg4,colorb4, rotate4, transx4,transy4,scalex4,scaley4);
glEnd();
glutSwapBuffers(); //
}
void TimerFunction(int value)
{
rotate+=5.0;
rotate2+=5.0;
rotate3+=5.0;
rotate4+=5.0;
glutPostRedisplay();
glutTimerFunc( 30, TimerFunction, 1 );
}
int main(int argc, char* *argv)
{
char header[]="This Bad Boy'll Draw any Icon you can think of"; //set up window title
glutInit(&argc, argv); // initialize glopen utility toolkit
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // Set up the display mode with a buffer and colors **
glutInitWindowSize(560,440); //window size and positoin
glutInitWindowPosition(140,20);
glutCreateWindow(header); // Open and label the window
glutDisplayFunc(RenderScene); //points to the fucntion that will be drawing the item // Set the state of the rendering machine
glutTimerFunc(30, TimerFunction, 1);
glutMainLoop(); // Call and activate the main
return 0;
}
相关文章:
- 与互斥锁相比,旋转锁可以保证上下文切换
- 绘制旋转的三角形
- 旋转模型矩阵时的形状失真
- 四边形的 2D 旋转
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- 发布旋转矩阵(openGL/glm)
- 顺时针迭代旋转 3 位数字
- 形状对象的旋转和缩放不正确C++
- 如何在 OpenSceneGraph 中缩放/旋转/移动资产
- 如何为 C++ Visual Studio 2019 指定应用程序图标?
- 如何有效地计算将单位立方体映射到自身的反射和旋转?
- 子轴围绕父轴而不是他自己的轴旋转
- 将使用太多的纹理插值器 - 带旋转的着色器
- 从 wxWidgets 中的.DLL加载图标
- 如何修复加载图标()函数不显示图标?
- SFML 向下移动时如何围绕屏幕中心旋转?
- QT Framewrok 和 Visual Studio - 图标冲突
- 如何在OpenGL中正确旋转和缩放对象?
- 如何在 c++ 中将图标放在按钮上?
- OpenGL:问题使图标旋转