0%

Linux OpenGL

OpenGL

OpenGL(全写Open Graphics Library)是个定义了一个跨编程语言、跨平台的应用程序接口(API)的规格,它用于生成二维、三维图像。这个接口由近三百五十个不同的函数调用组成,用来从简单的图形比特绘制复杂的三维景象。而另一种程序接口系统是仅用于Microsoft Windows上的[Direct3D。OpenGL常用于CAD、虚拟实境)、科学可视化程序和[电子游戏开发

用OpenGL绘制图形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* File: main.c
* Author: leo
*
* Drawing a simple 3D rectangle program with GLUT
* OpenGL ~~
*/


#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

//called to draw scene
void RenderScene(void)
{
//clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
//set current drawing color to read
glColor3f(1.0f,0.0f,0.0f); //设置以后绘制操作所用的颜色
//draw a filled rectangle with current color
glRectf(100.f,150.0f,150.0f,100.0f); //绘制一个填充的矩形
//flush drawing commands
glFlush();
}

//set up the rendering state
void SetupRC(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f);
}

//called by GLUT library when the window has changed size
void ChangeSize(GLsizei w, GLsizei h)
{
//prevent a divide by zero
if(h == 0)
h = 1;
//set viewport to window dimensions
glViewport(0,0,w,h);
//reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();//在每次进行任何矩阵处理之前都“复位”坐标系
//establish clipping volumn(left,right,bottom,top,near,far)
if(w <= h)
glOrtho(0.0f,250.0f,0.0f,250.0f*h/w,1.0,-1.0);
else
glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0,-1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int main(int argc, char** argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Plot Rect. using GLUT");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);//窗口大小发生变化,就重新设置坐标系
SetupRC();
glutMainLoop();
return (EXIT_SUCCESS);
}

处无为之事,行不言之教;作而弗始,生而弗有,为而弗恃,功成不居!

欢迎关注我的其它发布渠道