Skip to content

Commit e986079

Browse files
[render] Proper usage of texture
1 parent 9ca3fe6 commit e986079

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

libs/glutils/glslprogram.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,13 @@ void GLSLProgram::setUniform(const char* name, GLuint val) {
312312
glUniform1ui(loc, val);
313313
}
314314

315+
void GLSLProgram::setTexture(const char* name, GLuint textureId, GLuint textureSlot)
316+
{
317+
glActiveTexture(GL_TEXTURE0 + textureSlot);
318+
glBindTexture(GL_TEXTURE_2D, textureId);
319+
glUniform1i(getUniformLocation(name), textureSlot);
320+
}
321+
315322
void GLSLProgram::setUniform(const char* name, bool val) {
316323
int loc = getUniformLocation(name);
317324
glUniform1i(loc, val);

libs/glutils/glslprogram.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <map>
66
#include <glm/glm.hpp>
77
#include <stdexcept>
8+
#include <iostream>
89

910
class GLSLProgramException : public std::runtime_error {
1011
public:
@@ -67,6 +68,8 @@ class GLSLProgram {
6768
void setUniform(const char* name, bool val);
6869
void setUniform(const char* name, GLuint val);
6970

71+
void setTexture(const char* name, GLuint textureId, GLuint textureSlot);
72+
7073
void findUniformLocations();
7174
void printActiveUniforms();
7275
void printActiveUniformBlocks();
@@ -80,6 +83,11 @@ int GLSLProgram::getUniformLocation(const char* name) {
8083

8184
if (pos == uniformLocations.end()) {
8285
GLint loc = glGetUniformLocation(handle, name);
86+
if (loc == -1)
87+
{
88+
std::cerr << "Uniform do not exist : " << name << std::endl;
89+
}
90+
8391
uniformLocations[name] = loc;
8492
return loc;
8593
}

libs/glutils/texture.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "stb/stb_image.h"
33
#include "glutils.h"
44

5+
#include <iostream>
6+
57
/*static*/
68
GLuint Texture::loadTexture(const std::string& fName) {
79
int width, height;
@@ -15,9 +17,18 @@ GLuint Texture::loadTexture(const std::string& fName) {
1517

1618
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1719
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
20+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
21+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
22+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
23+
24+
glBindTexture(GL_TEXTURE_2D, 0);
1825

1926
Texture::deletePixels(data);
2027
}
28+
else
29+
{
30+
std::cerr << "Texture not found " << fName << std::endl;
31+
}
2132

2233
return tex;
2334
}

project01/scenecloth.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void SceneCloth::initScene()
4949
computeProg.setUniform("RestLengthDiag", sqrtf(dx * dx + dy * dy));
5050

5151
glActiveTexture(GL_TEXTURE0);
52-
Texture::loadTexture("../media/texture/me_textile.png");
52+
clothTexture = Texture::loadTexture("../res/texture/me_textile.png");
5353
}
5454

5555
void SceneCloth::initBuffers()
@@ -192,8 +192,11 @@ void SceneCloth::render()
192192
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
193193

194194
// Now draw the scene
195-
renderProg.use();
196195
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
196+
renderProg.use();
197+
198+
renderProg.setTexture("Tex", clothTexture, 0);
199+
197200
view = glm::lookAt(glm::vec3(3, 2, 5), glm::vec3(2, 1, 0), glm::vec3(0, 1, 0));
198201
model = glm::mat4(1.0f);
199202
setMatrices();

project01/scenecloth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class SceneCloth : public Scene
2727

2828
GLuint clothVao;
2929
GLuint numElements;
30+
GLuint clothTexture;
3031

3132
glm::ivec2 nParticles; // Number of particles in each dimension
3233
glm::vec2 clothSize; // Size of cloth in x and y

0 commit comments

Comments
 (0)