OpenGL初学者,下面这段代码运行结果为什么不是一个系列点,而只有一个点?请高手指点。多谢。
#include<gl/glaux.h>#include<GL/glut.h>#include<gl/GL.h>voidmyDisplay(void){floatdx,d...
#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;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,1.0f);
glPointSize(5.0f);
for(x=x0;x<x1;x++)
{
glBegin(GL_POINTS);
glVertex2d(x,(y+0.5));
glEnd();
y=y+k;
}
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(400, 400);
glutCreateWindow("第一个OpenGL程序");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
} 展开
#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;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,1.0f);
glPointSize(5.0f);
for(x=x0;x<x1;x++)
{
glBegin(GL_POINTS);
glVertex2d(x,(y+0.5));
glEnd();
y=y+k;
}
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(400, 400);
glutCreateWindow("第一个OpenGL程序");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
} 展开
展开全部
窗口的默认大小范围是从-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=x0;x<x1;x++)
{
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询