Opengl實(shí)驗(yàn)報(bào)告及源代碼實(shí)驗(yàn)五幾何變換(word文檔良心出品)_第1頁(yè)
Opengl實(shí)驗(yàn)報(bào)告及源代碼實(shí)驗(yàn)五幾何變換(word文檔良心出品)_第2頁(yè)
Opengl實(shí)驗(yàn)報(bào)告及源代碼實(shí)驗(yàn)五幾何變換(word文檔良心出品)_第3頁(yè)
Opengl實(shí)驗(yàn)報(bào)告及源代碼實(shí)驗(yàn)五幾何變換(word文檔良心出品)_第4頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、實(shí)驗(yàn)報(bào)告學(xué)生姓名:學(xué)號(hào):專業(yè)班級(jí):實(shí)驗(yàn)類型:驗(yàn)證綜合設(shè)計(jì)創(chuàng)新實(shí)驗(yàn)日期:2018.11實(shí)驗(yàn)成績(jī):一、實(shí)驗(yàn)名稱實(shí)驗(yàn)五幾何變換二、實(shí)驗(yàn)內(nèi)容1. 編寫(xiě)程序繪制若干三維物體,將其放置在場(chǎng)景的不同位置,并讓物體繞自身的某條軸做旋轉(zhuǎn)運(yùn)動(dòng);2. 編寫(xiě)一個(gè)可在三維場(chǎng)景中自由移動(dòng)和改變觀察方向的攝像機(jī),利用鍵盤(pán)和鼠標(biāo)控制攝像機(jī)實(shí)現(xiàn)三維場(chǎng)景的動(dòng)態(tài)漫游。三、實(shí)驗(yàn)?zāi)康?. 了解縮放、平移和旋轉(zhuǎn)等模型變換的實(shí)現(xiàn)原理,掌握模型變換矩陣的使用方法,能夠利用模型變換建立三維場(chǎng)景;2. 了解視點(diǎn)變換的實(shí)現(xiàn)原理,掌握視點(diǎn)變換與攝像機(jī)功能的具體關(guān)系和利用視點(diǎn)變換矩陣構(gòu)造攝像機(jī)的具體方法。3. 了解投影變換和視口變換的實(shí)現(xiàn)原理,掌握投

2、影變換與視口變換在場(chǎng)景縮放和顯示中的作用。四、實(shí)驗(yàn)步驟1. 建立立方體幾何模型。定義立方體頂點(diǎn)的位置坐標(biāo)和紋理坐標(biāo),設(shè)置不同立方體在世界坐標(biāo)系中的位置。/ Set up vertex data and attribute pointersGLfloat vertices = -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,0.5f, -0.5f, -0.5f, 1.0f, 0.0f,0.5f, 0.5f, -0.5f, 1.0f, 1.0f,0.5f, 0.5f, -0.5f, 1.0f, 1.0f,-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,-0.5f, -

3、0.5f, -0.5f, 0.0f, 0.0f,-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,0.5f, -0.5f, 0.5f, 1.0f, 0.0f,0.5f, 0.5f, 0.5f, 1.0f, 1.0f,0.5f, 0.5f, 0.5f, 1.0f, 1.0f,-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,-0.

4、5f, -0.5f, -0.5f, 0.0f, 1.0f,-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,0.5f, 0.5f, 0.5f, 1.0f, 0.0f,0.5f, 0.5f, -0.5f, 1.0f, 1.0f,0.5f, -0.5f, -0.5f, 0.0f, 1.0f,0.5f, -0.5f, -0.5f, 0.0f, 1.0f,0.5f, -0.5f, 0.5f, 0.0f, 0.0f,0.5f, 0.5f, 0.5f, 1.0f, 0.0f,-0.5f, -0.5f, -0.5f, 0.0f, 1.0

