OpenGL項目:繪制簡單場景_第1頁
OpenGL項目:繪制簡單場景_第2頁
OpenGL項目:繪制簡單場景_第3頁
OpenGL項目:繪制簡單場景_第4頁
OpenGL項目:繪制簡單場景_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

計算機圖形學報告(項目一)專業(yè):計算機科學與技術(shù)姓名:學號:班級:目錄TOC\o"1-3"\h\z1.引言 11.1編寫目的 11.2項目背景 12.開發(fā)結(jié)果 22.1開發(fā)環(huán)境 22.2主要功能及性能 22.3場景介紹 22.4操作介紹 22.5燈光介紹 23.總結(jié) 34.參考文獻 35.代碼 31.引言1.1編寫目的利用OpenGL繪制一個簡單的場景–包含球、正方形、網(wǎng)格數(shù)據(jù)–交互操作:平移、縮放、旋轉(zhuǎn)–可以使用不同的燈光1.2項目背景openGL編程在計算機圖形學中具有非常重要的作用,是實現(xiàn)計算機圖形的一個重要工 具,通過openGL編程,我們能更好的學習計算機圖形學,同時也能提高我們自己的實際動手能力。2.開發(fā)結(jié)果2.1開發(fā)環(huán)境Windows7旗艦版Vs20122.2主要功能及性能利用OpenGL繪制一個簡單的場景–包含球、正方形、網(wǎng)格數(shù)據(jù)–交互操作:平移、縮放、旋轉(zhuǎn)–可以使用不同的燈光2.3場景介紹繪制一個簡單的場景,包含球、正方形、網(wǎng)格數(shù)據(jù)。如圖:2.4操作介紹利用上下左右方向鍵進行上下左右移動,如圖:利用home,end,pgup,insertj進行上下左右的旋轉(zhuǎn),如圖:2.5燈光介紹包括三中燈光:環(huán)境光,漫射光,平面光利用鼠標右鍵進行燈光選擇:漫射光如圖:總結(jié)參考文獻1、OpenGL超級寶典2、博客:vs2012中OpenGL配置3、計算機圖像學基礎(chǔ)5.代碼//DiffuseLight.cpp//OpenGLSuperBible//Demonstratessimplediffuselighting//ProgrambyRichardS.WrightJr.#include<GLTools.h> //OpenGLtoolkit#include<GLMatrixStack.h>#include<GLFrame.h>#include<GLFrustum.h>#include<GLGeometryTransform.h>#include<StopWatch.h>#include<math.h>#ifdef__APPLE__#include<glut/glut.h>#else#defineFREEGLUT_STATIC#include<GL/glut.h>#endifGLFrameviewFrame;GLFrustumviewFrustum;GLTriangleBatchsphereBatch;//GLBatchsphereBatch;GLMatrixStackmodelViewMatrix;GLMatrixStackprojectionMatrix;GLGeometryTransformtransformPipeline;GLShaderManagershaderManager;GLuint ADSLightShader; GLint locAmbient; GLintlocDiffuse; GLintlocSpecular; GLint locLight; GLint locMVP; GLint locMV; GLint locNM; GLintlightOnOff=0;//pointsstaticconstfloatvertex_list[][3]={-0.5f,-1.5f,-0.5f,0.5f,-1.5f,-0.5f,0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-1.5f,0.5f,0.5f,-1.5f,0.5f,0.5f,-0.5f,0.5f,-0.5f,-0.5f,0.5f,};//pointsconnectstaticconstGLintindex_list[][4]={{0,1,2,3},{3,2,6,7},{4,5,6,7},{0,1,5,4},{1,5,6,2},{0,4,7,3}};//drawcubevoidDrawCube(void){inti,j; glColor3f(1.0,0.0,0.0); glBegin(GL_QUADS);for(i=0;i<6;++i){for(j=0;j<4;++j){glVertex3fv(vertex_list[index_list[i][j]]);}}glEnd();}voidSetupRC(void) { //Background glClearColor(0.0f,1.0f,0.0f,1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE);shaderManager.InitializeStockShaders();//viewFrame.MoveForward(4.0f);//MakethespheregltMakeSphere(sphereBatch,0.5f,26,13); ADSLightShader=shaderManager.LoadShaderPairWithAttributes("ADSGouraud.vp","ADSGouraud.fp",2,GLT_ATTRIBUTE_VERTEX,"vVertex", GLT_ATTRIBUTE_NORMAL,"vNormal"); locAmbient=glGetUniformLocation(ADSLightShader,"ambientColor"); locDiffuse=glGetUniformLocation(ADSLightShader,"diffuseColor"); locSpecular=glGetUniformLocation(ADSLightShader,"specularColor"); locLight=glGetUniformLocation(ADSLightShader,"vLightPosition"); locMVP=glGetUniformLocation(ADSLightShader,"mvpMatrix"); locMV=glGetUniformLocation(ADSLightShader,"mvMatrix"); locNM=glGetUniformLocation(ADSLightShader,"normalMatrix"); }//CleanupvoidShutdownRC(void){}voidRenderScene(void) { staticCStopWatchrotTimer; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(viewFrame); //modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds()*10.0f,0.0f,1.0f,0.0f); GLfloatvEyeLight[]={-100.0f,100.0f,100.0f}; GLfloatvAmbientColor[]={0.1f,0.1f,0.1f,1.0f}; GLfloatvDiffuseColor[]={0.0f,0.0f,1.0f,1.0f}; GLfloatvSpecularColor[]={1.0f,1.0f,1.0f,1.0f}; glUseProgram(ADSLightShader); if(lightOnOff==1) glUniform4fv(locAmbient,1,vAmbientColor); if(lightOnOff==2) glUniform4fv(locDiffuse,1,vDiffuseColor); if(lightOnOff==3) glUniform4fv(locSpecular,1,vSpecularColor); glUniform3fv(locLight,1,vEyeLight); glUniformMatrix4fv(locMVP,1,GL_FALSE,transformPipeline.GetModelViewProjectionMatrix()); glUniformMatrix4fv(locMV,1,GL_FALSE,transformPipeline.GetModelViewMatrix()); glUniformMatrix3fv(locNM,1,GL_FALSE,transformPipeline.GetNormalMatrix()); sphereBatch.Draw(); glColor3f(1.0,1.0,0.0); glRectf(-1.0,-1.0,1.0,1.0); GLfloatj=-3.0; for(inti=0;i<=30;i++) { glBegin(GL_LINES); glVertex2f(j,-3.0f); glVertex2f(j,3.0f); glEnd(); glBegin(GL_LINES); glVertex2f(-3.0f,j); glVertex2f(3.0f,j); glEnd(); j=j+0.2; } DrawCube();modelViewMatrix.PopMatrix();glutSwapBuffers(); glutPostRedisplay(); }voidChangeSize(intw,inth) { //Preventadividebyzero if(h==0) h=1; //SetViewporttowindowdimensionsglViewport(0,0,w,h);viewFrustum.SetPerspective(35.0f,float(w)/float(h),1.0f,100.0f);projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());transformPipeline.SetMatrixStacks(modelViewMatrix,projectionMatrix); }voidSpecialKeys(intkey,intx,inty){ GLfloatstepSize=0.1f; GLfloatinitx=0.0; GLfloatinity=0.0; if(key==GLUT_KEY_UP) inity+=stepSize; if(key==GLUT_KEY_DOWN) inity-=stepSize; if(key==GLUT_KEY_LEFT) initx-=stepSize; if(key==GLUT_KEY_RIGHT) initx+=stepSize; viewFrame.TranslateWorld(initx,inity,0); if(key==GLUT_KEY_HOME) viewFrame.RotateWorld(m3dDegToRad(-5.0),1.0f,0.0f,0.0f); if(key==GLUT_KEY_END) viewFrame.RotateWorld(m3dDegToRad(5.0),1.0f,0.0f,0.0f); if(key==GLUT_KEY_INSERT) viewFrame.RotateWorld(m3dDegToRad(-5.0),0.0f,1.0f,0.0f); if(key==GLUT_KEY_PAGE_UP) viewFrame.RotateWorld(m3dDegToRad(5.0),0.0f,1.0f,0.0f); if(key==GLUT_KEY_F1) viewFrame.MoveForward(1.0f); if(key==GLUT_KEY_F2) viewFrame.MoveForward(-1.0f); glutPostRedisplay(); }voidright_menu(intid){switch(id){ case0:lightOnOff=0; break; case1:lightOnOff=1; break; case2:lightOnOff=2; break; case3:lightOnOff=3; break; default: break; } SetupRC(); glutPostRedisplay();}///////////////////////////////////////////////////////////////////////////////intmain(intargc,char*argv[]){ gltSetWorkingDirectory(argv[0]); glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL); glutInitWindowSize(800,600); glutCreateWindow("ADSLighting,GouraudShading");glutReshapeFunc(ChangeSize);glutDisplayFunc(RenderScene); glutSpecialFunc(SpecialKeys); glutCreateMenu(right_menu);glutAddMenuEntry("off",0);glutAddMenuEntry("ambient",1);glutAddMenuEntry("diffuse",2); glutAddMenuEntry("specular",3);glutAttachMenu(GLUT_RIGHT_BUTTON); GLenumerr=glewInit(); if(GLEW_OK!=err){ fprintf(stderr,"GLEWError:%s\n",glewGetErrorString(err)); return1;} SetupRC(); glutMainLoop(); ShutdownRC(); return0;}.vp#version130//Incomingpervertex...positionandnormalinvec4vVertex;invec3vNormal;//Setperbatchuniformvec4ambientColor;uniformvec4diffuseColor; uniformvec4specularColor;uniformvec3 vLightPosition;uniformmat4 mvpMatrix;uniformmat4 mvMatrix;uniformmat3 normalMatrix;//Colortofragmentprogramsmoothoutvec4vVaryingColor;voidmain(void){//Getsurfacenormalineyecoordinatesvec3vEyeNormal=normalMatrix*vNormal;//Getvertexpositionineyecoordinatesvec4vPosition4=mvMatrix*vVertex;vec3vPosition3=vPosition4.xyz/vPosition4.w;//Ge

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論