openGL實現二維圖形和三維圖形


  openGL是一個強大的底層圖形庫,其命令最初的時候使用C語言實現的。openGL定義了一個圖形程序接口,常用於制作處理三維圖像,功能強大,調用方便,在圖像處理十分受歡迎。

  實現圖形主要使用的是openGL的一個工具包:GLUT。

  GLUT (pronounced like the glut in gluttony) is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL programs. It implements a simple windowing application programming interface (API) for OpenGL.

  GLUT is designed for constructing small to medium sized OpenGL programs. While GLUT is well-suited to learning OpenGL and developing simple OpenGL applications, GLUT is not a full-featured toolkit so large applications requiring sophisticated user interfaces are better off using native window system toolkits like Motif. GLUT is simple, easy, and small. 

  GLUT對於大型的項目來說可能功能不全,它主要針對一些中小型的openGL項目而設計。

  1.實現二維圖像相對簡單些,直接下代碼:

#include <GL/glut.h>

void Rectangle(void) {
//gl開頭的函數為openGL的標准函數 //(使用當前緩沖區的值來)清除指定的緩沖區 glClear(GL_COLOR_BUFFER_BIT);
     //畫矩形 //glRectf(-0.5f, -0.5f, 0.5f, 0.5f); //畫直線 glBegin(GL_LINES); glVertex2f(0.5f, 0.5f); glVertex2f(-0.5f, -0.5f); glEnd(); //刷新緩沖,保證繪圖命令能被執行 glFlush(); } int main(int argc, char *argv[]) { //初始化GLUT library glutInit(&argc, argv); //對窗口的大小進行初始化 glutInitWindowSize(300, 300); // 設置窗口出現的位置 //glutInitWindowPosition(int x, int y); //初始化程序展示模式 glutInitDisplayMode(GLUT_RGBA); glutCreateWindow("project of openGL"); //win: 指定子窗口的父窗口 //glutCreateSubWindow(int win, int x, int y, int width, int height); //為當前窗口設置函數回調,用於畫圖 glutDisplayFunc(&Rectangle); //進行glut事件循環,否則看不到圖形效果(一閃而過) glutMainLoop(); return 0; }

  實現效果:

         

  也可以在窗口中畫一個圓:

#include <math.h>
GLfloat r = 0.5f; GLfloat PI = 3.141592653f; int pre = 30; glBegin(GL_POLYGON);//畫多邊形 for (int i = 0; i < pre; i++) { glVertex2f(r * cos(2 * PI*i / pre), r * sin(2 * PI*i / pre)); } glEnd();

  實際上這個圓並不是圓,而是多邊形的近似。GL_POLYGON用於繪制多邊形,邊數達到一定程度,顯示出來的形狀近似於圓。

  

  2.接下來來實現三維圖像Cube。

  三維變換:  

  模型視圖變換(GL_MODELVIEW):從“相對移動”的觀點來看,改變觀察點的位置與方向和改變物體本身的位置與方向具有等效性。

  透視投影變換(GL_PROJECTION):定義一個可視空間,可視空間以外的物體不會被繪制到屏幕上。

  視口變換(glViewPort):通常情況下,程序默認像素填充整個窗口,通過視口變換設置像素在窗口上的填充情況。

  每次對物體進行變換的時候,都需要先設置當前操作的矩陣為某種變換的矩陣,在進行變換之前還需要將矩陣轉換為單位矩陣才能進行操作。

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

  完整代碼:

#include <GL/glut.h>
#include <math.h>

void setCube(void) {
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0, 0, 0);//設置黑色
	glLoadIdentity();//加載單位矩陣
	gluLookAt(6, 0, 2.5, 0, 0, 0, 1, 1, 0);
	//前三個參數設置觀察者的觀察位置,中三個參數設置觀察點的位置,后三個參數設置觀察者的觀察方向
	glLineWidth(2.0f);//設置邊的像素寬,默認為1.0f
	glutWireCube(2.0);
	glFlush();
}

void drawCube(void) {
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	gluLookAt(4, 0, 1.5, 0, 0, 0, 1, 1, 0);

	//繪制正方體的面
	glColor3f(0, 1, 0);
	glBegin(GL_QUADS);
		//---1---
		glNormal3f(-1, 0, 0);//設置點的法向量
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, -0.5);
		glVertex3f(0.5, 0.5, -0.5);
		//---2---
		glNormal3f(-1, 0, 0);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		//---3---
		glNormal3f(0, 1, 0);
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		glVertex3f(0.5, 0.5, -0.5);
		//---4---
		glNormal3f(0, -1, 0);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(0.5, -0.5, -0.5);
		//---5---
		glNormal3f(0, 0, 1);
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		//---6---
		glNormal3f(0, 0, -1);
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
	glEnd();

	//draw
	glColor3f(0, 0, 0);
	glLineWidth(2.0f);
	//繪制正方體的邊
	glBegin(GL_LINES);
		//---1---
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(0.5, 0.5, 0.5);
		//---2---
		glVertex3f(0.5, 0.5, -0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(0.5, -0.5, -0.5);
		glVertex3f(0.5, -0.5, -0.5);
		glVertex3f(0.5, 0.5, -0.5);
		//---3---
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, -0.5);
		glVertex3f(0.5, -0.5, -0.5);
		glVertex3f(0.5, 0.5, -0.5);
		glVertex3f(0.5, 0.5, -0.5);
		glVertex3f(0.5, 0.5, 0.5);
		//---4---
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		glVertex3f(-0.5, 0.5, 0.5);
	glEnd();
	glFlush();
}

void threeD(int w, int h) {
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);//調整視圖窗口大小
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 10.0);
	glMatrixMode(GL_MODELVIEW);

}

int main(int argc, char *argv[]) {

	//初始化GLUT library
	glutInit(&argc, argv);
	//對窗口的大小進行初始化
	glutInitWindowSize(500, 500); 
	// 設置窗口出現的位置
	//glutInitWindowPosition(int x, int y);
	//初始化程序展示模式
	glutInitDisplayMode(GLUT_RGBA);
	glutCreateWindow("project of openGL");
//為當前窗口設置函數回調,用於畫圖 glutDisplayFunc(drawCube); //窗口改變時的函數 glutReshapeFunc(threeD); glutMainLoop(); return 0; }

  可以調用openGL的glutWireCube(GLFloat size)來繪制立體正方形,如setCube函數。

  我想要實現帶有黑色邊緣的綠色正方體,必須對正方體進行繪制,如drawCube函數。

  實現效果:

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM