GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0; // Light start direction, which will be animated for seeing the effect. GLfloat lightDirection[3] = { 1.0, 1.0f, 1.0f }; // Increase the angle depending on the time. angle += time; // Let the light move from right to left and vice versa. lightDirection[0] = 2.0f * cosf(angle); // Need to normalize the light given in world space. glusVector3Normalizef(lightDirection); // Bring the light to camera space. glusMatrix4x4MultiplyVector3f(lightDirection, g_viewMatrix, lightDirection); glUniform3fv(g_lightDirectionLocation, 1, (GLfloat*) lightDirection); glClear(GL_COLOR_BUFFER_BIT); glDrawElements(GL_TRIANGLES, g_numberIndicesPlane, GL_UNSIGNED_INT, 0); return GLUS_TRUE; }
GLUSboolean updateWavefront(GLUSfloat time, GLUSfloat modelMatrix[16]) { GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; GLfloat lightDirection[3]; // Note that the scale matrix is for flipping the model upside down. glusMatrix4x4Identityf(modelViewMatrix); glusMatrix4x4Multiplyf(modelViewMatrix, modelViewMatrix, modelMatrix); glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); // Transform light to camera space, as it is currently in world space. glusMatrix4x4MultiplyVector3f(lightDirection, g_viewMatrix, g_light->direction); glUseProgram(g_program.program); glUniform3fv(g_lightLocations.directionLocation, 1, lightDirection); glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glBindVertexArray(g_vao); glDrawArrays(GL_TRIANGLES, 0, g_numberVertices); return GLUS_TRUE; }
Vector3 Matrix4x4::operator*(const Vector3& v) const { Vector3 result; glusMatrix4x4MultiplyVector3f(result.v, m, v.v); return result; }
GLUSvoid reshape(GLUSint width, GLUSint height) { GLfloat modelMatrix[16]; GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; GLfloat lightDirection[3]; reshapeWavefront(width, height); // glViewport(0, 0, width, height); glusPerspectivef(g_projectionMatrix, 40.0f, (GLfloat)width / (GLfloat)height, 1.0f, 100.0f); // Calculate the inverse. Needed for the SSAO shader to get from projection to view space. glusMatrix4x4Copyf(g_inverseProjectionMatrix, g_projectionMatrix, GLUS_FALSE); glusMatrix4x4Inversef(g_inverseProjectionMatrix); glUseProgram(g_ssaoProgram.program); glUniformMatrix4fv(g_ssaoInverseProjectionMatrixLocation, 1, GL_FALSE, g_inverseProjectionMatrix); glUniformMatrix4fv(g_ssaoProjectionMatrixLocation, 1, GL_FALSE, g_projectionMatrix); // glusMatrix4x4Multiplyf(g_viewProjectionMatrix, g_projectionMatrix, g_viewMatrix); glUseProgram(g_program.program); glUniformMatrix4fv(g_viewProjectionMatrixLocation, 1, GL_FALSE, g_viewProjectionMatrix); glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRxf(modelMatrix, -90.0f); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); // Calculation is in camera space glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glusMatrix4x4MultiplyVector3f(lightDirection, g_viewMatrix, g_light.direction); // Direction already normalized glUniform3fv(g_lightDirectionLocation, 1, lightDirection); }
GLUSvoid reshape(GLUSint width, GLUSint height) { GLfloat modelMatrix[16]; GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; GLfloat lightDirection[3]; g_width = width; g_height = height; reshapeWavefront(width, height); // glViewport(0, 0, width, height); glusPerspectivef(g_viewProjectionMatrix, 40.0f, (GLfloat) width / (GLfloat) height, 1.0f, 100.0f); glusMatrix4x4Multiplyf(g_viewProjectionMatrix, g_viewProjectionMatrix, g_viewMatrix); glUseProgram(g_program.program); glUniformMatrix4fv(g_viewProjectionMatrixLocation, 1, GL_FALSE, g_viewProjectionMatrix); glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4Translatef(modelMatrix, 0.0f, 0.0f, 3.0f); glusMatrix4x4RotateRxf(modelMatrix, -90.0f); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); // Calculation is in camera space glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glusMatrix4x4MultiplyVector3f(lightDirection, g_viewMatrix, g_light.direction); // Direction already normalized glUniform3fv(g_lightDirectionLocation, 1, lightDirection); }
GLUSboolean init(GLUSvoid) { // This is a white light. struct LightProperties light = { { 1.0f, 1.0f, 1.0f }, { 0.3f, 0.3f, 0.3f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }; // Silver color material with white specular color. struct MaterialProperties material = { { 0.75f, 0.75f, 0.75f, 1.0f }, { 0.75f, 0.75f, 0.75f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, 20.0f }; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLUSshape sphere; GLUStgaimage image; glusFileLoadText("../Example24/shader/erode.vert.glsl", &vertexSource); glusFileLoadText("../Example24/shader/erode.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // // Create perlin noise. The brightness of the texels represents the time to erode. // The brighter the texel, the earlier the pixel erodes. glusPerlinCreateNoise3D(&image, 64, 64, 64, 0, 8.0f, 192.0f, 0.5f, 4); // Generate and bind a texture. glGenTextures(1, &g_texture); glBindTexture(GL_TEXTURE_3D, g_texture); // Transfer the image data from the CPU to the GPU. glTexImage3D(GL_TEXTURE_3D, 0, image.format, image.width, image.height, image.depth, 0, image.format, GL_UNSIGNED_BYTE, image.data); // Setting the texture parameters. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT); glusImageDestroyTga(&image); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_modelViewMatrixLocation = glGetUniformLocation(g_program.program, "u_modelViewMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_light.directionLocation = glGetUniformLocation(g_program.program, "u_light.direction"); g_light.ambientColorLocation = glGetUniformLocation(g_program.program, "u_light.ambientColor"); g_light.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_light.diffuseColor"); g_light.specularColorLocation = glGetUniformLocation(g_program.program, "u_light.specularColor"); g_material.ambientColorLocation = glGetUniformLocation(g_program.program, "u_material.ambientColor"); g_material.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_material.diffuseColor"); g_material.specularColorLocation = glGetUniformLocation(g_program.program, "u_material.specularColor"); g_material.specularExponentLocation = glGetUniformLocation(g_program.program, "u_material.specularExponent"); g_textureLocation = glGetUniformLocation(g_program.program, "u_texture"); g_erodeLocation = glGetUniformLocation(g_program.program, "u_erode"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); // // Use a helper function to create a sphere. glusShapeCreateSpheref(&sphere, 1.0f, 64); g_numberIndicesSphere = sphere.numberIndices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, sphere.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) sphere.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, sphere.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) sphere.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sphere.numberIndices * sizeof(GLuint), (GLuint*) sphere.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusShapeDestroyf(&sphere); // glUseProgram(g_program.program); glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); // glusVector3Normalizef(light.direction); glusMatrix4x4LookAtf(g_viewMatrix, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // Transform light to camera space, as it is currently in world space. glusMatrix4x4MultiplyVector3f(light.direction, g_viewMatrix, light.direction); // Set up light ... glUniform3fv(g_light.directionLocation, 1, light.direction); glUniform4fv(g_light.ambientColorLocation, 1, light.ambientColor); glUniform4fv(g_light.diffuseColorLocation, 1, light.diffuseColor); glUniform4fv(g_light.specularColorLocation, 1, light.specularColor); // ... and material values. glUniform4fv(g_material.ambientColorLocation, 1, material.ambientColor); glUniform4fv(g_material.diffuseColorLocation, 1, material.diffuseColor); glUniform4fv(g_material.specularColorLocation, 1, material.specularColor); glUniform1f(g_material.specularExponentLocation, material.specularExponent); // glUniform1i(g_textureLocation, 0); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); // No back face culling by purpose. This allows to look through the eroded sphere. return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { // This is a white light. struct LightProperties light = { { 1.0f, 1.0f, 1.0f }, { 0.3f, 0.3f, 0.3f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }; // Green color material with white specular color, half transparent. struct MaterialProperties material = { { 0.0f, 1.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, 20.0f, 0.5f }; // Buffer for cleaning the head index testure. static GLuint clearBuffer[SCREEN_WIDTH * SCREEN_HEIGHT]; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLUSshape wavefrontObj; GLuint i; for (i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) { // 0xffffffff means end of list, so for the start tehre is no entry. clearBuffer[i] = 0xffffffff; } // glusFileLoadText("../Example36/shader/phong_linked_list.vert.glsl", &vertexSource); glusFileLoadText("../Example36/shader/phong_linked_list.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_modelViewMatrixLocation = glGetUniformLocation(g_program.program, "u_modelViewMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_light.directionLocation = glGetUniformLocation(g_program.program, "u_light.direction"); g_light.ambientColorLocation = glGetUniformLocation(g_program.program, "u_light.ambientColor"); g_light.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_light.diffuseColor"); g_light.specularColorLocation = glGetUniformLocation(g_program.program, "u_light.specularColor"); g_material.ambientColorLocation = glGetUniformLocation(g_program.program, "u_material.ambientColor"); g_material.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_material.diffuseColor"); g_material.specularColorLocation = glGetUniformLocation(g_program.program, "u_material.specularColor"); g_material.specularExponentLocation = glGetUniformLocation(g_program.program, "u_material.specularExponent"); g_material.alphaLocation = glGetUniformLocation(g_program.program, "u_material.alpha"); g_maxNodesLocation = glGetUniformLocation(g_program.program, "u_maxNodes"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); // glusFileLoadText("../Example36/shader/fullscreen_blend.vert.glsl", &vertexSource); glusFileLoadText("../Example36/shader/fullscreen_blend.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_blendFullscreenProgram, (const GLchar**)&vertexSource.text, 0, 0, 0, (const GLchar**)&fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // Atomic counter to gather a free node slot concurrently. glGenBuffers(1, &g_freeNodeIndex); glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, BINDING_ATOMIC_FREE_INDEX, g_freeNodeIndex); glBufferData(GL_ATOMIC_COUNTER_BUFFER, sizeof(GLuint), 0, GL_DYNAMIC_DRAW); // Head index texture/image, which contains the glGenTextures(1, &g_headIndexTexture); glBindTexture(GL_TEXTURE_2D, g_headIndexTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, SCREEN_WIDTH, SCREEN_HEIGHT, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); glBindImageTexture(BINDING_IMAGE_HEAD_INDEX, g_headIndexTexture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32UI); // Buffer to clear/reset the head pointers. glGenBuffers(1, &g_clearBuffer); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, g_clearBuffer); glBufferData(GL_PIXEL_UNPACK_BUFFER, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(GLuint), clearBuffer, GL_STATIC_COPY); // Buffer for the linked list. glGenBuffers(1, &g_linkedListBuffer); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, BINDING_BUFFER_LINKED_LIST, g_linkedListBuffer); // Size is RGBA, depth (5 * GLfloat), next pointer (1 * GLuint) and 2 paddings (2 * GLfloat). glBufferData(GL_SHADER_STORAGE_BUFFER, MAX_NODES * (sizeof(GLfloat) * 5 + sizeof(GLuint) * 1) + sizeof(GLfloat) * 2, 0, GL_DYNAMIC_DRAW); // // Use a helper function to load an wavefront object file. glusShapeLoadWavefront("dragon.obj", &wavefrontObj); g_numberVertices = wavefrontObj.numberVertices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, wavefrontObj.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) wavefrontObj.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, wavefrontObj.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) wavefrontObj.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glusShapeDestroyf(&wavefrontObj); // glUseProgram(g_blendFullscreenProgram.program); glGenVertexArrays(1, &g_blendFullscreenVAO); glBindVertexArray(g_blendFullscreenVAO); glBindVertexArray(0); // glUseProgram(g_program.program); glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindVertexArray(0); // glusMatrix4x4LookAtf(g_viewMatrix, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // glusVector3Normalizef(light.direction); // Transform light to camera space, as it is currently in world space. glusMatrix4x4MultiplyVector3f(light.direction, g_viewMatrix, light.direction); // Set up light ... glUniform3fv(g_light.directionLocation, 1, light.direction); glUniform4fv(g_light.ambientColorLocation, 1, light.ambientColor); glUniform4fv(g_light.diffuseColorLocation, 1, light.diffuseColor); glUniform4fv(g_light.specularColorLocation, 1, light.specularColor); // ... and material values. glUniform4fv(g_material.ambientColorLocation, 1, material.ambientColor); glUniform4fv(g_material.diffuseColorLocation, 1, material.diffuseColor); glUniform4fv(g_material.specularColorLocation, 1, material.specularColor); glUniform1f(g_material.specularExponentLocation, material.specularExponent); glUniform1f(g_material.alphaLocation, material.alpha); glUniform1ui(g_maxNodesLocation, MAX_NODES); // glDisable(GL_DEPTH_TEST); return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { // This is a white light. struct LightProperties light = { { 1.0f, 1.0f, 1.0f }, { 0.3f, 0.3f, 0.3f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }; GLUStextfile vertexSource; GLUStextfile geometrySource; GLUStextfile fragmentSource; GLUStgaimage image; GLUSshape bunnyShape; // glusFileLoadText("../Example26/shader/ambient_diffuse_texture.vert.glsl", &vertexSource); glusFileLoadText("../Example26/shader/ambient_diffuse_texture.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); glusFileLoadText("../Example26/shader/fur.vert.glsl", &vertexSource); glusFileLoadText("../Example26/shader/fur.geom.glsl", &geometrySource); glusFileLoadText("../Example26/shader/fur.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_programFur, (const GLUSchar**) &vertexSource.text, 0, 0, (const GLUSchar**) &geometrySource.text, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&geometrySource); glusFileDestroyText(&fragmentSource); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_modelViewMatrixLocation = glGetUniformLocation(g_program.program, "u_modelViewMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_light.directionLocation = glGetUniformLocation(g_program.program, "u_light.direction"); g_light.ambientColorLocation = glGetUniformLocation(g_program.program, "u_light.ambientColor"); g_light.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_light.diffuseColor"); g_light.specularColorLocation = glGetUniformLocation(g_program.program, "u_light.specularColor"); g_textureLocation = glGetUniformLocation(g_program.program, "u_textureFurColor"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); g_texCoordLocation = glGetAttribLocation(g_program.program, "a_texCoord"); g_projectionMatrixFurLocation = glGetUniformLocation(g_programFur.program, "u_projectionMatrix"); g_modelViewMatrixFurLocation = glGetUniformLocation(g_programFur.program, "u_modelViewMatrix"); g_normalMatrixFurLocation = glGetUniformLocation(g_programFur.program, "u_normalMatrix"); g_lightDirectionFurLocation = glGetUniformLocation(g_programFur.program, "u_lightDirection"); g_textureFurColorLocation = glGetUniformLocation(g_programFur.program, "u_textureFurColor"); g_textureFurStrengthLocation = glGetUniformLocation(g_programFur.program, "u_textureFurStrength"); g_vertexFurLocation = glGetAttribLocation(g_programFur.program, "a_vertex"); g_normalFurLocation = glGetAttribLocation(g_programFur.program, "a_normal"); g_texCoordFurLocation = glGetAttribLocation(g_programFur.program, "a_texCoord"); // // Color texture set up. glusImageLoadTga("tiger.tga", &image); glGenTextures(1, &g_textureFurColor); glBindTexture(GL_TEXTURE_2D, g_textureFurColor); glTexImage2D(GL_TEXTURE_2D, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); // Mipmap generation is now included in OpenGL 3 and above glGenerateMipmap(GL_TEXTURE_2D); // Trilinear filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, 0); glusImageDestroyTga(&image); // A simple noise texture influences the strength of each fur pixel. glusPerlinCreateNoise2D(&image, 64, 64, 0, 100.0f, 255.0f, 0.5f, 1); glGenTextures(1, &g_textureFurStrength); glBindTexture(GL_TEXTURE_2D, g_textureFurStrength); glTexImage2D(GL_TEXTURE_2D, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); // Mipmap generation is now included in OpenGL 3 and above glGenerateMipmap(GL_TEXTURE_2D); // Trilinear filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, 0); glusImageDestroyTga(&image); // // Use a helper function to load an wavefront object file. glusShapeLoadWavefront("bunny.obj", &bunnyShape); // This model does not have any texture coordinates, so generate them. glusShapeTexGenByAxesf(&bunnyShape, 2.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f); g_numberVertices = bunnyShape.numberVertices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, bunnyShape.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) bunnyShape.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, bunnyShape.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) bunnyShape.normals, GL_STATIC_DRAW); glGenBuffers(1, &g_texCoordsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_texCoordsVBO); glBufferData(GL_ARRAY_BUFFER, bunnyShape.numberVertices * 2 * sizeof(GLfloat), (GLfloat*) bunnyShape.texCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glusShapeDestroyf(&bunnyShape); // glUseProgram(g_program.program); glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ARRAY_BUFFER, g_texCoordsVBO); glVertexAttribPointer(g_texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_texCoordLocation); glUseProgram(g_programFur.program); glGenVertexArrays(1, &g_vaoFur); glBindVertexArray(g_vaoFur); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ARRAY_BUFFER, g_texCoordsVBO); glVertexAttribPointer(g_texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_texCoordLocation); // glusMatrix4x4LookAtf(g_viewMatrix, 0.0f, 0.75f, 3.0f, 0.0f, 0.75f, 0.0f, 0.0f, 1.0f, 0.0f); // glusVector3Normalizef(light.direction); // Transform light to camera space, as it is currently in world space. glusMatrix4x4MultiplyVector3f(light.direction, g_viewMatrix, light.direction); glUseProgram(g_program.program); // Set up light ... glUniform3fv(g_light.directionLocation, 1, light.direction); glUniform4fv(g_light.ambientColorLocation, 1, light.ambientColor); glUniform4fv(g_light.diffuseColorLocation, 1, light.diffuseColor); glUniform4fv(g_light.specularColorLocation, 1, light.specularColor); // ... and the texture. glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, g_textureFurColor); glUniform1i(g_textureLocation, 0); glUseProgram(g_programFur.program); glUniform3fv(g_lightDirectionFurLocation, 1, light.direction); // Fur color and position textures. glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, g_textureFurColor); glUniform1i(g_textureFurColorLocation, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, g_textureFurStrength); glUniform1i(g_textureFurStrengthLocation, 1); // glClearColor(0.8f, 0.8f, 0.8f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { // This is a white light. struct LightProperties light = { { 1.0f, 1.0f, 1.0f }, { 0.3f, 0.3f, 0.3f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }; // Green color material with white specular color, half transparent. struct MaterialProperties material = { { 0.0f, 1.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, 20.0f, 0.5f }; static GLfloat biasMatrix[] = { 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f }; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLUSshape wavefrontObj; glusFileLoadText("shader/phong_depth_peel.vert.glsl", &vertexSource); glusFileLoadText("shader/phong_depth_peel.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // g_biasMatrixLocation = glGetUniformLocation(g_program.program, "u_biasMatrix"); g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_modelViewMatrixLocation = glGetUniformLocation(g_program.program, "u_modelViewMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_light.directionLocation = glGetUniformLocation(g_program.program, "u_light.direction"); g_light.ambientColorLocation = glGetUniformLocation(g_program.program, "u_light.ambientColor"); g_light.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_light.diffuseColor"); g_light.specularColorLocation = glGetUniformLocation(g_program.program, "u_light.specularColor"); g_material.ambientColorLocation = glGetUniformLocation(g_program.program, "u_material.ambientColor"); g_material.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_material.diffuseColor"); g_material.specularColorLocation = glGetUniformLocation(g_program.program, "u_material.specularColor"); g_material.specularExponentLocation = glGetUniformLocation(g_program.program, "u_material.specularExponent"); g_material.alphaLocation = glGetUniformLocation(g_program.program, "u_material.alpha"); g_peelTextureLocation = glGetUniformLocation(g_program.program, "u_peelTexture"); g_layerLocation = glGetUniformLocation(g_program.program, "u_layer"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); // glusFileLoadText("shader/fullscreen_blend.vert.glsl", &vertexSource); glusFileLoadText("shader/fullscreen_blend.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_blendFullscreenProgram, (const GLchar**)&vertexSource.text, 0, 0, 0, (const GLchar**)&fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // g_framebufferTextureBlendFullscreenLocation = glGetUniformLocation(g_blendFullscreenProgram.program, "u_framebufferTexture"); g_layersBlendFullscreenLocation = glGetUniformLocation(g_blendFullscreenProgram.program, "u_layers"); // // Use a helper function to load an wavefront object file. glusShapeLoadWavefront("../../media/dragon.obj", &wavefrontObj); g_numberVertices = wavefrontObj.numberVertices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, wavefrontObj.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) wavefrontObj.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, wavefrontObj.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) wavefrontObj.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glusShapeDestroyf(&wavefrontObj); // // // Setting up the frame buffer. // glGenTextures(1, &g_colorTexture); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_ARRAY, g_colorTexture); glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, SCREEN_WIDTH, SCREEN_HEIGHT, LAYERS, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D_ARRAY, 0); // glGenTextures(2, g_depthTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, g_depthTexture[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, SCREEN_WIDTH, SCREEN_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); // Peel depth test "function". See shader for more information. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GREATER); glBindTexture(GL_TEXTURE_2D, g_depthTexture[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, SCREEN_WIDTH, SCREEN_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); // Peel depth test "function". See shader for more information. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GREATER); glBindTexture(GL_TEXTURE_2D, 0); // glGenFramebuffers(1, &g_blendFullscreenFBO); glBindFramebuffer(GL_FRAMEBUFFER, g_blendFullscreenFBO); // Attach the color buffer ... glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, g_colorTexture, 0, 0); // ... and the depth buffer, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, g_depthTexture[1], 0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { printf("GL_FRAMEBUFFER_COMPLETE error 0x%x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); return GLUS_FALSE; } glBindFramebuffer(GL_FRAMEBUFFER, 0); // glUseProgram(g_blendFullscreenProgram.program); glGenVertexArrays(1, &g_blendFullscreenVAO); glBindVertexArray(g_blendFullscreenVAO); glUniform1i(g_framebufferTextureBlendFullscreenLocation, 0); glUniform1i(g_layersBlendFullscreenLocation, LAYERS); glBindVertexArray(0); // glUseProgram(g_program.program); glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindVertexArray(0); // glusMatrix4x4LookAtf(g_viewMatrix, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // glusVector3Normalizef(light.direction); // Transform light to camera space, as it is currently in world space. glusMatrix4x4MultiplyVector3f(light.direction, g_viewMatrix, light.direction); // Set up light ... glUniform3fv(g_light.directionLocation, 1, light.direction); glUniform4fv(g_light.ambientColorLocation, 1, light.ambientColor); glUniform4fv(g_light.diffuseColorLocation, 1, light.diffuseColor); glUniform4fv(g_light.specularColorLocation, 1, light.specularColor); // ... and material values. glUniform4fv(g_material.ambientColorLocation, 1, material.ambientColor); glUniform4fv(g_material.diffuseColorLocation, 1, material.diffuseColor); glUniform4fv(g_material.specularColorLocation, 1, material.specularColor); glUniform1f(g_material.specularExponentLocation, material.specularExponent); glUniform1f(g_material.alphaLocation, material.alpha); glUniform1i(g_peelTextureLocation, 1); glUniformMatrix4fv(g_biasMatrixLocation, 1, GL_FALSE, biasMatrix); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { // This is a white light. struct LightProperties light = { { 1.0f, 1.0f, 1.0f }, { 0.3f, 0.3f, 0.3f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }; // Blue color material with white specular color. struct MaterialProperties material = { { 0.0f, 0.1f, 0.1f, 1.0f }, { 0.0f, 0.8f, 0.8f, 1.0f }, { 0.6f, 0.6f, 0.6f, 1.0f }, 60.0f }; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLUSshape sphere; GLUSshape plane; glusLoadTextFile("../Example18/shader/phong.vert.glsl", &vertexSource); glusLoadTextFile("../Example18/shader/phong.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_viewMatrixLocation = glGetUniformLocation(g_program.program, "u_viewMatrix"); g_modelMatrixLocation = glGetUniformLocation(g_program.program, "u_modelMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_light.directionLocation = glGetUniformLocation(g_program.program, "u_light.direction"); g_light.ambientColorLocation = glGetUniformLocation(g_program.program, "u_light.ambientColor"); g_light.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_light.diffuseColor"); g_light.specularColorLocation = glGetUniformLocation(g_program.program, "u_light.specularColor"); g_material.ambientColorLocation = glGetUniformLocation(g_program.program, "u_material.ambientColor"); g_material.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_material.diffuseColor"); g_material.specularColorLocation = glGetUniformLocation(g_program.program, "u_material.specularColor"); g_material.specularExponentLocation = glGetUniformLocation(g_program.program, "u_material.specularExponent"); g_planeLocation = glGetUniformLocation(g_program.program, "u_plane"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); // // Use a helper function to create a sphere. glusCreateSpheref(&sphere, 0.5f, 64); g_numberIndicesSphere = sphere.numberIndices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, sphere.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) sphere.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, sphere.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) sphere.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sphere.numberIndices * sizeof(GLuint), (GLuint*) sphere.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusDestroyShapef(&sphere); // glusCreatePlanef(&plane, 0.5f); g_numberIndicesPlane = plane.numberIndices; glGenBuffers(1, &g_verticesPlaneVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesPlaneVBO); glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) plane.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsPlaneVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsPlaneVBO); glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) plane.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesPlaneVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesPlaneVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, plane.numberIndices * sizeof(GLuint), (GLuint*) plane.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusDestroyShapef(&plane); // glUseProgram(g_program.program); glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); // glGenVertexArrays(1, &g_planeVAO); glBindVertexArray(g_planeVAO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesPlaneVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsPlaneVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesPlaneVBO); // glusLookAtf(g_viewMatrix, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, g_viewMatrix); // glusVector3Normalizef(light.direction); // Transform light to camera space, as it is currently in world space. glusMatrix4x4MultiplyVector3f(light.direction, g_viewMatrix, light.direction); // Set up light ... glUniform3fv(g_light.directionLocation, 1, light.direction); glUniform4fv(g_light.ambientColorLocation, 1, light.ambientColor); glUniform4fv(g_light.diffuseColorLocation, 1, light.diffuseColor); glUniform4fv(g_light.specularColorLocation, 1, light.specularColor); // ... and material values. glUniform4fv(g_material.ambientColorLocation, 1, material.ambientColor); glUniform4fv(g_material.diffuseColorLocation, 1, material.diffuseColor); glUniform4fv(g_material.specularColorLocation, 1, material.specularColor); glUniform1f(g_material.specularExponentLocation, material.specularExponent); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); // No back face culling, as we need it for the stencil algorithm. // Enable the stencil test - mainly needed for the plane. glEnable(GL_STENCIL_TEST); return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { GLUSshape shadowPlane; GLUSshape plane; GLUSshape torus, torusWithAdjacency; GLUStextfile vertexSource; GLUStextfile geometrySource; GLUStextfile fragmentSource; GLfloat viewMatrix[16]; GLfloat lightDirection[3]; lightDirection[0] = g_lightDirection[0]; lightDirection[1] = g_lightDirection[1]; lightDirection[2] = g_lightDirection[2]; glusVector3Normalizef(lightDirection); // glusLoadTextFile("../Example22/shader/color.vert.glsl", &vertexSource); glusLoadTextFile("../Example22/shader/color.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_viewMatrixLocation = glGetUniformLocation(g_program.program, "u_viewMatrix"); g_modelMatrixLocation = glGetUniformLocation(g_program.program, "u_modelMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_colorLocation = glGetUniformLocation(g_program.program, "u_shapeColor"); g_lightDirectionLocation = glGetUniformLocation(g_program.program, "u_lightDirection"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); // glusLoadTextFile("../Example22/shader/shadowvolume.vert.glsl", &vertexSource); glusLoadTextFile("../Example22/shader/shadowvolume.geom.glsl", &geometrySource); glusLoadTextFile("../Example22/shader/shadowvolume.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_programShadowVolume, (const GLUSchar**) &vertexSource.text, 0, 0, (const GLUSchar**) &geometrySource.text, (const GLUSchar**) &fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&geometrySource); glusDestroyTextFile(&fragmentSource); // g_projectionMatrixShadowVolumeLocation = glGetUniformLocation(g_programShadowVolume.program, "u_projectionMatrix"); g_viewMatrixShadowVolumeLocation = glGetUniformLocation(g_programShadowVolume.program, "u_viewMatrix"); g_modelMatrixShadowVolumeLocation = glGetUniformLocation(g_programShadowVolume.program, "u_modelMatrix"); g_lightDirectionShadowVolumeLocation = glGetUniformLocation(g_programShadowVolume.program, "u_lightDirection"); g_vertexShadowVolumeLocation = glGetAttribLocation(g_programShadowVolume.program, "a_vertex"); // glusLoadTextFile("../Example22/shader/shadow.vert.glsl", &vertexSource); glusLoadTextFile("../Example22/shader/shadow.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_programShadowPlane, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); // g_vertexShadowPlaneLocation = glGetAttribLocation(g_programShadowPlane.program, "a_vertex"); // glusCreateTorusf(&torus, 0.5f, 1.0f, 32, 32); glusCreateAdjacencyShapef(&torusWithAdjacency, &torus); glusDestroyShapef(&torus); g_numberIndices = torusWithAdjacency.numberIndices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, torusWithAdjacency.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) torusWithAdjacency.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, torusWithAdjacency.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) torusWithAdjacency.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, torusWithAdjacency.numberIndices * sizeof(GLuint), (GLuint*) torusWithAdjacency.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusDestroyShapef(&torusWithAdjacency); // glusCreatePlanef(&plane, 10.0f); g_numberIndicesPlane = plane.numberIndices; glGenBuffers(1, &g_verticesPlaneVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesPlaneVBO); glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) plane.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsPlaneVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsPlaneVBO); glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) plane.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesPlaneVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesPlaneVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, plane.numberIndices * sizeof(GLuint), (GLuint*) plane.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusDestroyShapef(&plane); // // The plane extends from -1.0 to 1.0 for both sides. So when rendering in NDC, the plane is always fullscreen. glusCreatePlanef(&shadowPlane, 1.0f); g_numberIndicesShadowPlane = shadowPlane.numberIndices; glGenBuffers(1, &g_verticesShadowPlaneVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesShadowPlaneVBO); glBufferData(GL_ARRAY_BUFFER, shadowPlane.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) shadowPlane.vertices, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesShadowPlaneVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesShadowPlaneVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, shadowPlane.numberIndices * sizeof(GLuint), (GLuint*) shadowPlane.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusDestroyShapef(&shadowPlane); // glusLookAtf(viewMatrix, g_cameraPosition[0], g_cameraPosition[1], g_cameraPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // Bring light from world to camera / view space glusMatrix4x4MultiplyVector3f(lightDirection, viewMatrix, lightDirection); glUseProgram(g_program.program); glUniform3fv(g_lightDirectionLocation, 1, lightDirection); // glUseProgram(g_programShadowVolume.program); glUniform3fv(g_lightDirectionShadowVolumeLocation, 1, lightDirection); // Torus glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); // Shadow Volume glGenVertexArrays(1, &g_vaoShadowVolume); glBindVertexArray(g_vaoShadowVolume); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexShadowVolumeLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexShadowVolumeLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); // Plane glGenVertexArrays(1, &g_vaoPlane); glBindVertexArray(g_vaoPlane); glBindBuffer(GL_ARRAY_BUFFER, g_verticesPlaneVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsPlaneVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesPlaneVBO); // Shadow Plane glGenVertexArrays(1, &g_vaoShadowPlane); glBindVertexArray(g_vaoShadowPlane); glBindBuffer(GL_ARRAY_BUFFER, g_verticesShadowPlaneVBO); glVertexAttribPointer(g_vertexShadowPlaneLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexShadowPlaneLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesShadowPlaneVBO); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glClearStencil(0); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glPolygonOffset(0.0f, 100.0f); return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { // This is a white light. struct LightProperties light = { { 1.0f, 1.0f, 1.0f }, { 0.3f, 0.3f, 0.3f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }; // Blue color material with white specular color. struct MaterialProperties material = { { 0.0f, 1.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, 20.0f }; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLUSshape wavefrontObj; glusFileLoadText("../Example16/shader/phong.vert.glsl", &vertexSource); glusFileLoadText("../Example16/shader/phong.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_modelViewMatrixLocation = glGetUniformLocation(g_program.program, "u_modelViewMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_light.directionLocation = glGetUniformLocation(g_program.program, "u_light.direction"); g_light.ambientColorLocation = glGetUniformLocation(g_program.program, "u_light.ambientColor"); g_light.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_light.diffuseColor"); g_light.specularColorLocation = glGetUniformLocation(g_program.program, "u_light.specularColor"); g_material.ambientColorLocation = glGetUniformLocation(g_program.program, "u_material.ambientColor"); g_material.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_material.diffuseColor"); g_material.specularColorLocation = glGetUniformLocation(g_program.program, "u_material.specularColor"); g_material.specularExponentLocation = glGetUniformLocation(g_program.program, "u_material.specularExponent"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); // // Use a helper function to load an wavefront object file. glusShapeLoadWavefront("monkey.obj", &wavefrontObj); g_numberVertices = wavefrontObj.numberVertices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, wavefrontObj.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) wavefrontObj.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, wavefrontObj.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) wavefrontObj.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glusShapeDestroyf(&wavefrontObj); // glUseProgram(g_program.program); glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); // glusMatrix4x4LookAtf(g_viewMatrix, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // glusVector3Normalizef(light.direction); // Transform light to camera space, as it is currently in world space. glusMatrix4x4MultiplyVector3f(light.direction, g_viewMatrix, light.direction); // Set up light ... glUniform3fv(g_light.directionLocation, 1, light.direction); glUniform4fv(g_light.ambientColorLocation, 1, light.ambientColor); glUniform4fv(g_light.diffuseColorLocation, 1, light.diffuseColor); glUniform4fv(g_light.specularColorLocation, 1, light.specularColor); // ... and material values. glUniform4fv(g_material.ambientColorLocation, 1, material.ambientColor); glUniform4fv(g_material.diffuseColorLocation, 1, material.diffuseColor); glUniform4fv(g_material.specularColorLocation, 1, material.specularColor); glUniform1f(g_material.specularExponentLocation, material.specularExponent); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { GLUSshape background; GLUSshape torus; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLfloat viewMatrix[16]; GLfloat lightDirection[3]; lightDirection[0] = g_lightDirection[0]; lightDirection[1] = g_lightDirection[1]; lightDirection[2] = g_lightDirection[2]; glusVector3Normalizef(lightDirection); // glusFileLoadText("../Example27/shader/shadow.vert.glsl", &vertexSource); glusFileLoadText("../Example27/shader/shadow.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_programShadow, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // glusFileLoadText("../Example27/shader/color.vert.glsl", &vertexSource); glusFileLoadText("../Example27/shader/color.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // g_projectionMatrixShadowLocation = glGetUniformLocation(g_programShadow.program, "u_projectionMatrix"); g_viewMatrixShadowLocation = glGetUniformLocation(g_programShadow.program, "u_viewMatrix"); g_shadowProjectionMatrixShadowLocation = glGetUniformLocation(g_programShadow.program, "u_shadowProjectionMatrix"); g_modelMatrixShadowLocation = glGetUniformLocation(g_programShadow.program, "u_modelMatrix"); g_vertexShadowLocation = glGetAttribLocation(g_programShadow.program, "a_vertex"); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_viewMatrixLocation = glGetUniformLocation(g_program.program, "u_viewMatrix"); g_modelMatrixLocation = glGetUniformLocation(g_program.program, "u_modelMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_colorLocation = glGetUniformLocation(g_program.program, "u_shapeColor"); g_lightDirectionLocation = glGetUniformLocation(g_program.program, "u_lightDirection"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); // glusShapeCreateTorusf(&torus, 0.5f, 1.0f, 32, 32); g_numberIndicesTorus = torus.numberIndices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, torus.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) torus.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, torus.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) torus.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, torus.numberIndices * sizeof(GLuint), (GLuint*) torus.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusShapeDestroyf(&torus); // glusShapeCreatePlanef(&background, 10.0f); g_numberIndicesBackground = background.numberIndices; glGenBuffers(1, &g_verticesBackgroundVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesBackgroundVBO); glBufferData(GL_ARRAY_BUFFER, background.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) background.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsBackgroundVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsBackgroundVBO); glBufferData(GL_ARRAY_BUFFER, background.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) background.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesBackgroundVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesBackgroundVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, background.numberIndices * sizeof(GLuint), (GLuint*) background.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusShapeDestroyf(&background); // glUseProgram(g_program.program); glusMatrix4x4LookAtf(viewMatrix, g_cameraPosition[0], g_cameraPosition[1], g_cameraPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glusMatrix4x4MultiplyVector3f(lightDirection, viewMatrix, lightDirection); glUniform3fv(g_lightDirectionLocation, 1, lightDirection); // Torus glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); // Plane glGenVertexArrays(1, &g_vaoBackground); glBindVertexArray(g_vaoBackground); glBindBuffer(GL_ARRAY_BUFFER, g_verticesBackgroundVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsBackgroundVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesBackgroundVBO); // glUseProgram(g_programShadow.program); // Torus glGenVertexArrays(1, &g_vaoShadow); glBindVertexArray(g_vaoShadow); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexShadowLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexShadowLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; GLuint primitivesWritten; // Field of view GLfloat rotationMatrix[16]; GLfloat positionTextureSpace[4]; GLfloat directionTextureSpace[3]; GLfloat leftNormalTextureSpace[3]; GLfloat rightNormalTextureSpace[3]; GLfloat backNormalTextureSpace[3]; GLfloat xzPosition2D[4]; // GLfloat tmvpMatrix[16]; // Animation update g_personView.cameraPosition[0] = -cosf(2.0f * GLUS_PI * angle / TURN_DURATION) * TURN_RADIUS * METERS_TO_VIRTUAL_WORLD_SCALE; g_personView.cameraPosition[2] = -sinf(2.0f * GLUS_PI * angle / TURN_DURATION) * TURN_RADIUS * METERS_TO_VIRTUAL_WORLD_SCALE; g_personView.cameraDirection[0] = sinf(2.0f * GLUS_PI * angle / TURN_DURATION); g_personView.cameraDirection[2] = -cosf(2.0f * GLUS_PI * angle / TURN_DURATION); if (g_animationOn) { angle += time; } glusMatrix4x4LookAtf(g_viewMatrix, g_activeView->cameraPosition[0], g_activeView->cameraPosition[1], g_activeView->cameraPosition[2], g_activeView->cameraPosition[0] + g_activeView->cameraDirection[0], g_activeView->cameraPosition[1] + g_activeView->cameraDirection[1], g_activeView->cameraPosition[2] + g_activeView->cameraDirection[2], g_activeView->cameraUp[0], g_activeView->cameraUp[1], g_activeView->cameraUp[2]); glusMatrix4x4Identityf(tmvpMatrix); glusMatrix4x4Multiplyf(tmvpMatrix, tmvpMatrix, g_projectionMatrix); glusMatrix4x4Multiplyf(tmvpMatrix, tmvpMatrix, g_viewMatrix); glusMatrix4x4Multiplyf(tmvpMatrix, tmvpMatrix, g_textureToWorldMatrix); // Position xzPosition2D[0] = g_personView.cameraPosition[0]; xzPosition2D[1] = 0.0f; xzPosition2D[2] = g_personView.cameraPosition[2]; xzPosition2D[3] = g_personView.cameraPosition[3]; glusMatrix4x4MultiplyPoint4f(positionTextureSpace, g_worldToTextureMatrix, xzPosition2D); // Direction glusMatrix4x4MultiplyVector3f(directionTextureSpace, g_worldToTextureMatrix, g_personView.cameraDirection); // Left normal of field of view glusMatrix4x4Identityf(rotationMatrix); glusMatrix4x4RotateRyf(rotationMatrix, g_personView.fov * (g_width / g_height) / 2.0f + 90.0f); glusMatrix4x4MultiplyVector3f(leftNormalTextureSpace, rotationMatrix, g_personView.cameraDirection); glusMatrix4x4MultiplyVector3f(leftNormalTextureSpace, g_worldToTextureNormalMatrix, leftNormalTextureSpace); // Right normal of field of view glusMatrix4x4Identityf(rotationMatrix); glusMatrix4x4RotateRyf(rotationMatrix, -g_personView.fov * (g_width / g_height) / 2.0f - 90.0f); glusMatrix4x4MultiplyVector3f(rightNormalTextureSpace, rotationMatrix, g_personView.cameraDirection); glusMatrix4x4MultiplyVector3f(rightNormalTextureSpace, g_worldToTextureNormalMatrix, rightNormalTextureSpace); // Back normal of field of view glusMatrix4x4Identityf(rotationMatrix); glusMatrix4x4RotateRyf(rotationMatrix, 180.0f); glusMatrix4x4MultiplyVector3f(backNormalTextureSpace, rotationMatrix, g_personView.cameraDirection); glusMatrix4x4MultiplyVector3f(backNormalTextureSpace, g_worldToTextureNormalMatrix, backNormalTextureSpace); // OpenGL stuff glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Pass one. // Disable any rasterization glEnable(GL_RASTERIZER_DISCARD); glUseProgram(g_programPassOne.program); glUniform4fv(g_positionTextureSpacePassOneLocation, 1, positionTextureSpace); glUniform3fv(g_leftNormalTextureSpacePassOneLocation, 1, leftNormalTextureSpace); glUniform3fv(g_rightNormalTextureSpacePassOneLocation, 1, rightNormalTextureSpace); glUniform3fv(g_backNormalTextureSpacePassOneLocation, 1, backNormalTextureSpace); glBindVertexArray(g_vaoPassOne); // Bind to vertices used in render pass two. To this buffer is written. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, g_verticesPassTwoVBO); // We need to know, how many primitives are written. So start the query. glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, g_transformFeedbackQuery); // Start the operation ... glBeginTransformFeedback(GL_POINTS); // ... render the elements ... glDrawElements(GL_POINTS, g_sNumPoints * g_tNumPoints, GL_UNSIGNED_INT, 0); // ... and stop the operation. glEndTransformFeedback(); // Now, we can also stop the query. glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN); glDisable(GL_RASTERIZER_DISCARD); glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0); glBindVertexArray(0); // Pass two glUseProgram(g_shaderProgramPassTwo.program); glUniformMatrix4fv(g_tmvpPassTwoLocation, 1, GL_FALSE, tmvpMatrix); glUniform4fv(g_positionTextureSpacePassTwoLocation, 1, positionTextureSpace); glBindVertexArray(g_vaoPassTwo); // Now get the number of primitives written in the first render pass. glGetQueryObjectuiv(g_transformFeedbackQuery, GL_QUERY_RESULT, &primitivesWritten); // No draw the final terrain. glDrawArrays(GL_PATCHES, 0, primitivesWritten); return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { GLUSshape background; GLUSshape torus; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLfloat viewMatrix[16]; GLfloat lightDirection[3]; GLenum none[] = {GL_NONE}; lightDirection[0] = g_lightPosition[0]; lightDirection[1] = g_lightPosition[1]; lightDirection[2] = g_lightPosition[2]; glusVector3Normalizef(lightDirection); // glusFileLoadText("../Example12_ES/shader/rendershadow.vert.glsl", &vertexSource); glusFileLoadText("../Example12_ES/shader/rendershadow.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_programShadow, (const GLUSchar**) &vertexSource.text, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // glusFileLoadText("../Example12_ES/shader/useshadow.vert.glsl", &vertexSource); glusFileLoadText("../Example12_ES/shader/useshadow.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // g_projectionMatrixShadowLocation = glGetUniformLocation(g_programShadow.program, "u_projectionMatrix"); g_modelViewMatrixShadowLocation = glGetUniformLocation(g_programShadow.program, "u_modelViewMatrix"); g_vertexShadowLocation = glGetAttribLocation(g_programShadow.program, "a_vertex"); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_viewMatrixLocation = glGetUniformLocation(g_program.program, "u_viewMatrix"); g_modelMatrixLocation = glGetUniformLocation(g_program.program, "u_modelMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_shadowMatrixLocation = glGetUniformLocation(g_program.program, "u_shadowMatrix"); g_shadowTextureLocation = glGetUniformLocation(g_program.program, "u_shadowTexture"); g_colorLocation = glGetUniformLocation(g_program.program, "u_shapeColor"); g_lightDirectionLocation = glGetUniformLocation(g_program.program, "u_lightDirection"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); // glGenTextures(1, &g_shadowTexture); glBindTexture(GL_TEXTURE_2D, g_shadowTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, g_shadowTextureSize, g_shadowTextureSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS); glBindTexture(GL_TEXTURE_2D, 0); // glGenFramebuffers(1, &g_fbo); glBindFramebuffer(GL_FRAMEBUFFER, g_fbo); glDrawBuffers(1, none); glReadBuffer(GL_NONE); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, g_shadowTexture, 0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { printf("GL_FRAMEBUFFER_COMPLETE error 0x%x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); return GLUS_FALSE; } glClearDepthf(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glBindFramebuffer(GL_FRAMEBUFFER, 0); // glusShapeCreateTorusf(&torus, 0.5f, 1.0f, 32, 32); g_numberIndicesSphere = torus.numberIndices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, torus.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) torus.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, torus.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) torus.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, torus.numberIndices * sizeof(GLuint), (GLuint*) torus.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusShapeDestroyf(&torus); // glusShapeCreatePlanef(&background, 10.0f); g_numberIndicesBackground = background.numberIndices; glGenBuffers(1, &g_verticesBackgroundVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesBackgroundVBO); glBufferData(GL_ARRAY_BUFFER, background.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) background.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsBackgroundVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsBackgroundVBO); glBufferData(GL_ARRAY_BUFFER, background.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) background.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesBackgroundVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesBackgroundVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, background.numberIndices * sizeof(GLuint), (GLuint*) background.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusShapeDestroyf(&background); // glUseProgram(g_program.program); glusMatrix4x4LookAtf(viewMatrix, g_cameraPosition[0], g_cameraPosition[1], g_cameraPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glusMatrix4x4MultiplyVector3f(lightDirection, viewMatrix, lightDirection); glUniform3fv(g_lightDirectionLocation, 1, lightDirection); glUniform1i(g_shadowTextureLocation, 0); // Torus glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); // Plane glGenVertexArrays(1, &g_vaoBackground); glBindVertexArray(g_vaoBackground); glBindBuffer(GL_ARRAY_BUFFER, g_verticesBackgroundVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsBackgroundVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesBackgroundVBO); // glUseProgram(g_programShadow.program); // Torus glGenVertexArrays(1, &g_vaoShadow); glBindVertexArray(g_vaoShadow); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexShadowLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexShadowLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); // Plane glGenVertexArrays(1, &g_vaoShadowBackground); glBindVertexArray(g_vaoShadowBackground); glBindBuffer(GL_ARRAY_BUFFER, g_verticesBackgroundVBO); glVertexAttribPointer(g_vertexShadowLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexShadowLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesBackgroundVBO); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepthf(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); // Needed when rendering the shadow map. This will avoid artifacts. glPolygonOffset(1.0f, 0.0f); return GLUS_TRUE; }
/* * @author Pablo Alonso-Villaverde Roza * @author Norbert Nopper */ GLUSboolean GLUSAPIENTRY glusCreateTorusf(GLUSshape* shape, const GLUSfloat innerRadius, const GLUSfloat outerRadius, const GLUSushort numberSlices, const GLUSushort numberStacks) { // s, t = parametric values of the equations, in the range [0,1] GLUSfloat s = 0; GLUSfloat t = 0; // sIncr, tIncr are increment values aplied to s and t on each loop iteration to generate the torus GLUSfloat sIncr; GLUSfloat tIncr; // to store precomputed sin and cos values GLUSfloat cos2PIs, sin2PIs, cos2PIt, sin2PIt; GLUSuint numberVertices; GLUSuint numberIndices; // used later to help us calculating tangents vectors GLUSfloat helpVector[3] = { 0.0f, 1.0f, 0.0f }; GLUSfloat helpQuaternion[4]; GLUSfloat helpMatrix[16]; // indices for each type of buffer (of vertices, indices, normals...) GLUSuint indexVertices, indexNormals, indexTangents, indexTexCoords; GLUSuint indexIndices; // loop counters GLUSuint sideCount, faceCount; // used to generate the indices GLUSuint v0, v1, v2, v3; GLUSfloat torusRadius = (outerRadius - innerRadius) / 2.0f; GLUSfloat centerRadius = outerRadius - torusRadius; numberVertices = (numberStacks + 1) * (numberSlices + 1); numberIndices = numberStacks * numberSlices * 2 * 3; // 2 triangles per face * 3 indices per triangle if (numberSlices < 3 || numberStacks < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) { return GLUS_FALSE; } if (!shape) { return GLUS_FALSE; } glusInitShapef(shape); shape->numberVertices = numberVertices; shape->numberIndices = numberIndices; shape->vertices = (GLUSfloat*) malloc(4 * numberVertices * sizeof(GLUSfloat)); shape->normals = (GLUSfloat*) malloc(3 * numberVertices * sizeof(GLUSfloat)); shape->tangents = (GLUSfloat*) malloc(3 * numberVertices * sizeof(GLUSfloat)); shape->texCoords = (GLUSfloat*) malloc(2 * numberVertices * sizeof(GLUSfloat)); shape->indices = (GLUSushort*) malloc(numberIndices * sizeof(GLUSushort)); if (!glusCheckShapef(shape)) { glusDestroyShapef(shape); return GLUS_FALSE; } sIncr = 1.0f / (GLUSfloat) numberSlices; tIncr = 1.0f / (GLUSfloat) numberStacks; // generate vertices and its attributes for (sideCount = 0; sideCount <= numberSlices; ++sideCount, s += sIncr) { // precompute some values cos2PIs = (GLUSfloat) cosf(2.0f * GLUS_PI * s); sin2PIs = (GLUSfloat) sinf(2.0f * GLUS_PI * s); t = 0.0f; for (faceCount = 0; faceCount <= numberStacks; ++faceCount, t += tIncr) { // precompute some values cos2PIt = (GLUSfloat) cosf(2.0f * GLUS_PI * t); sin2PIt = (GLUSfloat) sinf(2.0f * GLUS_PI * t); // generate vertex and stores it in the right position indexVertices = ((sideCount * (numberStacks + 1)) + faceCount) * 4; shape->vertices[indexVertices + 0] = (centerRadius + torusRadius * cos2PIt) * cos2PIs; shape->vertices[indexVertices + 1] = (centerRadius + torusRadius * cos2PIt) * sin2PIs; shape->vertices[indexVertices + 2] = torusRadius * sin2PIt; shape->vertices[indexVertices + 3] = 1.0f; // generate normal and stores it in the right position // NOTE: cos (2PIx) = cos (x) and sin (2PIx) = sin (x) so, we can use this formula // normal = {cos(2PIs)cos(2PIt) , sin(2PIs)cos(2PIt) ,sin(2PIt)} indexNormals = ((sideCount * (numberStacks + 1)) + faceCount) * 3; shape->normals[indexNormals + 0] = cos2PIs * cos2PIt; shape->normals[indexNormals + 1] = sin2PIs * cos2PIt; shape->normals[indexNormals + 2] = sin2PIt; // generate texture coordinates and stores it in the right position indexTexCoords = ((sideCount * (numberStacks + 1)) + faceCount) * 2; shape->texCoords[indexTexCoords + 0] = s; shape->texCoords[indexTexCoords + 1] = t; // use quaternion to get the tangent vector glusQuaternionRotateRzf(helpQuaternion, 360.0f * s); glusQuaternionGetMatrix4x4f(helpMatrix, helpQuaternion); indexTangents = ((sideCount * (numberStacks + 1)) + faceCount) * 3; glusMatrix4x4MultiplyVector3f(&shape->tangents[indexTangents], helpMatrix, helpVector); } } // generate indices indexIndices = 0; for (sideCount = 0; sideCount < numberSlices; ++sideCount) { for (faceCount = 0; faceCount < numberStacks; ++faceCount) { // get the number of the vertices for a face of the torus. They must be < numVertices v0 = ((sideCount * (numberStacks + 1)) + faceCount); v1 = (((sideCount + 1) * (numberStacks + 1)) + faceCount); v2 = (((sideCount + 1) * (numberStacks + 1)) + (faceCount + 1)); v3 = ((sideCount * (numberStacks + 1)) + (faceCount + 1)); // first triangle of the face, counter clock wise winding shape->indices[indexIndices++] = v0; shape->indices[indexIndices++] = v1; shape->indices[indexIndices++] = v2; // second triangle of the face, counter clock wise winding shape->indices[indexIndices++] = v0; shape->indices[indexIndices++] = v2; shape->indices[indexIndices++] = v3; } } if (!glusFinalizeShapef(shape)) { glusDestroyShapef(shape); return GLUS_FALSE; } return GLUS_TRUE; }
GLUSboolean GLUSAPIENTRY glusCreateDomef(GLUSshape* shape, const GLUSfloat radius, const GLUSushort numberSlices) { GLUSuint i, j; GLUSuint numberParallels = numberSlices / 4; GLUSuint numberVertices = (numberParallels + 1) * (numberSlices + 1); GLUSuint numberIndices = numberParallels * numberSlices * 6; GLUSfloat angleStep = (2.0f * GLUS_PI) / ((GLUSfloat) numberSlices); GLUSuint indexIndices; // used later to help us calculating tangents vectors GLUSfloat helpVector[3] = { 1.0f, 0.0f, 0.0f }; GLUSfloat helpQuaternion[4]; GLUSfloat helpMatrix[16]; if (numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) { return GLUS_FALSE; } if (!shape) { return GLUS_FALSE; } glusInitShapef(shape); shape->numberVertices = numberVertices; shape->numberIndices = numberIndices; shape->vertices = (GLUSfloat*) malloc(4 * numberVertices * sizeof(GLUSfloat)); shape->normals = (GLUSfloat*) malloc(3 * numberVertices * sizeof(GLUSfloat)); shape->tangents = (GLUSfloat*) malloc(3 * numberVertices * sizeof(GLUSfloat)); shape->texCoords = (GLUSfloat*) malloc(2 * numberVertices * sizeof(GLUSfloat)); shape->indices = (GLUSushort*) malloc(numberIndices * sizeof(GLUSushort)); if (!glusCheckShapef(shape)) { glusDestroyShapef(shape); return GLUS_FALSE; } for (i = 0; i < (GLUSuint)(numberParallels + 1); i++) { for (j = 0; j < (GLUSuint)(numberSlices + 1); j++) { GLUSuint vertexIndex = (i * (numberSlices + 1) + j) * 4; GLUSuint normalIndex = (i * (numberSlices + 1) + j) * 3; GLUSuint tangentIndex = (i * (numberSlices + 1) + j) * 3; GLUSuint texCoordsIndex = (i * (numberSlices + 1) + j) * 2; shape->vertices[vertexIndex + 0] = radius * sinf(angleStep * (GLUSfloat) i) * sinf(angleStep * (GLUSfloat) j); shape->vertices[vertexIndex + 1] = radius * cosf(angleStep * (GLUSfloat) i); shape->vertices[vertexIndex + 2] = radius * sinf(angleStep * (GLUSfloat) i) * cosf(angleStep * (GLUSfloat) j); shape->vertices[vertexIndex + 3] = 1.0f; shape->normals[normalIndex + 0] = shape->vertices[vertexIndex + 0] / radius; shape->normals[normalIndex + 1] = shape->vertices[vertexIndex + 1] / radius; shape->normals[normalIndex + 2] = shape->vertices[vertexIndex + 2] / radius; shape->texCoords[texCoordsIndex + 0] = (GLUSfloat) j / (GLUSfloat) numberSlices; shape->texCoords[texCoordsIndex + 1] = 1.0f - (GLUSfloat) i / (GLUSfloat) numberParallels; // use quaternion to get the tangent vector glusQuaternionRotateRyf(helpQuaternion, 360.0f * shape->texCoords[texCoordsIndex + 0]); glusQuaternionGetMatrix4x4f(helpMatrix, helpQuaternion); glusMatrix4x4MultiplyVector3f(&shape->tangents[tangentIndex], helpMatrix, helpVector); } } indexIndices = 0; for (i = 0; i < numberParallels; i++) { for (j = 0; j < numberSlices; j++) { shape->indices[indexIndices++] = i * (numberSlices + 1) + j; shape->indices[indexIndices++] = (i + 1) * (numberSlices + 1) + j; shape->indices[indexIndices++] = (i + 1) * (numberSlices + 1) + (j + 1); shape->indices[indexIndices++] = i * (numberSlices + 1) + j; shape->indices[indexIndices++] = (i + 1) * (numberSlices + 1) + (j + 1); shape->indices[indexIndices++] = i * (numberSlices + 1) + (j + 1); } } if (!glusFinalizeShapef(shape)) { glusDestroyShapef(shape); return GLUS_FALSE; } return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { GLUSshape wavefrontObj; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLfloat viewMatrix[16]; GLfloat lightDirection[3]; lightDirection[0] = g_lightPosition[0]; lightDirection[1] = g_lightPosition[1]; lightDirection[2] = g_lightPosition[2]; glusVector3Normalizef(lightDirection); // glusFileLoadText("../Example34/shader/renderdepthmap.vert.glsl", &vertexSource); glusFileLoadText("../Example34/shader/renderdepthmap.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_programDepthPass, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // glusFileLoadText("../Example34/shader/subsurfacescattering.vert.glsl", &vertexSource); glusFileLoadText("../Example34/shader/subsurfacescattering.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // g_projectionMatrixDepthPassLocation = glGetUniformLocation(g_programDepthPass.program, "u_projectionMatrix"); g_modelViewMatrixDepthPassLocation = glGetUniformLocation(g_programDepthPass.program, "u_modelViewMatrix"); g_vertexDepthPassLocation = glGetAttribLocation(g_programDepthPass.program, "a_vertex"); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_viewMatrixLocation = glGetUniformLocation(g_program.program, "u_viewMatrix"); g_modelMatrixLocation = glGetUniformLocation(g_program.program, "u_modelMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_depthPassMatrixLocation = glGetUniformLocation(g_program.program, "u_depthPassMatrix"); g_diffuseColorLocation = glGetUniformLocation(g_program.program, "u_diffuseColor"); g_scatterColorLocation = glGetUniformLocation(g_program.program, "u_scatterColor"); g_lightDirectionLocation = glGetUniformLocation(g_program.program, "u_lightDirection"); g_depthPassTextureLocation = glGetUniformLocation(g_program.program, "u_depthPassTexture"); g_nearFarLocation = glGetUniformLocation(g_program.program, "u_nearFar"); g_wrapLocation = glGetUniformLocation(g_program.program, "u_wrap"); g_scatterWidthLocation = glGetUniformLocation(g_program.program, "u_scatterWidth"); g_scatterFalloffLocation = glGetUniformLocation(g_program.program, "u_scatterFalloff"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); // glGenTextures(1, &g_depthPassTexture); glBindTexture(GL_TEXTURE_2D, g_depthPassTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, g_depthPassTextureSize, g_depthPassTextureSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); // glGenFramebuffers(1, &g_fbo); glBindFramebuffer(GL_FRAMEBUFFER, g_fbo); glDrawBuffer(GL_NONE); glReadBuffer(GL_NONE); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, g_depthPassTexture, 0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { printf("GL_FRAMEBUFFER_COMPLETE error 0x%x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); return GLUS_FALSE; } glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glBindFramebuffer(GL_FRAMEBUFFER, 0); // // Use a helper function to load an wavefront object file. glusShapeLoadWavefront("dragon.obj", &wavefrontObj); g_numberVertices = wavefrontObj.numberVertices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, wavefrontObj.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) wavefrontObj.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, wavefrontObj.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) wavefrontObj.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glusShapeDestroyf(&wavefrontObj); // glUseProgram(g_program.program); glusMatrix4x4LookAtf(viewMatrix, g_cameraPosition[0], g_cameraPosition[1], g_cameraPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glusMatrix4x4MultiplyVector3f(lightDirection, viewMatrix, lightDirection); glUniform3fv(g_lightDirectionLocation, 1, lightDirection); glUniform1i(g_depthPassTextureLocation, 0); // Dragon glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); // glUseProgram(g_programDepthPass.program); // Dragon glGenVertexArrays(1, &g_vaoDepthPass); glBindVertexArray(g_vaoDepthPass); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexDepthPassLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexDepthPassLocation); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); return GLUS_TRUE; }