Add texture support
parent
6c96181ead
commit
d49409718c
|
@ -25,7 +25,6 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
|||
set(OpenGL_GL_PREFERENCE GLVND)
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(GLEW REQUIRED)
|
||||
find_library(SOIL_LIBRARY SOIL lib/libSOIL.a)
|
||||
find_library(GLM REQUIRED)
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
|
@ -42,12 +41,13 @@ set(SOURCE_LIST
|
|||
src/graph/Node.cpp
|
||||
src/graph/Component.cpp
|
||||
src/game/MeshDrawComponent.cpp
|
||||
src/tests/tests.cpp)
|
||||
src/tests/tests.cpp
|
||||
src/libs/stb_image.c)
|
||||
|
||||
file(MAKE_DIRECTORY "bin")
|
||||
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin")
|
||||
set(EXECUTABLE_NAME "castle")
|
||||
|
||||
include_directories(${GLEW_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${GLFW_INCLUDE_DIR} ${GLM_INCLUDE_DIR} /usr/include/SOIL/)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/src//libs/ ${GLEW_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${GLFW_INCLUDE_DIR} ${GLM_INCLUDE_DIR})
|
||||
add_executable(${EXECUTABLE_NAME} ${SOURCE_LIST})
|
||||
target_link_libraries(${EXECUTABLE_NAME} ${STANDARD_TARGETS} ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} ${GLFW_LIBRARIES} ${SOIL_LIBRARY} ${GLM_LIBRARY})
|
||||
target_link_libraries(${EXECUTABLE_NAME} ${STANDARD_TARGETS} ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} ${GLFW_LIBRARIES} ${GLM_LIBRARY})
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 181 KiB |
|
@ -1,6 +1,12 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
||||
in vec3 ourColor;
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform sampler2D ourTexture;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = texture(ourTexture, TexCoord) * vec4(ourColor, 1.0);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,14 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
layout (location = 2) in vec2 aTexCoord;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||
out vec3 ourColor;
|
||||
out vec2 TexCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
ourColor = aColor;
|
||||
TexCoord = aTexCoord;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#include <utility>
|
||||
#include "../engine/gl.hpp"
|
||||
|
||||
#include "MeshDrawComponent.hpp"
|
||||
#include <stb_image.h>
|
||||
#include <iostream>
|
||||
|
||||
const std::string MeshDrawComponent::className = "MeshDrawComponent";
|
||||
|
||||
|
@ -12,10 +13,11 @@ MeshDrawComponent::MeshDrawComponent(Node::Ptr node, const ShaderProgram &shader
|
|||
glGenVertexArrays(1, &vao);
|
||||
|
||||
float vertices[] = {
|
||||
0.5f, 0.5f, 0.0f, // top right
|
||||
0.5f, -0.5f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, // bottom left
|
||||
-0.5f, 0.5f, 0.0f // top left
|
||||
// positions // colors // texture coords
|
||||
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
|
||||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
|
||||
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
|
||||
};
|
||||
unsigned int indices[] = { // note that we start from 0!
|
||||
0, 1, 3, // first triangle
|
||||
|
@ -23,15 +25,50 @@ MeshDrawComponent::MeshDrawComponent(Node::Ptr node, const ShaderProgram &shader
|
|||
};
|
||||
|
||||
glBindVertexArray(vao);
|
||||
|
||||
//
|
||||
// VBO
|
||||
//
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);// position attribute
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
// color attribute
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
// texture coord attribute
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
|
||||
//
|
||||
// EBO
|
||||
//
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
|
||||
|
||||
//
|
||||
// Textures
|
||||
//
|
||||
int width, height, nrChannels;
|
||||
unsigned char *data = stbi_load("assets/container.jpg", &width, &height, &nrChannels, 0);
|
||||
if (!data) {
|
||||
std::cerr << "Error! unable to load image" << std::endl;
|
||||
}
|
||||
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
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);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
stbi_image_free(data);
|
||||
|
||||
|
||||
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
@ -44,6 +81,7 @@ void MeshDrawComponent::update(float delta) {
|
|||
void MeshDrawComponent::draw() const {
|
||||
shader.use();
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glBindVertexArray(vao);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include "../engine/ShaderProgram.hpp"
|
||||
|
||||
class MeshDrawComponent : public Component {
|
||||
unsigned int vbo, ebo, vao;
|
||||
unsigned int vbo, ebo, vao, texture;
|
||||
const ShaderProgram &shader;
|
||||
|
||||
public:
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue