This commit is contained in:
karl 2020-01-14 13:14:33 +01:00
parent 704081dbc3
commit cbb0dc1164
7 changed files with 59 additions and 3 deletions

View File

@ -7,7 +7,7 @@ find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)
find_package(glm REQUIRED)
add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp ECS/Components/Transform.h ECS/Components/Movement.h ECS/Events/InputEvent.h ECS/Events/MouseMoveEvent.h ECS/Systems/GravitySystem.h ECS/Systems/PositionDebugSystem.h ECS/Systems/KeyboardMovementSystem.h ECS/Components/Camera.h ECS/Systems/RenderSystem.h ECS/Components/Mesh.h ECS/Systems/MouseLookSystem.h ECS/Components/MouseLook.h ECS/Components/ObjMesh.h Util/stb_setup.cpp)
add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp ECS/Components/Transform.h ECS/Components/Movement.h ECS/Events/InputEvent.h ECS/Events/MouseMoveEvent.h ECS/Systems/GravitySystem.h ECS/Systems/PositionDebugSystem.h ECS/Systems/KeyboardMovementSystem.h ECS/Components/Camera.h ECS/Systems/RenderSystem.h ECS/Components/Mesh.h ECS/Systems/MouseLookSystem.h ECS/Components/MouseLook.h ECS/Components/ObjMesh.h Util/stb_setup.cpp ECS/Components/Texture.h)
include_directories(${OPENGL_INCLUDE_DIRS})

37
ECS/Components/Texture.h Normal file
View File

@ -0,0 +1,37 @@
//
// Created by karl on 14.01.20.
//
#ifndef ECSGAME_TEXTURE_H
#define ECSGAME_TEXTURE_H
#include "../../Util/stb_image.h"
struct Texture {
explicit Texture(const std::string& path) {
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Mipmapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, nrChannels;
unsigned char *data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D); // Mipmapping
} else {
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
unsigned int id;
};
#endif //ECSGAME_TEXTURE_H

View File

@ -11,6 +11,7 @@
#include "../Components/Camera.h"
#include "../../Rendering/Shader.h"
#include "../Components/ObjMesh.h"
#include "../Components/Texture.h"
using namespace ECS;
@ -39,6 +40,14 @@ public:
mesh->render();
});
// Render ObjMeshes with textures
pWorld->each<ObjMesh, Transform, Texture>([&](Entity *ent, ComponentHandle<ObjMesh> mesh, ComponentHandle<Transform> transform, ComponentHandle<Texture> texture) {
shader.setMat4("model", transform->matrix);
glBindTexture(GL_TEXTURE_2D, texture->id);
mesh->render();
});
});
}

BIN
Resources/tex.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 MiB

View File

@ -1,7 +1,11 @@
#version 320 es
out mediump vec4 FragColor;
in mediump vec2 TexCoord;
uniform sampler2D ourTexture;
void main()
{
FragColor = vec4(1.0, 1.0, 1.0, 1.0);
FragColor = texture(ourTexture, TexCoord);
}

View File

@ -1,6 +1,9 @@
#version 320 es
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 uvPos;
layout (location = 1) in vec3 NORMAL;
layout (location = 2) in vec2 UV;
out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
@ -9,4 +12,5 @@ uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
TexCoord = UV;
}

View File

@ -13,6 +13,7 @@
#include "ECS/Systems/RenderSystem.h"
#include "ECS/Systems/MouseLookSystem.h"
#include "ECS/Components/ObjMesh.h"
#include "ECS/Components/Texture.h"
using namespace ECS;
@ -82,6 +83,7 @@ int main() {
Entity *box2 = world->create();
box2->assign<Transform>();
box2->assign<ObjMesh>("Resources/Monkey.obj");
box2->assign<Texture>("Resources/tex.jpg");
box2->get<Transform>()->translate(glm::vec3(0.0f, 0.0f, -5.0f));
Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs");