#include<GL/glut.h>
#include<math.h>
#define PI 3.141592f
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0)//窗口背景为白色
glMatrixMode(GL_PROJECTION)
gluOrtho2D(0.0,200.0,0.0,150.0)//投影范围
}
void lineSegment(void)
{
GLfloat x,y,z,angle
z=0.0f
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1.0,0.0,0.0)
glBegin( GL_LINES)
for(angle=0.0fangle<=PIangle+=(PI/1000000.0f))
{
x=100+50.0f*sin(angle)
y=60+50.0f*cos(angle)
glVertex3f(x,y,z)
x=100+50.0f*sin(angle+PI)
y=60+50.0f*cos(angle+PI)
glVertex3f(x,y,z)
}
glEnd()
glFlush()
}
int main(int argc,char**argv)
{
glutInit(&argc,argv)
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE )
glutInitWindowPosition(0,0)//设置显示窗口左上的坐标值
glutInitWindowSize(1000,700)//设置显示窗口的宽和高
glutCreateWindow("An example OpenGL Program")
init()
glutDisplayFunc(lineSegment)
glutMainLoop()
}
glMultMatrix 假设当前矩阵谨稿烂式C那么用矩阵M 调用glMultMatrix 对顶点v的变换就从原来的C × v变成C × M × 祥漏v
你的代码调用glMultMatrix 后看不到那个画的方框是因为单位矩阵要是4x4的,
可以调用
glGetDoublev(GL_MODELVIEW_MATRIX, 敬咐modelMatrix)查看当前矩阵的值
窗口的默认大小范围是从-1到1,当然显示不了别的点了。1.需要添加一个控制窗口大小范围的函数,void ChangeSize()。2.在主函数main()中添加一句glutReshapeFunc(ChangeSize)
显示结果,点比较密集,显示如下
#include<gl/glaux.h>#include<GL/glut.h>
#include<gl/GL.h>
void myDisplay(void)
{
float dx,dy,y,k
int x,x0=0,y0=0
float x1=200,y1=300
dx=x1-x0,dy=y1-y0
k=dy/dx,y=y0
glClearColor(1,1,1,1)//设置背景颜色为白色
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0.0f,0.0f,1.0f)
glPointSize(2.0f)
for(x=x0x<x1x++)
{
glBegin(GL_POINTS)
glVertex2d(x,(y+0.5))
glEnd()
y=y+k
}
glFlush()
}
//添加的函数
void ChangeSize(GLsizei w,GLsizei h)
{
GLfloat aspectRatio
GLfloat nRange=400.0f
if(h==0)
h=1
glViewport(0,0,w,h)
aspectRatio=(GLfloat)w/(GLfloat)h
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange*5.0f, nRange*5.0f)
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange*5.0f, nRange*5.0f)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv)
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)
早高 glutInitWindowPosition(0, 0)
glutInitWindowSize(400, 400)
胡腔 glutCreateWindow("第一个OpenGL程序裤睁衫")
glutReshapeFunc(ChangeSize)//添加的语句
glutDisplayFunc(&myDisplay)
glutMainLoop()
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)