5、f,0.5f, -0.5f, -0.5f, 1.0f, 1.0f,0.5f, -0.5f, 0.5f, 1.0f, 0.0f,0.5f, -0.5f, 0.5f, 1.0f, 0.0f,-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,0.5f, 0.5f, -0.5f, 1.0f, 1.0f,0.5f, 0.5f, 0.5f, 1.0f, 0.0f,0.5f, 0.5f, 0.5f, 1.0f, 0.0f,-0.5f, 0.5f, 0.5f, 0.0f,

6、0.0f,-0.5f, 0.5f, -0.5f, 0.0f, 1.0f;/ World space positions of our cubes glm:vec3 cubePositions = glm:vec3(0.0f, 0.0f, 0.0f), glm:vec3(2.0f, 5.0f, -15.0f),glm:vec3(-1.5f, -2.2f, -2.5f),glm:vec3(-3.8f, -2.0f, -12.3f),glm:vec3(2.4f, -0.4f, -3.5f),glm:vec3(-1.7f, 3.0f, -7.5f),glm:vec3(1.3f, -2.0f, -2.5

7、f),glm:vec3(1.5f, 2.0f, -2.5f),glm:vec3(1.5f, 0.2f, -1.5f),glm:vec3(-1.3f, 1.0f, -1.5f);2. 加載立方體模型的頂點(diǎn)數(shù)據(jù)。建立 VAO、VBO 對(duì)象變量,編寫(xiě)頂點(diǎn)數(shù)據(jù)加載函數(shù),組 織并加裝幾何模型的數(shù)據(jù)。GLuint VBO, VAO;glGenVertexArrays(1, &VAO);glGenBuffers(1, &VBO);glBindVertexArray(VAO);glBindBuffer(GL_ARRAY_BUFFER, VBO);glBufferData(GL_ARRAY_BU

8、FFER, sizeof(vertices), vertices, GL_STATIC_DRAW);/ Position attributeglVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0);/ TexCoord attributeglVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat);

9、glEnableVertexAttribArray(2);glBindVertexArray(0); / Unbind VAO3. 加載紋理圖像并生成紋理對(duì)象。 編寫(xiě)紋理加裝函數(shù), 創(chuàng)建紋理對(duì)象、 設(shè)置紋理屬性、加裝紋理圖像生成2D 紋理。/ Load and create a texture GLuint texture1; GLuint texture2;/ =/ Texture 1/ =glGenTextures(1, &texture1); glBindTexture(GL_TEXTURE_2D, texture1);/ All upcoming GL_TEXTURE_2D o

10、perations now have effect on our texture object/ Set our texture parametersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); / Set texture wrapping to GL_REPEATglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); / Set texture filteringglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_M

11、IN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);/ Load, create texture and generate mipmaps int width, height;unsigned char* image = SOIL_load_image("wall.jpg", &width, &height, 0, SOIL_LOAD_RGB);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width,

12、height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);glGenerateMipmap(GL_TEXTURE_2D);SOIL_free_image_data(image);glBindTexture(GL_TEXTURE_2D, 0); / Unbind texture when done, so we won't accidentily mess up our texture./ =/ Texture 2/ =glGenTextures(1, &texture2); glBindTexture(GL_TEXTURE_2D, texture2

13、);/ Set our texture parametersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); / Set texture filteringglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_

14、LINEAR); / Load, create texture and generate mipmapsimage = SOIL_load_image("test_rect.png", &width, &height, 0, SOIL_LOAD_RGB); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,image);glGenerateMipmap(GL_TEXTURE_2D);SOIL_free_image_data(image);glBi

15、ndTexture(GL_TEXTURE_2D, 0);4. 編寫(xiě)頂點(diǎn)著色器,在頂點(diǎn)著色器中處理頂點(diǎn)數(shù)據(jù),執(zhí)行模型、視點(diǎn)和投影變換。編寫(xiě)片段著色器,在片段著色器中進(jìn)行紋理采樣和多重紋理貼圖。#version 330 corelayout (location = 0) in vec3 position;layout (location = 2) in vec2 texCoord;out vec2 TexCoord;uniform mat4 model;uniform mat4 view;uniform mat4 projection;void main()gl_Position = projec

