• Advertisement
Sign in to follow this  

OpenGL SDL_image to textured quad

This topic is 3471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I've a got 2D game going on. I used SDL and also OpenGL. For loading images I always use .TGA's and I load them myself with code I mostly took from NeHe's site. It's like this: I declare this data type:
struct TextureImage
{
   GLubyte *imageData;
   GLuint bpp;
   GLuint width;
   GLuint height;
   GLuint texID;
};

Then I declare some variable:
TextureImage *whatever;

Then I declare this function:
bool LoadTGA(TextureImage *texture, string &filename)
{
   GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
   GLubyte TGAcompare[12];
   GLubyte header[6];
   GLuint bytesPerPixel;
   GLuint imageSize;
   GLuint temp;
   GLuint type=GL_RGBA;

   FILE *file = fopen(filename.c_str(), "rb");

   if(file==NULL||
      fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare)||
      memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0||
      fread(header,1,sizeof(header),file)!=sizeof(header))
   {
      if(file!=NULL)
         fclose(file);
      return false;
   }
   texture->width  = header[1] * 256 + header[0];
   texture->height = header[3] * 256 + header[2];

   if(texture->width<=0||texture->height<=0||(header[4]!=24 && header[4]!=32))
   {
      fclose(file);
      return false;
   }

   texture->bpp=header[4];
   bytesPerPixel=texture->bpp/8;
   imageSize=texture->width*texture->height*bytesPerPixel;

   texture->imageData = (GLubyte *)malloc(imageSize);  //Ah, malloc, C's answer to "new".  So when is this memory freed?
                                                       //you have to free it yourself NeHe has a Deinitialize function

   if(texture->imageData==NULL||fread(texture->imageData, 1, imageSize, file)!=imageSize)
   {
      if(texture->imageData!=NULL)
      {
         free(texture->imageData);  //well, I guess this is how you free it
         texture->imageData = NULL;
      }
      fclose(file);
      return false;
   }

   //for a fun time comment this out
   for(GLuint i=0; i<(unsigned int)imageSize; i+=bytesPerPixel)
   {
      temp=texture->imageData[i];
      texture->imageData[i] = texture->imageData[i + 2];
      texture->imageData[i + 2] = temp;
   }

   fclose (file);

   // Build A Texture From The Data
   glGenTextures(1, &texture[0].texID);  //texture is a pointer to a TextureImage
                                         //why not just texture.texID?  why &texture[0]??  wtf
                                         //the the address of a pointer?  the first element of it?  it has no elements, wtf?
                                         //wtf could [0] possibly mean?
   //oh I remember, you use -> for pointers instead of . like above
   //so here he wants to use . so maybe ampersand on a pointer "converts" it back to a regular data type?
   //I have no idea what's going on

   glBindTexture(GL_TEXTURE_2D, texture[0].texID);  //And here it needs no ampersand.  Why?  I don't know.
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

   if(texture[0].bpp==24)
   {
      type=GL_RGB;
   }

   //Here using . instead of -> so maybe [0] takes care of it?  Then why the ampersand above?
   glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height,0, type, GL_UNSIGNED_BYTE, texture[0].imageData);

   return true;
}

So now I pass *whatever to that function along with the name of a .tga file and I can bind *whatever whenver I want and slap on some quads.
glBindTexture(GL_TEXTURE_2D, whatever.texID);
glBegin(GL_POLYGON);
   glTexCoord2f(0.0,1.0);	glVertex3f(0,768,0.0);
   glTexCoord2f(1.0,1.0);	glVertex3f(1024,768,0.0);
   glTexCoord2f(1.0,0.0);	glVertex3f(1024,0,0.0);
   glTexCoord2f(0.0,0.0);	glVertex3f(0,0,0.0);
glEnd();

But TGA's always take up a few trillion terrabytes and no one's used them since Fred Flintstone so I want to start loading .PNG's instead. I want to use SDL_image loader and using SDL_image loader for an SDL game is easy enough except I want it to load the PNG into a data type that I can bind to an OpenGL texture like I'm doing above with .tga's. I cannot find how to do this. I don't think it would take more than a page of code, I figured there would be something out there I could just block copy and figure out later but I can't find anything. I don't know why, lots of people have probably loaded .png's with SDL_image and textured OpenGL quads with them.

