from : http://blog.sina.com.cn/s/blog_5f0d72800100i0wb.html
原型为 glTexCoord2f(GLfloat s, GLfloat t)
s和t 代表坐标 s为 X轴坐标,t为Y轴坐标
用于绘制图形时指定纹理的坐标
第一个参数是X坐标,0.0是纹理的左侧,0.5是纹理的中点,1.0是纹理的右侧。第二个参数是Y坐标,0.0是纹理的底部,0.5是纹理的中点,1.0是纹理的顶部。
为了将纹理正确的映射到四边形上,您必须将纹理的右上角映射到四边形的右上角,纹理的左上角映射到四边形的左上角,纹理的右下角映射到四边形的右下角,纹理的左下角映射到四边形的左下角。如果映射错误的话,图像显示时可能上下颠倒,侧向一边或者什么都不是。
举例
纹理的左上坐标是X:0.0,Y:1.0f,四边形的左上顶点是X:-1.0,Y:1.0。其余三点依此类推。下面是一个具体的例子。
glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 );
glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 );
glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 );
OpenGL默认坐标系:
我们再看一下glVertex*指定顶点的代码:
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(0.5, -0.5, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(-0.5, 0.5, 0.0);
为什么上述就指定了一个矩形的四个顶点呢?需要说明的是,在OpenGL中默认坐标体系与Windows 中常用的不同,Windows中常用的坐标体系(仅2D)是用户区的左上角为坐标原点,即(0.0,0.0)点,右为坐标轴的X轴正方向,下为Y轴正方向,OpenGL中的坐标轴(3D)默认以客户区中心点为坐标原点(0.0,0.0,0.0),右为坐标轴的X轴正方向,上为Y轴正方向,垂直指出屏幕的方向为Z轴正方向。长度定义是将客户区范围为按单位长度定义,即整个客户区恰好是(-1,-1)(左下)到(1,1)(右上)。因此以上述四个点为顶点的矩形刚好在窗口的正中间,占据窗口的 1/4 。
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(0.5, -0.5, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(-0.5, 0.5, 0.0);
以上述四个点为顶点的矩形刚好充满了整个窗口。
一、画一个正方形
#include <GL/glut.h>
#include <gl/gl.h>
void display()
{
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1.0,0.0,0.0,1.0); // set the quad color
glBegin(GL_QUADS);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,-0.5,0.0);
glVertex3f(0.5,0.5,0.0);
glVertex3f(-0.5,0.5,0.0);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("第一个OpenGL程序");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
二、画四个顶点并且可以设定顶点的大小
#include <GL/glut.h>
#include <gl/gl.h>
void display()
{
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1.0,0.0,0.0,1.0); // set the point color
glPointSize(10);
glBegin(GL_POINTS);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,-0.5,0.0);
glVertex3f(0.5,0.5,0.0);
glVertex3f(-0.5,0.5,0.0);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("第一个OpenGL程序");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
三、画两条平行的直线,并用glLineWidth将直线宽度设置为5
#include <GL/glut.h>
#include <gl/gl.h>
void display()
{
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1.0,0.0,0.0,1.0); // set the line color
glLineWidth(5);
glBegin(GL_LINES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,-0.5,0.0);
glVertex3f(0.5,0.5,0.0);
glVertex3f(-0.5,0.5,0.0);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("第一个OpenGL程序");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}