- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
简而言之,我被要求在 OpenGL 中制作这个草图。我附上了到目前为止所做的截图。
我的问题是如何让这 4 个水平方向保持倾斜。我编写了使这 4 条水平线旋转 的代码(使用n
和m
键来旋转这 4 条水平线围绕中心)但我被要求同时倾斜和旋转 .
我想我需要使用模数运算符,但当角度超过某个点,如 10(例如角度 % 10)时,它会跳回 0,导致棍子突然移动。另外,如何让棍子的底部保持在一个位置,让棍子的末端倾斜(我的意思是上下移动),这样倾斜就很自然了,不像旋转。
有什么提示可以使倾斜变得平滑吗?我走在正确的轨道上吗?
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159265
static GLfloat lpos[] = { 0.0, 5.0, 4.0, 1.0 };
static GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
static GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
static GLfloat gray[] = { 0.5, 0.5, 0.5, 1.0 };
static GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
static GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
static GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
static GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
static GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
static GLfloat darkcyan[] = { 0.0, 0.4, 0.4, 1.0 };
static GLfloat lightgreen[] = { 0.5,1.0,0.5,1.0 };
static float alpha = 0.0;
static float beta = PI / 6.0;
static float zoom = 15.0;
static bool lightSource = true;
float numberOfTriangles = 1;
static GLdouble cpos[3];
bool showNormalVectors = false;
static double xPointer, yPointer, zPointer;
static double fenceHeight = -0.5;
static int angle = 0;
void writemessage()
{
printf("HW-3 - Computer Graphics - Professor Suzuki\n");
printf("Seyedamirhossein Hesamian\n\n");
printf(" X => x++ <= Move light source in direction of +X\n");
printf(" Y => y++ <= Move light source in direction of +Y\n");
printf(" Z => z++ <= Move light source in direction of +Z\n");
printf("\n");
printf("^X => x-- <= Move light source in direction of -X\n");
printf("^Y => y-- <= Move light source in direction of -Y\n");
printf("^Z => z-- <= Move light source in direction of -Z\n");
printf("\n");
printf(" ^ => Move camera up\n");
printf(" > => Move camera right\n");
printf(" < => Move camera left\n");
printf(" down arrow => Move camera down\n");
printf("\n");
printf(" t => More Twist\n");
printf(" f => Less Twist\n");
printf("\n");
printf(" q => More Propeller\n");
printf(" f => Less Propeller\n");
printf("\n");
printf(" e => More Triangles\n");
printf(" d => Less Triangles\n");
printf("\n");
printf(" 0 => Toggling light source\n");
printf("\n");
printf(" r => Rotates Propeller\n");
printf("\n");
printf(" n => Toggle to show Normal Vectors\n");
printf("\n");
printf(" s => Toggle to show Inside Triangles\n");
printf("\n");
printf(" w => Toggle to show wire frame mode\n");
printf("\n");
printf(" You can not move the light source when the light source is off !!!");
}
void doGlTranslatef(){
glTranslatef(xPointer, yPointer, zPointer);
}
void releaseGlTranslatef(){
glTranslatef(-xPointer, -yPointer, -zPointer);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.01, 20.0);
glMatrixMode(GL_MODELVIEW);
}
void DrawSticksArroundYard(){
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, black);
GLUquadricObj *quadObj;
// Right-Line
xPointer = 4.8;
yPointer = 1.0 + fenceHeight;
zPointer = -5.0;
doGlTranslatef();
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 10, 10, 10);
releaseGlTranslatef();
// Left-Line
xPointer = -4.8;
yPointer = 1.0 + fenceHeight;
zPointer = -5.0;
doGlTranslatef();
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 10, 10, 10);
releaseGlTranslatef();
// Back-Line
xPointer = -4.8;
yPointer = 1.0 + fenceHeight;
zPointer = -5.0;
doGlTranslatef();
glRotatef(90, 0, 1, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 9.6, 10, 10);
glRotatef(-90, 0, 1, 0);
releaseGlTranslatef();
// Front-Line
xPointer = 4.8;
yPointer = 1.0 + fenceHeight;
zPointer = 5.0;
doGlTranslatef();
glRotatef(-90, 0, 1, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 9.6, 10, 10);
glRotatef(90, 0, 1, 0);
releaseGlTranslatef();
// Pin-Front-Right
xPointer = 4.8;
yPointer = 0.0;
zPointer = 5.0;
doGlTranslatef();
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
releaseGlTranslatef();
// Pin-Front-Left
xPointer = -4.8;
yPointer = 0.0;
zPointer = 5.0;
doGlTranslatef();
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
releaseGlTranslatef();
// Pin-Back-Left
xPointer = -4.8;
yPointer = 0.0;
zPointer = -5.0;
doGlTranslatef();
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
releaseGlTranslatef();
// Pin-Back-Right
xPointer = 4.8;
yPointer = 0.0;
zPointer = -5.0;
doGlTranslatef();
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
releaseGlTranslatef();
// Pin-Back-Center
xPointer = 0.0;
yPointer = 0.0;
zPointer = -5.0;
doGlTranslatef();
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
releaseGlTranslatef();
// Pin-Front-Center
xPointer = 0.0;
yPointer = 0.0;
zPointer = 5.0;
doGlTranslatef();
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
releaseGlTranslatef();
// Pin-Right-Center
xPointer = 4.8;
yPointer = 0.0;
zPointer = 0.0;
doGlTranslatef();
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
releaseGlTranslatef();
// Pin-Left-Center
xPointer = -4.8;
yPointer = 0.0;
zPointer = 0.0;
doGlTranslatef();
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
releaseGlTranslatef();
}
void DrawYardFloor(){
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, lightgreen);
glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, lightgreen);
glBegin(GL_POLYGON);
glNormal3f(0, 1, 0);
glVertex3f(-5.3, -0.005, -5.3);
glVertex3f(-5.3, -0.005, 5.3);
glVertex3f(5.3, -0.005, 5.3);
glVertex3f(5.3, -0.005, -5.3);
glEnd();
}
void DrawCenterPin(){
xPointer = 0.0;
yPointer = 0.0;
zPointer = 0.0;
doGlTranslatef();
glRotatef(-90, 1, 0, 0);
GLUquadricObj *quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.2, 3, 10, 10);
glRotatef(90, 1, 0, 0);
releaseGlTranslatef();
}
void DrawHorizontalStickNum(){
xPointer = 0.0;
yPointer = 2.9;
zPointer = -3.0;
doGlTranslatef();
GLUquadricObj *quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 3, 10, 10);
releaseGlTranslatef();
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64);
cpos[0] = zoom * cos(beta) * sin(alpha);
cpos[1] = zoom * sin(beta);
cpos[2] = zoom * cos(beta) * cos(alpha);
gluLookAt(cpos[0], cpos[1], cpos[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
if (lightSource == true){
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glMaterialfv(GL_FRONT, GL_EMISSION, white);
glPushMatrix();
glTranslatef(lpos[0], lpos[1], lpos[2]);
glutSolidSphere(0.1, 10, 8);
glPopMatrix();
glMaterialfv(GL_FRONT, GL_EMISSION, black);
}
DrawYardFloor();
DrawSticksArroundYard();
DrawCenterPin();
// We can use N and M to rotate the sticks arround the center pin
glRotatef(angle, 0, 1, 0);
for (int i = 0; i < 4; i++){
glPushMatrix();
glRotatef(i * 360 / 4, 0, 1, 0);
// For tilting the sticks
//glRotatef(angle % 10, 1, 1, 0);
DrawHorizontalStickNum();
//glRotatef(angle % 10, 1, 1, 0);
glPopMatrix();
}
glRotatef(angle, 0, 1, 0);
glutSwapBuffers();
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
static int polygonmode[2];
switch (key) {
case 27:
exit(0);
break;
case 'x':
if (lightSource == true)
lpos[0] = lpos[0] + 0.2;
glutPostRedisplay();
break;
case 'X':
if (lightSource == true)
lpos[0] = lpos[0] - 0.2;
glutPostRedisplay();
break;
case 'y':
if (lightSource == true)
lpos[1] = lpos[1] + 0.2;
glutPostRedisplay();
break;
case 'Y':
if (lightSource == true)
lpos[1] = lpos[1] - 0.2;
glutPostRedisplay();
break;
case 'z':
if (lightSource == true)
lpos[2] = lpos[2] + 0.2;
glutPostRedisplay();
break;
case 'Z':
if (lightSource == true)
lpos[2] = lpos[2] - 0.2;
glutPostRedisplay();
break;
case '+':
if (zoom != 1.5)zoom = zoom - 0.5;
glutPostRedisplay();
break;
case '-':
if (zoom != 17)zoom = zoom + 0.5;
glutPostRedisplay();
break;
case '0':
if (lightSource == true){
glDisable(GL_LIGHT0);
lightSource = false;
}
else{
glEnable(GL_LIGHT0);
lightSource = true;
}
glutPostRedisplay();
break;
case 'e':
if(fenceHeight<2)
fenceHeight += 0.5;
glutPostRedisplay();
break;
case 'd':
if (fenceHeight > -0.5)
fenceHeight -= 0.5;
glutPostRedisplay();
break;
case 'w':
glGetIntegerv(GL_POLYGON_MODE, polygonmode);
if (polygonmode[0] == GL_FILL)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glutPostRedisplay();
break;
case 'n':
angle++;
glutPostRedisplay();
break;
case 'm':
angle--;
glutPostRedisplay();
break;
default:
break;
}
}
void specialkey(GLint key, int x, int y)
{
switch (key) {
case GLUT_KEY_RIGHT:
alpha = alpha + PI / 180;
if (alpha > 2 * PI) alpha = alpha - 2 * PI;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
alpha = alpha - PI / 180;
if (alpha < 0) alpha = alpha + 2 * PI;
glutPostRedisplay();
break;
case GLUT_KEY_UP:
if (beta < 0.45*PI) beta = beta + PI / 180;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
if (beta > -0.05*PI) beta = beta - PI / 180;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char** argv)
{
writemessage();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1200, 800);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
/* initially GL_FILL mode (default), later GL_LINE to show wireframe */
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_LIGHTING);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_LIGHT0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 5.0, 10.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialkey);
glutMainLoop();
return 0;
}
最佳答案
归根结底,这是将很多矩阵拼接在一起,实现不同的效果。使用模运算符是完全没有必要的,它是不断跳回起点的来源。当您想要生成重复模式时,可以使用模数运算符...例如旋转 10 度并在 0 度重新开始,这就是它在那里所做的。
key 正在使用右 vector 旋转。围绕 vector <0, 1, 0> 旋转就是围绕垂直向上和向下的 Y 轴旋转,这将导致圆柱体看起来像是在原地旋转。围绕 <1, 0, 0> 旋转将围绕 X 轴旋转,这是平坦的。这样做会导致气缸向前或向后旋转,或倾斜。根据您应用矩阵变换的顺序,您可以使用绕 X 轴的旋转来向前或向后倾斜圆柱体,然后再绕 Y 轴旋转以将圆柱体旋转到位。
关于c++ - Opengl/Glut - 使倾斜变得平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26624214/
我用 geom_line 和 geom_ribbon 创建了一个图(图 1),结果还不错,但为了美观,我希望线条和丝带更平滑。我知道我可以使用 geom_smooth 作为线条(图 2),但我不确定是
我正在使用 Javascript 制作 HTML5 Canvas 游戏。 我想让一个物体平滑地转向某个方向。 我将方向存储为变量,并使用弧度。代码的工作原理如下: window.setInterval
我正在尝试平滑数字列表,出于绘图目的,我不想绘制超过 5000 个项目的图表,因为渲染时间太长,而且我们不需要额外的数据。 假设我们有一个简单的列表: let v = [1,2,3]; // max
我正在尝试制作一款倒置飞扬的小鸟游戏,但我面临一个大问题。 我尝试从上到下生成管道障碍物,但动画看起来很像文本编辑器,而且不流畅,所以我该如何平滑它?我尝试使用 requestanimationfra
我有一些数据由不均匀采样的 2D 空间位置组成,其中每个 x, y 坐标都有一个介于 0 和 2pi 之间的相关相位值 theta。我希望能够将 theta 值插入到常规 x, y 网格中。在相同(或
我有一个(3D)直方图,我喜欢对其应用高斯平滑: cv::MatND Hist; 在 1D 和 2D 情况下,我通过以下方式对其进行模糊处理: cv::GaussianBlur(Hist, Hist,
我的应用程序通过调整其 alpha 值在各种媒体和文本层之间淡入淡出。然而,当使用线性交叉淡入淡出时,亮度似乎在中途“下降”,然后又淡出。经过一番搜索,我找到了 this answer这解释了这个问题
我必须在屏幕上拖动一些 View 。我正在通过触摸监听器的 ACTION_MOVE 上的运动事件更改其布局参数的左侧和顶部来修改它们的位置。有没有办法让“拖动”元素更顺畅?因为这种“拖拽”一点也不顺畅
我只想问有没有你推荐的资源描述Image smoothing简单地。 谢谢。 最佳答案 就像一些评论者提到的那样,图像平滑可能意味着很多事情。但主要是,当有人使用这个术语时,他们的意思是模糊或低通滤波
我有一个带有简单视差的 React 组件,可以更改顶部和不透明度值。问题是滚动动画有点不稳定。有什么办法可以平滑过渡吗?我在 vanilla JS 中使用 requestAnimationFrame(
我在使用 libGDX 获得平滑字体时遇到问题。我已经在这个网站上搜索,并在谷歌上,我尝试了这些问题的解决方案here和 here ,但我的字体渲染效果总是很差。 示例: 我尝试了多种方法,总是得到与
我正在尝试为 Himmelblau's function 绘制一个简单的等高线图(在 gnuplot 中)使用以下代码: f(x,y)=(((x**2)+(y)-11)**2)+(((x)+(y**2
我想知道如何平滑 JScrollPane 的自动过渡。我正在使用 JScrollBar scroll = scollpane.getVerticalScrollBar(); scroll.setVal
我有一个高分辨率的healpix贴图(nside = 4096),我想在给定半径(假设为10 arcmin)的圆盘中对其进行平滑处理。 对healpy非常陌生,在阅读了文档后,我发现一种不太好的方法是
我使用下面的代码在 anchor 链接上平滑滚动 jQuery(function() { jQuery('a[href*=#]:not([href=#])').click(function() {
与这个问题非常相似:Rx IObservable buffering to smooth out bursts of events ,我有兴趣消除可能突然发生的可观察量。 希望下图说明我的目标: Ra
我正在开发代码战链接(一个 div)。我希望 div 内的 svg 在悬停时连续旋转。产生的悬停效果远非平滑,当鼠标在其上滚动时 svg 会跳跃。 mouseout 事件被注释掉了,因为它值得。优化这
有没有办法平滑转换(平移和旋转)的 BufferedImage 的锯齿状边缘? 测试图像的放大 View : (请注意,这不是将要使用的实际 BufferedImage,仅用于此处演示)。 已使用双线
为什么这没有产生平滑的圆?有什么想法吗? public void draw(ShapeRenderer sRenderer) { sRenderer.begin(ShapeType.Fille
我正在做一个项目,我的 ImageView 包含文本和曲线。当在计算机上查看图形时,它们看起来很好。如果带有 ImageView 的应用程序加载到 iPad 2 上,文本(字体:helvetica n
我是一名优秀的程序员,十分优秀!