我只想要这样的视频:https://youtu.be/dGWtdYlryQQ
它显示了如何在OpenGL中使用glTranslate
,glRotate
,gluOrtho2d
,但它并没有引导我任何事情
就我而言,我画的是钻石而不是三角形,这是我的条件
条件:
这是我的代码:
#include <stdlib.h>
#include <windows.h>
#include <GL/glut.h>
#include <iostream>
using namespace std;
float angle = 0;
float t,s=0.5,m=0;
void myinit(void){
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
}
void keyboard(unsigned char key, int x, int y){
if(key==27)
{
exit(0);
}else if(key == 82 || key == 114){
angle-=0.1;
glRotatef(angle,0,0,1);
glutPostRedisplay();
}else if(key == 84 || key == 116 )
{
t+=0.01;
glTranslatef(t,0,0);
glutPostRedisplay();
}else if(key == 43){
s+=0.01;
// m-=0.1;
// glTranslatef(m,m,0.0);
glScalef(s,s,0);
glutPostRedisplay();
}
(void)(x);
(void)(y);
}
void hut(void){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.5,0.4,0.0);
glVertex3f(0.42,0.5,0.0); // GREEN
glVertex3f(0.44,0.5,0.0);
glColor3f(1.5,1.0,0.0);
glVertex3f(0.46,0.5,0.0);
glColor3f(0.25,0.0,0.0);
glVertex3f(0.57,0.5,0.0);
glEnd();
glFlush();
glColor3f(1.5,1.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.44,0.55,0.0);
glVertex3f(0.42,0.5,0.0);
glVertex3f(0.46,0.5,0.0);
glColor3f(0.25,0.0,0.0);
glVertex3f(0.48,0.55,0.0);
glEnd();
glColor3f(1.5,1.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.48,0.55,0.0);
glVertex3f(0.46,0.5,0.0);
glVertex3f(0.50,0.5,0.0);
glColor3f(0.25,0.0,0.0);
glVertex3f(0.52,0.55,0.0);
glEnd();
glColor3f(1.5,1.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.52,0.55,0.0);
glVertex3f(0.50,0.5,0.0);
glVertex3f(0.54,0.5,0.0);
glColor3f(0.25,0.0,0.0);
glVertex3f(0.56,0.55,0.0);
glEnd();
glColor3f(1.5,1.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.56,0.55,0.0);
glVertex3f(0.54,0.5,0.0);
glVertex3f(0.57,0.5,0.0);
glEnd();
glFlush();
}
int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);
glutCreateWindow("Polygon with viewport");
myinit();
glutDisplayFunc(hut);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
这是我的输出:https://drive.google.com/file/d/14HHRiCbOHK9ZSZtDOqSl4GP4aSy7UQLh/view?usp=sharing 它与此https://youtu.be/dGWtdYlryQQ
不相似答案 0 :(得分:1)
glRotate围绕原点(0,0)旋转。给定您的投影矩阵(使用glOrtho设置),原点最初位于屏幕的左下角,除非您使用glTranslate。你的钻石不是以原点为中心,而是稍微远离它。您需要做的是更改 void hut(void)方法中的顶点值,使钻石以0,0为中心。然后使用glTranslate将渲染原点(以及钻石)移动到你想要的位置,然后使用glRotate。
答案 1 :(得分:1)
矩阵堆栈上的操作基于彼此。每个操作的参考系统是当前的转换。
请参阅glTranslate
的文档:
glTranslate
按x y z
生成翻译。当前矩阵(参见glMatrixMode
)乘以此平移矩阵,产品替换当前矩阵,
并查看glRotate
的文档:
glRotate
围绕向量x y z
产生角度旋转。当前矩阵(参见glMatrixMode
)乘以旋转矩阵,产品替换当前矩阵。
这意味着glRotate
围绕当前本地系统的原点进行旋转。
glRotatetf
后跟glTranslatef
会导致:
glTranslatef
后跟glRotatef
会导致:
由于您的对象被移位,您必须以这种方式进行翻译,将旋转点放置在原点:
glTranslatef(-0.5f, -0.5f, 0.0f);
然后你可以讨厌它:
glRotatef(angle, 0.0f, 0.0f, 1.0f);
然后将其移回:
glTranslatef(0.5f, 0.5f, 0.0f);
注意,在Fixed Function Pipeline堆栈上,您必须&#34;推送&#34;此操作的顺序相反。此外,您应该使用GL_MODELVIEW
矩阵堆栈。 (见glMatrixMode
)。
从函数keyboard
中删除所有矩阵运算,并将followig添加到函数hut
中:
void hut(void)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(t, 0.0f, 0.0f);
glTranslatef(0.5f, 0.5f, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glScalef(s, s, 0.0f);
glTranslatef(-0.5f, -0.5f, 0.0f);
.....
此外,您的对象会被视图的宽高比破坏。在设置投影矩阵时,可以通过注意纵横比来解决这个问题:
float w = 640.0f;
float h = 480.0f;
glOrtho(0.0,w/h,0.0,1.0,-1.0,1.0);