Started working on texturing

master
Barney Wilks 2018-07-07 20:33:56 +01:00
parent 6e7fadd8fc
commit 7099ca847e
6 changed files with 8156 additions and 2 deletions

View File

@ -7,6 +7,7 @@ project(${project_name})
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(code_root voxels-sandbox)
set(libs_root ${code_root}/third_party)
set(src_root ${code_root}/src)
set(src
@ -15,6 +16,9 @@ set(src
${src_root}/q3perlin.cc
${src_root}/q3renderer.cc
${src_root}/q3window.cc
${src_root}/q3texture.cc
${libs_root}/lodepng.cc
)
set(headers_root ${code_root}/include/qub3d)
@ -25,9 +29,14 @@ set(headers
${headers_root}/q3renderer.hpp
${headers_root}/q3window.hpp
${headers_root}/q3shared_constants.hpp
${headers_root}/q3texture.hpp
${libs_root}/lodepng/lodepng.h
)
include_directories(${code_root}/include)
include_directories(${libs_root})
add_executable(${project_name} ${src} ${headers})
# Configure dependencies

View File

@ -0,0 +1,14 @@
#pragma once
class Q3Texture {
public:
Q3Texture(const char *filepath);
void bind();
void unbind();
unsigned int getTextureId();
private:
unsigned int m_glTexture;
};

View File

@ -2,9 +2,11 @@
#include <qub3d/q3opengl.hpp>
#include <qub3d/q3shared_constants.hpp>
#include <qub3d/q3texture.hpp>
#include <glm/gtc/matrix_transform.hpp>
Q3Texture *texture;
Q3Renderer::Q3Renderer()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
@ -23,6 +25,10 @@ Q3Renderer::Q3Renderer()
glm::mat4 perspective = glm::perspective(Q3_FOV, (float)Q3_WINDOWWIDTH / Q3_WINDOWHEIGHT, 0.1f, 100.0f);
glLoadMatrixf(&perspective[0][0]);
glEnable(GL_TEXTURE_2D);
texture = new Q3Texture("assets/grass.png");
}
void Q3Renderer::clear()
@ -44,11 +50,17 @@ void Q3Renderer::drawCube(float x, float y, float z, float sx, float sy, float s
glBegin(GL_QUADS);
{
// top
glColor3f(topCol.x, topCol.y, topCol.z);
//glColor3f(topCol.x, topCol.y, topCol.z);
texture->bind();
glTexCoord2f(0, 0);
glVertex3f(sx, sy, -sz);
glTexCoord2f(0, 1);
glVertex3f(-sx, sy, -sz);
glTexCoord2f(1, 1);
glVertex3f(-sx, sy, sz);
glTexCoord2f(1, 0);
glVertex3f(sx, sy, sz);
texture->unbind();
// bottom
glColor3f(89.f / 255.f, 87.f / 255.f, 87.f / 255.f);

View File

@ -0,0 +1,47 @@
#include <qub3d/q3texture.hpp>
#include <qub3d/q3opengl.hpp>
#include <lodepng/lodepng.h>
Q3Texture::Q3Texture(const char *filepath): m_glTexture(0)
{
glGenTextures(1, &m_glTexture);
bind();
std::vector<unsigned char> pixels;
unsigned int w, h;
lodepng::decode(pixels, w, h, filepath);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
unsigned char *pData = pixels.data();
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA,
w, h,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
pData
);
unbind();
}
unsigned int Q3Texture::getTextureId()
{
return m_glTexture;
}
void Q3Texture::bind() {
glBindTexture(GL_TEXTURE_2D, m_glTexture);
}
void Q3Texture::unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}

6302
voxels-sandbox/third_party/lodepng.cc vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff