码迷,mamicode.com
首页 > 系统相关 > 详细

Ubuntu OpenGL 画心

时间:2015-11-14 13:43:48      阅读:812      评论:0      收藏:0      [点我收藏+]

标签:

参考文章。http://ptbsare.org/2014/05/17/ubuntu%E4%B8%8B%E4%BD%BF%E7%94%A8opengl%E5%9B%BE%E5%BD%A2%E5%BA%93/

安装各种需要的库。

1、基本环境

sudo apt-get install build-essential

2、安装OpenGL library

sudo apt-get install libgl1-mesa-dev

3、安装OpenGL Utilities

sudo apt-get install libglu1-mesa-dev

 4、安装OpenGL Utility Toolkit

sudo apt-get install freeglut3-dev

以上几个库安装完毕后即可编写OpenGL程序。

测试程序:随手画了个风骚的心型图。

    #include <GL/glut.h>

    void init() //初始化
    {
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glMatrixMode(GL_PROJECTION);
        gluOrtho2D(0.0, 400.0, 0.0, 300.0);
        return;
    }

    void setPixel(GLint xCoord, GLint yCoord)
    {
	glColor3f(1.0, 0.5, 1.5);
	glBegin (GL_POINTS);
	    glVertex2i (xCoord, yCoord);
	glEnd ();
    }

    void drawHeart()//画心
    {
        glClear(GL_COLOR_BUFFER_BIT);
        GLint x = 200, y = 100;
	for (x = 200; x <= 400; x++) {
	    for (y = 100; y<= 300; y++) {
		GLint xt = x - 200;
		GLint yt = y - 150;
		if (2*xt*xt - 2*xt*yt + 2*yt*yt <= 5000){
		    setPixel (x, y);
		    setPixel (400 - x, y);
		}
	    }
	}
	glFlush ();

        return;
    }

    int main(int argc, char *argv[])
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
        glutInitWindowPosition(400, 50);
        glutInitWindowSize(400, 300);
        glutCreateWindow("drawHeart");
        init();
        glutDisplayFunc(drawHeart);
        glutMainLoop();

        return 0;
    }

 编译:在控制台下编译

命令:

g++ -o [final_name] [coded_file] -lGL -lGLU -lglut 

 

 编译通过后执行

命令:

./[final_name]

结果

技术分享

end;;;;

 

Ubuntu OpenGL 画心

标签:

原文地址:http://www.cnblogs.com/yicoder/p/opengl_drawHeart.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!