Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<windows.h>
- #include <GLTools.h>
- #include <GLShaderManager.h>
- #include <GL/glut.h>
- #include <math.h>
- #include <GLMatrixStack.h>
- #include <GLFrame.h>
- #include <GLBatch.h>
- #include <GLFrustum.h>
- #include <GLGeometryTransform.h>
- #include <stdio.h>
- #include <math3d.h>
- GLFrame viewFrame;
- GLFrustum viewFrustum;
- GLTriangleBatch sphereBatch_;
- GLMatrixStack modelViewMatrix;
- GLMatrixStack projectionMatrix;
- GLGeometryTransform transformPipeline;
- GLShaderManager shaderManager;
- GLuint ADSLightShader; // The diffuse light shader
- GLint locAmbient; // The location of the ambient color
- GLint locDiffuse; // The location of the diffuse color
- GLint locSpecular; // The location of the specular color
- GLint locLight; // The location of the Light in eye coordinates
- GLint locMVP; // The location of the ModelViewProjection matrix uniform
- GLint locMV; // The location of the ModelView matrix uniform
- GLint locNM; // The location of the Normal matrix uniform
- GLTriangleBatch sphereBatch;
- GLfloat fRadius = 30;
- GLint iSlices = 20;
- GLint iStacks = 20;
- // Texture objects
- #define TEXTURE_EARTH 0
- #define TEXTURE_COUNT 1
- GLuint textures[TEXTURE_COUNT];
- const char *szTextureFiles[TEXTURE_COUNT] = {"d:\project\AAAAAA\texturemap.tga"};
- // This function does any needed initialization on the rendering
- // context.
- void SetupRC(void)
- {
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_CULL_FACE);
- shaderManager.InitializeStockShaders();
- viewFrame.MoveForward(4.0f);
- gltMakeSphere(sphereBatch_, 1.0f, 26, 13);
- ADSLightShader = shaderManager.LoadShaderPairWithAttributes("d:\project\AAAAAA\ADSPhong.vp", "d:\project\AAAAAA\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");
- GLbyte *pBytes;
- GLint iWidth, iHeight, iComponents;
- GLenum eFormat;
- GLint iLoop_;
- // Load textures
- glGenTextures(TEXTURE_COUNT, textures);
- for(iLoop_ = 0; iLoop_ < TEXTURE_COUNT; iLoop_++)
- {
- // Bind to next texture object
- glBindTexture(GL_TEXTURE_2D, textures[iLoop_]);
- // Load texture, set filter and wrap modes
- pBytes = gltReadTGABits(szTextureFiles[iLoop_],&iWidth, &iHeight,&iComponents,&eFormat);
- // Load texture, set filter and wrap modes
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
- glGenerateMipmap(GL_TEXTURE_2D);
- // Don't need original texture data any more
- free(pBytes);
- }
- GLfloat drho = (GLfloat)(3.141592653589) / (GLfloat) iStacks;
- GLfloat dtheta = 2.0f * (GLfloat)(3.141592653589) / (GLfloat) iSlices;
- GLfloat ds = 1.0f / (GLfloat) iSlices;
- GLfloat dt = 1.0f / (GLfloat) iStacks;
- GLfloat t = 1.0f;
- GLfloat s = 0.0f;
- GLint i, j; // Looping variables
- sphereBatch.BeginMesh(iSlices * iStacks * 6);
- for (i = 0; i < iStacks; i++)
- {
- GLfloat rho = (GLfloat)i * drho;
- GLfloat srho = (GLfloat)(sin(rho));
- GLfloat crho = (GLfloat)(cos(rho));
- GLfloat srhodrho = (GLfloat)(sin(rho + drho));
- GLfloat crhodrho = (GLfloat)(cos(rho + drho));
- // Many sources of OpenGL sphere drawing code uses a triangle fan
- // for the caps of the sphere. This however introduces texturing
- // artifacts at the poles on some OpenGL implementations
- s = 0.0f;
- M3DVector3f vVertex[4];
- M3DVector3f vNormal[4];
- M3DVector2f vTexture[4];
- for ( j = 0; j < iSlices; j++)
- {
- GLfloat theta = (j == iSlices) ? 0.0f : j * dtheta;
- GLfloat stheta = (GLfloat)(-sin(theta));
- GLfloat ctheta = (GLfloat)(cos(theta));
- GLfloat x = stheta * srho;
- GLfloat y = ctheta * srho;
- GLfloat z = crho;
- vTexture[0][0] = s;
- vTexture[0][1] = t;
- vNormal[0][0] = x;
- vNormal[0][1] = y;
- vNormal[0][2] = z;
- vVertex[0][0] = x * fRadius;
- vVertex[0][1] = y * fRadius;
- vVertex[0][2] = z * fRadius;
- x = stheta * srhodrho;
- y = ctheta * srhodrho;
- z = crhodrho;
- vTexture[1][0] = s;
- vTexture[1][1] = t - dt;
- vNormal[1][0] = x;
- vNormal[1][1] = y;
- vNormal[1][2] = z;
- vVertex[1][0] = x * fRadius;
- vVertex[1][1] = y * fRadius;
- vVertex[1][2] = z * fRadius;
- theta = ((j+1) == iSlices) ? 0.0f : (j+1) * dtheta;
- stheta = (GLfloat)(-sin(theta));
- ctheta = (GLfloat)(cos(theta));
- x = stheta * srho;
- y = ctheta * srho;
- z = crho;
- s += ds;
- vTexture[2][0] = s;
- vTexture[2][1] = t;
- vNormal[2][0] = x;
- vNormal[2][1] = y;
- vNormal[2][2] = z;
- vVertex[2][0] = x * fRadius;
- vVertex[2][1] = y * fRadius;
- vVertex[2][2] = z * fRadius;
- x = stheta * srhodrho;
- y = ctheta * srhodrho;
- z = crhodrho;
- vTexture[3][0] = s;
- vTexture[3][1] = t - dt;
- vNormal[3][0] = x;
- vNormal[3][1] = y;
- vNormal[3][2] = z;
- vVertex[3][0] = x * fRadius;
- vVertex[3][1] = y * fRadius;
- vVertex[3][2] = z * fRadius;
- sphereBatch.AddTriangle(vVertex, vNormal, vTexture);
- // Rearrange for next triangle
- memcpy(vVertex[0], vVertex[1], sizeof(M3DVector3f));
- memcpy(vNormal[0], vNormal[1], sizeof(M3DVector3f));
- memcpy(vTexture[0], vTexture[1], sizeof(M3DVector2f));
- memcpy(vVertex[1], vVertex[3], sizeof(M3DVector3f));
- memcpy(vNormal[1], vNormal[3], sizeof(M3DVector3f));
- memcpy(vTexture[1], vTexture[3], sizeof(M3DVector2f));
- sphereBatch.AddTriangle(vVertex, vNormal, vTexture);
- }
- t -= dt;
- }
- sphereBatch.End();
- }
- // Cleanup
- void ShutdownRC(void)
- {
- glDeleteTextures(TEXTURE_COUNT, textures);
- }
- // Called to draw scene
- void RenderScene(void)
- {
- // Clear the window and the depth buffer
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- modelViewMatrix.PushMatrix(viewFrame);
- //modelViewMatrix.PushMatrix();
- 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());
- shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE,transformPipeline.GetModelViewProjectionMatrix(), 0);
- glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_EARTH]);
- sphereBatch.Draw();
- sphereBatch_.Draw();
- modelViewMatrix.PopMatrix();
- glutSwapBuffers();
- glutPostRedisplay();
- }
- void ChangeSize(int w, int h)
- {
- // Prevent a divide by zero
- if (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 programs
- int 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: %sn", glewGetErrorString(err));
- return 1;
- }
- SetupRC();
- glutMainLoop();
- ShutdownRC();
- return 0;
- }
Add Comment
Please, Sign In to add comment