vec3 calulateNormal(vec3 p0, vec3 p1, vec3 p2) { vec3 v1; vec3 v2; v1.x = (p1.x - p0.x); v1.y = (p1.y - p0.y); v1.z = (p1.z - p0.z); v2.x = (p2.x - p0.x); v2.y = (p2.y - p0.y); v2.z = (p2.z - p0.z); return makeNormal(v1, v2); }
void init (void) { // make normals from vertices shared surface normals // bottom normals flipped "up" for top view, as there is no bottom view. makeNormal( 0, glm::normalize(glm::vec3(-0.00, 0.30, 0.95) + glm::vec3( 0.00, -1.00, 0.00) + glm::vec3(-0.95, 0.30, 0.00))); // front left bottom makeNormal( 3, glm::normalize(glm::vec3(-0.00, 0.30, 0.95) + glm::vec3( 0.00, -1.00, 0.00) + glm::vec3( 0.00, -1.00, 0.00) + glm::vec3( 0.95, 0.30, 0.00))); // front right bottom makeNormal( 6, glm::normalize(glm::vec3( 0.00, 0.30, -0.95) + glm::vec3( 0.00, -1.00, 0.00) + glm::vec3( 0.95, 0.30, 0.00))); // back right bottom makeNormal( 9, glm::normalize(glm::vec3( 0.00, 0.30, -0.95) + glm::vec3( 0.00, -1.00, 0.00) + glm::vec3( 0.00, -1.00, 0.00) + glm::vec3(-0.95, 0.30, 0.00))); // back left bottom makeNormal(12, glm::normalize(glm::vec3(-0.00, 0.30, 0.95) + glm::vec3( 0.00, 0.30, -0.95) + glm::vec3( 0.95, 0.30, 0.00) + glm::vec3(-0.95, 0.30, 0.00))); // apex shaderProgram = loadShaders(vertexShaderFile,fragmentShaderFile); glUseProgram(shaderProgram); // set up the indices buffer glGenBuffers(1, &ibo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); // set up the vertex attributes glGenVertexArrays( 1, &vao ); glBindVertexArray( vao ); // initialize a buffer object glGenBuffers( 1, &vbo ); // glEnableVertexAttribArray(vbo); glBindBuffer( GL_ARRAY_BUFFER, vbo ); glBufferData( GL_ARRAY_BUFFER, sizeof(point) + sizeof(texCoords) + sizeof(normal), NULL, GL_STATIC_DRAW ); glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(point), point ); glBufferSubData( GL_ARRAY_BUFFER, sizeof(point), sizeof(texCoords), texCoords); glBufferSubData( GL_ARRAY_BUFFER, sizeof(point) + sizeof(texCoords), sizeof(normal), normal ); // set up vertex arrays (after shaders are loaded) vPosition = glGetAttribLocation( shaderProgram, "vPosition" ); glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); glEnableVertexAttribArray( vPosition ); vTexCoord = glGetAttribLocation( shaderProgram, "vTexCoord" ); glVertexAttribPointer( vTexCoord, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(point)) ); glEnableVertexAttribArray( vTexCoord ); vNormal = glGetAttribLocation( shaderProgram, "vNormal" ); glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(point) + sizeof(texCoords)) ); glEnableVertexAttribArray( vNormal ); NormalMatrix = glGetUniformLocation(shaderProgram, "NormalMatrix"); MVP = glGetUniformLocation(shaderProgram, "MVP"); // load texture texture = loadRawTexture( texture, fileName, width, height); if (texture != 0) { printf("texture file, read, texture generated and bound \n"); Texture = glGetUniformLocation(shaderProgram, "Texture"); } else // texture file loaded printf("Texture in file %s NOT LOADED !!! \n"); // If you want to see what these values are... printf("vbo %3d ibo %3d\n", vbo, ibo); printf("locations: vPosition %3d, vTexCoord %3d, vNormal %3d, NormalMatrix %3d, MVP %3d, Texture %3d \n", vPosition, vTexCoord, vNormal, NormalMatrix, MVP, Texture); rotationMatrix = glm::mat4(); // initially use a front view eye = glm::vec3(300.0f, 300.0f, 300.0f); // eye is 300 "out of screen" from origin at = glm::vec3( 0.0f, 0.0f, 0.0f); // looking at origin up = glm::vec3( 0.0f, 1.0f, 0.0f); // camera'a up vector // set render state values glCullFace(GL_BACK); // show only front faces glEnable(GL_DEPTH_TEST); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); }