OPENGL編程寶典Phong著色

來源:互聯網
上載者:User
// DiffuseLight.cpp// OpenGL SuperBible// Demonstrates simple diffuse lighting// Program by Richard S. Wright Jr.#include <GLTools.h>// OpenGL toolkit#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#define FREEGLUT_STATIC#include <GL/glut.h>#endifGLFrame             viewFrame;GLFrustum           viewFrustum;GLTriangleBatch     sphereBatch;GLMatrixStack       modelViewMatrix;GLMatrixStack       projectionMatrix;GLGeometryTransform transformPipeline;GLShaderManager     shaderManager;GLuintADSLightShader;// The diffuse light shaderGLintlocAmbient;// The location of the ambient colorGLint   locDiffuse;// The location of the diffuse colorGLint   locSpecular;// The location of the specular colorGLintlocLight;// The location of the Light in eye coordinatesGLintlocMVP;// The location of the ModelViewProjection matrix uniformGLintlocMV;// The location of the ModelView matrix uniformGLintlocNM;// The location of the Normal matrix uniform// This function does any needed initialization on the rendering// context. void SetupRC(void){// BackgroundglClearColor(0.0f, 0.0f, 0.0f, 1.0f );glEnable(GL_DEPTH_TEST);glEnable(GL_CULL_FACE);    shaderManager.InitializeStockShaders();    viewFrame.MoveForward(4.0f);    // Make the sphere    gltMakeSphere(sphereBatch, 1.0f, 26, 13);ADSLightShader = shaderManager.LoadShaderPairWithAttributes("ADSPhong.vp", "ADSPhong.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");}// Cleanupvoid ShutdownRC(void){}// Called to draw scenevoid RenderScene(void){static CStopWatch rotTimer;// Clear the window and the depth bufferglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    modelViewMatrix.PushMatrix(viewFrame);modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() * 10.0f, 0.0f, 1.0f, 0.0f);GLfloat vEyeLight[] = { -100.0f, 100.0f, 100.0f };GLfloat vAmbientColor[] = { 0.1f, 0.1f, 0.1f, 1.0f };GLfloat vDiffuseColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };GLfloat vSpecularColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };glUseProgram(ADSLightShader);glUniform4fv(locAmbient, 1, vAmbientColor);glUniform4fv(locDiffuse, 1, vDiffuseColor);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();    modelViewMatrix.PopMatrix();    glutSwapBuffers();glutPostRedisplay();}void ChangeSize(int w, int h){// Prevent a divide by zeroif(h == 0)h = 1;// Set Viewport to window dimensions    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);}///////////////////////////////////////////////////////////////////////////////// Main entry point for GLUT based programsint main(int argc, char* argv[])    {gltSetWorkingDirectory(argv[0]);glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);glutInitWindowSize(800, 600);glutCreateWindow("ADS Lighting, Phong Shading");    glutReshapeFunc(ChangeSize);    glutDisplayFunc(RenderScene);GLenum err = glewInit();if (GLEW_OK != err) {fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));return 1;    }SetupRC();    glutMainLoop();ShutdownRC();return 0;    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.