open gl textures

11
22/10/2014 OpenGL - Textures https://open.gl/textures 1/11 Textures objects and parameters Just like VBOs and VAOs, textures are objects that need to be generated first by calling a function. It shouldn't be a surprise at this point what this function is called. GLuint tex; glGenTextures(1, &tex); Textures are typically used for images to decorate 3D models, but in reality they can be used to store many different kinds of data. It's possible to have 1D, 2D and even 3D textures, which can be used to store bulk data on the GPU. An example of another use for textures is storing terrain information. This article will pay attention to the use of textures for images, but the principles generally apply to all kinds of textures. glBindTexture(GL_TEXTURE_2D, tex); Just like other objects, textures have to be bound to apply operations to them. Since images are 2D arrays of pixels, it will be bound to the GL_TEXTURE_2D target. The pixels in the texture will be addressed using texture coordinates during drawing operations. These coordinates range from 0.0 to 1.0 where (0,0) is conventionally the bottom-left corner and (1,1) is the top-right corner of the texture image. The operation that uses these texture coordinates to retrieve color information from the pixels is called sampling. There are different ways to approach this problem, each being appropriate for different scenarios. OpenGL offers you many options to control how this sampling is done, of which the common ones will be discussed here. Wrapping The first thing you'll have to consider is how the texture should be sampled when a coordinate outside the range of 0 to 1 is given. OpenGL offers 4 ways of handling this: GL_REPEAT : The integer part of the coordinate will be ignored and a repeating pattern is formed. GL_MIRRORED_REPEAT : The texture will also be repeated, but it will be mirrored when the integer part of the coordinate is odd. GL_CLAMP_TO_EDGE : The coordinate will simply be clamped between 0 and 1 . GL_CLAMP_TO_BORDER : The coordinates that fall outside the range will be given a specified border color. These explanations may still be a bit cryptic and since OpenGL is all about graphics, let's see what all of these cases actually look like: The clamping can be set per coordinate, where the equivalent of (x,y,z) in texture coordinates is called (s,t,r) . Texture parameter are changed with the glTexParameter* functions as demonstrated here. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); As before, the i here indicates the type of the value you want to specify. If you use GL_CLAMP_TO_BORDER and you want to change the border color, you need to change the value of GL_TEXTURE_BORDER_COLOR by passing an RGBA float array: float color[] = { 1.0f, 0.0f, 0.0f, 1.0f }; glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color); This operation will set the border color to red. Filtering Since texture coordinates are resolution independent, they won't always match a pixel exactly. This happens when a texture image is stretched beyond its original size or when it's sized down. OpenGL offers various methods to decide on the sampled color when this happens. This process is called filtering and the following methods are available: GL_NEAREST : Returns the pixel that is closest to the coordinates. Links Easy-to-build code Matrix math tutorials OpenGL reference Introduction Context creation Drawing polygons Textures Transformations Depth and stencils Frame buffers Geometry shaders Transform Feedback

Upload: ucg

Post on 20-Feb-2023

1 views

Category:

Documents


0 download

TRANSCRIPT

22/10/2014 OpenGL - Textures

https://open.gl/textures 1/11

Textures objects and parameters

Just like VBOs and VAOs, textures are objects that need to be generated first by calling a function. It shouldn't be asurprise at this point what this function is called.

GLuint tex;glGenTextures(1, &tex);

Textures are typically used for images to decorate 3D models, but in reality they can be used to store many differentkinds of data. It's possible to have 1D, 2D and even 3D textures, which can be used to store bulk data on the GPU. Anexample of another use for textures is storing terrain information. This article will pay attention to the use of texturesfor images, but the principles generally apply to all kinds of textures.

glBindTexture(GL_TEXTURE_2D, tex);

Just like other objects, textures have to be bound to apply operations to them. Since images are 2D arrays of pixels, itwill be bound to the GL_TEXTURE_2D target.

The pixels in the texture will be addressed using texture coordinates during drawing operations. These coordinatesrange from 0.0 to 1.0 where (0,0) is conventionally the bottom-left corner and (1,1) is the top-right corner of thetexture image. The operation that uses these texture coordinates to retrieve color information from the pixels is calledsampling. There are different ways to approach this problem, each being appropriate for different scenarios. OpenGLoffers you many options to control how this sampling is done, of which the common ones will be discussed here.

