Пример #1
0
Renderer::Renderer(int width, int height, std::vector< std::unique_ptr<RenderableObject> >& objects)
    :m_viewWidth(width),
    m_viewHeight(height),
    m_objects(objects)
{
    //initialise the viewport? 
    glViewport(0, 0, width, height);
    
    //initialise perspectiveTransform
    float fFrustumScale = 1.0f; float fzNear = 0.1f; float fzFar = 100.0f;
    m_perspectiveTransform.set(fFrustumScale, fzNear, fzFar, width, height);

    //initialise all the openGL extensions    
    GLenum returnCode = glewInit();
    if ( returnCode != GLEW_OK)
    {
        //error!
    }

    glGenBuffers(1, &positionBufferObject); //create a buffer object, store in global variable
    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject); //bind to context (?)
   
    const std::vector<float>& vertices = m_objects[0]->getVertexVector();
    const float* vertexPointer = &vertices[0];

    //glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW); //alloc some memory of sizeof(vertexData) then copy in vertexData
    glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), vertexPointer, GL_STATIC_DRAW); //alloc some memory of sizeof(vertexData) then copy in vertexData
    glBindBuffer(GL_ARRAY_BUFFER, 0); //usually best to unbind what you bind as a cleanup
    initialiseShaders(); // my function
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    timeLocation = glGetUniformLocation(*m_shaderProgram, "time");
    // If you want to set uniforms once, do it here but you first have to
    // enable the program (useprogram) then disable it... they can only be set
    // while program is enabled

    GLuint perspectiveTransformLocation = glGetUniformLocation(*m_shaderProgram, "perspectiveTransform");

    //copy into vertex shader
    glUseProgram(*m_shaderProgram);
    glUniformMatrix4fv(perspectiveTransformLocation, 1, GL_FALSE, m_perspectiveTransform.data()); 
    glUseProgram(0);
    //this could be an array of matrices, the one indicates only 1 matrix!
    //GL_FALSE means row-major=false


    glEnable(GL_CULL_FACE); //enable face culling
    glCullFace(GL_BACK); //backface culling
    glFrontFace(GL_CW); // clockwise winding is front face
    glEnable(GL_DEPTH_TEST); // depth testing
    glDepthMask(GL_TRUE);
    glDepthFunc(GL_LEQUAL); // less than or equal to
    
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //set the colour that a clear will paint
    glClearDepth(1.0f);
}
/*Constructs a Model Object.*/
Model::Model(std::string vertexShaderFileLocation, std::string fragmentShaderFileLocation, std::string objFileName,
	std::unordered_map<std::string, Object*> &objects, std::unordered_map<std::string, Shader*> &shaders)
{
	/*initialise the material*/
	material = "Untextured";

	/*initialise the shader*/
	initialiseShaders(vertexShaderFileLocation, fragmentShaderFileLocation, shaders);
	/*initialise the object*/
	initialiseVAO(objFileName, objects);
}