16、tion * view * model * vec4(position, 1.0f);TexCoord = vec2(texCoord.x, 1.0 - texCoord.y);#version 330 corein vec2 TexCoord;out vec4 color;uniform sampler2D ourTexture1;uniform sampler2D ourTexture2;void main()color = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, TexCoord), 0.2);5. 編寫(xiě)場(chǎng)景渲染函

17、數(shù),在其中綁定 VAO、啟用著色器、激活紋理對(duì)象,并將紋理數(shù)據(jù)傳送到片段著色器中指定的紋理采樣器變量,再調(diào)用繪制函數(shù)進(jìn)行場(chǎng)景繪制。/ Bind Textures using texture units glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture1); glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture1"), 0); glActiveTexture(GL_TEXTURE1);glBindTexture(GL_T

18、EXTURE_2D, texture2); glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture2"), 1);/ Activate shader ourShader.Use();6. 編寫(xiě)攝像機(jī)類,定以攝像機(jī)的位置、朝向、視點(diǎn)空間坐標(biāo)系等屬性變量,和構(gòu)造函數(shù)、視點(diǎn)空間坐標(biāo)系構(gòu)造函數(shù)。/ Create transformationsglm:mat4 view;glm:mat4 projection;view = glm:translate(view, glm:vec3(0.0f, 0.0f, -3.0

19、f);projection = glm:perspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f); / Get their uniform locationGLint modelLoc = glGetUniformLocation(ourShader.Program, "model"); GLint viewLoc = glGetUniformLocation(ourShader.Program, "view"); GLint projLoc = glGetUniformLoca

20、tion(ourShader.Program, "projection"); / Pass the matrices to the shaderglUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm:value_ptr(view);/ Note: currently we set the projection matrix each frame, but since the projection matrix rarely changes it's often best practice to set it outside the

21、main loop only once. glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm:value_ptr(projection);glBindVertexArray(VAO);for (GLuint i = 0; i < 10; i+)/ Calculate the model matrix for each object and pass it to shader before drawing glm:mat4 model;model = glm:translate(model, cubePositionsi); GLfloat angle

22、 = 20.0f * i;model = glm:rotate(model, angle, glm:vec3(1.0f, 0.3f, 0.5f); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm:value_ptr(model);glDrawArrays(GL_TRIANGLES, 0, 36);glBindVertexArray(0);/ Swap the screen buffers glfwSwapBuffers(window);7. 實(shí)現(xiàn)攝像機(jī)類的成員函數(shù)以及涉嫌機(jī)漫游的鍵盤(pán)控制函數(shù)。Camera:Camera(glm:vec3 positi

23、on, glm:vec3 up, GLfloat yaw, GLfloat pitch): front(glm:vec3(0.0f, 0.0f, -1.0f), movementSpeed(SPEED), mouseSensitivity(SENSITIVTY),zoom(ZOOM)this->position = position; this->worldUp = up; this->yaw = yaw; this->pitch = pitch; this->updateCameraVectors();Camera:Camera(GLfloat posX, GL

24、float posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ,GLfloat yaw, GLfloat pitch): front(glm:vec3(0.0f, 0.0f, -1.0f), movementSpeed(SPEED), mouseSensitivity(SENSITIVTY),zoom(ZOOM)this->position = glm:vec3(posX, posY, posZ);this->worldUp = glm:vec3(upX, upY, upZ);this->yaw = yaw;th

25、is->pitch = pitch;this->updateCameraVectors();void Camera:processKeyboard(Camera_Movement direction, float deltaTime)float velocity = movementSpeed * deltaTime;if (direction = FORWARD)position += front * velocity;if (direction = BACKWARD)position -= front * velocity;if (direction = LEFT)position -= right * velocity;if (direction = RIGHT)position += right * velocity;voidCamera:processMouseMovement(floatxoffset,floatyoffset,GLboolean constrainPitch)xoffset *= mouseSensitivity;yoffset *= mouseSensitivity;yaw += xoffset;pitch += yoffset;if

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論