Wrapping

The first thing you'll have to consider is how the texture should be sampled when a coordinate outside the range of 0to 1 is given. OpenGL offers 4 ways of handling this:

GL_REPEAT : The integer part of the coordinate will be ignored and a repeating pattern is formed.GL_MIRRORED_REPEAT : The texture will also be repeated, but it will be mirrored when the integer part of thecoordinate is odd.GL_CLAMP_TO_EDGE : The coordinate will simply be clamped between 0 and 1 .GL_CLAMP_TO_BORDER : The coordinates that fall outside the range will be given a specified border color.

These explanations may still be a bit cryptic and since OpenGL is all about graphics, let's see what all of these casesactually look like:

The clamping can be set per coordinate, where the equivalent of (x,y,z) in texture coordinates is called (s,t,r) .Texture parameter are changed with the glTexParameter* functions as demonstrated here.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

As before, the i here indicates the type of the value you want to specify. If you use GL_CLAMP_TO_BORDER and youwant to change the border color, you need to change the value of GL_TEXTURE_BORDER_COLOR by passing an RGBAfloat array:

float color[] = { 1.0f, 0.0f, 0.0f, 1.0f };glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color);

This operation will set the border color to red.

Filtering

Since texture coordinates are resolution independent, they won't always match a pixel exactly. This happens when atexture image is stretched beyond its original size or when it's sized down. OpenGL offers various methods to decideon the sampled color when this happens. This process is called filtering and the following methods are available:

GL_NEAREST : Returns the pixel that is closest to the coordinates.

LinksEasy-to-build codeMatrix math tutorialsOpenGL reference

Introduction

Context creation

Drawing polygons

Textures

Transformations

Depth and stencils

Frame buffers

Geometry shaders

Transform Feedback

22/10/2014 OpenGL - Textures

https://open.gl/textures 2/11

GL_LINEAR : Returns the weighted average of the 4 pixels surrounding the given coordinates.GL_NEAREST_MIPMAP_NEAREST , GL_LINEAR_MIPMAP_NEAREST , GL_NEAREST_MIPMAP_LINEAR ,GL_LINEAR_MIPMAP_LINEAR : Sample from mipmaps instead.

Before discussing mipmaps, let's first see the difference between nearest and linear interpolation. The original imageis 16 times smaller than the rectangle it was rasterized on.

While linear interpolation gives a smoother result, it isn't always the most ideal option. Nearest neighbourinterpolation is more suited in games that want to mimic 8 bit graphics, because of the pixelated look.

You can specify which kind of interpolation should be used for two separate cases: scaling the image down andscaling the image up. These two cases are identified by the keywords GL_TEXTURE_MIN_FILTER andGL_TEXTURE_MAG_FILTER .

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

As you've seen, there is another way to filter textures: mipmaps. Mipmaps are smaller copies of your texture that havebeen sized down and filtered in advance. It is recommended that you use them because they result in both a higherquality and higher performance.

glGenerateMipmap(GL_TEXTURE_2D);

Generating them is as simple as calling the function above, so there's no excuse for not using them! Note that you dohave to load the texture image itself before mipmaps can be generated from it.

To use mipmaps, select one of the four mipmap filtering methods.

GL_NEAREST_MIPMAP_NEAREST : Uses the mipmap that most closely matches the size of the pixel being texturedand samples with nearest neighbour interpolation.GL_LINEAR_MIPMAP_NEAREST : Samples the closest mipmap with linear interpolation.GL_NEAREST_MIPMAP_LINEAR : Uses the two mipmaps that most closely match the size of the pixel being texturedand samples with nearest neighbour interpolation.GL_LINEAR_MIPMAP_LINEAR : Samples closest two mipmaps with linear interpolation.

There are some other texture parameters available, but they're suited for specialized operations. You can read aboutthem in the specification.

Loading texture images

Now that the texture object has been configured it's time to load the texture image. This is done by simply loading anarray of pixels into it:

// Black/white checkerboardfloat pixels[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f};glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_FLOAT, pixels);

