(broken) texture loading
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
@@ -77,6 +77,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>.\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<AdditionalLibraryDirectories>.\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
@@ -124,11 +125,15 @@
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="mesh.cpp" />
|
||||
<ClCompile Include="shader.cpp" />
|
||||
<ClCompile Include="stb_image.c" />
|
||||
<ClCompile Include="texture.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="headers/display.h" />
|
||||
<ClInclude Include="headers/mesh.h" />
|
||||
<ClInclude Include="headers/shader.h" />
|
||||
<ClInclude Include="display.h" />
|
||||
<ClInclude Include="mesh.h" />
|
||||
<ClInclude Include="shader.h" />
|
||||
<ClInclude Include="stb_image.h" />
|
||||
<ClInclude Include="texture.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@@ -5,21 +5,34 @@
|
||||
<ClCompile Include="display.cpp" />
|
||||
<ClCompile Include="mesh.cpp" />
|
||||
<ClCompile Include="shader.cpp" />
|
||||
<ClCompile Include="stb_image.c">
|
||||
<Filter>helpers</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="texture.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Filter Include="headers">
|
||||
<UniqueIdentifier>{3e9ef4e5-bcd4-4161-8243-90e67d85dc43}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="helpers">
|
||||
<UniqueIdentifier>{3b3efd97-0e64-421a-b292-4d37dfc52e96}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="headers/display.h">
|
||||
<ClInclude Include="mesh.h">
|
||||
<Filter>headers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="headers/mesh.h">
|
||||
<ClInclude Include="display.h">
|
||||
<Filter>headers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="headers/shader.h">
|
||||
<ClInclude Include="shader.h">
|
||||
<Filter>headers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="texture.h">
|
||||
<Filter>headers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="stb_image.h">
|
||||
<Filter>helpers</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <GL/glew.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "headers/display.h"
|
||||
#include "display.h"
|
||||
|
||||
Display::Display(int width, int height, const std::string& title) {
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
|
||||
@@ -1,29 +1,39 @@
|
||||
#include <iostream>
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "headers/display.h"
|
||||
#include "headers/mesh.h"
|
||||
#include "headers/shader.h"
|
||||
#include "display.h"
|
||||
#include "mesh.h"
|
||||
#include "shader.h"
|
||||
#include "texture.h"
|
||||
|
||||
#undef main
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Display display(800, 600, "Crumpet Engine");
|
||||
Display display(800, 800, "Crumpet Engine");
|
||||
glClearColor(0.1f, 0.45f, 0.9f, 1.0f);
|
||||
|
||||
GLfloat vertices[] = {
|
||||
-0.5f, -0.5f, 0.0f,
|
||||
0.5f, -0.5f, 0.0f,
|
||||
0.0f, 0.5f, 0.0f
|
||||
// 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
|
||||
};
|
||||
|
||||
Mesh mesh(vertices, sizeof(vertices) / sizeof(vertices[0]));
|
||||
Shader shader;
|
||||
unsigned int indices[] = {
|
||||
0, 1, 3, // first triangle
|
||||
1, 2, 3 // second triangle
|
||||
};
|
||||
|
||||
Mesh mesh(vertices, indices, sizeof(vertices) / sizeof(vertices[0]));
|
||||
Shader shader("E:/Games/Practicing/OpenGL/resources/shaders/simple2d");
|
||||
Texture chanceCube("E:/Games/Practicing/OpenGL/resources/textures/chance-cube.jpg");
|
||||
|
||||
while(!display.isClosed()) {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
shader.Bind();
|
||||
chanceCube.Bind(0);
|
||||
mesh.Draw();
|
||||
|
||||
display.Update();
|
||||
|
||||
@@ -1,20 +1,32 @@
|
||||
#include "headers/mesh.h"
|
||||
#include "mesh.h"
|
||||
|
||||
Mesh::Mesh(GLfloat *vertices, unsigned int numVerticies) {
|
||||
Mesh::Mesh(GLfloat *vertices, unsigned int *indices, unsigned int numVerticies) {
|
||||
m_drawCount = numVerticies;
|
||||
|
||||
glGenVertexArrays(1, &m_VAO);
|
||||
glGenBuffers(1, &m_VBO);
|
||||
glGenBuffers(1, &m_EBO);
|
||||
|
||||
glBindVertexArray(m_VAO);
|
||||
|
||||
glGenBuffers(NUM_BUFFERS, m_VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_VBO[POSITION_VB]);
|
||||
glBufferData(GL_ARRAY_BUFFER, numVerticies * sizeof(vertices[0]), vertices, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, 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);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
std::cout << "Mesh loaded successfully" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
#include <GL/glew.h>
|
||||
|
||||
class Mesh {
|
||||
public:
|
||||
Mesh(GLfloat *vertices, unsigned int numVerticies);
|
||||
Mesh(GLfloat *vertices, unsigned int *indices, unsigned int numVerticies);
|
||||
void Draw();
|
||||
virtual ~Mesh();
|
||||
private:
|
||||
enum {
|
||||
POSITION_VB,
|
||||
NUM_BUFFERS
|
||||
NUM_BUFFERS,
|
||||
TEXCOORD_VB
|
||||
};
|
||||
|
||||
unsigned int m_VAO;
|
||||
unsigned int m_VBO[NUM_BUFFERS];
|
||||
unsigned int m_VBO;
|
||||
unsigned int m_EBO;
|
||||
|
||||
unsigned int m_drawCount;
|
||||
};
|
||||
@@ -1,22 +1,12 @@
|
||||
#include "headers/shader.h"
|
||||
#include "shader.h"
|
||||
|
||||
const char *vertexShaderSource = "#version 330 core\n"
|
||||
"layout (location = 0) in vec3 aPos;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
||||
"}\0";
|
||||
const char *fragmentShaderSource = "#version 330 core\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" FragColor = vec4(0.25f, 1.0f, 0.49f, 1.0f);\n"
|
||||
"}\n\0";
|
||||
Shader::Shader(std::string path) {
|
||||
std::string vertexPath = path + "_vertex.glsl";
|
||||
std::string fragmentPath = path + "_fragment.glsl";
|
||||
|
||||
Shader::Shader() {
|
||||
m_program = glCreateProgram();
|
||||
m_shaders[0] = CreateShader(vertexShaderSource, GL_VERTEX_SHADER);
|
||||
m_shaders[1] = CreateShader(fragmentShaderSource, GL_FRAGMENT_SHADER);
|
||||
m_shaders[0] = CreateShader(LoadFile(vertexPath), GL_VERTEX_SHADER);
|
||||
m_shaders[1] = CreateShader(LoadFile(fragmentPath), GL_FRAGMENT_SHADER);
|
||||
|
||||
for (unsigned int i = 0; i < NUM_SHADERS; i++) {
|
||||
glAttachShader(m_program, m_shaders[i]);
|
||||
@@ -70,9 +60,24 @@ void Shader::Bind() {
|
||||
}
|
||||
|
||||
std::string Shader::LoadFile(std::string path) {
|
||||
std::ifstream file;
|
||||
file.open((path).c_str());
|
||||
|
||||
std::string output;
|
||||
std::string line;
|
||||
|
||||
if (file.is_open()) {
|
||||
while (file.good()) {
|
||||
getline(file, line);
|
||||
output.append(line + "\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cout << "Unable to load shader: " << path << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Successfully loaded " + path << std::endl;
|
||||
return output;
|
||||
}
|
||||
|
||||
Shader::~Shader() {
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <GL/glew.h>
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
Shader();
|
||||
Shader(std::string path);
|
||||
void Bind();
|
||||
virtual ~Shader();
|
||||
private:
|
||||
37
OpenGL/texture.cpp
Normal file
37
OpenGL/texture.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "texture.h"
|
||||
#include "stb_image.h"
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
Texture::Texture(std::string fileName) {
|
||||
int width, height, numComponents;
|
||||
unsigned char* imageData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4);
|
||||
|
||||
if (imageData == NULL)
|
||||
std::cout << "Loading failed for texture: " << fileName << std::endl;
|
||||
|
||||
glGenTextures(1, &m_texture);
|
||||
glBindTexture(GL_TEXTURE_2D, m_texture);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
|
||||
|
||||
stbi_image_free(imageData);
|
||||
std::cout << "Loaded texture: " << fileName << std::endl;
|
||||
}
|
||||
|
||||
void Texture::Bind(unsigned int unit) {
|
||||
assert(unit >= 0 && unit <= 31);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0 + unit);
|
||||
glBindTexture(GL_TEXTURE_2D, m_texture);
|
||||
}
|
||||
|
||||
Texture::~Texture() {
|
||||
glDeleteTextures(1, &m_texture);
|
||||
}
|
||||
16
OpenGL/texture.h
Normal file
16
OpenGL/texture.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <GL/glew.h>
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
Texture(std::string fileName);
|
||||
|
||||
void Bind(unsigned int unit);
|
||||
|
||||
virtual ~Texture();
|
||||
private:
|
||||
GLuint m_texture;
|
||||
};
|
||||
|
||||
11
resources/shaders/simple2d_fragment.glsl
Normal file
11
resources/shaders/simple2d_fragment.glsl
Normal file
@@ -0,0 +1,11 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec3 ourColor;
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform sampler2D ourTexture;
|
||||
|
||||
void main() {
|
||||
FragColor = texture(ourTexture, TexCoord);
|
||||
}
|
||||
13
resources/shaders/simple2d_vertex.glsl
Normal file
13
resources/shaders/simple2d_vertex.glsl
Normal file
@@ -0,0 +1,13 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
layout (location = 2) in vec2 aTexCoord;
|
||||
|
||||
out vec3 ourColor;
|
||||
out vec2 TexCoord;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
ourColor = aColor;
|
||||
TexCoord = aTexCoord;
|
||||
}
|
||||
BIN
resources/textures/chance-cube.jpg
Normal file
BIN
resources/textures/chance-cube.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 55 KiB |
Reference in New Issue
Block a user