mirror of
https://github.com/Linloir/SceneEditor.git
synced 2026-02-04 12:23:34 +08:00
skybox基本实现
实现了skybox类,添加了一个skybox着色器,能正常显示,目前的bug为贴图上下颠倒
This commit is contained in:
parent
608b0fb71a
commit
12aac4c2e7
@ -114,6 +114,7 @@
|
||||
<ClCompile Include="renderable.cpp" />
|
||||
<ClCompile Include="sceneviewer.cpp" />
|
||||
<ClCompile Include="shader.cpp" />
|
||||
<ClCompile Include="skybox.cpp" />
|
||||
<ClCompile Include="texture.cpp" />
|
||||
<ClCompile Include="vao.cpp" />
|
||||
<ClCompile Include="vbo.cpp" />
|
||||
|
||||
@ -100,6 +100,9 @@
|
||||
<ClCompile Include="logger.cpp">
|
||||
<Filter>Source Files\Utils</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="skybox.cpp">
|
||||
<Filter>Source Files\OpenGL Abstractions</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="camera.h">
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
#include "shader.h"
|
||||
#include "logger.h"
|
||||
#include "model.h"
|
||||
#include "skybox.h"
|
||||
|
||||
using std::vector;
|
||||
|
||||
@ -65,10 +66,22 @@ void SceneViewer::initializeGL() {
|
||||
vertexShader.dispose();
|
||||
fragmentShader.dispose();
|
||||
|
||||
skyShader.ensureInitialized();
|
||||
|
||||
VertexShader vertexShader_sky("./temp/shaders/skyboxShader.vs");
|
||||
FragmentShader fragmentShader_sky("./temp/shaders/skyboxShader.fs");
|
||||
skyShader.attachShader(vertexShader_sky);
|
||||
skyShader.attachShader(fragmentShader_sky);
|
||||
vertexShader_sky.dispose();
|
||||
fragmentShader_sky.dispose();
|
||||
|
||||
|
||||
Model* backpackModel = new Model("D:\\ProgrammingFile\\SceneEditor\\Models\\backpack\\backpack.obj");
|
||||
Logger::info("Model loaded");
|
||||
Renderable backpack(backpackModel);
|
||||
_objects.push_back(backpack);
|
||||
|
||||
sky = new skybox("D:/ProgrammingFile/SceneEditor/skybox");
|
||||
|
||||
_camera.setPosition(glm::vec3(0.0f, 0.0f, 5.0f));
|
||||
}
|
||||
@ -93,6 +106,13 @@ void SceneViewer::paintGL() {
|
||||
}
|
||||
|
||||
_shaderProgram.unbind();
|
||||
|
||||
skyShader.bind();
|
||||
view = glm::mat4(glm::mat3(view));
|
||||
skyShader.setUniform("view", view);
|
||||
skyShader.setUniform("projection", projection);
|
||||
sky->render();
|
||||
skyShader.unbind();
|
||||
}
|
||||
|
||||
void SceneViewer::mousePressEvent(QMouseEvent* event) {
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
#include "renderable.h"
|
||||
#include "vao.h"
|
||||
#include "utils.h"
|
||||
#include "skybox.h"
|
||||
|
||||
class SceneViewer : public QOpenGLWidget, protected QOpenGLFunctions
|
||||
{
|
||||
@ -23,6 +24,8 @@ private:
|
||||
std::vector<Renderable> _objects;
|
||||
// Shader program for objects
|
||||
ShaderProgram _shaderProgram = ShaderProgram::empty();
|
||||
ShaderProgram skyShader = ShaderProgram::empty();
|
||||
skybox* sky;
|
||||
// Main camera
|
||||
Camera _camera;
|
||||
float _cameraMovementSpeed = 0.02f;
|
||||
|
||||
69
FinalProject/skybox.cpp
Normal file
69
FinalProject/skybox.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include "utils.h"
|
||||
#include "skybox.h"
|
||||
#include "shader.h"
|
||||
#include "camera.h"
|
||||
|
||||
#include <STBImage/stb_image.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
skybox::skybox(std::string path){
|
||||
faces.clear();
|
||||
faces.push_back(path + "/right.jpg");
|
||||
faces.push_back(path + "/left.jpg");
|
||||
faces.push_back(path + "/top.jpg");
|
||||
faces.push_back(path + "/bottom.jpg");
|
||||
faces.push_back(path + "/front.jpg");
|
||||
faces.push_back(path + "/back.jpg");
|
||||
|
||||
cubemapTexture = loadCubemap(faces);
|
||||
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &skyboxVAO);
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &skyboxVBO);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(skyboxVAO);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
|
||||
OPENGL_EXTRA_FUNCTIONS->glEnableVertexAttribArray(0);
|
||||
OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
}
|
||||
|
||||
void skybox::render() {
|
||||
OPENGL_EXTRA_FUNCTIONS->glDepthFunc(GL_LEQUAL); // change depth function so depth test passes when values are equal to depth buffer's content
|
||||
// skybox cube
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(skyboxVAO);
|
||||
OPENGL_EXTRA_FUNCTIONS->glActiveTexture(GL_TEXTURE0);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
|
||||
OPENGL_EXTRA_FUNCTIONS->glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||
OPENGL_EXTRA_FUNCTIONS->glDepthFunc(GL_LESS); // set depth function back to default
|
||||
}
|
||||
|
||||
unsigned int skybox::loadCubemap(std::vector<std::string> faces)
|
||||
{
|
||||
unsigned int textureID;
|
||||
OPENGL_FUNCTIONS->glGenTextures(1, &textureID);
|
||||
OPENGL_FUNCTIONS->glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
|
||||
|
||||
int width, height, nrChannels;
|
||||
for (unsigned int i = 0; i < faces.size(); i++)
|
||||
{
|
||||
unsigned char* data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0);
|
||||
if (data)
|
||||
{
|
||||
OPENGL_FUNCTIONS->glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||
stbi_image_free(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
|
||||
stbi_image_free(data);
|
||||
}
|
||||
}
|
||||
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
|
||||
return textureID;
|
||||
}
|
||||
BIN
skybox/back.jpg
Normal file
BIN
skybox/back.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 723 KiB |
BIN
skybox/bottom.jpg
Normal file
BIN
skybox/bottom.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 274 KiB |
BIN
skybox/front.jpg
Normal file
BIN
skybox/front.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 462 KiB |
BIN
skybox/left.jpg
Normal file
BIN
skybox/left.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 588 KiB |
BIN
skybox/right.jpg
Normal file
BIN
skybox/right.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 525 KiB |
BIN
skybox/top.jpg
Normal file
BIN
skybox/top.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 338 KiB |
Loading…
x
Reference in New Issue
Block a user