Home > Software engineering >  In the VC using openGL draw a straight line is very small
In the VC using openGL draw a straight line is very small

Time:09-30

Beginners computer graphics, a lot of don't understand, code is a reference to the glViewport gluOrtho2D () (), glColor3f () and so on function searched information also is not very good,
Straight line is the starting point of (0, 5), the finish is (9, 0), draw straight lines curled up in the lower left corner, is this why?

The code is as follows:
//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);/* the draw in red */

/* set up viewing: */
/* 500 x 500 Windows with origin the 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 and delta_y, x, y;
Int dx, dy, steps;
Dx=xb - xa;
Dy=yb - ya;
If (abs (dx) & gt; Abs (dy))
Steps=abs (dx);
The 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 the display (void)
{
GlClear (GL_COLOR_BUFFER_BIT);/* clear the Windows */
//glViewport (0, 0, 500, 500);
,5,9,0 dda_line (0);
GlFlush ();
}

Int main (int arg c, char * * argv)
{

/* Standard GLUT initialization */

GlutInit (& amp; Arg c, argv);
GlutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);/* default, not men */
GlutInitWindowSize (500500);/* 500 x 500 pixel window */
GlutInitWindowPosition (0, 0);/* place the window top left on the display */
GlutCreateWindow (" Digital Differential Analyser Line "); Window title/* */
GlutDisplayFunc (display);/* display callback invoked when the window the opened */
Myinit ();/* set attributes */
GlutMainLoop ();/* enter the event loop */
}

The results are as follows:


, every brother please give directions!

CodePudding user response:

Search online tutorial "learning OpenGL 3 d games",

CodePudding user response:

GluOrtho2D (0.0, 500.0, 0.0, 500.0); ->
GluOrtho2D (250.0, 250.0, 250.0, 250.0);
This center

CodePudding user response:

Gdi's
Void DDAline (int x1, int y1, int x2, int y2, CDC * pDC)
{
Int wndWidth=480;
Int wndHeight=640;
Int steps;
Steps=abs (x2 - x1) & gt;=abs (y2 - y1)? Abs (x2 - x1) : abs (y2 - y1); Steps//
AfxDump & lt; Float x, y, dx, dy;
//start point at middle
X=(float) (x1 + wndWidth/2 + 0.5);
Y=(float) (y1 + wndHeight/2 + 0.5);//start
//
Dx=((float) x2 - (float) x1)/steps;
AfxDump & lt; Dy=((float) y2 - (float) y1)/steps;//each step growth
AfxDump & lt; COLORREF color=RGB (0, 255);
for(int i=0; i<=steps; I++)
{
PDC - & gt; SetPixel ((int), x, y (int), color);
X +=dx;
Y +=dy;
}//end for
}//end DDALine

///////////////////////////////////////////////////////////////////////
CPaintDC dc (this);
//this is to draw straight lines
DDAline (0, 0, 250, 70, & amp; Dc);
DDAline (0, 0, 250, 70, & amp; Dc);
DDAline (0, 0, 70, 250, & amp; Dc);
DDAline (0, 0, 70, 250, & amp; Dc);
DDAline (0, 0, 250, 70, & amp; Dc);
DDAline (0, 0, - 250-70, & amp; Dc);
DDAline (0, 0, 70, 250, & amp; Dc);
DDAline (0, 0, - 70-250, & amp; Dc);
  • Related