Loading of multiple textures

This commit is contained in:
Caesar2011
2016-09-07 13:06:48 +02:00
parent 364ed31d8e
commit 4f347a6ef5
5 changed files with 33 additions and 34 deletions

BIN
img/tests/flieger.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

BIN
img/tests/flieger2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

View File

@@ -54,22 +54,6 @@ GLuint LoadTexture(char *filename,int *textw,int *texth) {
} }
void DrawTexture(int x, int y, GLuint textureid,int textw,int texth) {
//printf("x: %d\ty: %d\tw: %d\th: %d\n", x, y, textw, texth);
//int textw,texth;
// tell opengl to use the generated texture name
glBindTexture(GL_TEXTURE_2D, textureid);
glEnable(GL_TEXTURE_2D);
// make a rectangle
glBegin(GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2f(-1, -1);
glTexCoord2d(1.0,0.0); glVertex2f(1, -1);
glTexCoord2d(1.0,1.0); glVertex2f(1, 1);
glTexCoord2d(0.0,1.0); glVertex2f(-1, 1);
glEnd();
glDisable(GL_TEXTURE_2D);
}
GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){ GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){
// Create the shaders // Create the shaders
@@ -168,8 +152,9 @@ GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path
int main(int argc, char* args[]) { int main(int argc, char* args[]) {
GLFWwindow* window; GLFWwindow* window;
GLuint tex_jpe; GLuint tex1, tex2;
int textw, texth; int textw, texth;
int textw2, texth2;
/* Initialize the library */ /* Initialize the library */
@@ -209,10 +194,6 @@ int main(int argc, char* args[]) {
} }
// INIT // INIT
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// An array of 3 vectors which represents 3 vertices // An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = { static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f, // (bottom - left ) TRIANGLE 1 -1.0f, -1.0f, 0.0f, // (bottom - left ) TRIANGLE 1
@@ -247,20 +228,31 @@ int main(int argc, char* args[]) {
tex_jpe = LoadTexture("../img/tests/test.png",&textw,&texth); tex1 = LoadTexture("../img/tests/flieger.jpg",&textw,&texth);
glfwSetWindowSize (window, textw, texth); printf("Tex1: %d", tex1);
tex2 = LoadTexture("../img/tests/flieger2.jpg",&textw2,&texth2);
printf("Tex2: %d", tex2);
glfwSetWindowSize(window, textw, texth);
glViewport(0, 0, textw, texth); glViewport(0, 0, textw, texth);
glfwSetWindowSizeLimits(window, textw, texth, textw, texth); glfwSetWindowSizeLimits(window, textw, texth, textw, texth);
GLuint programID = LoadShaders("trimap.vertexshader", "trimap.fragmentshader"); GLuint programID = LoadShaders("trimap.vertexshader", "trimap.fragmentshader");
GLint textureLocation = glGetUniformLocation(programID, "myTextureSampler"); GLint textureLoc1 = glGetUniformLocation(programID, "myTextureSampler");
GLint widthLocation = glGetUniformLocation(programID, "pixWidth"); GLint textureLoc2 = glGetUniformLocation(programID, "myTextureSampler2");
GLint heigthLocation = glGetUniformLocation(programID, "pixHeight"); GLint widthLoc = glGetUniformLocation(programID, "pixWidth");
GLint heigthLoc = glGetUniformLocation(programID, "pixHeight");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tex2);
printf("Shader program ID: %d\n", programID); printf("Shader program ID: %d\n", programID);
printf("textureLocation: %d\n", textureLocation); printf("textureLoc1: %d\n", textureLoc1);
printf("widthLocation: %d\n", widthLocation); printf("textureLoc2: %d\n", textureLoc2);
printf("heigthLocation: %d\n", heigthLocation); printf("widthLoc: %d\n", widthLoc);
printf("heigthLoc: %d\n", heigthLoc);
printf("Width: %f\n", 1.0f/textw); printf("Width: %f\n", 1.0f/textw);
printf("Height: %f\n", 1.0f/texth); printf("Height: %f\n", 1.0f/texth);
@@ -273,8 +265,11 @@ int main(int argc, char* args[]) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID); glUseProgram(programID);
glUniform1f(widthLocation, 1.0f/textw); glUniform1f(widthLoc, 1.0f/textw);
glUniform1f(heigthLocation, 1.0f/texth); glUniform1f(heigthLoc, 1.0f/texth);
glUniform1i(textureLoc1, 0);
glUniform1i(textureLoc2, 1);
// 1rst attribute buffer : vertices // 1rst attribute buffer : vertices
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

View File

@@ -8,6 +8,7 @@ out vec3 color;
// Values that stay constant for the whole mesh. // Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler; uniform sampler2D myTextureSampler;
uniform sampler2D myTextureSampler2;
// Relative width/height of a pixel // Relative width/height of a pixel
uniform float pixWidth; uniform float pixWidth;
@@ -22,13 +23,16 @@ int UNSET = -1;
vec3 getColor(vec2 uvCoord) { vec3 getColor(vec2 uvCoord) {
return texture(myTextureSampler, uvCoord).rgb; return texture(myTextureSampler2, uvCoord).rgb;
} }
bool isBackground(vec3 c) { bool isBackground(vec3 c) {
// 49,206, 11 /*// 49,206, 11
if (all( lessThanEqual(c, vec3(51.0f/255.0f, 208.0f/255.0f, 13.0f/255.0f))) && if (all( lessThanEqual(c, vec3(51.0f/255.0f, 208.0f/255.0f, 13.0f/255.0f))) &&
all(greaterThanEqual(c, vec3(47.0f/255.0f, 204.0f/255.0f, 9.0f/255.0f))) all(greaterThanEqual(c, vec3(47.0f/255.0f, 204.0f/255.0f, 9.0f/255.0f)))*/
// 0, 255, 0
if (all( lessThanEqual(c, vec3(2.0f/255.0f, 255.0f/255.0f, 2.0f/255.0f))) &&
all(greaterThanEqual(c, vec3(0.0f/255.0f, 253.0f/255.0f, 0.0f/255.0f)))
) { ) {
return true; return true;
} else { } else {