The first parameter after the texture target is the level-of-detail, where 0 is the base image. This parameter can beused to load your own mipmap images. The second parameter specifies the internal pixel format, the format in whichpixels should be stored on the graphics card. Many different formats are available, including compressed formats, soit's certainly worth taking a look at all of the options. The third and fourth parameters specify the width and height ofthe image. The fifth parameter should always have a value of 0 per the specification. The next two parameterdescribe the format of the pixels in the array that will be loaded and the final parameter specifies the array itself. Thefunction begins loading the image at coordinate (0,0) , so pay attention to this.

22/10/2014 OpenGL - Textures

https://open.gl/textures 3/11

But how is the pixel array itself established? Textures in graphics applications will usually be a lot more sophisticatedthan simple patterns and will be loaded from files. Best practice is to have your files in a format that is nativelysupported by the hardware, but it may sometimes be more convenient to load textures from common image formatslike JPG and PNG. Unfortunately OpenGL doesn't offer any helper functions to load pixels from these image files, butthat's where third-party libraries come in handy again! The SOIL library will be discussed here along with some of thealternatives.

SOIL

SOIL (Simple OpenGL Image Library) is a small and easy-to-use library that loads image files directly into textureobjects or creates them for you. You can start using it in your project by linking with SOIL and adding the srcdirectory to your include path. It includes Visual Studio project files to compile it yourself.

Although SOIL includes functions to automatically create a texture from an image, it uses features that aren'tavailable in modern OpenGL. Because of this we'll simply use SOIL as image loader and create the texture ourselves.

int width, height;unsigned char* image = SOIL_load_image("img.png", &width, &height, 0, SOIL_LOAD_RGB);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);

You can start configuring the texture parameters and generating mipmaps after this.

SOIL_free_image_data(image);

You can clean up the image data right after you've loaded it into the texture.

Alternative options

Other libraries that support a wide range of file types like SOIL are DevIL and FreeImage. If you're just interested inone file type, it's also possible to use libraries like libpng and libjpeg directly. If you're looking for more of anadventure, have a look at the specification of the BMP and TGA file formats, it's not that hard to implement a loaderfor them yourself.

Using a texture

As you've seen, textures are sampled using texture coordinates and you'll have to add these as attributes to yourvertices. Let's modify the last sample from the previous chapter to include these texture coordinates. The new vertexarray will now include the s and t coordinates for each vertex:

float vertices[] = {// Position Color Texcoords -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // Bottom-left};

The vertex shader needs to be modified so that the texture coordinates are interpolated over the fragments:

...

in vec2 texcoord;

out vec3 Color;out vec2 Texcoord;

...

void main(){ Texcoord = texcoord;

Just like when the color attribute was added, the attribute pointers need to be adapted to the new format:

glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 7*sizeof(float), 0);glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 7*sizeof(float), (void*)(2*sizeof(float)));

GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord");glEnableVertexAttribArray(texAttrib);glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE,

22/10/2014 OpenGL - Textures

https://open.gl/textures 4/11

7*sizeof(float), (void*)(5*sizeof(float)));

As two floats were added for the coordinates, one vertex is now 7 floats in size and the texture coordinate attributeconsists of 2 of those floats.

Now just one thing remains: providing access to the texture in the fragment shader to sample pixels from it. This isdone by adding a uniform of type sampler2D , which will have a default value of 0. This only needs to be changed whenaccess has to be provided to multiple textures, which will be considered in the next section.

For this sample, the image of the kitten used above will be loaded using the SOIL library. Make sure that it is locatedin the working directory of the application.

int width, height;unsigned char* image = SOIL_load_image("sample.png", &width, &height, 0, SOIL_LOAD_RGB);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);SOIL_free_image_data(image);

To sample a pixel from a 2D texture using the sampler, the function texture can be called with the relevant samplerand texture coordinate as parameters. We'll also multiply the sampled color with the color attribute to get aninteresting effect. Your fragment shader will now look like this:

#version 150

in vec3 Color;in vec2 Texcoord;

out vec4 outColor;

uniform sampler2D tex;

void main(){ outColor = texture(tex, Texcoord) * vec4(Color, 1.0);}

When running this application, you should get the following result:

If you get a black screen, make sure that your shaders compiled successfully and that the image is correctly loaded. Ifyou can't find the problem, try comparing your code to the sample code.

Texture units

The sampler in your fragment shader is bound to texture unit 0 . Texture units are references to texture objects that

22/10/2014 OpenGL - Textures

