Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pgr.h" // includes all PGR libraries, like shader, glm, assimp ...
- #include "PyramidNode.h"
- GLuint PyramidNode::m_vertexArrayObject = 0;
- GLuint PyramidNode::m_vertexBufferObject = 0;
- GLuint PyramidNode::m_program = 0;
- GLint PyramidNode::m_PVMmatrixLoc = -1;
- GLint PyramidNode::m_posLoc = -1;
- GLint PyramidNode::m_colLoc = -1;
- GLint PyramidNode::m_norLoc = -1;
- // For all tasks - multiple colors and matrix multiplication
- /**
- static const std::string strVertexShader(
- "#version 130\n"
- "uniform mat4 PVMmatrix;\n"
- "in vec4 position;\n"
- "in vec4 color;\n"
- "smooth out vec4 theColor;\n"
- "void main()\n"
- "{\n"
- " gl_Position = PVMmatrix * position;\n"
- " theColor = color;\n"
- "}\n"
- );
- static const std::string strFragmentShader(
- "#version 130\n"
- "smooth in vec4 theColor;\n"
- "out vec4 outputColor;\n"
- "void main()\n"
- "{\n"
- " outputColor = theColor;\n"
- "}\n"
- );
- */
- static const float vertexData[] = {
- // vertices for the base of the pyramid
- -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- // vertices for the sides of the pyramid
- 0.3f, -0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- //1
- -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- -0.3f, -0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- 0.3f, -0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- 0.3f, -0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- //3
- 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- 0.3f, -0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- 0.3f, 0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- //4
- -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- 0.3f, 0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- //5
- -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- -0.3f, 0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- //6
- -0.3f, -0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- -0.3f, 0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- //7
- -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
- 0.3f, 0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- -0.3f, 0.3f, -1.0f, 1.0f, 1.0f, 1.0f,
- //8
- };
- PyramidNode::PyramidNode(const char * name, SceneNode * parent):
- SceneNode(name, parent)
- {
- if(m_program == 0)
- {
- std::vector<GLuint> shaderList;
- // Push vertex shader and fragment shader
- //shaderList.push_back(pgr::createShader(GL_VERTEX_SHADER, strVertexShader ));
- //shaderList.push_back(pgr::createShader(GL_FRAGMENT_SHADER, strFragmentShader));
- shaderList.push_back(pgr::createShader(GL_VERTEX_SHADER, "MeshNode.vp"));
- shaderList.push_back(pgr::createShader(GL_FRAGMENT_SHADER, "MeshNode.fp"));
- // Create the program with two shaders
- m_program = pgr::createProgram(shaderList);
- m_PVMmatrixLoc = glGetUniformLocation( m_program, "PVMmatrix");
- m_posLoc = glGetAttribLocation( m_program, "position");
- m_colLoc = glGetAttribLocation( m_program, "color");
- m_norLoc = glGetAttribLocation( m_program, "normal");
- }
- if(m_vertexArrayObject == 0)
- {
- glGenBuffers(1, &m_vertexBufferObject);
- glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject);
- ///this buffers vertexData
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glGenVertexArrays(1, &m_vertexArrayObject );
- glBindVertexArray( m_vertexArrayObject );
- glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject);
- // vertices of triangles
- glEnableVertexAttribArray(m_posLoc);
- glVertexAttribPointer(m_posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
- // 8 = 4 + 4 floats per vertex - color
- //glEnableVertexAttribArray(m_colLoc);
- glVertexAttribPointer(m_norLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
- //glVertexAttribPointer(m_colLoc, 6, GL_FLOAT, GL_FALSE, 0, (void*)(6*sizeof(vertexData)/(12)));
- glBindVertexArray( 0 );
- }
- }
- void PyramidNode::draw(const glm::mat4 & view_matrix, const glm::mat4 & projection_matrix)
- {
- // inherited draw - draws all children
- SceneNode::draw(view_matrix, projection_matrix);
- glm::mat4 matrix = projection_matrix * view_matrix * globalMatrix();
- glUseProgram(m_program);
- glUniformMatrix4fv(m_PVMmatrixLoc, 1, GL_FALSE, glm::value_ptr(matrix) );
- glBindVertexArray( m_vertexArrayObject );
- glDrawArrays(GL_TRIANGLES, 0, sizeof(vertexData)/(3*sizeof(float)));
- glBindVertexArray( 0 );
- }
- void PyramidNode::drawPick(const glm::mat4 & view_matrix, const glm::mat4 & projection_matrix)
- {
- PyramidNode::draw(view_matrix, projection_matrix);
- glm::mat4 matrix = projection_matrix * view_matrix * globalMatrix();
- glUseProgram(m_program);
- glUniform1i(glGetUniformLocation(m_program, "pick_id"), 42);
- glUniformMatrix4fv(m_PVMmatrixLoc, 1, GL_FALSE, glm::value_ptr(matrix) );
- glBindVertexArray( m_vertexArrayObject );
- glDrawArrays(GL_LINES, 0, 6);
- glBindVertexArray( 0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment