OpenGL项目绘制简单场景Word文档格式.docx
《OpenGL项目绘制简单场景Word文档格式.docx》由会员分享,可在线阅读,更多相关《OpenGL项目绘制简单场景Word文档格式.docx(17页珍藏版)》请在冰豆网上搜索。
利用鼠标右键进行灯光选择:
漫射光如图:
3.总结
4.参考文献
1、OpenGL超级宝典
2、博客:
vs2012中OpenGL配置
3、计算机图像学基础
5.代码
//DiffuseLight.cpp
//OpenGLSuperBible
//Demonstratessimplediffuselighting
//ProgrambyRichardS.WrightJr.
#include<
GLTools.h>
//OpenGLtoolkit
GLMatrixStack.h>
GLFrame.h>
GLFrustum.h>
GLGeometryTransform.h>
StopWatch.h>
math.h>
#ifdef__APPLE__
glut/glut.h>
#else
#defineFREEGLUT_STATIC
GL/glut.h>
#endif
GLFrameviewFrame;
GLFrustumviewFrustum;
GLTriangleBatchsphereBatch;
//GLBatchsphereBatch;
GLMatrixStackmodelViewMatrix;
GLMatrixStackprojectionMatrix;
GLGeometryTransformtransformPipeline;
GLShaderManagershaderManager;
GLuintADSLightShader;
GLintlocAmbient;
GLintlocDiffuse;
GLintlocSpecular;
GLintlocLight;
GLintlocMVP;
GLintlocMV;
GLintlocNM;
GLintlightOnOff=0;
//points
staticconstfloatvertex_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,
};
//pointsconnect
staticconstGLintindex_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}
//drawcube
voidDrawCube(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);
//Makethesphere
gltMakeSphere(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"
//Cleanup
voidShutdownRC(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);
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;
//SetViewporttowindowdimensions
glViewport(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);
voidright_menu(intid)
switch(id)
case0:
lightOnOff=0;
break;
case1:
lightOnOff=1;
case2:
lightOnOff=2;
case3:
lightOnOff=3;
default:
SetupRC();
///////////////////////////////////////////////////////////////////////////////
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);
ambient"
1);
diffuse"
2);
specular"
3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
GLenumerr=glewInit();
if(GLEW_OK!
=err){
fprintf(stderr,"
GLEWError:
%s\n"
glewGetErrorString(err));
return1;
glutMainLoop();
ShutdownRC();
return0;
.vp
#version130
//Incomingpervertex...positionandnormal
invec4vVertex;
invec3vNormal;
//Setperbatch
uniformvec4ambientColor;
uniformvec4diffuseColor;
uniformvec4specularColor;
uniformvec3vLightPosition;
uniformmat4mvpMatrix;
uniformmat4mvMatrix;
uniformmat3normalMatrix;
//Colortofragmentprogram
smoothoutvec4vVaryingColor;
voidmain(void)
{
//Getsurfacenormalineyecoordinates
vec3vEyeNormal=normalMatrix*vNormal;
//Getvertexpositionineyecoordinates
vec4vPosition4=mvMatrix*vVertex;
vec3vPosition3=vPosition4.xyz/vPosition4.w;
//Getvectortolightsource
vec3vLightDir=normalize(vLightPosition-vPosition3);
//Dotproductgivesusdiffuseintensity
floatdiff=max(0.0,dot(vEyeNormal,vLightDir));
//Multiplyintensitybydiffusecolor,forcealphato1.0
vVaryingColor=diff*diffuseColor;
//Addinambientlight
vVaryingColor+=ambientColor;
//SpecularLight
vec3vReflection=normalize(reflect(-vLightDir,vEyeNormal));
floatspec=max(0.0,dot(vEyeNormal,vReflection));
if(diff!
=0){
floatfSpec=pow(spec,128.0);
vVaryingColor.rgb+=vec3(fSpec,fSpec,fSpec);
//Don'
tforgettotransformthegeometry!
gl_Position=mvpMatrix*vVertex;
.fp
outvec4vFragColor;
smoothinvec4vVaryingColor;
voidmain(void)
vFragColor=vVaryingColor;