https://open.gl/textures 5/11

can be sampled in a shader. Textures are bound to texture units using the glBindTexture function you've usedbefore. Because you didn't explicitly specify which texture unit to use, the texture was bound to GL_TEXTURE0 . That'swhy the default value of 0 for the sampler in your shader worked fine.

The function glActiveTexture specifies which texture unit a texture object is bound to when glBindTexture is called.

glActiveTexture(GL_TEXTURE0);

The amount of texture units supported differs per graphics card, but it will be at least 48. It is safe to say that you willnever hit this limit in even the most extreme graphics applications.

To practice with sampling from multiple textures, let's try blending the images of the kitten and one of a puppy to getthe best of both worlds! Let's first modify the fragment shader to sample from two textures and blend the pixels:

...

uniform sampler2D texKitten;uniform sampler2D texPuppy;

void main(){ vec4 colKitten = texture(texKitten, Texcoord); vec4 colPuppy = texture(texPuppy, Texcoord); outColor = mix(colKitten, colPuppy, 0.5);}

The mix function here is a special GLSL function that linearly interpolates between two variables based on the thirdparameter. A value of 0.0 will result in the first value, a value of 1.0 will result in the second value and a value inbetween will result in a mixture of both values. You'll have the chance to experiment with this in the exercises.

Now that the two samplers are ready, you'll have to assign the first two texture units to them and bind the twotextures to those units. This is done by adding the proper glActiveTexture calls to the texture loading code.

GLuint textures[2];glGenTextures(2, textures);

int width, height;unsigned char* image;

glActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE_2D, textures[0]);image = SOIL_load_image("sample.png", &width, &height, 0, SOIL_LOAD_RGB);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);SOIL_free_image_data(image);glUniform1i(glGetUniformLocation(shaderProgram, "texKitten"), 0);

glActiveTexture(GL_TEXTURE1);glBindTexture(GL_TEXTURE_2D, textures[1]);image = SOIL_load_image("sample2.png", &width, &height, 0, SOIL_LOAD_RGB);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);SOIL_free_image_data(image);glUniform1i(glGetUniformLocation(shaderProgram, "texPuppy"), 1);

The texture units of the samplers are set using the glUniform function you've seen in the previous chapter. It simplyaccepts an integer specifying the texture unit. This code should result in the following image.

22/10/2014 OpenGL - Textures

https://open.gl/textures 6/11

As always, have a look at the sample source code if you have trouble getting the program to work.

Now that texture sampling has been covered in this chapter, you're finally ready to dive into transformations andultimately 3D. The knowledge you have at this point should be sufficient for producing most types of 2D games,except for transformations like rotation and scaling which will be covered in the next chapter.

Exercises

Animate the blending between the textures by adding a time uniform. (Solution)Draw a reflection of the kitten in the lower half of the rectangle. (Solution)Now try adding distortion with sin and the time variable to simulate water. (Expected result, Solution)

78 Comments Open.GL Login

Sort by Best Share ⤤

Join the discussion…

• Reply •

inlinevoid • a year ago

For those already using SFML 2.0 for their OpenGL context, you can also use SFML for image loading as well.

#include <sfml graphics.hpp="">

sf::Image image;image.loadFromFile("myImage.jpg");

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getSize().x, image.getSize().y, 0, GL_RGBA,GL_UNSIGNED_BYTE, image.getPixelsPtr());

A little bit cleaner than using SOIL, imo. 17△ ▽

• Reply •

Guester • 3 months ago> inlinevoid

In case anyone else was having issues with glew and SFML graphics module for use of sf::Image in visualstudio (and linking statically), make sure to include the glew32s.lib BEFORE the other SFML libs in Linker->input->Additional Dependencies

Great Tutorial! △ ▽

Hanz Fiddler • 3 months ago> inlinevoid

Awesome!

Favorite ★

Share ›

Share ›

22/10/2014 OpenGL - Textures

https://open.gl/textures 7/11

• Reply •

Awesome! △ ▽

• Reply •

Raremuh • 2 years ago

Thumbs up for new Part, will read soon :-) 5△ ▽

• Reply •

Jacob Peterson • a year ago

I spent the last couple days trying to figure this out. I have successfully mixed the images of the cat and puppy,but it gives a result that looks like its from an old broken tube tv: Also, here is the code I have:http://pastebin.com/vCKvDCae

