生活随笔
收集整理的這篇文章主要介紹了
OpenGL 基础图形绘制与投影变换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文參考《Computer Graphics Using OpenGL》,第一個例子繪制了
1. 參數定義的House
2.?a flurry of filled rectangles
3.?Sierpinski曲線
含有鼠標和鍵盤響應函數onmouse和onkeyboard。
[cpp]?view plain
?copy ?? ? ? ? ?? ?? ?? #include?"GL/glut.h"?? #include?"stdlib.h"?? #include?<iostream>?? using?namespace?std;?? #define?screenHeight?480?? ?? class?GLintPoint{?? public:??? ????GLint?x,?y;?? };?? ?? ?? ?? int?random(int?m)?? {?? ????return?rand()%m;?? }?? ?? void?drawDot?(GLint?x,?GLint?y)?? {?? ????glPointSize(3);?? ????glBegin(GL_POINTS);?? ????glVertex2i(x,y);?? ????glEnd();?? }?? ?? typedef?struct?? {?? ????GLfloat?r,?g,?b;?? }?GLfloatRGBColour;?? ?? GLfloatRGBColour?colour[8]?=?{?{0.0f,?0.0f,?0.0f},?{0.0f,?0.0f,?1.0f},?? {0.0f,?1.0f,?0.0f},?{1.0f,?0.0f,?0.0f},?? {0.0f,?1.0f,?1.0f},?{1.0f,?0.0f,?1.0f},?? {1.0f,?1.0f,?0.0f},?{1.0f,?1.0f,?1.0f}};?? ?? void?setPenColour(GLfloatRGBColour?newColour)?? {?? ????glColor3f(newColour.r,?newColour.g,?newColour.b);?? }?? ?? ?? ?? ?? void?parameterizedHouse(GLintPoint?peak,?GLint?width,?GLint?height)?? ?????? ?????? {?? ????glBegin(GL_LINE_LOOP);?? ????glVertex2i(peak.x,?????????????peak.y);???? ????glVertex2i(peak.x?+?width?/?2,?peak.y?-?3?*?height?/8);?? ????glVertex2i(peak.x?+?width?/?2,??peak.y?-?????height);?? ????glVertex2i(peak.x?-?width?/?2,?peak.y?-?????height);?? ????glVertex2i(peak.x?-?width?/?2,?peak.y?-?3?*?height?/8);??? ????glEnd();?? }?? ?? void?drawFlurry(int?num,?int?Width,?int?Height)?? ?????? {?? ????for?(int?i?=?0;?i?<?num;?i++)??? ????{?? ????????GLint?x1?=?random(Width);????????????? ????????GLint?y1?=?random(Height);?? ????????GLint?x2?=?random(Width);????????? ????????GLint?y2?=?random(Height);?? ????????GLfloat?lev?=?random(10)/10.0;???????? ????????glColor3f(lev,lev,lev);??????????? ????????glRecti(x1,?y1,?x2,?y2);?????????????? ????}?? ????glFlush();?? }???? ?? void?drawSierpinski(GLintPoint?corner[3])?? {??? ????int?i,?index,?tcolour=0;?? ????GLintPoint?point;?? ????point?=?corner[random(3)];?? ????drawDot(point.x,?point.y);?? ?? ????for?(i?=?0;?i?<?1000;?i++)?? ????{?? ????????index?=?random(3);?? ????????point.x?=?(point.x?+?corner[index].x)/2;?? ????????point.y?=?(point.y?+?corner[index].y)/2;???? ????????tcolour?=?(++tcolour)%7;????????? ????????setPenColour(colour[tcolour]);?? ????????drawDot(point.x,?point.y);?? ????}?? }?? ?? ?? ?? ?? void?myMouse(int?button,?int?state,?int?x,?int?y)?? {?? ????static?GLintPoint?corners[3];?? ????static?int?numCorners;?? ?? ????if?(button?==?GLUT_LEFT_BUTTON?&&?state?==?GLUT_DOWN)?? ????{?? ????????corners[numCorners].x?=?x;?? ????????corners[numCorners].y?=?screenHeight?-?y?-?1;?? ????????if?(++numCorners?==?3)?? ????????{?? ????????????drawSierpinski(corners);?? ????????????numCorners?=?0;?? ????????}?? ????}?? ????else?if?(button==GLUT_RIGHT_BUTTON)?? ????????glClear(GL_COLOR_BUFFER_BIT);?? ????glFlush();?? }?? ?? void?onKeyBoard(unsigned?char?key,int?mousex,?int?mousey)?? {?? ????switch?(key)?? ????{?? ????case?'q':?? ????????exit(0);?? ????case?'r':?? ????????static?GLintPoint?corners[3];?? ????????for?(int?i=0;i<3;i++)?? ????????{?? ????????????corners[i].x?=?random(640);?? ????????????corners[i].y?=?random(screenHeight);?? ????????}?? ????????drawSierpinski(corners);?? ????default:?? ????????break;?? ????}?? }?? ?? ?? void?Init(void)??????? {?? ????glClearColor(1.0,1.0,1.0,0.0);??? ????glColor3f(0.0f,0.0f,0.0f);?????? ????glMatrixMode(GL_PROJECTION);?? ????glLoadIdentity();?? ????gluOrtho2D(0.0,640.0,0.0,480.0);?? }?? ?? void?myDisplay()?? {?? ????glClear(GL_COLOR_BUFFER_BIT);????????? ????GLintPoint?Mypoint?=?{200,100};?? ????parameterizedHouse(Mypoint,100,100);?? ????drawFlurry(4,100,100);?? ????glFlush();?? }?? ?? void?main(int?argc,char?*argv[])?? {?? ????glutInit(&argc,?argv);???? ????glutInitDisplayMode(GLUT_RGB?|?GLUT_SINGLE);???? ????glutInitWindowPosition(100,?150);???? ????glutInitWindowSize(640,?480);???????? ????glutCreateWindow("parameterizedHouse,?Flurry?and?drawSierpinski");??? ????glutDisplayFunc(myDisplay);??? ????glutMouseFunc(myMouse);?? ????glutKeyboardFunc(onKeyBoard);?? ????Init();?? ????glutMainLoop();???? }??
效果圖:
第二個例子繪制了這樣一系列圖形:
在其中有空間投影變換,主要應用了三個函數:
投影變換函數glViewport(), 矩陣平移函數glTranslated() 和正射投影函數?glOrtho()
上圖實現代碼參考《計算機圖形學-用OpenGL實現第2版》:
[cpp]?view plain
?copy #include?<windows.h>??//suitable?when?using?Windows?95/98/NT?? #include?<gl/Gl.h>?? #include?<gl/Glu.h>?? #include?<gl/glut.h>?? ?? void?axis(double?length)?? {??? ????glPushMatrix();?? ????glBegin(GL_LINES);?? ????glVertex3d(0,?0,?0);?glVertex3d(0,0,length);??? ????glEnd();?? ????glTranslated(0,?0,length?-0.2);??? ????glutWireCone(0.04,?0.2,?12,?9);?? ????glPopMatrix();?? }????? ?? void?displayWire(void)?? {?? ????glMatrixMode(GL_PROJECTION);??? ????glLoadIdentity();?? ????glOrtho(-2.0*64/48.0,?2.0*64/48.0,?-2.0,?2.0,?0.1,?100);?? ????glMatrixMode(GL_MODELVIEW);??? ????glLoadIdentity();?? ????gluLookAt(2.0,?2.0,?2.0,?0.0,?0.0,?0.0,?0.0,?1.0,?0.0);?? ?? ?????? ????glClear(GL_COLOR_BUFFER_BIT);??? ????glColor3d(0,0,0);??? ????axis(0.5);????????????????????? ????glPushMatrix();??? ????glRotated(90,?0,1.0,?0);?? ????axis(0.5);???????????????????? ????glRotated(-90.0,?1,?0,?0);?? ????axis(0.5);???????????????????? ????glPopMatrix();???? ?? ?????? ????glPushMatrix();?? ????glTranslated(0.5,?0.5,?0.5);??? ????glutWireCube(1.0);?? ????glPopMatrix();?? ?? ?????? ????glPushMatrix();??? ????glTranslated(1.0,1.0,0);?????? ????glutWireSphere(0.25,?10,?8);?? ????glPopMatrix();???? ?? ?????? ????glPushMatrix();??? ????glTranslated(1.0,0,1.0);?????? ????glutWireCone(0.2,?0.5,?10,?8);?? ????glPopMatrix();?? ?? ?????? ????glPushMatrix();?? ????glTranslated(1,1,1);?? ????glutWireTeapot(0.2);??? ????glPopMatrix();?? ?? ?????? ????glPushMatrix();?? ????glTranslated(0,?1.0?,0);??? ????glRotated(90.0,?1,0,0);?? ????glutWireTorus(0.1,?0.3,?10,10);?? ????glPopMatrix();?? ?? ?????? ????glPushMatrix();?? ????glTranslated(1.0,?0?,0);??? ????glScaled(0.15,?0.15,?0.15);?? ????glutWireDodecahedron();?? ????glPopMatrix();?? ?? ????glPushMatrix();?? ????glTranslated(0,?1.0?,1.0);??? ????glutWireCube(0.25);?? ????glPopMatrix();?? ?? ????glPushMatrix();?? ????glTranslated(0,?0?,1.0);??? ????GLUquadricObj?*?qobj;?? ????qobj?=?gluNewQuadric();?? ????gluQuadricDrawStyle(qobj,GLU_LINE);?? ????gluCylinder(qobj,?0.2,?0.2,?0.4,?8,8);?? ????glPopMatrix();?? ????glFlush();?? }?? ?? ?? void?main(int?argc,?char?**argv)?? {?? ????glutInit(&argc,?argv);?? ????glutInitDisplayMode(GLUT_SINGLE?|?GLUT_RGB?);?? ????glutInitWindowSize(640,480);?? ????glutInitWindowPosition(100,?100);?? ????glutCreateWindow("Transformation?testbed?-?wireframes");?? ????glutDisplayFunc(displayWire);?? ????glClearColor(1.0f,?1.0f,?1.0f,0.0f);???? ????glViewport(0,?0,?640,?480);?? ????glutMainLoop();?? }??
Reference:
http://www.oocities.org/uniq_friq/c_files/openGL/1lab/dots.htm
from:?http://blog.csdn.net/abcjennifer/article/details/8587466
總結
以上是生活随笔為你收集整理的OpenGL 基础图形绘制与投影变换的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。