点就是用GL_POINTS,折线就用GL_LINE_STRIP的模式
其次就是正弦和余弦曲线用程序表示就是:重点在于for循环
先看看用点画的-π到π区间的正弦曲线
//用点画正弦曲线
glColor3f(1.0,0.0,0.0);
glTranslatef(250,250,0);
glBegin(GL_POINTS);
for(float x=-PI ; x<PI ; x+=2*PI/180){
float y=sin(x);
glVertex2f(50*x,50*y);//为了放大xy比例所以*50,要不然太小了
}
glEnd();
再看用折线画的正弦曲线:
//用折线画正弦曲线
glColor3f(1.0,0.0,0.0);
glTranslatef(250,250,0);
glBegin(GL_LINE_STRIP);
for(float x=-PI ; x<PI ; x+=2*PI/180){
float y=sin(x);
glVertex2f(50*x,50*y);
}
glEnd();
显然用点画的放大了之后可以看到中间的间隙,折线可以看到锯齿的感觉。
余弦的效果就不写了,看代码吧:
#include<GL\freeglut.h>
#include <math.h>
#define PI 3.1416
int winWidth=500,winHeight=500;
void myinit(void)
{
glClearColor(1.0f,1.0f,1.0f,1.0f);
}
void myReshape(int w,int h)
{
winWidth=w;
winHeight=h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,w,0.0,h);
}
//每桢OpenGL都会调用这个函数,用户应该把显示代码放在这个函数中
void display(void)
{
//设置清除屏幕的颜色,并清除屏幕和深度缓冲
glClear(GL_COLOR_BUFFER_BIT);
//画坐标系
glColor3f(0.0,1.0,0.0);
glBegin(GL_LINES);
//x轴
glVertex2f(0,250.0);
glVertex2f(500.0,250.0);
//y轴
glVertex2f(250.0,0);
glVertex2f(250.0,500.0);
glEnd();
/*//用点画正弦曲线
glColor3f(1.0,0.0,0.0);
glTranslatef(250,250,0);
glBegin(GL_POINTS);
for(float x=-PI ; x<PI ; x+=2*PI/180){
float y=sin(x);
glVertex2f(50*x,50*y);
}
glEnd();*/
/*//用折线画正弦曲线
glColor3f(1.0,0.0,0.0);
glTranslatef(250,250,0);
glBegin(GL_LINE_STRIP);
for(float x=-PI ; x<PI ; x+=2*PI/180){
float y=sin(x);
glVertex2f(50*x,50*y);
}
glEnd();*/
/*//用点画余弦曲线
glColor3f(1.0,0.0,0.0);
glTranslatef(250,250,0);
glBegin(GL_POINTS);
for(float x=-PI ; x<PI ; x+=2*PI/180){
float y=cos(x);
glVertex2f(50*x,50*y);
}
glEnd();*/
/*//用折线画余弦曲线
glColor3f(1.0,0.0,0.0);
glTranslatef(250,250,0);
glBegin(GL_LINE_STRIP);
for(float x=-PI ; x<PI ; x+=2*PI/180){
float y=cos(x);
glVertex2f(50*x,50*y);
}
glEnd();*/
//交换前后缓冲区
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
//初始化OPENGL显示方式
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
//设定OPENGL窗口位置和大小
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
//打开窗口
glutCreateWindow ("");
//调用初始化函数
myinit();
//设定窗口大小变化的回调函数
glutReshapeFunc(myReshape);
//开始OPENGL的循环
glutDisplayFunc(display);
glutMainLoop();
return 0;
}