|
发表于 16-10-2008 11:00 PM
|
显示全部楼层
- #include <Gl/glut.h>
- #include <stdlib.h>
- #include <math.h>
- class screenPt
- {
- private:
- int x, y;
- public:
- screenPt() {
- x = y = 0; //default constructor
- }
- void setCoords(int xCoordValue, int yCoordValue){
- x = xCoordValue;
- y = yCoordValue;
- }
- int getx() const {
- return x;
- }
- int gety() const {
- return y;
- }
- void incrementx() {
- x++;
- }
- void decrementy() {
- y--;
- }
- };
- //=================================================================================================================
- void Init() //initialise the function to display
- {
- glClearColor(0.0,0.0,0.0,0.0); //set the background color to black
- glMatrixMode(GL_PROJECTION); //set projection parameter
- gluOrtho2D(0.0, 300.0, 0.0, 300.0);
- }
- //=================================================================================================================
- //=================================================================================================================
- void setPixel(GLint xCoord,GLint yCoord)
- {
- glBegin(GL_POINTS);
- glVertex2i(xCoord, yCoord);
- glEnd();
- }
- //=================================================================================================================
- //=================================================================================================================
- //Circle Midpoint function
- void circleMidpoint(int xc,int yc, int radius)
- {
- screenPt circPt;
- GLint p = 1 - radius;
- circPt.setCoords(0, radius);
-
- void circlePlotPoints(int,int, screenPt);
- circlePlotPoints(xc,yc, circPt);
- while (circPt.getx()<circPt.gety())
- {
- circPt.incrementx();
- if(p < 0)
- p += 2 * circPt.getx() + 1;
- else
- {
- circPt.decrementy();
- p += 2 * (circPt.getx() - circPt.gety()) + 1;
- }
- circlePlotPoints(xc,yc, circPt);
- }
- }
- //=================================================================================================================
- //=================================================================================================================
- //Circle Plotpoints
- void circlePlotPoints(int xc, int yc, screenPt circPt)
- {
- setPixel(xc + circPt.getx(),yc + circPt.gety());
- setPixel(xc - circPt.getx(),yc + circPt.gety());
- setPixel(xc + circPt.getx(),yc - circPt.gety());
- setPixel(xc - circPt.getx(),yc - circPt.gety());
- setPixel(xc + circPt.gety(),yc + circPt.getx());
- setPixel(xc - circPt.gety(),yc + circPt.getx());
- setPixel(xc + circPt.gety(),yc - circPt.getx());
- setPixel(xc - circPt.gety(),yc - circPt.getx());
- }
- //=================================================================================================================
- //=================================================================================================================
- //The function for clearing the screen and loading the data into the frame buffer
- void circleDraw(void)
- {
- glClear(GL_COLOR_BUFFER_BIT); //clears display window
- glPointSize(10); //sets point size for use by GL_POINTS
- glColor3f(1.0, 0.0, 0.0); //sets colour to red
- circleMidpoint(150,150,100); //call to the function 'lineBres' which performs calculation for the Bressenham algorithm
- glFlush(); //process all OpenGL routines as quickly as possible
- }
- //=================================================================================================================
- //=================================================================================================================
- //The main function
- int main(int argc, char **argv)
- {
-
- glutInit(&argc, argv); //initailize GLUT
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set display mode
- glutInitWindowPosition(440,200); // sets the display window position
- glutInitWindowSize(400,400); //set display window width and height
- glutCreateWindow("Circle Drawing Program in OpenGL"); //create display window with the name in the argument
- Init(); //execute initialization procedure - preparing the screen
- glutDisplayFunc(circleDraw); //set graphic to display window
- glutMainLoop(); //display everything and wait - runs a loop so that the output can be seen by the user
- return 0;
- }
复制代码 这是用open gl 画的,建议用dev c++来compile , Vc++要加入opengl32.lib , glut.lib , glut32.lib这三个library.
另外也有dos 版的,要TC3 才可以执行,比较麻烦。
所以建议用以上这个。 |
|