Share this post


Link to post
Share on other sites
Advertisement
Enjoy all the love of many different formats the SDL_IMage loads


bool TextureLoader::Load(std::string& filename)
{
SDL_Surface* tex = IMG_Load(filename.c_str());
if(!tex)
return false;
//setup your texture parameters however you like here
glBindTexture(...);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, tex->w, tex->h, 0, GL_RGB, GL_UNSIGNED_BYTE, tex->pixels);

SDL_FreeSurface(tex);
return true;
}





[Edited by - MARS_999 on July 9, 2008 1:30:19 AM]

Share this post


Link to post
Share on other sites
I don't really get it. What is this TextureLoader class? Am I supposed to write it myself and add imageWidth and all those variables to it and add the Load(string) function?

It looks to me like all it does is return true. tex is freed at the end, I don't see what the result is. I need something that I can bind and then map to a quad. Sorry I'm probably just not understanding it, I don't understand graphics very well.

Share this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
I don't really get it. What is this TextureLoader class? Am I supposed to write it myself and add imageWidth and all those variables to it and add the Load(string) function?

It looks to me like all it does is return true. tex is freed at the end, I don't see what the result is. I need something that I can bind and then map to a quad. Sorry I'm probably just not understanding it, I don't understand graphics very well.


When you call glTexImage2d, it will actually copy the data over to the video card, not just bind it. Once you done this it isn't necessary to keep the image loaded. In his snippet, he only shows how to use SDL's image library capabilities. You can use your knowledge on OpenGL's texture ability with his code to -texture a quad-. He also didn't show exactly how the params are passes as it isn't necessary, you can come up with it.

Share this post


Link to post
Share on other sites
Why do I need to make a TextureLoader class? Why do I need a class? Why can't I just make a function like this:


void Load(SDL_Surface* tex, std::string& filename)
{
tex = IMG_Load(filename.c_str());
if(!tex)
return false;
//setup your texture parameters however you like here

return true;
}



And then after I use that function I have my tex variable to work with so I can go like this back in my main program:


glBindTexture(...);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, tex->w, tex->h, 0, GL_RGB, GL_UNSIGNED_BYTE, tex->pixels);
SDL_FreeSurface(tex);



Really I'm not even sure why I even need that glTexImage2D function. Usually I just bind a texture and then draw a quad and I'm done. Unless I need glTexImage2D back in my Load function?

Share this post


Link to post
Share on other sites
The function you are questioning is what dumps the image into video memory! This way the video card can use its own memory rather than the slow transfer of ram -> video ram.

Here is a snippet of my texture class:

#include "Texture.h"
#include <string>
#include <sdl/sdl_image.h>
#include <sdl/sdl_opengl.h>

Texture::Texture(const char *filename)
{
// Load Texture into Memory
SDL_Surface *image = IMG_Load(filename);
if(!image)
throw std::pair<const char*, const char*>("Texture::Texture", "Error Loading Image from Disk");

// Create Video-Card Texture
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

SDL_FreeSurface(image);
}


void Texture::Bind()
{
glBindTexture(GL_TEXTURE_2D, tex_id);
}



Using something like this makes it a little neater (at least in my opinion) when binding different textures at different instances. Also the destructor can take care of any texture releasing, etc, if needed to be done in real time. =)

Share this post


Link to post
Share on other sites
I think I almost get it. That *image you declared is used and then freed on the spot. Before it's freed the image is loaded into video memory via that function I dared to question. So that function call goes out of scope. Now anytime I draw a quad that texture is going to be mapped to it until I call Texture::Texture(const char *filename) again. Am I right?

Share this post


Link to post
Share on other sites
For small projects, keep all of the texture objects in video memory. Bind which ever texture you need to use by calling the Bind method in the class, that way the next textured raster being rendered it will use the newly bound texture without having to do any IO (still in video memory). Basically the less IO you do, the faster... Much faster. :)

