初學計算機圖形學,很多不懂。代碼是參考的,其中的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 */
}
運行結果如下:

請各位大佬指點!
uj5u.com熱心網友回復:
搜網路教程“學OpenGL編3D游戲”。uj5u.com熱心網友回復:
gluOrtho2D(0.0, 500.0, 0.0, 500.0); ->gluOrtho2D(-250.0, 250.0, -250.0, 250.0);
這樣居中
uj5u.com熱心網友回復:
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/98316.html
標籤:基礎類
