SimpleMesh::SimpleMesh(GLuint a_programId, int a_slicesNum, int a_meshType, float a_halfSize) { if(a_meshType == SPHERE) glusCreateSpheref(&m_glusShape, a_halfSize, a_slicesNum); else if(a_meshType == CUBE) glusCreateCubef(&m_glusShape, a_halfSize); else if(a_meshType == CUBE_OPEN) glusCreateCubeOpenf(&m_glusShape, a_halfSize); else if(a_meshType == PLANE) { if(a_slicesNum <= 2) { glusCreatePlanef(&m_glusShape, a_halfSize); for(unsigned int i=0;i<2*m_glusShape.numberVertices;i++) m_glusShape.texCoords[i] *= 2.0f; } else glusCreatePlaneSlicedf(&m_glusShape, a_halfSize, a_slicesNum); } else if(a_meshType == TORUS) glusCreateTorusf(&m_glusShape, 0.5f*a_halfSize, a_halfSize, a_slicesNum, 32*a_slicesNum); else glusCreateCubef(&m_glusShape, 1.0f); CreateGPUData(a_programId); }
SimpleMesh::SimpleMesh(GLuint a_programId, int a_slicesNum, int a_meshType, float a_halfSize) { if(a_meshType == SPHERE) glusCreateSpheref(&m_glusShape, a_halfSize, a_slicesNum); else if(a_meshType == CUBE) glusCreateCubef(&m_glusShape, a_halfSize); else if(a_meshType == PLANE) { glusCreatePlanef(&m_glusShape, a_halfSize); for(unsigned int i=0;i<2*m_glusShape.numberVertices;i++) m_glusShape.texCoords[i] *= 2.0f; } else if(a_meshType == TORUS) glusCreateTorusf(&m_glusShape, 0.5f*a_halfSize, a_halfSize, a_slicesNum, 32*a_slicesNum); else glusCreateCubef(&m_glusShape, 1.0f); m_vertexPosBufferObject = 0; m_vertexPosLocation = glGetAttribLocation(a_programId, "vertex"); m_vertexNormLocation = glGetAttribLocation(a_programId, "normal"); m_vertexTexCoordsLocation = glGetAttribLocation(a_programId, "texCoord"); GLint maxVertexAttributes = 0; glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttributes); // create buffers a,d fill them with data // vertex positions // glGenBuffers(1, &m_vertexPosBufferObject); CHECK_GL_ERRORS; glBindBuffer(GL_ARRAY_BUFFER, m_vertexPosBufferObject); CHECK_GL_ERRORS; glBufferData(GL_ARRAY_BUFFER, m_glusShape.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) m_glusShape.vertices, GL_STATIC_DRAW); CHECK_GL_ERRORS; // vertex normals // glGenBuffers(1, &m_vertexNormBufferObject); CHECK_GL_ERRORS; glBindBuffer(GL_ARRAY_BUFFER, m_vertexNormBufferObject); CHECK_GL_ERRORS; glBufferData(GL_ARRAY_BUFFER, m_glusShape.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) m_glusShape.normals, GL_STATIC_DRAW); CHECK_GL_ERRORS; // vertex texture coordinates // glGenBuffers(1, &m_vertexTexCoordsBufferObject); CHECK_GL_ERRORS; glBindBuffer(GL_ARRAY_BUFFER, m_vertexTexCoordsBufferObject); CHECK_GL_ERRORS; glBufferData(GL_ARRAY_BUFFER, m_glusShape.numberVertices * 2 * sizeof(GLfloat), (GLfloat*) m_glusShape.texCoords, GL_STATIC_DRAW); CHECK_GL_ERRORS; // index buffer // glGenBuffers(1, &m_indexBufferObject); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBufferObject); glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_glusShape.numberIndices * sizeof(GLuint), (GLuint*)m_glusShape.indices, GL_STATIC_DRAW); // create VAO and bind each buffer to appropriate "pointer", called vertex array attribute // glGenVertexArrays(1, &m_vertexArrayObject); CHECK_GL_ERRORS; glBindVertexArray(m_vertexArrayObject); CHECK_GL_ERRORS; if(m_vertexPosLocation < GLuint(maxVertexAttributes)) { glBindBuffer(GL_ARRAY_BUFFER, m_vertexPosBufferObject); CHECK_GL_ERRORS; glEnableVertexAttribArray(m_vertexPosLocation); CHECK_GL_ERRORS; glVertexAttribPointer(m_vertexPosLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); CHECK_GL_ERRORS; } if(m_vertexNormLocation < GLuint(maxVertexAttributes)) { glBindBuffer(GL_ARRAY_BUFFER, m_vertexNormBufferObject); CHECK_GL_ERRORS; glEnableVertexAttribArray(m_vertexNormLocation); CHECK_GL_ERRORS; glVertexAttribPointer(m_vertexNormLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); CHECK_GL_ERRORS; } if(m_vertexTexCoordsLocation < GLuint(maxVertexAttributes)) { glBindBuffer(GL_ARRAY_BUFFER, m_vertexTexCoordsBufferObject); CHECK_GL_ERRORS; glEnableVertexAttribArray(m_vertexTexCoordsLocation); CHECK_GL_ERRORS; glVertexAttribPointer(m_vertexTexCoordsLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); CHECK_GL_ERRORS; } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBufferObject); CHECK_GL_ERRORS; glBindVertexArray(0); // unbind VAO }
ShapePtr ShapeFactory::createBox(float halfExtent) { GLUSshape box; glusCreateCubef(&box, halfExtent); return boost::make_shared<Shape>(box.vertices, box.numberVertices, box.normals, box.indices, box.numberIndices, box.texCoords); }
/** * Function for initialization. */ GLUSboolean init(GLUSvoid) { // Matrix for the model GLfloat model[16]; GLUSshape cube; GLUStgaimage image; GLUStextfile vertexSource; GLUStextfile fragmentSource; // Load the source of the vertex shader. glusLoadTextFile("../Example05/Vertex.vs", &vertexSource); // Load the source of the fragment shader. glusLoadTextFile("../Example05/Fragment.fs", &fragmentSource); // Build and ... glusBuildProgram(&g_program, (const GLUSchar**)&vertexSource.text, 0, (const GLUSchar**)&fragmentSource.text); // Destroy the text resource glusDestroyTextFile(&vertexSource); // Destroy the text resource glusDestroyTextFile(&fragmentSource); // ToDo: glGenVertexArrays(1, &g_vao); // ToDo: glBindVertexArray(g_vao); glusCreateCubef(&cube, 0.5f); numberIndices = cube.numberIndices; // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_projectionLocation = glGetUniformLocation(g_program.program, "projectionMatrix"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_modelViewLocation = glGetUniformLocation(g_program.program, "modelViewMatrix"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_textureLocation = glGetUniformLocation(g_program.program, "firstTexture"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetAttribLocation.xml g_vertexLocation = glGetAttribLocation(g_program.program, "vertex"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetAttribLocation.xml g_normalLocation = glGetAttribLocation(g_program.program, "normal"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetAttribLocation.xml g_texCoordLocation = glGetAttribLocation(g_program.program, "texCoord"); // ToDo: glBindFragDataLocation(g_program.program, 0, "fragColor"); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_vertices); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_vertices); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ARRAY_BUFFER, cube.numberVertices*4*sizeof(GLfloat), (GLfloat*)cube.vertices, GL_STATIC_DRAW); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_normals); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_normals); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ARRAY_BUFFER, cube.numberVertices*3*sizeof(GLfloat), (GLfloat*)cube.normals, GL_STATIC_DRAW); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_texCoords); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_texCoords); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ARRAY_BUFFER, cube.numberVertices*2*sizeof(GLfloat), (GLfloat*)cube.texCoords, GL_STATIC_DRAW); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_indices); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indices); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ELEMENT_ARRAY_BUFFER, cube.numberIndices*sizeof(GLuint), (GLuint*)cube.indices, GL_STATIC_DRAW); glusDestroyShapef(&cube); glusLoadTgaImage("crate.tga", &image); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/gentextures.html glGenTextures(1, &g_texture); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/bindtexture.html glBindTexture(GL_TEXTURE_2D, g_texture); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html glTexImage2D(GL_TEXTURE_2D, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // http://www.opengl.org/sdk/docs/man/xhtml/glUseProgram.xml glUseProgram(g_program.program); // Calculate the model matrix ... glusLoadIdentityf(model); glusRotateRzRyRxf(model, 30.0f, 30.0f, 0.0f); // ... and the view matrix ... glusLookAtf(g_modelView, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // ... to get the final model view matrix glusMultMatrixf(g_modelView, g_modelView, model); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniformMatrix4fv(g_modelViewLocation, 1, GL_FALSE, g_modelView); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_vertices); // http://www.opengl.org/sdk/docs/man/xhtml/glVertexAttribPointer.xml glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); // http://www.opengl.org/sdk/docs/man/xhtml/glEnableVertexAttribArray.xml glEnableVertexAttribArray(g_vertexLocation); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_normals); // http://www.opengl.org/sdk/docs/man/xhtml/glVertexAttribPointer.xml glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); // http://www.opengl.org/sdk/docs/man/xhtml/glEnableVertexAttribArray.xml glEnableVertexAttribArray(g_normalLocation); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_texCoords); // http://www.opengl.org/sdk/docs/man/xhtml/glVertexAttribPointer.xml glVertexAttribPointer(g_texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); // http://www.opengl.org/sdk/docs/man/xhtml/glEnableVertexAttribArray.xml glEnableVertexAttribArray(g_texCoordLocation); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniform1i(g_textureLocation, 0); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/clearcolor.html glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/cleardepth.html glClearDepth(1.0f); //http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/enable.html glEnable(GL_DEPTH_TEST); //http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/enable.html glEnable(GL_CULL_FACE); return GLUS_TRUE; }