3△ ▽

• Reply •

Jacob Peterson • a year ago> Jacob Peterson

Nevermind I figured it out. When I was callling the 'glTexImage2D' function, I was using GL_RGB for thethird and seventh value, when I should have been using the GL_RGBA

7△ ▽

• Reply •

Vinicius Epiplon • 5 months ago> Jacob Peterson

That's rather strange, because I used GL_RGB and worked as intended. △ ▽

• Reply •

Programmer • a year ago

This is the best tutorial I've seen online, Great work! I'm learning openGL using SuperBible book and I read here if something isn't clear enough. Great job!

2△ ▽

• Reply •

Matthias • 2 years ago

Thanks for the great tutorials. Is there a reason why the change of glUniform1i to glUniform1ui in the last exampledoes not work as intended (at least on my system)? Only one texture is showing up.

2△ ▽

• Reply •

Scott Atkins • 2 years ago

Great tutorials so far! Keep them coming! 2△ ▽

• Reply •

Guest • a year ago

Hi, I'm trying to get the image loaded in, and while it seems to get the image, there's a slight problem, seen below.

My code is here: http://pastebin.com/SdnmaCfY

Using GLFW on OSX 10.8 Mountain Lion

2△ ▽

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

22/10/2014 OpenGL - Textures

https://open.gl/textures 8/11

• Reply •

Chris Latham • 7 months ago> Guest

I realise I'm almost a year late and you've either stopped caring or fixed this already, but here you go:

Line 145 is this:

glVertexAttribPointer( texAttrib, 3, GL_FLOAT, GL_FALSE, 7*sizeof(float), (void*)( 5*sizeof(float) ) );

It should be this:

glVertexAttribPointer( texAttrib, 2, GL_FLOAT, GL_FALSE, 7*sizeof(float), (void*)( 5*sizeof(float) ) );

You were telling it you had three numbers per texture coordinate, when in actuality you should only havetwo.

5△ ▽

• Reply •

Augus1990 • 4 months ago

Hi, I can't compile the sample source code =S

CodeBlocks show this errors:

D:\Program Files\...\Simple OpenGL Image Library\lib\libSOIL.a(SOIL.o):SOIL.c|| undefined reference to`glReadPixels@28'|D:\Program Files\...\Simple OpenGL Image Library\lib\libSOIL.a(SOIL.o):SOIL.c|| undefined reference to`glGetIntegerv@8'|

do you know what I'm doing wrong? 1△ ▽

• Reply •

Michael • a year ago

I'm using a JPG image and the render is skewed horribly to one side. I switch to a PNG image and it works justfine. Doesn't look like SOIL handles the format too well. Can someone confirm?

1△ ▽

• Reply •

excitedelektron • 8 months ago> Michael

Confirmed. Some JPG images are not loaded correctly by SOIL. 1△ ▽

• Reply •

Perry • a year ago

The animation works better with (float)glfwGetTime() 1△ ▽

• Reply •

Jordan Bonser • 2 years ago

Hey Great Tutorials I am having one problem though. I have taken my project much further on now but since thestart I had Linker Warnings like this:defaultlib "library" conflicts with use of other libs; use /NODEFAULTLIB:library

where "library" was MSVCRT.lib LIBCMT.lib.. For a long time i just ignored these warnings and everything seemedto work fine. I have since tried to fix them and managed to do so by recompiling the libraries but now I have got aheap corruption which I am sure is due to this issue. I have tried recompiling and linking with various versions ofthese libraries to no avail.

My question is could you supply a visual studio solution with GLFW and GLEW already linked without warningsand that won't corrupt the heap? or possibly provide the library files that will definitely work? Or should I just backtrack to a version that had the linker warnings and just carry on from there and ignore them?

I am working on 64 bit Win7 and I am also using SOIL.

Thanks in advance 1△ ▽

• Reply •

Priyank Ahuja • 2 years ago> Jordan Bonser

You can do one thing for "defaultlib "library" conflicts with use of other libs; use /NODEFAULTLIB:library"

go to linker properties and uncheck "use default libs".

Tell me what other warnings or errors you are facing, I can help you with linking libraries and removingwarnings.

△ ▽

• Reply •

