在VC中用openGL画直线很小

lttra 2017-05-26 07:31:50
初学计算机图形学,很多不懂。代码是参考的,其中的glViewport() gluOrtho2D(), glColor3f()等等函数搜了资料也不是很懂。
直线的起点是(0,5),终点是(9,0),画出来的直线蜷缩在左下角,这是为什么?

代码如下:
// DDA.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.h"
#include "GL/glut.h"

/* initialization: */
void myinit(void)
{

/* attributes */

glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */
glColor3f(1.0, 0.0, 0.0); /* draw in red */

/* set up viewing: */
/* 500 x 500 window with origin lower left */

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
}

void dda_line(int xa,int ya,int xb,int yb)
{
GLfloat delta_x,delta_y,x,y;
int dx,dy,steps;
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
delta_x=(GLfloat)dx/(GLfloat)steps;
delta_y=(GLfloat)dy/(GLfloat)steps;
x=xa;
y=ya;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glVertex3f(x,y,0);
for(int k=1;k<=steps;k++)
{
x+=delta_x;
y+=delta_y;
glBegin(GL_POINTS);
glVertex3f(x,y,0);
glEnd();
}

}

/* the display callback: */
void display( void )
{
glClear(GL_COLOR_BUFFER_BIT); /*clear the window */
// glViewport(0, 0, 500, 500);
dda_line(0,5,9,0);
glFlush();
}

int main(int argc, char** argv)
{

/* Standard GLUT initialization */

glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
glutInitWindowSize(500,500); /* 500 x 500 pixel window */
glutInitWindowPosition(0,0); /* place window top left on display */
glutCreateWindow("Digital Differential Analyser Line"); /* window title */
glutDisplayFunc(display); /* display callback invoked when window opened */
myinit(); /* set attributes */
glutMainLoop(); /* enter event loop */
}

运行结果如下:


请各位大佬指点!
...全文
303 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2017-06-10
  • 打赏
  • 举报
回复
gdi 的 void DDAline(int x1, int y1, int x2, int y2, CDC* pDC) { int wndWidth=480; int wndHeight=640; int steps; steps=abs(x2-x1) >= abs(y2-y1) ? abs(x2-x1):abs(y2-y1);//步数 afxDump << steps << "\n";// 250 float x,y,dx,dy; // start point at middle x=(float)(x1 + wndWidth/2+0.5); y=(float)(y1 + wndHeight/2+0.5);//起点 // dx=((float)x2-(float)x1)/steps; afxDump << dx << " =dx\n"; dy=((float)y2-(float)y1)/steps;//每步增长值 afxDump << dy << " =dy\n"; COLORREF color=RGB(255,0,0); for(int i=0;i<=steps;i++) { pDC->SetPixel((int)x,(int)y,color); x+=dx; y+=dy; }// end for }// end DDALine /////////////////////////////////////////////////////////////////////// CPaintDC dc(this); //这个是要绘制的直线 DDAline(0, 0, 250, 70, &dc); DDAline(0, 0, -250, 70, &dc); DDAline(0, 0, 70, 250, &dc); DDAline(0, 0, -70, 250, &dc); DDAline(0, 0, 250, -70, &dc); DDAline(0, 0, -250, -70, &dc); DDAline(0, 0, 70, -250,&dc); DDAline(0, 0, -70, -250,&dc);
Forgis 2017-06-09
  • 打赏
  • 举报
回复
gluOrtho2D(0.0, 500.0, 0.0, 500.0); -> gluOrtho2D(-250.0, 250.0, -250.0, 250.0); 这样居中
赵4老师 2017-05-27
  • 打赏
  • 举报
回复
搜网络教程“学OpenGL编3D游戏”。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