Ok. So I am trying to create a texture loading class. And things are actually going pretty well! However the problem is. That this code is supposed to display a crate, with a awesome face on it. However it draws a very solid awesome face on it.
The code is below. Some things to note.
- If you only make one of the texture classes (comment out the two initilization lines, and the two using lines) you get a crate
- If you use both it only draws an awesome face, however the shader is supposed to (and is tested and proven to) blend the two of them.
main.cpp:
#include <iostream>
// We want glew to be static
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <SOIL.h>
#include "Shaders.h"
#include "KeyboardD.h"
#include "Texture.h"
Texture* tex1;
Texture* tex2;
int main()
{
//Initialization stuff
void Init();
//More initilization stuff
while (//Game is running)
{
//Clear color
Render();
//Swap buffers
}
//Delete vertex buffers
}
void Init()
{
//Shader class stuff. I have tested and it works
//Abunch of stuff reguarding passing rectangle coords to the gpu
Texture texxx1("container.jpg");
tex1 = &texxx1;
Texture texxx2("awesomeface.png);
texr1 = &texxx2;
}
void Render()
{
//Activate takes in a string that is used to name the attribute, and the pointer to the shader class
tex1->Activate(ourShader, "ourTexture1");
tex2->Activate(ourShader, "ourTexture2");
//Use shader, bind VAO
///Draw elements
//Unbind the vertex array
}
Texture.h
#pragma once
#include <vector>
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <SOIL.h>
#include "Shaders.h"
int amm = 0;
class Texture
{
public:
Texture(const char* name)
{
glGenTextures(1, &this->programid);
glBindTexture(GL_TEXTURE_2D, this->programid); // We are now editing the data for texture 1
// Set texture wrapping to GLRepeat for both x and y axis
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture filtering so things are smooth
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load, create texture and generate mipmaps
//Have soil load the texture into an array of unsigned chars
unsigned char* image = SOIL_load_image(name, &this->width, &this->height, 0, SOIL_LOAD_RGB);
if (image == '\0')
{
std::cout << "UNABLE TO LOAD IMAGE" << std::endl;
}
else
{
std::cout << "SUCESS" << this->programid << std::endl;
}
//Tell gl the texture dimensions color space, data type and pointer to data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, this->width, this->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
//Makes the minimaps of the image
glGenerateMipmap(GL_TEXTURE_2D);
//Free the SOIL data
SOIL_free_image_data(image);
//glBindTexture(GL_TEXTURE_2D, 0); //We are no longer editing texture 1*/
this->samm = amm;
amm += 1;
}
void Activate(Shader* sh, const char* uniformName)
{
// Activate a texture
glActiveTexture(GL_TEXTURE0 + this->samm);
//Bind the texture so we can draw it
glBindTexture(GL_TEXTURE_2D, this->programid);
//Pass the uniform to the shader. The shader will be looking for a value named this
glUniform1i(glGetUniformLocation(sh->Program, uniformName), this->programid);
}
private:
int samm;
int width;
int height;
GLuint programid;
};
And incase you want to see it. I have tested it but with texture loading code that doesn’t use classes
Shader1.frag
#version 330 core
in vec3 ourColor;
in vec2 TexCoord;
out vec4 color;
// Texture samplers
uniform sampler2D ourTexture1;
uniform sampler2D ourTexture2;
void main()
{
// Linearly interpolate between both textures (second texture is only slightly combined)
color = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, TexCoord), 0.2);
}
Thank you much for reading over it! I would love to know why it is happening. Here are some extra things that might help you help me figure this out.
Project File Can Be Downloaded At: https://app.box.com/s/c0e3iv6hmyqbi69at8f2xjqdiqldv87h
And I decided to pull some breakpoints and show what variables visual studio decided to show. A ton of that data that is shown is somehow garbage. Here are some pictures at different spots.
Pic1 This is the first time Activate is called:
[ATTACH=CONFIG]1180[/ATTACH]
Pic2 This is the first time Activate is called but first executed line:
[ATTACH=CONFIG]1181[/ATTACH]
Pic3 this is the second time Activate is called:
[ATTACH=CONFIG]1182[/ATTACH]
Pic4 This is the third time activate is called but first executed line:
[ATTACH=CONFIG]1183[/ATTACH]
Pic5 This shows that the values are properly set at the initializer function:
[ATTACH=CONFIG]1184[/ATTACH]
Thankyou so much! It would me a lot to have this working as soon as possible, and although I have been (and will be) looking at the code for awhile your more practiced eyes will be a big help.