GLUSboolean init(GLUSvoid) { GLUSshape torus; GLUSshape backgroundSphere; GLUStgaimage image; GLUStextfile vertexSource; GLUStextfile fragmentSource; glusLoadTextFile("../Example11_ES/shader/glass.vert.glsl", &vertexSource); glusLoadTextFile("../Example11_ES/shader/glass.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_program, (const GLUSchar**) &vertexSource.text, (const GLUSchar**) &fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); // glusLoadTextFile("../Example11_ES/shader/background.vert.glsl", &vertexSource); glusLoadTextFile("../Example11_ES/shader/background.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_programBackground, (const GLUSchar**) &vertexSource.text, (const GLUSchar**) &fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); // g_viewProjectionMatrixLocation = glGetUniformLocation(g_program.program, "u_viewProjectionMatrix"); g_modelMatrixLocation = glGetUniformLocation(g_program.program, "u_modelMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_cameraLocation = glGetUniformLocation(g_program.program, "u_camera"); g_cubemapLocation = glGetUniformLocation(g_program.program, "u_cubemap"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); // g_viewProjectionMatrixBackgroundLocation = glGetUniformLocation(g_programBackground.program, "u_viewProjectionMatrix"); g_modelMatrixBackgroundLocation = glGetUniformLocation(g_programBackground.program, "u_modelMatrix"); g_cubemapBackgroundLocation = glGetUniformLocation(g_programBackground.program, "u_cubemap"); g_vertexBackgroundLocation = glGetAttribLocation(g_programBackground.program, "a_vertex"); // glGenTextures(1, &g_cubemap); glBindTexture(GL_TEXTURE_CUBE_MAP, g_cubemap); glusLoadTgaImage("cm_pos_x.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glusLoadTgaImage("cm_neg_x.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glusLoadTgaImage("cm_pos_y.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glusLoadTgaImage("cm_neg_y.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glusLoadTgaImage("cm_pos_z.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glusLoadTgaImage("cm_neg_z.tga", &image); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_CUBE_MAP, 0); // glusCreateTorusf(&torus, 0.25f, 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); glusDestroyShapef(&torus); // glusCreateSpheref(&backgroundSphere, g_circleRadius, 32); g_numberIndicesBackground = backgroundSphere.numberIndices; glGenBuffers(1, &g_verticesBackgroundVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesBackgroundVBO); glBufferData(GL_ARRAY_BUFFER, backgroundSphere.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) backgroundSphere.vertices, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesBackgroundVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesBackgroundVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, backgroundSphere.numberIndices * sizeof(GLuint), (GLuint*) backgroundSphere.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusDestroyShapef(&backgroundSphere); // glUseProgram(g_program.program); glUniform1i(g_cubemapLocation, 0); 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); // glUseProgram(g_programBackground.program); glUniform1i(g_cubemapBackgroundLocation, 0); glGenVertexArrays(1, &g_vaoBackground); glBindVertexArray(g_vaoBackground); glBindBuffer(GL_ARRAY_BUFFER, g_verticesBackgroundVBO); glVertexAttribPointer(g_vertexBackgroundLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexBackgroundLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesBackgroundVBO); // glBindTexture(GL_TEXTURE_CUBE_MAP, g_cubemap); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepthf(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); 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; }
/** * Function for initialization. */ GLUSboolean init(GLUSvoid) { GLUSshape sphere; GLUStextfile vertexSource; GLUStextfile geometrySource; GLUStextfile fragmentSource; // Load the source of the vertex shader. glusLoadTextFile("../src/Example09/Vertex.vs", &vertexSource); // Load the source of the geometry shader. glusLoadTextFile("../src/Example09/Geometry.gs", &geometrySource); // Load the source of the fragment shader. glusLoadTextFile("../src/Example09/Fragment.fs", &fragmentSource); // Build and ... glusBuildProgram(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, (const GLUSchar**) &geometrySource.text, (const GLUSchar**) &fragmentSource.text); // Destroy the text resource glusDestroyTextFile(&vertexSource); // Destroy the text resource glusDestroyTextFile(&geometrySource); // Destroy the text resource glusDestroyTextFile(&fragmentSource); // ToDo: glGenVertexArrays(1, &g_vao); // ToDo: glBindVertexArray(g_vao); glusCreateSpheref(&sphere, 1.0f, 32); numberIndices = sphere.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/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/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, sphere.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) sphere.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, sphere.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) sphere.normals, 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, sphere.numberIndices * sizeof(GLuint), (GLuint*) sphere.indices, GL_STATIC_DRAW); glusDestroyShapef(&sphere); // http://www.opengl.org/sdk/docs/man/xhtml/glUseProgram.xml glUseProgram(g_program.program); // ... and the view matrix ... glusLookAtf(g_modelView, 0.0f, 0.0f, 6.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // 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/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; }
/** * Function for initialization. */ GLUSboolean init(GLUSvoid) { // Points of a triangle. GLfloat* points = (GLfloat*)malloc(WATER_PLANE_LENGTH*WATER_PLANE_LENGTH*4*sizeof(GLfloat)); GLuint* indices = (GLuint*)malloc(WATER_PLANE_LENGTH*(WATER_PLANE_LENGTH-1)*2*sizeof(GLuint)); GLUSshape background; GLUStgaimage image; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLuint x, z, i, k; GLuint waterTexture; for (z = 0; z < WATER_PLANE_LENGTH; z++) { for (x = 0; x < WATER_PLANE_LENGTH; x++) { points[(x+z*(WATER_PLANE_LENGTH))*4 + 0] = -(GLfloat)WATER_PLANE_LENGTH/2 + 0.5f + (GLfloat)x; points[(x+z*(WATER_PLANE_LENGTH))*4 + 1] = 0.0f; points[(x+z*(WATER_PLANE_LENGTH))*4 + 2] = +(GLfloat)WATER_PLANE_LENGTH/2 - 0.5f - (GLfloat)z; points[(x+z*(WATER_PLANE_LENGTH))*4 + 3] = 1.0f; } } for (k = 0; k < WATER_PLANE_LENGTH-1; k++) { for (i = 0; i < WATER_PLANE_LENGTH; i++) { if (k%2 == 0) { indices[(i+k*(WATER_PLANE_LENGTH))*2 + 0] = i + k*WATER_PLANE_LENGTH; indices[(i+k*(WATER_PLANE_LENGTH))*2 + 1] = i + (k+1)*WATER_PLANE_LENGTH; } else { indices[(i+k*(WATER_PLANE_LENGTH))*2 + 0] = WATER_PLANE_LENGTH - 1 - i + k*WATER_PLANE_LENGTH; indices[(i+k*(WATER_PLANE_LENGTH))*2 + 1] = WATER_PLANE_LENGTH - 1 - i + (k+1)*WATER_PLANE_LENGTH; } } } // Load the source of the vertex shader. glusLoadTextFile("../src/Example14/Vertex.vs", &vertexSource); // Load the source of the fragment shader. glusLoadTextFile("../src/Example14/Fragment.fs", &fragmentSource); // Build and ... glusBuildProgram(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 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); // 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_waterPlaneLengthLocation = glGetUniformLocation(g_program.program, "waterPlaneLength"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_inverseCameraLocation = glGetUniformLocation(g_program.program, "inverseCameraMatrix"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_cubemapLocation = glGetUniformLocation(g_program.program, "cubemap"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_waterLocation = glGetUniformLocation(g_program.program, "waterTexture"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_passedTimeLocation = glGetUniformLocation(g_program.program, "passedTime"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_waveParametersLocation = glGetUniformLocation(g_program.program, "waveParameters"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_waveDirectionsLocation = glGetUniformLocation(g_program.program, "waveDirections"); // 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/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, WATER_PLANE_LENGTH*WATER_PLANE_LENGTH * 4 * sizeof(GLfloat), (GLfloat*) points, 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, WATER_PLANE_LENGTH*(WATER_PLANE_LENGTH-1) * 2 * sizeof(GLuint), (GLuint*) indices, GL_STATIC_DRAW); // free(points); free(indices); // // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/gentextures.html glGenTextures(1, &g_cubemap); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/bindtexture.html glBindTexture(GL_TEXTURE_CUBE_MAP, g_cubemap); glusLoadTgaImage("water_pos_x.tga", &image); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glusLoadTgaImage("water_neg_x.tga", &image); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glusLoadTgaImage("water_pos_y.tga", &image); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glusLoadTgaImage("water_neg_y.tga", &image); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glusLoadTgaImage("water_pos_z.tga", &image); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glusLoadTgaImage("water_neg_z.tga", &image); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 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_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_CUBE_MAP, 0); // // http://www.opengl.org/sdk/docs/man/xhtml/glUseProgram.xml glUseProgram(g_program.program); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniform1f(g_waterPlaneLengthLocation, (GLUSfloat)WATER_PLANE_LENGTH); // 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/glActiveTexture.xml glActiveTexture(GL_TEXTURE0); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/bindtexture.html glBindTexture(GL_TEXTURE_CUBE_MAP, g_cubemap); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniform1i(g_cubemapLocation, 0); // Get the render buffer texture waterTexture = initTexture((GLUSfloat)WATER_PLANE_LENGTH); // http://www.opengl.org/sdk/docs/man/xhtml/glUseProgram.xml glUseProgram(g_program.program); // http://www.opengl.org/sdk/docs/man/xhtml/glActiveTexture.xml glActiveTexture(GL_TEXTURE1); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/bindtexture.html glBindTexture(GL_TEXTURE_2D, waterTexture); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniform1i(g_waterLocation, 1); // // Load the source of the vertex shader. glusLoadTextFile("../src/Example14/VertexBackground.vs", &vertexSource); // Load the source of the fragment shader. glusLoadTextFile("../src/Example14/FragmentBackground.fs", &fragmentSource); // Build and ... glusBuildProgram(&g_programBackground, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); // Destroy the text resource glusDestroyTextFile(&vertexSource); // Destroy the text resource glusDestroyTextFile(&fragmentSource); // ToDo: glGenVertexArrays(1, &g_vaoBackground); // ToDo: glBindVertexArray(g_vaoBackground); glusCreateSpheref(&background, (GLfloat)(GLfloat)WATER_PLANE_LENGTH/2.0f + 0.5f, 32); g_numberIndicesBackground = background.numberIndices; // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_projectionLocationBackground = glGetUniformLocation(g_programBackground.program, "projectionMatrix"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_modelViewLocationBackground = glGetUniformLocation(g_programBackground.program, "modelViewMatrix"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetUniformLocation.xml g_cubemapLocationBackground = glGetUniformLocation(g_programBackground.program, "cubemap"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetAttribLocation.xml g_vertexLocationBackground = glGetAttribLocation(g_programBackground.program, "vertex"); // http://www.opengl.org/sdk/docs/man/xhtml/glGetAttribLocation.xml g_normalLocationBackground = glGetAttribLocation(g_programBackground.program, "normal"); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_verticesBackground); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_verticesBackground); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ARRAY_BUFFER, background.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) background.vertices, GL_STATIC_DRAW); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_normalsBackground); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_normalsBackground); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ARRAY_BUFFER, background.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) background.normals, GL_STATIC_DRAW); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_indicesBackground); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesBackground); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ELEMENT_ARRAY_BUFFER, background.numberIndices * sizeof(GLuint), (GLuint*) background.indices, GL_STATIC_DRAW); glusDestroyShapef(&background); // http://www.opengl.org/sdk/docs/man/xhtml/glUseProgram.xml glUseProgram(g_programBackground.program); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniformMatrix4fv(g_modelViewLocationBackground, 1, GL_FALSE, g_modelView); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_verticesBackground); // http://www.opengl.org/sdk/docs/man/xhtml/glVertexAttribPointer.xml glVertexAttribPointer(g_vertexLocationBackground, 4, GL_FLOAT, GL_FALSE, 0, 0); // http://www.opengl.org/sdk/docs/man/xhtml/glEnableVertexAttribArray.xml glEnableVertexAttribArray(g_vertexLocationBackground); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_normalsBackground); // http://www.opengl.org/sdk/docs/man/xhtml/glVertexAttribPointer.xml glVertexAttribPointer(g_normalLocationBackground, 3, GL_FLOAT, GL_FALSE, 0, 0); // http://www.opengl.org/sdk/docs/man/xhtml/glEnableVertexAttribArray.xml glEnableVertexAttribArray(g_normalLocationBackground); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniform1i(g_cubemapLocationBackground, 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); return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { GLUStextfile vertexSource; GLUStextfile fragmentSource; GLUStgaimage image; GLUSgroupList* groupWalker; GLUSmaterialList* materialWalker; GLUSshape sphere; // // Each point light is rendered as a sphere. // glusLoadTextFile("../Example31/shader/point_light.vert.glsl", &vertexSource); glusLoadTextFile("../Example31/shader/point_light.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_programPointLight, (const GLUSchar**)&vertexSource.text, 0, 0, 0, (const GLUSchar**)&fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); // g_projectionMatrixPointLightLocation = glGetUniformLocation(g_programPointLight.program, "u_projectionMatrix"); g_viewMatrixPointLightLocation = glGetUniformLocation(g_programPointLight.program, "u_viewMatrix"); g_modelMatrixPointLightLocation = glGetUniformLocation(g_programPointLight.program, "u_modelMatrix"); g_positionMatrixPointLightLocation = glGetUniformLocation(g_programPointLight.program, "u_positionMatrix"); g_biasMatrixPointLightLocation = glGetUniformLocation(g_programPointLight.program, "u_biasMatrix"); g_radiusPointLightLocation = glGetUniformLocation(g_programPointLight.program, "u_lightRadius"); g_diffusePointLightLocation = glGetUniformLocation(g_programPointLight.program, "u_diffuse"); g_specularPointLightLocation = glGetUniformLocation(g_programPointLight.program, "u_specular"); g_positionPointLightLocation = glGetUniformLocation(g_programPointLight.program, "u_position"); g_normalPointLightLocation = glGetUniformLocation(g_programPointLight.program, "u_normal"); g_vertexPointLightLocation = glGetAttribLocation(g_programPointLight.program, "a_vertex"); // Use a helper function to create a cube. glusCreateSpheref(&sphere, POINT_LIGHT_RADIUS, 32); g_numberIndicesPointLight = sphere.numberIndices; glGenBuffers(1, &g_verticesPointLightVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesPointLightVBO); // Transfer the vertices from CPU to GPU. glBufferData(GL_ARRAY_BUFFER, sphere.numberVertices * 4 * sizeof(GLfloat), (GLfloat*)sphere.vertices, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesPointLightVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesPointLightVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sphere.numberIndices * sizeof(GLuint), (GLuint*)sphere.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusDestroyShapef(&sphere); glGenVertexArrays(1, &g_vaoPointLight); glBindVertexArray(g_vaoPointLight); glBindBuffer(GL_ARRAY_BUFFER, g_verticesPointLightVBO); glVertexAttribPointer(g_vertexPointLightLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexPointLightLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesPointLightVBO); glBindVertexArray(0); // // The point lights are moving forth and back between the columns and rows. // Here, a random position and moving direction of the point light is generated. // glusRandomSetSeed(13); // Note: If more than 16 lights are used, make sure to store them in another matrix or buffer. for (GLint i = 0; i < POINT_LIGHT_COUNT; i++) { g_positionMatrix[i] = glusRandomUniformGetFloatf(0.0f, (float)POINT_LIGHT_COUNT - 2.0f); g_directionMatrix[i] = rand() % 2 == 0 ? 1.0f : -1.0f; } // // // glusLoadTextFile("../Example31/shader/deferred_shading.vert.glsl", &vertexSource); glusLoadTextFile("../Example31/shader/deferred_shading.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_programDeferredShading, (const GLUSchar**)&vertexSource.text, 0, 0, 0, (const GLUSchar**)&fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); // g_projectionMatrixLocation = glGetUniformLocation(g_programDeferredShading.program, "u_projectionMatrix"); g_viewMatrixLocation = glGetUniformLocation(g_programDeferredShading.program, "u_viewMatrix"); g_modelMatrixLocation = glGetUniformLocation(g_programDeferredShading.program, "u_modelMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_programDeferredShading.program, "u_normalMatrix"); g_material.diffuseColorLocation = glGetUniformLocation(g_programDeferredShading.program, "u_material.diffuseColor"); g_material.specularColorLocation = glGetUniformLocation(g_programDeferredShading.program, "u_material.specularColor"); g_material.specularExponentLocation = glGetUniformLocation(g_programDeferredShading.program, "u_material.specularExponent"); g_material.diffuseTextureLocation = glGetUniformLocation(g_programDeferredShading.program, "u_material.diffuseTexture"); g_useTextureLocation = glGetUniformLocation(g_programDeferredShading.program, "u_useTexture"); g_vertexLocation = glGetAttribLocation(g_programDeferredShading.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_programDeferredShading.program, "a_normal"); g_texCoordLocation = glGetAttribLocation(g_programDeferredShading.program, "a_texCoord"); // // Use a helper function to load the wavefront object file. // glusLoadGroupedObjFile("ChessPawn.obj", &g_wavefront); glGenBuffers(1, &g_wavefront.verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_wavefront.verticesVBO); glBufferData(GL_ARRAY_BUFFER, g_wavefront.numberVertices * 4 * sizeof(GLfloat), (GLfloat*)g_wavefront.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_wavefront.normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_wavefront.normalsVBO); glBufferData(GL_ARRAY_BUFFER, g_wavefront.numberVertices * 3 * sizeof(GLfloat), (GLfloat*)g_wavefront.normals, GL_STATIC_DRAW); glGenBuffers(1, &g_wavefront.texCoordsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_wavefront.texCoordsVBO); glBufferData(GL_ARRAY_BUFFER, g_wavefront.numberVertices * 2 * sizeof(GLfloat), (GLfloat*)g_wavefront.texCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // // Set up indices and the VAOs for each group // glUseProgram(g_programDeferredShading.program); groupWalker = g_wavefront.groups; while (groupWalker) { glGenBuffers(1, &groupWalker->group.indicesVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, groupWalker->group.indicesVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, groupWalker->group.numberIndices * sizeof(GLuint), (GLuint*)groupWalker->group.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // glGenVertexArrays(1, &groupWalker->group.vao); glBindVertexArray(groupWalker->group.vao); glBindBuffer(GL_ARRAY_BUFFER, g_wavefront.verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_wavefront.normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ARRAY_BUFFER, g_wavefront.texCoordsVBO); glVertexAttribPointer(g_texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_texCoordLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, groupWalker->group.indicesVBO); glBindVertexArray(0); groupWalker = groupWalker->next; } // // Load the textures, if there are available // materialWalker = g_wavefront.materials; while (materialWalker) { if (materialWalker->material.diffuseTextureFilename[0] != '\0') { // Load the image. glusLoadTgaImage(materialWalker->material.diffuseTextureFilename, &image); // Generate and bind a texture. glGenTextures(1, &materialWalker->material.diffuseTextureName); glBindTexture(GL_TEXTURE_2D, materialWalker->material.diffuseTextureName); // Transfer the image data from the CPU to the GPU. glTexImage2D(GL_TEXTURE_2D, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); // Setting the texture parameters. 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); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); } materialWalker = materialWalker->next; } // // Setting up the deferred shading geometry buffer. // glGenTextures(1, &g_dsDiffuseTexture); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, g_dsDiffuseTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); // glGenTextures(1, &g_dsSpecularTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, g_dsSpecularTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); // glGenTextures(1, &g_dsPositionTexture); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, g_dsPositionTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_RGBA, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); // glGenTextures(1, &g_dsNormalTexture); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, g_dsNormalTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_RGB, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); // glGenTextures(1, &g_dsDepthTexture); glActiveTexture(GL_TEXTURE4); glBindTexture(GL_TEXTURE_2D, g_dsDepthTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE0); // glGenFramebuffers(1, &g_dsFBO); glBindFramebuffer(GL_FRAMEBUFFER, g_dsFBO); // Attach the color buffer ... glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_dsDiffuseTexture, 0); // Attach the normal buffer ... glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, g_dsSpecularTexture, 0); // Attach the color buffer ... glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, g_dsPositionTexture, 0); // Attach the normal buffer ... glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, g_dsNormalTexture, 0); // ... and the depth buffer, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, g_dsDepthTexture, 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); // // // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); // The color buffer is accumulated for each light, so use this blend function when doing the lighting pass. glBlendFunc(GL_ONE, GL_ONE); return GLUS_TRUE; }
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 }
GLUSboolean init(GLUSvoid) { GLfloat viewMatrix[16]; GLfloat normalMatrix[9]; // 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.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, 20.0f }; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLUSshape sphere; glusLoadTextFile("../Example05/shader/phong.vert.glsl", &vertexSource); glusLoadTextFile("../Example05/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_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 create a sphere. glusCreateSpheref(&sphere, 0.5f, 32); g_numberIndices = 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); // 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); // glusLookAtf(viewMatrix, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // The calculations are done in camera / view space. So pass the view matrix, which is a rigid body transform. glusMatrixExtract3x3f(normalMatrix, viewMatrix); glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, viewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); // glusVectorNormalizef(light.direction); // Transform light to camera space, as it is currently in world space. glusMatrixMultiplyVectorf(light.direction, 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 backgroundSphere; GLUSshape wavefront; // 6 sides of diffuse and specular; all roughness levels of specular. GLUShdrimage image[6 * NUMBER_ROUGHNESS + 6]; // The look up table (LUT) is stored in a raw binary file. GLUSbinaryfile rawimage; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLchar buffer[27] = "doge2/doge2_POS_X_00_s.hdr"; GLint i, k, m; // glusLoadTextFile("../Example33/shader/brdf.vert.glsl", &vertexSource); glusLoadTextFile("../Example33/shader/brdf.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_modelProgram, (const GLchar**)&vertexSource.text, 0, 0, 0, (const GLchar**)&fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); g_viewProjectionMatrixModelLocation = glGetUniformLocation(g_modelProgram.program, "u_viewProjectionMatrix"); g_modelMatrixModelLocation = glGetUniformLocation(g_modelProgram.program, "u_modelMatrix"); g_normalMatrixModelLocation = glGetUniformLocation(g_modelProgram.program, "u_normalMatrix"); g_eyeModelLocation = glGetUniformLocation(g_modelProgram.program, "u_eye"); g_textureSpecularModelLocation = glGetUniformLocation(g_modelProgram.program, "u_textureSpecular"); g_textureDiffuseModelLocation = glGetUniformLocation(g_modelProgram.program, "u_textureDiffuse"); g_textureLUTModelLocation = glGetUniformLocation(g_modelProgram.program, "u_textureLUT"); g_colorMaterialModelLocation = glGetUniformLocation(g_modelProgram.program, "u_colorMaterial"); g_roughnessMaterialModelLocation = glGetUniformLocation(g_modelProgram.program, "u_roughnessMaterial"); g_roughnessScaleModelLocation = glGetUniformLocation(g_modelProgram.program, "u_roughnessScale"); g_R0MaterialModelLocation = glGetUniformLocation(g_modelProgram.program, "u_R0Material"); g_vertexModelLocation = glGetAttribLocation(g_modelProgram.program, "a_vertex"); g_normalModelLocation = glGetAttribLocation(g_modelProgram.program, "a_normal"); // glusLoadTextFile("../Example33/shader/fullscreen.vert.glsl", &vertexSource); glusLoadTextFile("../Example33/shader/fullscreen.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_fullscreenProgram, (const GLchar**)&vertexSource.text, 0, 0, 0, (const GLchar**)&fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); // g_framebufferTextureFullscreenLocation = glGetUniformLocation(g_fullscreenProgram.program, "u_framebufferTexture"); g_msaaSamplesFullscreenLocation = glGetUniformLocation(g_fullscreenProgram.program, "u_msaaSamples"); g_exposureFullscreenLocation = glGetUniformLocation(g_fullscreenProgram.program, "u_exposure"); g_gammaFullscreenLocation = glGetUniformLocation(g_fullscreenProgram.program, "u_gamma"); // // glusLoadTextFile("../Example33/shader/background.vert.glsl", &vertexSource); glusLoadTextFile("../Example33/shader/background.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_backgroundProgram, (const GLUSchar**)&vertexSource.text, 0, 0, 0, (const GLUSchar**)&fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); // g_viewProjectionMatrixBackgroundLocation = glGetUniformLocation(g_backgroundProgram.program, "u_viewProjectionMatrix"); g_textureBackgroundLocation = glGetUniformLocation(g_backgroundProgram.program, "u_texture"); g_vertexBackgroundLocation = glGetAttribLocation(g_backgroundProgram.program, "a_vertex"); // // Setting up the full screen frame buffer. // glGenTextures(1, &g_fullscreenTexture); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, g_fullscreenTexture); // Create MSAA texture. glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, MSAA_SAMPLES, GL_RGB32F, SCREEN_WIDTH, SCREEN_HEIGHT, GL_TRUE); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); // No need to access the depth buffer, so a render buffer is sufficient. glGenRenderbuffers(1, &g_fullscreenDepthRenderbuffer); glBindRenderbuffer(GL_RENDERBUFFER, g_fullscreenDepthRenderbuffer); glRenderbufferStorageMultisample(GL_RENDERBUFFER, MSAA_SAMPLES, GL_DEPTH_COMPONENT, SCREEN_WIDTH, SCREEN_HEIGHT); glBindRenderbuffer(GL_RENDERBUFFER, 0); // glGenFramebuffers(1, &g_fullscreenFBO); glBindFramebuffer(GL_FRAMEBUFFER, g_fullscreenFBO); // Attach the color buffer ... glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, g_fullscreenTexture, 0); // ... and the depth buffer. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, g_fullscreenDepthRenderbuffer); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { printf("GL_FRAMEBUFFER_COMPLETE error 0x%x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); return GLUS_FALSE; } glBindFramebuffer(GL_FRAMEBUFFER, 0); // // // for (i = 0; i < 2; i++) { if (i == 0) { buffer[21] = 's'; } else { buffer[21] = 'd'; } for (k = 0; k < NUMBER_ROUGHNESS; k++) { if (i == 1 && k > 0) { continue; } buffer[18] = '0' + k / 10; buffer[19] = '0' + k % 10; for (m = 0; m < 6; m++) { if (m % 2 == 0) { buffer[12] = 'P'; buffer[13] = 'O'; buffer[14] = 'S'; } else { buffer[12] = 'N'; buffer[13] = 'E'; buffer[14] = 'G'; } switch (m) { case 0: case 1: buffer[16] = 'X'; break; case 2: case 3: buffer[16] = 'Y'; break; case 4: case 5: buffer[16] = 'Z'; break; } printf("Loading '%s' ...", buffer); if (!glusLoadHdrImage(buffer, &image[i*NUMBER_ROUGHNESS*6 + k*6 + m])) { printf(" error!\n"); continue; } printf(" done.\n"); } } } glGenTextures(1, &g_texture[0]); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, g_texture[0]); glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGB32F, image[0].width, image[0].height, 6*NUMBER_ROUGHNESS, 0, GL_RGB, GL_FLOAT, 0); glusLogPrintError(GLUS_LOG_INFO, "glTexImage3D()"); for (i = 0; i < NUMBER_ROUGHNESS; i++) { for (k = 0; k < 6; k++) { glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, 0, 0, 6*i + k, image[i*6 + k].width, image[i*6 + k].height, 1, image[i*6 + k].format, GL_FLOAT, image[i*6 + k].data); glusLogPrintError(GLUS_LOG_INFO, "glTexSubImage3D() %d %d", i, k); } } glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, 0); // glGenTextures(1, &g_texture[1]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_CUBE_MAP, g_texture[1]); for (i = 0; i < 6; i++) { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, image[i + 6*NUMBER_ROUGHNESS].format, image[i + 6*NUMBER_ROUGHNESS].width, image[i + 6*NUMBER_ROUGHNESS].height, 0, image[i + 6*NUMBER_ROUGHNESS].format, GL_FLOAT, image[i + 6*NUMBER_ROUGHNESS].data); glusLogPrintError(GLUS_LOG_INFO, "glTexImage2D() %d", i); } glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_CUBE_MAP, 0); // printf("Loading 'doge2/EnvironmentBRDF_1024.data' ..."); if (!glusLoadBinaryFile("doge2/EnvironmentBRDF_1024.data", &rawimage)) { printf(" error!\n"); } else { printf(" done.\n"); } glGenTextures(1, &g_texture[2]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, g_texture[2]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32F, 1024, 1024, 0, GL_RG, GL_FLOAT, (GLfloat*)rawimage.binary); glusLogPrintError(GLUS_LOG_INFO, "glTexImage2D()"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); glusDestroyBinaryFile(&rawimage); // for (i = 0; i < 2; i++) { for (k = 0; k < NUMBER_ROUGHNESS; k++) { if (i == 1 && k > 0) { continue; } for (m = 0; m < 6; m++) { glusDestroyHdrImage(&image[i*NUMBER_ROUGHNESS*6 + k*6 + m]); } } } // glusCreateSpheref(&backgroundSphere, 500.0f, 32); g_numberIndicesBackground = backgroundSphere.numberIndices; glGenBuffers(1, &g_verticesBackgroundVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesBackgroundVBO); glBufferData(GL_ARRAY_BUFFER, backgroundSphere.numberVertices * 4 * sizeof(GLfloat), (GLfloat*)backgroundSphere.vertices, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesBackgroundVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesBackgroundVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, backgroundSphere.numberIndices * sizeof(GLuint), (GLuint*)backgroundSphere.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusDestroyShapef(&backgroundSphere); // // // Use a helper function to load an wavefront object file. glusLoadObjFile("venusm.obj", &wavefront); g_numberVerticesModel = wavefront.numberVertices; glGenBuffers(1, &g_verticesModelVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesModelVBO); glBufferData(GL_ARRAY_BUFFER, wavefront.numberVertices * 4 * sizeof(GLfloat), (GLfloat*)wavefront.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsModelVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsModelVBO); glBufferData(GL_ARRAY_BUFFER, wavefront.numberVertices * 3 * sizeof(GLfloat), (GLfloat*)wavefront.normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glusDestroyShapef(&wavefront); // glUseProgram(g_modelProgram.program); glUniform4fv(g_eyeModelLocation, 1, g_eye); glUniform1i(g_textureSpecularModelLocation, 0); glUniform1i(g_textureDiffuseModelLocation, 1); glUniform1i(g_textureLUTModelLocation, 2); glUniform1f(g_roughnessScaleModelLocation, (GLfloat)(NUMBER_ROUGHNESS - 1)); glGenVertexArrays(1, &g_modelVAO); glBindVertexArray(g_modelVAO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesModelVBO); glVertexAttribPointer(g_vertexModelLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexModelLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsModelVBO); glVertexAttribPointer(g_normalModelLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalModelLocation); // glUseProgram(g_fullscreenProgram.program); glUniform1i(g_framebufferTextureFullscreenLocation, 0); glUniform1i(g_msaaSamplesFullscreenLocation, MSAA_SAMPLES); glGenVertexArrays(1, &g_fullscreenVAO); glBindVertexArray(g_fullscreenVAO); // glUseProgram(g_backgroundProgram.program); glUniform1i(g_textureBackgroundLocation, 0); glGenVertexArrays(1, &g_backgroundVAO); glBindVertexArray(g_backgroundVAO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesBackgroundVBO); glVertexAttribPointer(g_vertexBackgroundLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexBackgroundLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesBackgroundVBO); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_CULL_FACE); return GLUS_TRUE; }