One way I have been doing this is creating a texture manager, where it will load images from a directory, than the user can request a reference of the texture object for rendering. Once the manager goes out of scope all the resources are freed.

Share this post


Link to post
Share on other sites
Sorry man, I don't really understand what you just said.

I don't really understand how the textures you have loaded into video memory are ever accessed again. When I did it with the LoadTGA(TextureImage *texture, string &filename) function that I listed in my first post *texture was NOT freed at the end like your *image is. It's up to me to make sure it's freed later.

That way whenever I load a new map and new monsters I assign a TextureImage* to every new TGA file by sending it into my LoadTGA() function along with the name of the file. Then my Draw() function executes continuously but as it draws the map and hero and monsters it just binds those TextureImage*'s over and over.

Your *image is freed at the end of the function. It is bound so it can be texture mapped right then. But then if something else is bound then it seems to me that the image is lost.

I just don't see how you can free the image from memory right after you bind it. The Draw() function uses that image every cycle. Why would you free it?

Share this post


Link to post
Share on other sites
What? Sorry, but you need to do a little more research on this topic.

When he deletes the memory for the SDL surface he is releasing the memory on the CPU side that was used to load the image from the file. You don't need that pointer anymore as the data is uploaded to the videocard after you call glTexImage(), and with the texture object you call with glBindTexture() you basically retrieve that image to be used again. So there is no need to keep around the data you used with loading the images, unless you need to reload the textures because you lose your RC.

Share this post


Link to post
Share on other sites
Okay, so it's loaded into video memory.

I used to pass a member of my TextureImage struct to glBindTexture like this:

glBindTexture(GL_TEXTURE_2D, whatever.texID);

texID is a GLuint

You're telling me I can free my pointer because the image is in video memory. But then what do I pass to glBindTexture to make it bind that same image again?

Is that why you guys use a class and I only have a function that is not part of a class? I mean do you guys build your Texture class to hold lots of images and keep a GLuint for each new texture to find it in video memory again?

I guess I'll do some reading, I don't really know where to start.

Share this post


Link to post
Share on other sites
Yes you keep the GLuint variable around to access the texture with a call to glBindTexture(). Most people use a texture manager, but if your project is small enough you can just use an array with constants defined to keep track.


enum
{
GRASS, DIRT, WATER, SKY, TOTAL_TEXURES
};
GLuint textures[TOTAL_TEXTURES] = {0};

//load textures and setup texture parameters
glGenTextures(1, &textures[GRASS]);
glBindTexture(GL_TEXTURE_2D, textures[GRASS]);
glTexParameteri();
glTexImage2D();

//load next
glGenTextures(1, &textures[DIRT]);
glBindTexture(GL_TEXTURE_2D, textures[DIRT]);
glTexParameteri();
glTexImage2D();



//Now draw textured polygons
glBindTexture(GL_TEXTURE_2D, textures[GRASS]);
//draw some terrain with grass

glBindTexture(GL_TEXTURE_2D, textures[SKY]);
//draw sky




HTH

Share this post


Link to post
Share on other sites
Okay, thanks. So we free that pointer ourselves. We keep around a GLint variable somehow and access the image in video memory whenever we want.

So... who frees it from video memory?

Share this post


Link to post
Share on other sites
The driver will. IIRC it happens when you call glDeleteTextures, but I am not 100% sure on that.

Share this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
Uh... I've sort of like NEVER called glDeleteTextures so am I leaking video memory all over the place?

No, it's called for you when you delete the OpenGL context. In SDL, the context is deleted whenever you call SDL_FreeSurface on the VideoMode surface (AKA, 'screen' or whatever you name it). Because SDL actually calls SDL_FreeSurface(screen) for you, you don't even need to do that.

All you need to do is close SDL when you are done, using SDL_Quit. SDL_Quit will call SDL_FreeSurface(myScreenSurface), along with other things, which will call glDeleteTextures or something equivalent.

It's important to note, that since deleting the VideoMode surface deletes all your OpenGL textures, if you resize the SDL screen, you'll need to reload all your images again. (Or else, not free the SDL_Surface loaded, and keep a copy in non-videocard memory, and just re-glTexImage2D them) I keep a std::string with the filename of the images I load, and reload them whenever I resize the screen.

