I'm trying to texture a tube object in OpenGL, for a project, and a have problem texturing it. The texture is coming up nicely but there is a white line in the middle of the back of the tube that I can't get rid of. I'm using standard texture class that I build from a tutorial that I read. the mesh and the texture are upload normally- meaning nothing is unusual.
Back of the tube
Front of the tube
Texture::Texture(const std::string& fileName)
{
int width, height, numComponents;
unsigned char* data = stbi_load((fileName).c_str(), &width, &height, &numComponents, 4);
if (data == NULL)
std::cerr << "Unable to load texture: " << fileName << std::endl;
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
Texture::~Texture()
{
glDeleteTextures(1, &m_texture);
}
void Texture::Bind()
{
glBindTexture(GL_TEXTURE_2D, m_texture);
}
#version 130--fragment shader
varying vec2 texCoord0;
varying vec3 normal0;
varying vec3 color0;
uniform sampler2D ourTexture1; // added
uniform vec3 lightDirection;
uniform vec3 MinMax;
void main()
{
//vec3 tmp = dot(-lightDirection, normal0) * color0 ;44
gl_FragColor = texture(ourTexture1, texCoord0);
if(color0.y<MinMax.x||color0.y>MinMax.y)
gl_FragColor=vec4(1.0,1.0,1.0,1.0);
}
#version 120-vertex shader
attribute vec3 position;
attribute vec2 texCoord;
attribute vec3 normal;
attribute vec3 color;
varying vec2 texCoord0;
varying vec3 normal0;
varying vec3 color0;
uniform mat4 MVP;
uniform mat4 Normal;
void main()
{
gl_Position = MVP * vec4(position, 1.0);
texCoord0 = texCoord;
texCoord0[0]=0.25+texCoord0[0];
if(texCoord0[0]>=1)
{
texCoord0[0]=texCoord0[0]-1;
}
texCoord0[1]=1-texCoord0[1];
color0 = position;
normal0 = (Normal * vec4(normal, 0.0)).xyz;
}
The problem almost certainly comes from the following part of your vertex shader:
texCoord0[0]=0.25+texCoord0[0];
if(texCoord0[0]>=1)
{
texCoord0[0]=texCoord0[0]-1;
}
I'm not entirely sure what you are trying to accomplish with this, but it will cause neighbouring vertices to have values that are very far appart, which means that almost the entire texture gets squeezed in between these two vertices.
Normally, you would want to just apply the offset, and let the rendering pipeline take care of of the modulus operation. So I would have expected to just see this:
texCoord0[0]=0.25+texCoord0[0];
N.B.
You might still see the issue if you are sharing vertices accross the the entire circumference of the tube. The point of the mesh where the texture coordinate "loops" around should have duplicated vertices with different UVs.
Related
I have a problem that a texture covers only part of an object in height dimensions and start over again rendering a smaller texture in the bottom as shown in the pic.
I'm posting my Texture and Shader code as well. note that I'm using glTexSubImage2D for the background of the object. the same problem occurred when I didn't use the glTexSubImage2D and just glTexImage2D.
texture code
try {
data = stbi_load((fileName).c_str(), &width, &height, &numComponents, 4);
if (data == NULL)
std::cerr << "Unable to load texture: " << fileName << std::endl;
else
std::cerr << "texture loaded " << fileName << std::endl;
std::string fileName2 = t ? filePathe + "//images//" + mediaColor + ".bmp" : "./res/textures/plane1.bmp";
int width2, height2, numComponents2;
unsigned char* data2 = stbi_load((fileName2).c_str(), &width2, &height2, &numComponents2, 4);
if (data2 == NULL)
std::cerr << "Unable to load texture: " << fileName2 << std::endl;
else
std::cerr << "texture loaded " << fileName2 << std::endl;
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height , GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
stbi_image_free(data2);
}
catch (std::invalid_argument &e)
{
vertex shader
#version 120
attribute vec3 position;
attribute vec2 texCoord;
attribute vec3 normal;
attribute vec3 color;
varying vec2 texCoord0;
varying vec3 normal0;
varying vec3 color0;
varying float index0;
uniform mat4 MVP;
uniform mat4 Normal;
uniform float index;
void main()
{
gl_Position = MVP * vec4(position, 1.0);
texCoord0 = texCoord;
if (index == 0.0) {
//texCoord0[0]=0.25+texCoord0[0];
texCoord0[1]=1-texCoord0[1];
}
}
fragment shader
#version 130
varying vec2 texCoord0;
varying vec3 normal0;
varying vec3 color0;
varying float index0;
uniform sampler2D ourTexture1; // added
uniform vec3 lightDirection;
uniform vec3 capOffSet;
uniform vec3 mediaColor;
uniform vec3 capColor;
void main()
{
gl_FragColor = texture(ourTexture1, texCoord0);
}
I'm following this tutorial step by step and I even copy-pasted the entire code but it still fails to load the texture. Here's my code, the parts that concern the question:
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); // All upcoming GL_TEXTURE_2D operations now have effect on this texture object
// Set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT (usually basic wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load image, create texture and generate mipmaps
int width, height;
unsigned char* image = SOIL_load_image("container.jpg", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture when done, so we won't accidentily mess up our texture.
And here are my shaders:
#version 330 core
in vec3 ourColor;
in vec2 TexCoord;
out vec4 color;
uniform sampler2D ourTexture;
void main()
{
color = texture(ourTexture, TexCoord);
}
And
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 texCoord;
out vec3 ourColor;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(position, 1.0f);
ourColor = color;
TexCoord = texCoord;
}
I'm using SOIL to load image data. Is it too outdated? What should I do?
The tutorial code you are following seems to be wrong since it does not call glActiveTexture nor glUniform. See the game loop code of the other file at end of the tutorial.
Maybe you are missing something like this:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture"), 0);
I'm trying to add a texture to my square displayed on the screen, but after i've done all the code the square is still white, and I cant figure out what's wrong, I think I might be missing something after I've loaded the texture.
this is the code I use for creating the texture:
void textureManager::createTexture(const char* fileName) {
GLuint textureID = SOIL_load_OGL_texture(fileName, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
if (0 == textureID) {
cout << "error" << endl;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
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_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
And I call this function after the vertex and index buffer data, before the shaders compile
this is my vertex and fragment shader code:
#version 430
in layout(location = 0) vec2 position;
in layout(location = 1) vec2 texCoord;
out vec2 vertex_texCoord;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
vertex_texCoord = texCoord;
}
#version 430
out vec4 fragmentColor;
in vec2 vertex_texCoord;
uniform sampler2d textureSampler;
void main()
{
fragmentColor = texture(textureSampler, vertex_texCoord);
}
can someone help me please
Your shaders look fine. You are making the GLuint id too. Hm, are you sending it to the shader in your drawcall?
It should look something like this:
glBindTexture(GL_TEXTURE_2D, textureID); //texture uniform
and be called when you fill the vbos before the final draw.
I'm currently trying to implement deferred shading in OpenGL 3.2 and have a problem that I just can't seem to solve no matter what I try.
I implemented it in two steps(geometry pass and lighting pass) like one would expect. After compiling and running it the screen shows the scene I prepared almost like one would expect it to look like. The colors of the objects are correct and they are also positioned where and how I wanted them to be.
The thing is, that the light calculations seem to have no influence on the color, what so ever. After a lot of hours I found out, that the textures for the positions and normals seem to contain the same content like the color texture.
If one changes the last line in the lighting fragment shader from fragColor = lightIntensity * color; to fragColor = lightIntensity * norm; or fragColor = lightIntensity * pos; it has absolutely no impact on how the screen is rendered.
I have tried a lot to figure out what is going wrong but honestly have no idea what it could be.
It would be awesome if someone could help me.
My render method looks like this:
void render()
{
//geometry pass
gBuffer->bindForWriting();
geometryShader->use(true);
calculateGBuffer();
//lighting pass
gBuffer->bindForReading(lightShader->programID());
lightShader->use(true);
drawOnScreen();
}
The initialization of the gBuffer object is like this:
void GBuffer::initializeFBO(int viewWidth, int viewHeight)
{
//initialize fbo and corresponding textures;
glGenFramebuffers(1, &fbo_ID);
glBindFramebuffer(GL_FRAMEBUFFER, fbo_ID);
glGenTextures(1, &colorTexture_ID);
glBindTexture(GL_TEXTURE_2D, colorTexture_ID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, viewWidth, viewHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture_ID, 0);
glGenTextures(1, &posTexture_ID);
glBindTexture(GL_TEXTURE_2D, posTexture_ID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, viewWidth, viewHeight, 0, GL_RGB, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, posTexture_ID, 0);
glGenTextures(1, &normTexture_ID);
glBindTexture(GL_TEXTURE_2D, normTexture_ID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, viewWidth, viewHeight, 0, GL_RGB, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, normTexture_ID, 0);
GLuint attachments[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, attachments);
glGenRenderbuffers(1, &depthBuffer_ID);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer_ID);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, viewWidth, viewHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer_ID);
//Check Status
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
qDebug() << "error while initializing framebuffer" << glCheckFramebufferStatus(GL_FRAMEBUFFER);
else{
qDebug() << "framebuffer successfully created";
initialized = true;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
The methods bindForReading and bindForWriting:
void GBuffer::bindForWriting()
{
glBindFramebuffer(GL_FRAMEBUFFER, fbo_ID);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void GBuffer::bindForReading(GLuint programID)
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorTexture_ID);
GLuint samplerTexture_ID = glGetUniformLocation(programID, "colorTexture");
glUniform1i(samplerTexture_ID, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, posTexture_ID);
samplerTexture_ID = glGetUniformLocation(programID, "positionTexture");
glUniform1i(samplerTexture_ID, 1);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, normTexture_ID);
samplerTexture_ID = glGetUniformLocation(programID, "normTexture");
glUniform1i(samplerTexture_ID, 2);
}
And at last the 4 Shaders:
Geometry Vertex Shader:
#version 150
#extension GL_ARB_separate_shader_objects : enable
uniform mat4 MVPMatrix;
uniform mat4 modelMatrix;
in vec4 in_position;
in vec4 in_color;
in vec2 in_texcoord;
in vec3 in_norm;
out vec4 color_varying;
out vec3 frag_position;
out vec3 norm_vec;
out vec2 texcoord_varying;
void main()
{
gl_Position = MVPMatrix * in_position;
vec4 worldPosition = (modelMatrix * in_position);
frag_position = worldPosition.xyz;
norm_vec = in_norm;
color_varying = in_color;
texcoord_varying = in_texcoord;
}
Geometry Fragment Shader:
#version 150
#extension GL_ARB_explicit_attrib_location : enable
in vec4 color_varying;
in vec3 frag_position;
in vec3 norm_vec;
in vec2 texcoord_varying;
layout (location = 0) out vec4 fragColor;
layout (location = 1) out vec3 fragPosition;
layout (location = 2) out vec3 frag_norm_vec;
uniform sampler2D myTexture;
void main()
{
vec4 texel = texture(myTexture, texcoord_varying);
fragColor = texel * color_varying;
fragPosition = frag_position;
frag_norm_vec = normalize(norm_vec);
}
Lighting VertexShader:
#version 150
#extension GL_ARB_explicit_attrib_location : enable
layout (location = 0) in vec2 in_position;
out vec2 texCoord;
void main()
{
gl_Position = vec4(in_position, 0, 1.0f);
texCoord = in_position;
if(texCoord.x == -1.0f)
texCoord.x = 0.0f;
if(texCoord.y == -1.0f)
texCoord.y = 0.0f;
}
Lighting Fragment Shader(without lighting calculation to make it shorter)
#version 150
#extension GL_ARB_separate_shader_objects : enable
out vec4 fragColor;
in vec2 texCoord;
uniform sampler2D colorTexture;
uniform sampler2D positionTexture;
uniform sampler2D normTexture;
void main()
{
//extract fragment data from fbo
vec3 pos = texture(positionTexture, texCoord).rgb;
vec3 norm = texture(normTexture, texCoord).rgb;
vec4 color = texture(colorTexture, texCoord);
fragColor = lightIntensity * color;
}
Sry for the code spamming but I can't narrow down the error.
The problem is most likely in your order of operations here:
gBuffer->bindForReading(lightShader->programID());
lightShader->use(true);
where, in bindForReading(), you have calls like this one:
samplerTexture_ID = glGetUniformLocation(programID, "positionTexture");
glUniform1i(samplerTexture_ID, 1);
The glUniform*() calls set uniform values on the currently active program. Since you make the lightShader active after you make these calls, the uniform values will be set on the previously active program, which probably doesn't even have these uniforms.
Simply changing the order of these calls might already fix this:
lightShader->use(true);
gBuffer->bindForReading(lightShader->programID());
Also, you're using GL_RGB16F as the format of two of your buffers. The OpenGL implementation you use may support this, but this is not a format that is required to be color-renderable in the spec. If you want your code to work across platforms, you should use GL_RGBA16F, which is guaranteed to be color-renderable.
I am working with openGL quad buffer to render stereo 3D images (and I am very new to openGL).
I came across a question on how to define the output texture for each back buffer, which I supose is done in the fragment buffer by defining the output color (corresponding to a specified texture).
This is the code I am using for the shader sources:
// Shader sources
const GLchar* vertexSource =
"#version 150 core\n"
"in vec2 position;"
"in vec3 color;"
"in vec2 texcoord;"
"out vec3 Color;"
"out vec2 Texcoord;"
"void main() {"
" Color = color;"
" Texcoord = texcoord;"
" gl_Position = vec4(position, 0.0, 1.0);"
"}"; // Vertex buffer source
const GLchar* fragmentSource =
"#version 150 core\n"
"in vec3 Color;"
"in vec2 Texcoord;"
"out vec4 outColor;"
"uniform sampler2D texRight;"
"uniform sampler2D texLeft;"
"void main() {"
" outColor = texture(texRight, Texcoord);"
" outColor = texture(texLeft, Texcoord);"
"}"; // Fragment buffer source
This is the code I am using to fill the back buffers, that I use in the display function for the freeglut context window.
// Clear Buffers
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f,1.0f);
cudaGLMapBufferObject((void**)d_glpointer, pbo);
cudaGLMapBufferObject((void**)d_glpointer, pbo_Right);
//...
// Unmap buffer object
cudaGLUnmapBufferObject(pbo);
glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo);
glDrawBuffer(GL_BACK_LEFT);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texturesID[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE,NULL); // NULL specifies that the image is in memory
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glUniform1i(glGetUniformLocation(shaderProgram, "texLeft"), 1);
// Draw a rectangle from the 2 triangles using 6 indices
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// Unmap buffer object
cudaGLUnmapBufferObject(pbo_Right);
glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo_Right);
// Draw Back Buffers
glDrawBuffer(GL_BACK_RIGHT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texturesID[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE,NULL); // NULL specifies that the image is in memory
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glUniform1i(glGetUniformLocation(shaderProgram, "texRight"), 0);
// Draw a rectangle from the 2 triangles using 6 indices
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// Swap buffers
glutSwapBuffers();
By using this code I am able to see only the last outcolor defined, but I was not able to figure out a functional way to choose between the outColor definition depending on the buffer I am using.
I am using Cuda to put the images in memory, but the problem doesn't seem related to this mapping, since I am always able to see the image associated with texture defined last in the shader's outcolor.
I'll appreciate some help on figuring this out.
Edit 1:
I added a counter to the shader that verifies if the frame count (texCount) is even or odd. Although, I have no output image now.
#version 150 core
in vec3 Color;
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D texRight;
uniform sampler2D texLeft;
uniform int texCount=1;
void main() {
if (texCount%2){ // if texCount division by two is exact then this is false
outColor = texture(texLeft, Texcoord);
}else{
outColor = texture(texRight, Texcoord);
}
texCount++;
}
I think the following should work.
The new fragment shader:
#version 150 core in vec3 Color;
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D texIndex;
void main() {
outColor = texture(texIndex, Texcoord);
}
Then replace ”texLeft” and ”texRight” in your code by ”texIndex”.
Note that you shouldn't have to upload the textures at each frame if they are static. Ideally, you would put all the commands that need to be executed only once in a setup function, which you would execute only when the OS notify a window reshape.