1. 程式人生 > >OpenGL 在Win7(32)上配置開發環境流程及注意問題

OpenGL 在Win7(32)上配置開發環境流程及注意問題

#include <GLTools.h>            // OpenGL toolkit
#include <GLShaderManager.h>    // Shader Manager Class
#include<GLFrustum.h>
#include<math3d.h>
#include<StopWatch.h>
#include <GL/glut.h>            // Windows FreeGlut equivalent

GLBatchtriangleBatch;
GLShaderManagershaderManager;
GLTriangleBatch sphere;
GLFrustum frustum;

M3DMatrix44f translation, rotation, modelviewmatrix, finalmatrix;

///////////////////////////////////////////////////////////////////////////////

void ChangeSize(int w, int h)
{
if (h == 0)
h = 1;
glViewport(0, 0, w, h);

frustum.SetPerspective(45.0, float(w) / float(h), 1.0, 10.0);
}

///////////////////////////////////////////////////////////////////////////////

void SetupRC()
{
// Blue background
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
shaderManager.InitializeStockShaders();
gltMakeSphere(sphere, 0.6,40, 20);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
}

///////////////////////////////////////////////////////////////////////////////

void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
static CStopWatch time;
GLfloat deg = time.GetElapsedSeconds()*45.0f;
m3dTranslationMatrix44(translation, 0.0f, 0.0f, x);
m3dRotationMatrix44(rotation, m3dDegToRad(deg), 1.0f, 0.0f, 0.0f);
m3dMatrixMultiply44(modelviewmatrix, translation, rotation);
m3dMatrixMultiply44(finalmatrix, frustum.GetProjectionMatrix(), modelviewmatrix);
GLfloat vBlack[] = { 0.0f, 0.0f, 0.0f, 1.0f };
shaderManager.UseStockShader(GLT_SHADER_FLAT, finalmatrix,vBlack);
sphere.Draw();
// Perform the buffer swap to display back buffer
glutSwapBuffers();
glutPostRedisplay();
}

///////////////////////////////////////////////////////////////////////////////

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("Triangle");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}