(Note: I am still a beginner at OpenGL, so I might be wrong here)

Share this post


Link to post
Share on other sites
What if while my program is running the player leaves one map and goes to another and does that a lot of times and lots and lots of images are loaded into video memory? What if all the video memory is taken? From what you told me I will run out of video memory because it won't be freed until SDL_Quit is called and that doesn't happen until the player closes the program.

And what if the program crashes or the the user exits the program in some way he's not supposed to? Then is all that video memory lost until the computer is rebooted?

Share this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
What if while my program is running the player leaves one map and goes to another and does that a lot of times and lots and lots of images are loaded into video memory? What if all the video memory is taken?
This is why you have the option of doing it yourself via glDeleteTextures. If your maps aren't too large, you'd probably want to load only one map's textures, or only the nearby maps' textures, when a map is loaded, and free all unnecessary textures when the map is freed. If all maps share the same textures, and you don't have many textures, just load them all at the start of your game, and free them, or let SDL free them, at the end of your program.

glDeleteTextures frees the texture(s) associated with the array of GLuint you pass it, from the video card. If you want to delete one texture from the video card, just do this: glDeleteTextures(1, &myTextureID). If you want to delete an array, do this: glDeleteTextures(lengthOfTheArray, myTextureIDArray).

Share this post


Link to post
Share on other sites
Quote:
Original post by Servant of the Lord
Quote:
Original post by icecubeflower
Uh... I've sort of like NEVER called glDeleteTextures so am I leaking video memory all over the place?

No, it's called for you when you delete the OpenGL context. In SDL, the context is deleted whenever you call SDL_FreeSurface on the VideoMode surface (AKA, 'screen' or whatever you name it). Because SDL actually calls SDL_FreeSurface(screen) for you, you don't even need to do that.

All you need to do is close SDL when you are done, using SDL_Quit. SDL_Quit will call SDL_FreeSurface(myScreenSurface), along with other things, which will call glDeleteTextures or something equivalent.


Just to clear up any confusion, you can *never* call SDL_FreeSurface() on the video surface. The video surface is a special one which can be freed by either calling SDL_QuitSubSystem(SDL_INIT_VIDEO) or SDL_Quit(). While logically you can think of these two as including the equivalent to a call to SDL_FreeSurface() on the video surface, this doesn't mean that this is precisely what happens.

Quote:

It's important to note, that since deleting the VideoMode surface deletes all your OpenGL textures, if you resize the SDL screen, you'll need to reload all your images again.


I believe this was actually a bug, and I have vague recollections of it being fixed. However, your advice still holds if the bug hasn't been fixed, or you are unable to obtain a version of SDL.dll that contains the fix.

Share this post


Link to post
Share on other sites
It seems that this thread has served its purpose for the most part, but I thought I'd mention that NeHe has a version of its tutorials written with SDL which are perfect for these situations. Just scroll to the bottom of the tutorial you are following and download the Linux/SDL source code. I know there is code for loading textures from SDL_Surfaces (along with many other useful topics).

Share this post


Link to post
Share on other sites
Quote:
Original post by Servant of the Lord
Quote:
Original post by icecubeflower
What if while my program is running the player leaves one map and goes to another and does that a lot of times and lots and lots of images are loaded into video memory? What if all the video memory is taken?
This is why you have the option of doing it yourself via glDeleteTextures. If your maps aren't too large, you'd probably want to load only one map's textures, or only the nearby maps' textures, when a map is loaded, and free all unnecessary textures when the map is freed. If all maps share the same textures, and you don't have many textures, just load them all at the start of your game, and free them, or let SDL free them, at the end of your program.

...


You don't have to do anything special even if the video memory is going to be run out, as the video memory is managed by OpenGL. And, you have not much explicit control over the video memory management.

When you upload a texture to OpenGL, it will keep a copy of the texture in main memory. And, it is OpenGL decision to put a texture onto video memory or not. Although there are OpenGL function for you to check whether a texture is video memory resident or not, you cannot force a texture to be video memory resident explicitly.