Cleroth Sun • a month ago

Is it really that safe to say you won't reach 48 texture units? Surely textures in games can quickly add up to a fairamount.

△ ▽

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

22/10/2014 OpenGL - Textures

https://open.gl/textures 9/11

• Reply • △ ▽

• Reply •

Lemon • a month ago> Cleroth Sun

Well the limit is for how many are loaded into a shader at one time or something, so for each model youmight only have a few textures. You probably don't render the entire world and all its textures in 1 massiveshader.

△ ▽

• Reply •

Cleroth Sun • a month ago> Lemon

From what I've learned, he minimum for textures used in a shader is 32, and the minimum combinedtextures is 48 (although most graphics card have far more than 48).

△ ▽

• Reply •

Alexander Overvoorde • a month agoMod > Cleroth Sun

You're talking about GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, which is themaximum combined amount of texture units used in a single shader program (vertex +fragment + ...), which indeed has a minimum of 48. There is no hard limit on the amount oftexture objects apart from the GPU memory available, you can easily have 1000s of low-restextures, but you can only use 48 at the same time per draw call (at a minimum).

1△ ▽

• Reply •

Cleroth Sun • a month ago> Alexander Overvoorde

That clarifies a lot of things! Thanks a lot.Also, these tutorials are pretty good.

△ ▽

• Reply •

eugene • 4 months ago

how do you get the image back to jpeg/png format? △ ▽

• Reply •

Oddity • 5 months ago

>The third and fourth parameters specify the width and height of the image.

That's actually the 4th and 5th parameters. there's a GL_RGB after the 0. △ ▽

• Reply •

Ediens • 5 months ago

Hello, I am having a problem I can't figure out.The image appears half in each tringle, but the right side appears on the left triangle and the left side appears onthe right.

I am using freeglut instead for context creation, but aside from that I am following closely as possible your code.This is probably a very simple problem, but I'll appreciate your input.

△ ▽

• Reply •

Omveer Chaudhary • 5 months ago

Very helpful article. △ ▽

• Reply •

Alex • 6 months ago

Also what is the default edge treatment without calling

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, modification); ? △ ▽

• Reply •

Alex • 6 months ago

Nice Tutorial.But when texture-wrapping is needed?

I mean how it can happen that you ask pixels outside the texture? I think it would be good to answer on thisquestion in the tutorial. It s only needed to use GL_MIRRORED_REPEAT if you blur your texture and so on...

△ ▽

• Reply •

Clay Sweetser • 7 months ago

After the end of the section where we lay the image of the kitten onto the colored vertices, is there any particularreason why the image of the kitten would be flipped? I've checked my vertices data, index data, fragment shaderand vertex shader source code, and nothing seems out of place..

△ ▽

Kieren Johnstone • 10 months ago

Hi - can you explain how the texture mapping is perspective-correct and not affine? Behind the scenes, it seems

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

22/10/2014 OpenGL - Textures

https://open.gl/textures 10/11

• Reply •

the positions and colours of the vertices are interpolated linearly. There doesn't look to be any reference to depthor vec3s or position anywhere in the texture mapping calls or code, so how does perspective-correct texturemapping happen? Thanks!

△ ▽

• Reply •

esgames.org • 10 months ago

"The vertex shader needs to be modified so that the texture coordinates are interpolated over the fragments:"

shouldn't this be the fragment shader? △ ▽

• Reply •

Alexander Overvoorde • 10 months agoMod > esgames.org

Eventually yes, but the first thing that needs to be changed is the vertex shader to pass the texturecoordinates.

△ ▽

• Reply •

Secret Library • a year ago

Isn't the texture coordinate sentence wrong? I thought OpenGL had the (0, 0) coordinates at the bottom left andthe (1, 1) coordinates at the top right...

△ ▽

• Reply •

Alexander Overvoorde • a year agoMod > Secret Library

You are correct! I'll fix it ASAP. 1△ ▽

• Reply •

dalius • a year ago> Alexander Overvoorde

Hi. I'm a little confused by this. In the section in which you define the vertices[] array to be used forthe final image: the texcoords for the top-left vertex of the rectangle are (0, 0). If the origin fortexcoords was bottom-left, shouldn't the texcoords be (0, 1) to map the top-left of the texture to thetop-left of the square? If I change the values for texcoords to a bottom-left origin, the image ends upupside down.

