Skip to content

Commit

Permalink
Texture fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ruberith committed Sep 22, 2024
1 parent 538d0c8 commit de9e0b8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion GUI/OpenGL/MiniGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ VectorXf MiniGL::m_lightPosition;
GLuint MiniGL::m_vao = 0;
GLuint MiniGL::m_vbo_vertices = 0;
GLuint MiniGL::m_vbo_normals = 0;
GLuint MiniGL::m_vbo_texcoords = 0;
GLuint MiniGL::m_vbo_faces = 0;

void MiniGL::bindTexture()
Expand Down Expand Up @@ -519,6 +520,7 @@ void MiniGL::init(const int width, const int height, const char *name, const boo
glBindVertexArray(m_vao);
glGenBuffers(1, &m_vbo_vertices);
glGenBuffers(1, &m_vbo_normals);
glGenBuffers(1, &m_vbo_texcoords);
glGenBuffers(1, &m_vbo_faces);
// Set the default normal (cf. glNormal).
glVertexAttrib3r(1, 0.0, 0.0, 1.0);
Expand Down Expand Up @@ -547,7 +549,7 @@ void MiniGL::initTexture()

glGenTextures(1, &m_texId);
glBindTexture(GL_TEXTURE_2D, m_texId);
glTexImage2D(GL_TEXTURE_2D, 0, 3, IMAGE_COLS, IMAGE_ROWS, 0, GL_RGB,
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, IMAGE_COLS, IMAGE_ROWS, 0, GL_RGB,
GL_UNSIGNED_BYTE, texData); // Create texture from image data
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Expand All @@ -574,6 +576,7 @@ void MiniGL::destroy ()
{
glDeleteBuffers(1, &m_vbo_vertices);
glDeleteBuffers(1, &m_vbo_normals);
glDeleteBuffers(1, &m_vbo_texcoords);
glDeleteBuffers(1, &m_vbo_faces);
glDeleteVertexArrays(1, &m_vao);
}
Expand Down
8 changes: 8 additions & 0 deletions GUI/OpenGL/MiniGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ namespace SPH
static GLuint m_vao;
static GLuint m_vbo_vertices;
static GLuint m_vbo_normals;
static GLuint m_vbo_texcoords;
static GLuint m_vbo_faces;

static void reshape (GLFWwindow* glfw_window, int w, int h);
Expand Down Expand Up @@ -237,6 +238,7 @@ namespace SPH
static const GLuint getVao() { return m_vao; }
static const GLuint getVboVertices() { return m_vbo_vertices; }
static const GLuint getVboNormals() { return m_vbo_normals; }
static const GLuint getVboTexcoords() { return m_vbo_texcoords; }
static const GLuint getVboFaces() { return m_vbo_faces; }

// Fill a VBO with vector data and map to the VAO attribute at the specified index.
Expand All @@ -254,6 +256,12 @@ namespace SPH
{
supplyVectors(index, m_vbo_normals, 3, numVectors, data);
}
// Fill the dedicated VBO with 2D texcoord data and map to the VAO attribute at the specified index.
template<typename T>
static void supplyTexcoords(GLuint index, unsigned int numVectors, const T* data)
{
supplyVectors(index, m_vbo_texcoords, 2, numVectors, data);
}
// Fill a VBO with index data.
static void supplyIndices(GLuint vbo, unsigned int n, const unsigned int* data);
// Fill the dedicated VBO with face index data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ namespace SPH
const unsigned int nFaces = mesh.numFaces();
const Vector3r *vertexNormals = mesh.getVertexNormals().data();

SPH::MiniGL::supplyVertices(0, mesh.getVertexNormals().size(), &pd.getPosition(offset)[0]);
SPH::MiniGL::supplyNormals(2, mesh.getVertexNormals().size(), &vertexNormals[0][0]);
SPH::MiniGL::supplyVertices(0, mesh.numVertices(), &pd.getPosition(offset)[0]);
SPH::MiniGL::supplyNormals(2, mesh.numVertices(), &vertexNormals[0][0]);
SPH::MiniGL::supplyFaces(3 * nFaces, faces);

glDrawElements(GL_TRIANGLES, (GLsizei)3 * nFaces, GL_UNSIGNED_INT, (void*)0);
Expand Down

0 comments on commit de9e0b8

Please sign in to comment.