SimpleMesh::SimpleMesh(GLuint a_programId, int a_slicesNum, int a_meshType, float a_halfSize) { if(a_meshType == SPHERE) glusCreateSpheref(&m_glusShape, a_halfSize, a_slicesNum); else if(a_meshType == CUBE) glusCreateCubef(&m_glusShape, a_halfSize); else if(a_meshType == CUBE_OPEN) glusCreateCubeOpenf(&m_glusShape, a_halfSize); else if(a_meshType == PLANE) { if(a_slicesNum <= 2) { glusCreatePlanef(&m_glusShape, a_halfSize); for(unsigned int i=0;i<2*m_glusShape.numberVertices;i++) m_glusShape.texCoords[i] *= 2.0f; } else glusCreatePlaneSlicedf(&m_glusShape, a_halfSize, a_slicesNum); } else if(a_meshType == TORUS) glusCreateTorusf(&m_glusShape, 0.5f*a_halfSize, a_halfSize, a_slicesNum, 32*a_slicesNum); else glusCreateCubef(&m_glusShape, 1.0f); CreateGPUData(a_programId); }
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) { 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; }
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) { 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); // glusLoadTextFile("../Example12_ES/shader/rendershadow.vert.glsl", &vertexSource); glusLoadTextFile("../Example12_ES/shader/rendershadow.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_programShadow, (const GLUSchar**) &vertexSource.text, (const GLUSchar**) &fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); // glusLoadTextFile("../Example12_ES/shader/useshadow.vert.glsl", &vertexSource); glusLoadTextFile("../Example12_ES/shader/useshadow.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_program, (const GLUSchar**) &vertexSource.text, (const GLUSchar**) &fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&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); // glusCreateTorusf(&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); glusDestroyShapef(&torus); // glusCreatePlanef(&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); glusDestroyShapef(&background); // glUseProgram(g_program.program); glusLookAtf(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; }