△ ▽

• Reply •

Alexander Overvoorde • a year agoMod > dalius

It's no surprise you're confused, because the problem hasn't been fixed yet. (0, 0) should bethe bottom-left corner, the current code is wrong.

△ ▽

• Reply •

Danny • a year ago

Rasta kitten. This tutorial rocks. Nuff said! :) △ ▽

• Reply •

anon • a year ago> Danny

Rastafari... buh △ ▽

• Reply •

drmanitoba • a year ago

EDIT: Smaller image, sorry about that.

Hi, I'm trying to get the image loaded in, and while it seems to get the image, there's a slight problem, seen below.

My code is here: http://pastebin.com/SdnmaCfY

Using GLFW on OSX 10.8 Mountain Lion

△ ▽

• Reply •

drmanitoba • a year ago> drmanitoba

Nevermind, I figured out where I went wrong. I had the size of the texAttrib glVertexAttribPointer call set to3 when it should've been 2. Sorry about the double post as a guest. Don't know how to delete that :(

△ ▽

JoeyDewd • 2 years ago

I'm getting crazy o.O I can't seem to get Texture Units working. Keep getting an 1283 OpenGL error whenever Itry to set the uniforms. I'm doing the texture calls after glewInit() and after the shaders have been compiled andlinked.

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

22/10/2014 OpenGL - Textures

https://open.gl/textures 11/11

Load more comments

Geometry shaders3 comments • a year ago

Simon Jackson — I found this link (http://www.gamedev.net/page/re... ) to be a muchbetter tutorial for instancing

Context creation1 comment • a month ago

Martin — I'd like to point out, that using SDL, I neededto add GL/glx.h header to properly compile the file,otherwise the compilator couldn't find …

ALSO ON OPEN.GL

• Reply •

Can any of you see what I'm doing wrong since it's close to a 1on1 copy of the tutorial's code.http://pastebin.com/KyFuDPNP

△ ▽

• Reply •

Alexander Overvoorde • 2 years agoMod > JoeyDewd

Interesting, I've never seen the GL_STACK_OVERFLOW error before. Honestly, I have no idea whatcould be causing that apart from functions like glPushMatrix (which are not used in this tutorial).

△ ▽

• Reply •

JoeyDewd • 2 years ago> Alexander Overvoorde

Thanks for the quick reply! I've read the value wrong. Error code is 1282 meaningGL_INVALID_OPERATION. I still don't know what enum values I might've passed in wrongfully?Everything just seems to be correct code?I've posted all my code with irrelevant sections cut out on pastebin: might be that anything's off? :http://pastebin.com/m5hdnWt0

△ ▽

• Reply •

Alexander Overvoorde • 2 years agoMod > JoeyDewd

Try putting glGetError() throughout the code to find the exact call that's causing it. △ ▽

• Reply •

JoeyDewd • 2 years ago> Alexander Overvoorde

It's this call: glUniform1i(glGetUniformLocation(basicShader.shaderProgram, "texKitten"), 0);.. And to be more precise: The glUniform1i call

△ ▽

• Reply •

Alexander Overvoorde • 2 years agoMod > JoeyDewd

See this for the things to check (you haven't specified all code):http://www.opengl.org/sdk/docs...

△ ▽

• Reply •

JoeyDewd • 2 years ago> Alexander Overvoorde

None of the errors seem to bo the problem. The glGetUniformlocation gives me a value of 0and 1 respectively for the uniform locations which should work. The glUniform1 commandreceives two GLints so that shouldn't be a problem as well. It's a weird problem o.O Code Iposted had all the (which I believed to be) irrelevant code removed but I'll post all the codethen: http://pastebin.com/SBVJ42G2

△ ▽

• Reply •

JoeyDewd • 2 years ago> JoeyDewd

Problem has been solved! I forgot to activate the current shader (basicShader.use()) whichprevented me from accesing the uniforms! Thanks for your help :) appreciate the quickreply's! :)

1△ ▽

• Reply •

Y u no wurk tutorul >.< • 4 months ago> JoeyDewd

I'm getting error 1282 while initializing my shaders and linking them. △ ▽

WHAT'S THIS?

Subscribe✉ Add Disqus to your sited Privacy

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›

Share ›