void pool::init_object_matrix(int num_obj, GLfloat scale_coeff_x, GLfloat scale_coeff_y, GLfloat scale_coeff_z, GLfloat translate_coeff_x, GLfloat translate_coeff_y, GLfloat translate_coeff_z, GLfloat rotate_coeff_x, GLfloat rotate_coeff_z) { float4x4 rotationMatrix, scaleMatrix, translateMatrix; float4x4 transformMatrix1, transformMatrix2; glusRotateRzRyRxf(rotationMatrix.L(), rotate_coeff_x, 0.0f, rotate_coeff_z); glusScalef(scaleMatrix.L(), scale_coeff_x, scale_coeff_y, scale_coeff_z); glusTranslatef(translateMatrix.L(), translate_coeff_x, translate_coeff_y, translate_coeff_z); glusMultMatrixf(transformMatrix1.L(), rotationMatrix.L(), scaleMatrix.L()); glusMultMatrixf(object_matrix_[num_obj].L(), translateMatrix.L(), transformMatrix1.L()); glusInverseMatrixf(inversed_object_matrix_[num_obj].L(), object_matrix_[num_obj].L()); size_x_[num_obj] = (int) scale_coeff_x; size_z_[num_obj] = (int) scale_coeff_z; }
/** * 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; }
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; }