GLUSvoid GLUSAPIENTRY glusRotatef(GLUSfloat matrix[16], GLUSfloat angle, GLUSfloat x, GLUSfloat y, GLUSfloat z) { GLUSfloat temp[16]; GLUSfloat s = sinf(2.0f*PIf*angle/360.0f); GLUSfloat c = cosf(2.0f*PIf*angle/360.0f); GLUSfloat vector[3] = {x, y, z}; glusNormalizef(vector); x = vector[0]; y = vector[1]; z = vector[2]; glusLoadIdentityf(temp); temp[0] = x*x*(1 - c) + c; temp[1] = x*y*(1 - c) + z*s; temp[2] = x*z*(1 - c) - y*s; temp[4] = x*y*(1 - c) - z*s; temp[5] = y*y*(1 - c) + c; temp[6] = y*z*(1 - c) + x*s; temp[8] = x*z*(1 - c) + y*s; temp[9] = y*z*(1 - c) - x*s; temp[10] = z*z*(1 - c) + c; glusMultMatrixf(matrix, matrix, temp); }
GLUSvoid GLUSAPIENTRY glusRotateRzRyRxf(GLUSfloat matrix[16], GLUSfloat anglex, GLUSfloat angley, GLUSfloat anglez) { GLUSfloat temp[16]; GLUSfloat rx = 2.0f*PIf*anglex/360.0f; GLUSfloat ry = 2.0f*PIf*angley/360.0f; GLUSfloat rz = 2.0f*PIf*anglez/360.0f; GLUSfloat sx = sinf(rx); GLUSfloat cx = cosf(rx); GLUSfloat sy = sinf(ry); GLUSfloat cy = cosf(ry); GLUSfloat sz = sinf(rz); GLUSfloat cz = cosf(rz); glusLoadIdentityf(temp); temp[0] = cy*cz; temp[1] = cy*sz; temp[2] = -sy; temp[4] = cz*sx*sy-cx*sz; temp[5] = cx*cz+sx*sy*sz; temp[6] = cy*sx; temp[8] = cx*cz*sy+sx*sz; temp[9] = -cz*sx+cx*sy*sz; temp[10] = cx*cy; glusMultMatrixf(matrix, matrix, temp); }
GLUSvoid GLUSAPIENTRY glusScalef(GLUSfloat matrix[16], GLUSfloat x, GLUSfloat y, GLUSfloat z) { GLUSfloat temp[16]; glusLoadIdentityf(temp); temp[0] = x; temp[5] = y; temp[10] = z; glusMultMatrixf(matrix, matrix, temp); }
GLUSvoid GLUSAPIENTRY glusTranslatef(GLUSfloat matrix[16], GLUSfloat x, GLUSfloat y, GLUSfloat z) { GLUSfloat temp[16]; glusLoadIdentityf(temp); temp[12] = x; temp[13] = y; temp[14] = z; glusMultMatrixf(matrix, matrix, temp); }
/** * Function to render and display content. */ GLUSboolean update(GLUSfloat time) { // Angle for rotation static GLfloat angle = 0.0f; // Matrix for the model GLfloat model[16]; // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/clear.html glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Calculate the model matrix ... glusLoadIdentityf(model); glusRotateRzRyRxf(model, 45.0f, angle, 0.0f); // ... and the view matrix ... glusLookAtf(g_modelView, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glusCopyMatrixf(g_inverseCamera, g_modelView, GLUS_TRUE); glusInverseMatrixf(g_inverseCamera, g_inverseCamera); // ... to get the final model view matrix glusMultMatrixf(g_modelView, g_modelView, model); // 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/glUniform.xml glUniformMatrix4fv(g_inverseCameraLocation, 1, GL_FALSE, g_inverseCamera); // http://www.opengl.org/sdk/docs/man/xhtml/glDrawElements.xml glDrawElements(GL_TRIANGLES, 6 * 2 * 3, GL_UNSIGNED_INT, 0); angle += 20.0f * time; return GLUS_TRUE; }
/** * Renders the water */ GLUSvoid renderWater(GLUSfloat passedTime, GLUSfloat angle) { static WaveParameters waveParameters[NUMBERWAVES]; static WaveDirections waveDirections[NUMBERWAVES]; static GLfloat overallSteepness = 0.2f; GLfloat model[16]; // Calculate the model matrix ... glusLoadIdentityf(model); // ... and the view matrix ... glusLookAtf(g_modelView, 0.0f, 1.0f, 0.0f, (GLfloat)0.5f * sinf(angle), 1.0f, -(GLfloat)0.5f * cosf(angle), 0.0f, 1.0f, 0.0f); // ... to get the final model view matrix glusMultMatrixf(g_modelView, g_modelView, model); // Copy the original view matrix ... glusCopyMatrixf(g_inverseCamera, g_modelView, GLUS_TRUE); // ... and inverse it glusInverseMatrixf(g_inverseCamera, g_inverseCamera); memset(waveParameters, 0, sizeof(waveParameters)); memset(waveDirections, 0, sizeof(waveDirections)); // Waves can be faded in and out. // Wave One waveParameters[0].speed = 1.0f; waveParameters[0].amplitude = 0.01f; waveParameters[0].wavelength = 4.0f; waveParameters[0].steepness = overallSteepness/(waveParameters[0].wavelength*waveParameters[0].amplitude*(GLfloat)NUMBERWAVES); waveDirections[0].x = +1.0f; waveDirections[0].z = +1.0f; // Wave Two waveParameters[1].speed = 0.5f; waveParameters[1].amplitude = 0.02f; waveParameters[1].wavelength = 3.0f; waveParameters[1].steepness = overallSteepness/(waveParameters[1].wavelength*waveParameters[1].amplitude*(GLfloat)NUMBERWAVES); waveDirections[1].x = +1.0f; waveDirections[1].z = +0.0f; // Wave Thre waveParameters[2].speed = 0.1f; waveParameters[2].amplitude = 0.015f; waveParameters[2].wavelength = 2.0f; waveParameters[2].steepness = overallSteepness/(waveParameters[1].wavelength*waveParameters[1].amplitude*(GLfloat)NUMBERWAVES); waveDirections[2].x = -0.1f; waveDirections[2].z = -0.2f; // Wave Four waveParameters[3].speed = 1.1f; waveParameters[3].amplitude = 0.008f; waveParameters[3].wavelength = 1.0f; waveParameters[3].steepness = overallSteepness/(waveParameters[1].wavelength*waveParameters[1].amplitude*(GLfloat)NUMBERWAVES); waveDirections[3].x = -0.2f; waveDirections[3].z = -0.1f; // http://www.opengl.org/sdk/docs/man/xhtml/glUseProgram.xml glUseProgram(g_program.program); // 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/glUniform.xml glUniformMatrix4fv(g_inverseCameraLocation, 1, GL_FALSE, g_inverseCamera); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniform1f(g_passedTimeLocation, passedTime); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniform4fv(g_waveParametersLocation, 4*NUMBERWAVES, (GLfloat*)waveParameters); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniform2fv(g_waveDirectionsLocation, 2*NUMBERWAVES, (GLfloat*)waveDirections); // ToDo: glBindVertexArray(g_vao); // ToDo: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/disable.html glDisable(GL_CULL_FACE); // http://www.opengl.org/sdk/docs/man/xhtml/glDrawElements.xml glDrawElements(GL_TRIANGLE_STRIP, WATER_PLANE_LENGTH*(WATER_PLANE_LENGTH-1)*2, GL_UNSIGNED_INT, 0); }
GLUSboolean GLUSAPIENTRY glusInverseMatrixf(GLUSfloat matrix[16], const GLUSfloat source[16]) { GLUSuint i; GLUSint column; GLUSint row; GLUSfloat copy[16]; glusLoadIdentityf(matrix); // // Copy the original matrix as we want to manipulate it // for (i = 0; i < 16; i++) { copy[i] = source[i]; } // // Make triangle form // for (column = 0; column < 4; column++) { GLUSuint row; for (row = column; row < 4; row++) { // // Is row all zero, then return false // if (glusIsRowZerof(copy, row)) return GLUS_FALSE; // // Divide, if not zero, by copy[column*4+row] // if (copy[column*4+row] != 0.0f) { glusDevideRowByf(matrix, copy, row, copy[column*4+row]); } } // // Is column all zero, then return false // if (glusIsColumnZerof(copy, column)) return GLUS_FALSE; // // Is pivot [column*4+column] = 0.0f // for (row = column+1; row < 4; row++) { if (copy[column*4+row] == 1.0f) { // // Swap with pivot row = column // glusSwapRowf(matrix, copy, column, row); break; } } for (row = column+1; row < 4; row++) { // // Subtract, [column*4+row] not zero, current row minus pivot row = column // if (copy[column*4+row] != 0.0f) { glusAddRowf(matrix, copy, row, column, -1.0f); } } } // // Make diagonal form // for (column = 3; column >= 0; column--) { for (row = column-1; row >= 0; row--) { // // Subtract, if [column*4+row] not zero, current row minus pivot row = column with factor [column*4+row] // if (copy[column*4+row] != 0.0f) { glusAddRowf(matrix, copy, row, column, -copy[column*4+row]); } } } return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { try { static float elaspedTimeFromStart = 0; elaspedTimeFromStart += 10*time; glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // make our program current // glUseProgram(g_program.program); CHECK_GL_ERRORS; // Set shader variables. // Note, that you can only assign shader variables (bind or set uiforms) // if appropriate program was bind as current by glUseProgram. // setUniform(g_program.program, "g_screenWidth", g_width); setUniform(g_program.program, "g_screenHeight", g_height); setUniform(g_program.program, "g_bBoxMin", g_boxMin); setUniform(g_program.program, "g_bBoxMax", g_boxMax); setUniform(g_program.program, "g_bgColor", float4(0,0,1,0)); // Calc ray matrix. You should notice that rayMatrix is actually inverse of worldMatrix. // Because if you do some transform with an object, you need to do inverse transform with the rays. // Do not forget it please! // float4x4 rayMatrix; float4x4 camRotMatrix, camTransMatrix; glusLoadIdentityf(camRotMatrix.L()); glusLoadIdentityf(camTransMatrix.L()); glusRotateRzRyRxf(camRotMatrix.L(), -input.cam_rot[0], -input.cam_rot[1], 0.0f); glusTranslatef(camTransMatrix.L(), g_camPos.x, g_camPos.y, g_camPos.z); glusMultMatrixf(rayMatrix.L(), camRotMatrix.L(), camTransMatrix.L()); setUniform(g_program.program, "g_rayMatrix", rayMatrix); // Now finally draw something // disable depth and cull tests, because we just want to draw full screen quad // glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glViewport(0, 0, g_width, g_height); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); g_pFullScreenQuad->Draw(); return GLUS_TRUE; } catch(std::runtime_error e) { std::cerr << e.what() << std::endl; exit(-1); } catch(...) { std::cerr << "Unexpected Exception(render)!" << std::endl; exit(-1); } }
/** * Function for initialization. */ GLUSboolean init(GLUSvoid) { // Matrix for the model GLfloat model[16]; GLUSshape cube; GLUStgaimage image; GLUStextfile vertexSource; GLUStextfile fragmentSource; // Load the source of the vertex shader. glusLoadTextFile("../Example05/Vertex.vs", &vertexSource); // Load the source of the fragment shader. glusLoadTextFile("../Example05/Fragment.fs", &fragmentSource); // Build and ... glusBuildProgram(&g_program, (const GLUSchar**)&vertexSource.text, 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); glusCreateCubef(&cube, 0.5f); numberIndices = cube.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/glGetUniformLocation.xml g_textureLocation = glGetUniformLocation(g_program.program, "firstTexture"); // 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/glGetAttribLocation.xml g_texCoordLocation = glGetAttribLocation(g_program.program, "texCoord"); // ToDo: glBindFragDataLocation(g_program.program, 0, "fragColor"); // 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, cube.numberVertices*4*sizeof(GLfloat), (GLfloat*)cube.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, cube.numberVertices*3*sizeof(GLfloat), (GLfloat*)cube.normals, GL_STATIC_DRAW); // http://www.opengl.org/sdk/docs/man/xhtml/glGenBuffers.xml glGenBuffers(1, &g_texCoords); // http://www.opengl.org/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_texCoords); // http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml glBufferData(GL_ARRAY_BUFFER, cube.numberVertices*2*sizeof(GLfloat), (GLfloat*)cube.texCoords, 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, cube.numberIndices*sizeof(GLuint), (GLuint*)cube.indices, GL_STATIC_DRAW); glusDestroyShapef(&cube); glusLoadTgaImage("crate.tga", &image); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/gentextures.html glGenTextures(1, &g_texture); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/bindtexture.html glBindTexture(GL_TEXTURE_2D, g_texture); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html glTexImage2D(GL_TEXTURE_2D, 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_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/texparameter.html glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // http://www.opengl.org/sdk/docs/man/xhtml/glUseProgram.xml glUseProgram(g_program.program); // Calculate the model matrix ... glusLoadIdentityf(model); glusRotateRzRyRxf(model, 30.0f, 30.0f, 0.0f); // ... and the view matrix ... glusLookAtf(g_modelView, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // ... to get the final model view matrix glusMultMatrixf(g_modelView, g_modelView, model); // 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/sdk/docs/man/xhtml/glBindBuffer.xml glBindBuffer(GL_ARRAY_BUFFER, g_texCoords); // http://www.opengl.org/sdk/docs/man/xhtml/glVertexAttribPointer.xml glVertexAttribPointer(g_texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); // http://www.opengl.org/sdk/docs/man/xhtml/glEnableVertexAttribArray.xml glEnableVertexAttribArray(g_texCoordLocation); // http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniform1i(g_textureLocation, 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); //http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/enable.html glEnable(GL_CULL_FACE); return GLUS_TRUE; }