标签:
原文链接:
//////////////////////////////////////////////////////////////////////////////// // Filename: modelclass.h //////////////////////////////////////////////////////////////////////////////// #ifndef _MODELCLASS_H_ #define _MODELCLASS_H_
////////////// // INCLUDES // ////////////// #include <fstream> using namespace std; /////////////////////// // MY CLASS INCLUDES // /////////////////////// #include "textureclass.h" //////////////////////////////////////////////////////////////////////////////// // Class name: ModelClass //////////////////////////////////////////////////////////////////////////////// class ModelClass { private: struct VertexType { float x, y, z; float tu, tv; float nx, ny, nz; };
struct ModelType { float x, y, z; float tu, tv; float nx, ny, nz; }; public: ModelClass(); ModelClass(const ModelClass&); ~ModelClass();
bool Initialize(OpenGLClass*, char*, char*, unsigned int, bool); void Shutdown(OpenGLClass*); void Render(OpenGLClass*); private: bool InitializeBuffers(OpenGLClass*); void ShutdownBuffers(OpenGLClass*); void RenderBuffers(OpenGLClass*); bool LoadTexture(OpenGLClass*, char*, unsigned int, bool); void ReleaseTexture();
bool LoadModel(char*); void ReleaseModel(); private: int m_vertexCount, m_indexCount; unsigned int m_vertexArrayId, m_vertexBufferId, m_indexBufferId; TextureClass* m_Texture;
ModelType* m_model; }; #endif
//////////////////////////////////////////////////////////////////////////////// // Filename: modelclass.cpp //////////////////////////////////////////////////////////////////////////////// #include "modelclass.h" ModelClass::ModelClass() { m_Texture = 0;
m_model = 0; } ModelClass::ModelClass(const ModelClass& other) { } ModelClass::~ModelClass() { }
bool ModelClass::Initialize(OpenGLClass* OpenGL, char* modelFilename, char* textureFilename, unsigned int textureUnit, bool wrap) { bool result;
// Load in the model data. result = LoadModel(modelFilename); if(!result) { return false; } // Initialize the vertex and index buffers that hold the geometry for the model. result = InitializeBuffers(OpenGL); if(!result) { return false; } // Load the texture for this model. result = LoadTexture(OpenGL, textureFilename, textureUnit, wrap); if(!result) { return false; } return true; } void ModelClass::Shutdown(OpenGLClass* OpenGL) { // Release the texture used for this model. ReleaseTexture(); // Release the vertex and index buffers. ShutdownBuffers(OpenGL);
// Release the model data. ReleaseModel(); return; } void ModelClass::Render(OpenGLClass* OpenGL) { // Put the vertex and index buffers on the graphics pipeline to prepare them for drawing. RenderBuffers(OpenGL); return; } bool ModelClass::InitializeBuffers(OpenGLClass* OpenGL) { VertexType* vertices; unsigned int* indices; int i;
// Create the vertex array. vertices = new VertexType[m_vertexCount]; if(!vertices) { return false; } // Create the index array. indices = new unsigned int[m_indexCount]; if(!indices) { return false; }
// Load the vertex array and index array with data. for(i=0; i<m_vertexCount; i++) { vertices[i].x = m_model[i].x; vertices[i].y = m_model[i].y; vertices[i].z = m_model[i].z; vertices[i].tu = m_model[i].tu; vertices[i].tv = 1.0f - m_model[i].tv; vertices[i].nx = m_model[i].nx; vertices[i].ny = m_model[i].ny; vertices[i].nz = m_model[i].nz; indices[i] = i; } // Allocate an OpenGL vertex array object. OpenGL->glGenVertexArrays(1, &m_vertexArrayId); // Bind the vertex array object to store all the buffers and vertex attributes we create here. OpenGL->glBindVertexArray(m_vertexArrayId); // Generate an ID for the vertex buffer. OpenGL->glGenBuffers(1, &m_vertexBufferId); // Bind the vertex buffer and load the vertex (position, texture, and normal) data into the vertex buffer. OpenGL->glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferId); OpenGL->glBufferData(GL_ARRAY_BUFFER, m_vertexCount * sizeof(VertexType), vertices, GL_STATIC_DRAW); // Enable the three vertex array attributes. OpenGL->glEnableVertexAttribArray(0); // Vertex position. OpenGL->glEnableVertexAttribArray(1); // Texture coordinates. OpenGL->glEnableVertexAttribArray(2); // Normals. // Specify the location and format of the position portion of the vertex buffer. OpenGL->glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferId); OpenGL->glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(VertexType), 0); // Specify the location and format of the texture coordinate portion of the vertex buffer. OpenGL->glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferId); OpenGL->glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof(VertexType), (unsigned char*)NULL + (3 * sizeof(float))); // Specify the location and format of the normal vector portion of the vertex buffer. OpenGL->glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferId); OpenGL->glVertexAttribPointer(2, 3, GL_FLOAT, false, sizeof(VertexType), (unsigned char*)NULL + (5 * sizeof(float))); // Generate an ID for the index buffer. OpenGL->glGenBuffers(1, &m_indexBufferId); // Bind the index buffer and load the index data into it. OpenGL->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBufferId); OpenGL->glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexCount* sizeof(unsigned int), indices, GL_STATIC_DRAW); // Now that the buffers have been loaded we can release the array data. delete [] vertices; vertices = 0; delete [] indices; indices = 0; return true; } void ModelClass::ShutdownBuffers(OpenGLClass* OpenGL) { // Disable the two vertex array attributes. OpenGL->glDisableVertexAttribArray(0); OpenGL->glDisableVertexAttribArray(1); // Release the vertex buffer. OpenGL->glBindBuffer(GL_ARRAY_BUFFER, 0); OpenGL->glDeleteBuffers(1, &m_vertexBufferId); // Release the index buffer. OpenGL->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); OpenGL->glDeleteBuffers(1, &m_indexBufferId); // Release the vertex array object. OpenGL->glBindVertexArray(0); OpenGL->glDeleteVertexArrays(1, &m_vertexArrayId); return; } void ModelClass::RenderBuffers(OpenGLClass* OpenGL) { // Bind the vertex array object that stored all the information about the vertex and index buffers. OpenGL->glBindVertexArray(m_vertexArrayId); // Render the vertex buffer using the index buffer. glDrawElements(GL_TRIANGLES, m_indexCount, GL_UNSIGNED_INT, 0); return; } bool ModelClass::LoadTexture(OpenGLClass* OpenGL, char* textureFilename, unsigned int textureUnit, bool wrap) { bool result; // Create the texture object. m_Texture = new TextureClass; if(!m_Texture) { return false; } // Initialize the texture object. result = m_Texture->Initialize(OpenGL, textureFilename, textureUnit, wrap); if(!result) { return false; } return true; } void ModelClass::ReleaseTexture() { // Release the texture object. if(m_Texture) { m_Texture->Shutdown(); delete m_Texture; m_Texture = 0; } return; }
bool ModelClass::LoadModel(char* filename) { ifstream fin; char input; int i; // Open the model file. fin.open(filename); // If it could not open the file then exit. if(fin.fail()) { return false; } // Read up to the value of vertex count. fin.get(input); while(input != ':') { fin.get(input); } // Read in the vertex count. fin >> m_vertexCount; // Set the number of indices to be the same as the vertex count. m_indexCount = m_vertexCount; // Create the model using the vertex count that was read in. m_model = new ModelType[m_vertexCount]; if(!m_model) { return false; } // Read up to the beginning of the data. fin.get(input); while(input != ':') { fin.get(input); } fin.get(input); fin.get(input); // Read in the vertex data. for(i=0; i<m_vertexCount; i++) { fin >> m_model[i].x >> m_model[i].y >> m_model[i].z; fin >> m_model[i].tu >> m_model[i].tv; fin >> m_model[i].nx >> m_model[i].ny >> m_model[i].nz; } // Close the model file. fin.close(); return true; }
void ModelClass::ReleaseModel() { if(m_model) { delete [] m_model; m_model = 0; } return; }
//////////////////////////////////////////////////////////////////////////////// // Filename: graphicsclass.h //////////////////////////////////////////////////////////////////////////////// #ifndef _GRAPHICSCLASS_H_ #define _GRAPHICSCLASS_H_ /////////////////////// // MY CLASS INCLUDES // /////////////////////// #include "openglclass.h" #include "cameraclass.h" #include "modelclass.h" #include "lightshaderclass.h" #include "lightclass.h" ///////////// // GLOBALS // ///////////// const bool FULL_SCREEN = true; const bool VSYNC_ENABLED = true; const float SCREEN_DEPTH = 1000.0f; const float SCREEN_NEAR = 0.1f; //////////////////////////////////////////////////////////////////////////////// // Class name: GraphicsClass //////////////////////////////////////////////////////////////////////////////// class GraphicsClass { public: GraphicsClass(); GraphicsClass(const GraphicsClass&); ~GraphicsClass(); bool Initialize(OpenGLClass*, HWND); void Shutdown(); bool Frame(); private: bool Render(float); private: OpenGLClass* m_OpenGL; CameraClass* m_Camera; ModelClass* m_Model; LightShaderClass* m_LightShader; LightClass* m_Light; }; #endif
//////////////////////////////////////////////////////////////////////////////// // Filename: graphicsclass.cpp //////////////////////////////////////////////////////////////////////////////// #include "graphicsclass.h" GraphicsClass::GraphicsClass() { m_OpenGL = 0; m_Camera = 0; m_Model = 0; m_LightShader = 0; m_Light = 0; } GraphicsClass::GraphicsClass(const GraphicsClass& other) { } GraphicsClass::~GraphicsClass() { } bool GraphicsClass::Initialize(OpenGLClass* OpenGL, HWND hwnd) { bool result; // Store a pointer to the OpenGL class object. m_OpenGL = OpenGL; // Create the camera object. m_Camera = new CameraClass; if(!m_Camera) { return false; } // Set the initial position of the camera. m_Camera->SetPosition(0.0f, 0.0f, -10.0f); // Create the model object. m_Model = new ModelClass; if(!m_Model) { return false; }
// Initialize the model object. result = m_Model->Initialize(m_OpenGL, "../Engine/data/cube.txt", "../Engine/data/opengl.tga", 0, true); if(!result) { MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK); return false; } // Create the light shader object. m_LightShader = new LightShaderClass; if(!m_LightShader) { return false; } // Initialize the light shader object. result = m_LightShader->Initialize(m_OpenGL, hwnd); if(!result) { MessageBox(hwnd, L"Could not initialize the light shader object.", L"Error", MB_OK); return false; } // Create the light object. m_Light = new LightClass; if(!m_Light) { return false; }
// Initialize the light object. m_Light->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); m_Light->SetDirection(0.0f, 0.0f, 1.0f); return true; } void GraphicsClass::Shutdown() { // Release the light object. if(m_Light) { delete m_Light; m_Light = 0; } // Release the light shader object. if(m_LightShader) { m_LightShader->Shutdown(m_OpenGL); delete m_LightShader; m_LightShader = 0; } // Release the model object. if(m_Model) { m_Model->Shutdown(m_OpenGL); delete m_Model; m_Model = 0; } // Release the camera object. if(m_Camera) { delete m_Camera; m_Camera = 0; } // Release the pointer to the OpenGL class object. m_OpenGL = 0; return; } bool GraphicsClass::Frame() { bool result; static float rotation = 0.0f; // Update the rotation variable each frame. rotation += 0.0174532925f * 2.0f; if(rotation > 360.0f) { rotation -= 360.0f; } // Render the graphics scene. result = Render(rotation); if(!result) { return false; } return true; } bool GraphicsClass::Render(float rotation) { float worldMatrix[16]; float viewMatrix[16]; float projectionMatrix[16]; float lightDirection[3]; float diffuseLightColor[4]; // Clear the buffers to begin the scene. m_OpenGL->BeginScene(0.0f, 0.0f, 0.0f, 1.0f); // Generate the view matrix based on the camera's position. m_Camera->Render(); // Get the world, view, and projection matrices from the opengl and camera objects. m_OpenGL->GetWorldMatrix(worldMatrix); m_Camera->GetViewMatrix(viewMatrix); m_OpenGL->GetProjectionMatrix(projectionMatrix); // Get the light properties. m_Light->GetDirection(lightDirection); m_Light->GetDiffuseColor(diffuseLightColor); // Rotate the world matrix by the rotation value so that the triangle will spin. m_OpenGL->MatrixRotationY(worldMatrix, rotation); // Set the light shader as the current shader program and set the matrices that it will use for rendering. m_LightShader->SetShader(m_OpenGL); m_LightShader->SetShaderParameters(m_OpenGL, worldMatrix, viewMatrix, projectionMatrix, 0, lightDirection, diffuseLightColor); // Render the model using the light shader. m_Model->Render(m_OpenGL); // Present the rendered scene to the screen. m_OpenGL->EndScene(); return true; }
标签:
原文地址:http://blog.csdn.net/weyson/article/details/47008843