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,当然显示不了别的点了。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;
}
启帆信息
2024-11-22 广告
2024-11-22 广告
作为启帆信息科技股份有限公司的工作人员,对于Omniverse View有一定了解。Omniverse View是NVIDIA Omniverse平台中的一个强大工具,它易于导航且功能丰富,可以支持任何技能水平的专业人员展示和审查3D设计项...
点击进入详情页
本回答由启帆信息提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询