OpenGL项目:绘制简单场景_第1页
OpenGL项目:绘制简单场景_第2页
OpenGL项目:绘制简单场景_第3页
OpenGL项目:绘制简单场景_第4页
OpenGL项目:绘制简单场景_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

计算机图形学报告(项目一)专业:计算机科学与技术姓名:学号:班级:目录TOC\o"1-3"\h\z1.引言 11.1编写目的 11.2项目背景 12.开发结果 22.1开发环境 22.2主要功能及性能 22.3场景介绍 22.4操作介绍 22.5灯光介绍 23.总结 34.参考文献 35.代码 31.引言1.1编写目的利用OpenGL绘制一个简单的场景–包含球、正方形、网格数据–交互操作:平移、缩放、旋转–可以使用不同的灯光1.2项目背景openGL编程在计算机图形学中具有非常重要的作用,是实现计算机图形的一个重要工 具,通过openGL编程,我们能更好的学习计算机图形学,同时也能提高我们自己的实际动手能力。2.开发结果2.1开发环境Windows7旗舰版Vs20122.2主要功能及性能利用OpenGL绘制一个简单的场景–包含球、正方形、网格数据–交互操作:平移、缩放、旋转–可以使用不同的灯光2.3场景介绍绘制一个简单的场景,包含球、正方形、网格数据。如图:2.4操作介绍利用上下左右方向键进行上下左右移动,如图:利用home,end,pgup,insertj进行上下左右的旋转,如图:2.5灯光介绍包括三中灯光:环境光,漫射光,平面光利用鼠标右键进行灯光选择:漫射光如图:总结参考文献1、OpenGL超级宝典2、博客:vs2012中OpenGL配置3、计算机图像学基础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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论