When the video memory is going to be exceeded for whatever reason, OpenGL will begin to swap memory between video memory and main memory. So, you can upload as many textures as you wanted onto OpenGL even if it is going to exceed the video memory.

The only problem you need to worry about is the peak video memory usage. For example, your video card have only 100 MB video memory. It is perfectly fine to upload four textures of 50 MB (200 MB as a whole) onto OpenGL as long as you don't bind them all at the same time. However, if you draw a quad which binding all four textures as multi-textures at the same time, OpenGL will probably ignore this command as if no texture is active. And, you will have a black quad in this case.

Share this post


Link to post
Share on other sites

#include "Texture.h"
#include <string>
#include <sdl/sdl_image.h>
#include <sdl/sdl_opengl.h>

Texture::Texture(const char *filename)
{
// Load Texture into Memory
SDL_Surface *image = IMG_Load(filename);
if(!image)
throw std::pair<const char*, const char*>("Texture::Texture", "Error Loading Image from Disk");

// Create Video-Card Texture
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

SDL_FreeSurface(image);
}


void Texture::Bind()
{
glBindTexture(GL_TEXTURE_2D, tex_id);
}



Hey in DarkInsanePyro's code above tex_id is a GLuint, I think, and it's the key to accessing the image which is being stored in video memory. How come in glGenTextures there's an ampersand? glGenTextures needs the address of the integer? But then in glBindTextures tex_id is passed without the ampersand so it just gets the value stored in tex_id. What is going on?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Now

  • Advertisement
  • Similar Content

    • By reenigne
      For those that don't know me. I am the individual who's two videos are listed here under setup for https://wiki.libsdl.org/Tutorials
      I also run grhmedia.com where I host the projects and code for the tutorials I have online.
      Recently, I received a notice from youtube they will be implementing their new policy in protecting video content as of which I won't be monetized till I meat there required number of viewers and views each month.

      Frankly, I'm pretty sick of youtube. I put up a video and someone else learns from it and puts up another video and because of the way youtube does their placement they end up with more views.
      Even guys that clearly post false information such as one individual who said GLEW 2.0 was broken because he didn't know how to compile it. He in short didn't know how to modify the script he used because he didn't understand make files and how the requirements of the compiler and library changes needed some different flags.

      At the end of the month when they implement this I will take down the content and host on my own server purely and it will be a paid system and or patreon. 

      I get my videos may be a bit dry, I generally figure people are there to learn how to do something and I rather not waste their time. 
      I used to also help people for free even those coming from the other videos. That won't be the case any more. I used to just take anyone emails and work with them my email is posted on the site.

      I don't expect to get the required number of subscribers in that time or increased views. Even if I did well it wouldn't take care of each reoccurring month.
      I figure this is simpler and I don't plan on putting some sort of exorbitant fee for a monthly subscription or the like.
      I was thinking on the lines of a few dollars 1,2, and 3 and the larger subscription gets you assistance with the content in the tutorials if needed that month.
      Maybe another fee if it is related but not directly in the content. 
      The fees would serve to cut down on the number of people who ask for help and maybe encourage some of the people to actually pay attention to what is said rather than do their own thing. That actually turns out to be 90% of the issues. I spent 6 hours helping one individual last week I must have asked him 20 times did you do exactly like I said in the video even pointed directly to the section. When he finally sent me a copy of the what he entered I knew then and there he had not. I circled it and I pointed out that wasn't what I said to do in the video. I didn't tell him what was wrong and how I knew that way he would go back and actually follow what it said to do. He then reported it worked. Yea, no kidding following directions works. But hey isn't alone and well its part of the learning process.

      So the point of this isn't to be a gripe session. I'm just looking for a bit of feed back. Do you think the fees are unreasonable?
      Should I keep the youtube channel and do just the fees with patreon or do you think locking the content to my site and require a subscription is an idea.

      I'm just looking at the fact it is unrealistic to think youtube/google will actually get stuff right or that youtube viewers will actually bother to start looking for more accurate videos. 
    • By Balma Alparisi
      i got error 1282 in my code.
      sf::ContextSettings settings; settings.majorVersion = 4; settings.minorVersion = 5; settings.attributeFlags = settings.Core; sf::Window window; window.create(sf::VideoMode(1600, 900), "Texture Unit Rectangle", sf::Style::Close, settings); window.setActive(true); window.setVerticalSyncEnabled(true); glewInit(); GLuint shaderProgram = createShaderProgram("FX/Rectangle.vss", "FX/Rectangle.fss"); float vertex[] = { -0.5f,0.5f,0.0f, 0.0f,0.0f, -0.5f,-0.5f,0.0f, 0.0f,1.0f, 0.5f,0.5f,0.0f, 1.0f,0.0f, 0.5,-0.5f,0.0f, 1.0f,1.0f, }; GLuint indices[] = { 0,1,2, 1,2,3, }; GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW); GLuint ebo; glGenBuffers(1, &ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(float) * 5, (void*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof(float) * 5, (void*)(sizeof(float) * 3)); glEnableVertexAttribArray(1); GLuint texture[2]; glGenTextures(2, texture); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); sf::Image* imageOne = new sf::Image; bool isImageOneLoaded = imageOne->loadFromFile("Texture/container.jpg"); if (isImageOneLoaded) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageOne->getSize().x, imageOne->getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageOne->getPixelsPtr()); glGenerateMipmap(GL_TEXTURE_2D); } delete imageOne; glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture[1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); sf::Image* imageTwo = new sf::Image; bool isImageTwoLoaded = imageTwo->loadFromFile("Texture/awesomeface.png"); if (isImageTwoLoaded) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageTwo->getSize().x, imageTwo->getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageTwo->getPixelsPtr()); glGenerateMipmap(GL_TEXTURE_2D); } delete imageTwo; glUniform1i(glGetUniformLocation(shaderProgram, "inTextureOne"), 0); glUniform1i(glGetUniformLocation(shaderProgram, "inTextureTwo"), 1); GLenum error = glGetError(); std::cout << error << std::endl; sf::Event event; bool isRunning = true; while (isRunning) { while (window.pollEvent(event)) { if (event.type == event.Closed) { isRunning = false; } } glClear(GL_COLOR_BUFFER_BIT); if (isImageOneLoaded && isImageTwoLoaded) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture[1]); glUseProgram(shaderProgram); } glBindVertexArray(vao); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); glBindVertexArray(0); window.display(); } glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbo); glDeleteBuffers(1, &ebo); glDeleteProgram(shaderProgram); glDeleteTextures(2,texture); return 0; } and this is the vertex shader
      #version 450 core layout(location=0) in vec3 inPos; layout(location=1) in vec2 inTexCoord; out vec2 TexCoord; void main() { gl_Position=vec4(inPos,1.0); TexCoord=inTexCoord; } and the fragment shader
      #version 450 core in vec2 TexCoord; uniform sampler2D inTextureOne; uniform sampler2D inTextureTwo; out vec4 FragmentColor; void main() { FragmentColor=mix(texture(inTextureOne,TexCoord),texture(inTextureTwo,TexCoord),0.2); } I was expecting awesomeface.png on top of container.jpg

    • By khawk
      We've just released all of the source code for the NeHe OpenGL lessons on our Github page at https://github.com/gamedev-net/nehe-opengl. code - 43 total platforms, configurations, and languages are included.
      Now operated by GameDev.net, NeHe is located at http://nehe.gamedev.net where it has been a valuable resource for developers wanting to learn OpenGL and graphics programming.

      View full story
    • By TheChubu
      The Khronos™ Group, an open consortium of leading hardware and software companies, announces from the SIGGRAPH 2017 Conference the immediate public availability of the OpenGL® 4.6 specification. OpenGL 4.6 integrates the functionality of numerous ARB and EXT extensions created by Khronos members AMD, Intel, and NVIDIA into core, including the capability to ingest SPIR-V™ shaders.
      SPIR-V is a Khronos-defined standard intermediate language for parallel compute and graphics, which enables content creators to simplify their shader authoring and management pipelines while providing significant source shading language flexibility. OpenGL 4.6 adds support for ingesting SPIR-V shaders to the core specification, guaranteeing that SPIR-V shaders will be widely supported by OpenGL implementations.
      OpenGL 4.6 adds the functionality of these ARB extensions to OpenGL’s core specification:
      GL_ARB_gl_spirv and GL_ARB_spirv_extensions to standardize SPIR-V support for OpenGL GL_ARB_indirect_parameters and GL_ARB_shader_draw_parameters for reducing the CPU overhead associated with rendering batches of geometry GL_ARB_pipeline_statistics_query and GL_ARB_transform_feedback_overflow_querystandardize OpenGL support for features available in Direct3D GL_ARB_texture_filter_anisotropic (based on GL_EXT_texture_filter_anisotropic) brings previously IP encumbered functionality into OpenGL to improve the visual quality of textured scenes GL_ARB_polygon_offset_clamp (based on GL_EXT_polygon_offset_clamp) suppresses a common visual artifact known as a “light leak” associated with rendering shadows GL_ARB_shader_atomic_counter_ops and GL_ARB_shader_group_vote add shader intrinsics supported by all desktop vendors to improve functionality and performance GL_KHR_no_error reduces driver overhead by allowing the application to indicate that it expects error-free operation so errors need not be generated In addition to the above features being added to OpenGL 4.6, the following are being released as extensions:
      GL_KHR_parallel_shader_compile allows applications to launch multiple shader compile threads to improve shader compile throughput WGL_ARB_create_context_no_error and GXL_ARB_create_context_no_error allow no error contexts to be created with WGL or GLX that support the GL_KHR_no_error extension “I’m proud to announce OpenGL 4.6 as the most feature-rich version of OpenGL yet. We've brought together the most popular, widely-supported extensions into a new core specification to give OpenGL developers and end users an improved baseline feature set. This includes resolving previous intellectual property roadblocks to bringing anisotropic texture filtering and polygon offset clamping into the core specification to enable widespread implementation and usage,” said Piers Daniell, chair of the OpenGL Working Group at Khronos. “The OpenGL working group will continue to respond to market needs and work with GPU vendors to ensure OpenGL remains a viable and evolving graphics API for all its customers and users across many vital industries.“
      The OpenGL 4.6 specification can be found at https://khronos.org/registry/OpenGL/index_gl.php. The GLSL to SPIR-V compiler glslang has been updated with GLSL 4.60 support, and can be found at https://github.com/KhronosGroup/glslang.
      Sophisticated graphics applications will also benefit from a set of newly released extensions for both OpenGL and OpenGL ES to enable interoperability with Vulkan and Direct3D. These extensions are named:
      GL_EXT_memory_object GL_EXT_memory_object_fd GL_EXT_memory_object_win32 GL_EXT_semaphore GL_EXT_semaphore_fd GL_EXT_semaphore_win32 GL_EXT_win32_keyed_mutex They can be found at: https://khronos.org/registry/OpenGL/index_gl.php
      Industry Support for OpenGL 4.6
      “With OpenGL 4.6 our customers have an improved set of core features available on our full range of OpenGL 4.x capable GPUs. These features provide improved rendering quality, performance and functionality. As the graphics industry’s most popular API, we fully support OpenGL and will continue to work closely with the Khronos Group on the development of new OpenGL specifications and extensions for our customers. NVIDIA has released beta OpenGL 4.6 drivers today at https://developer.nvidia.com/opengl-driver so developers can use these new features right away,” said Bob Pette, vice president, Professional Graphics at NVIDIA.
      "OpenGL 4.6 will be the first OpenGL release where conformant open source implementations based on the Mesa project will be deliverable in a reasonable timeframe after release. The open sourcing of the OpenGL conformance test suite and ongoing work between Khronos and X.org will also allow for non-vendor led open source implementations to achieve conformance in the near future," said David Airlie, senior principal engineer at Red Hat, and developer on Mesa/X.org projects.

      View full story
    • By _OskaR
      Hi,
      I have an OpenGL application but without possibility to wite own shaders.
      I need to perform small VS modification - is possible to do it in an alternative way? Do we have apps or driver modifictions which will catch the shader sent to GPU and override it?
  • Advertisement