一直对opengl有兴趣,可是到现在还是略懂而已。
现在才算是明白如何把opengl那套原始的屏幕坐标转换为我所熟悉的坐标系统
先弄个小例子
- #include <stdlib.h>
- #include <GL/glut.h>
-
- void init(void)
- {
- glClearColor (0.5, 0.5, 0.0, 0.0);
- glShadeModel (GL_SMOOTH);
- glLoadIdentity();
- gluOrtho2D(0,100,0,100);
- }
-
- void display(void)
- {
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- gluOrtho2D(0,100,0,100);
-
- glBegin(GL_TRIANGLES);
- glScalef(5.0,5.0,5.0);
- glColor3f(1.0,0.0,0.0);
- glVertex2f(50.0,75.0);
- glColor3f(0.0,1.0,0.0);
- glVertex2f(50.0,25.0);
- glColor3f(0.0,0.0,1.0);
- glVertex2f(25.0,0.0);
- glEnd();
-
- glFlush ();
- }
-
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27:
- exit(0);
- break;
- }
- }
-
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize (400, 400);
- glutInitWindowPosition (0, 0);
- glutCreateWindow (argv[0]);
- init ();
- glutDisplayFunc(display);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- return 0;
- }
呵呵 这都是gluOrtho2D的功劳啊
这里给出我所熟悉的一组函数
1.gluOrtho2D — define a 2D orthographic projection matrix
void gluOrtho2D(GLdouble left,GLdouble right,GLdouble bottom,GLdouble top);
该函数就是把屏幕左下角坐标设计为left, bottom,右上角设置为 top,right
2. void glTranslatef(GLfloat x,GLfloat y,GLfloat z)
该函数用来平移图像
当然它还有向量版本
3. void glRotatef(GLfloat angle,GLfloat x,GLfloat y,GLfloat z)
该函数用来翻转图像,第一个参数是角度
例如glRotatef(45,0,0,1.0);
把图像研z轴翻转45角度
4. void glScalef(GLfloat x, GLfloat y, GLfloat z);
是采用来缩放图像的函数
附注:
去年开始接触opengl现在还是这个水平 -_-!
(ccsdu2004) |