• Advertisement
Sign in to follow this  

OpenGL Transparency using glDrawPixels

This topic is 3452 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

Does OpenGL use pre-multiplied alpha? I can't find anything about it in the red book or even on google but i have been using the SOIL library with my game for a while now an i use it with the flag SOIL_FLAG_MULTIPLY_ALPHA. When i use it without this some of my images have while where it should be transparent. That was for my GUI, now i am up to writing the code for the game maps and (for various reasons) i am using raw pixel data instead of GL textures. But i don't know if i need to pre-multiply the pixel data. I tried with and without and it definitely seems to use pre-multiplied alpha. But the background of the image is still black. I am drawing the pixel data over the top of a textured quad and the image should show the texture through the transparent parts. Well, i'll just show you... The blend function is (GL_ONE, GL_ONE_MINUS_SRC_ALPHA). [Edited by - XTAL256 on July 27, 2008 9:50:09 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by XTAL256
Does OpenGL use pre-multiplied alpha?

No, it doesn't.

As to why you don't get transparency, there can be lots of reasons - blending not enabled, alpha channel not transferred, incorrectly configured pixel transfer pipeline, etc. You don't provide enough information to proceed any further than pure speculation.

Share this post


Link to post
Share on other sites
Gl uses whatever blend mode you tell it to. By looking at your blend function it seems like you are using pre-multiplied alpha. Make sure you have all relevant alpha options turned on, and that you are using alpha format for your texture.

Share this post


Link to post
Share on other sites
Oh yeah, I didn't notice that you use a strange blending mode.

The best and most straightforward way is to use (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). Your alpha value then simply represents opacity.

Share this post


Link to post
Share on other sites
Quote:
Original post by Yann L
Oh yeah, I didn't notice that you use a strange blending mode.

The best and most straightforward way is to use (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). Your alpha value then simply represents opacity.



That's not "the best", it has all sorts of problems compared to premultiplied: http://home.comcast.net/~tom_forsyth/blog.wiki.html#%5B%5BPremultiplied%20alpha%5D%5D

Both are equally straightforward as they involve setting the blending mode.

I'm guessing previous posters are correct and part of the state is missing (i.e blending disabled, etc).

Share this post


Link to post
Share on other sites
Quote:
Original post by raigan
That's not "the best", it has all sorts of problems compared to premultiplied: http://home.comcast.net/~tom_forsyth/blog.wiki.html#%5B%5BPremultiplied%20alpha%5D%5D

And premultiplied alpha has even more problems that (conveniently) aren't mentioned in that article. Standard alpha blending is the 'best' in the sense that it is the easiest, most intuitive and most flexible method that is achievable with todays' non-programmable blending stages.

Premultiplied alpha is in no way more 'correct' than standard alpha blending. Physically, they're both entirely wrong. However, while premultiplied alpha can have its uses in image composition (keep in mind that it was originally designed to speed up CPU blending !), it is often more incorrect than standard alpha blending when introducing concepts such as HDR, non-linear blending or the dreaded HDR-MSAA-Tonemapping combo. And with modern programmable pipelines, what do you call 'pre'-multiplied alpha anyway ? When does the post-multiply become a pre-multiply ? It doesn't really make any sense.

This article cites the whole DXT compression deal as an advantage for premultiplied alpha. That is entirely incorrect. This is strictly a question about the behaviour of lossy compression algorithms regarding to edge cases, especially colour values that are totally transparent. Premultiplying them with alpha is in no way a guarantee to increase DXT compression quality. Infact, while it might improve quality on some images, it can seriously degrade quality on others. And beside doing nothing (ie. leaving the colour values as-is) or multiplying with alpha, there are a whole pile of other techniques that consistently improve quality on almost all cases, and that have absolutely nothing to do with premultiplied alpha.

Whether or not this is what the OP is after is another question. I have personally never felt the need for PM alpha. If you don't need physically correct blending, then standard alpha is perfectly fine and gives you the option to modify transparency on the fly. If you need physically correct transparency, then you can't use the standard blending pipeline anyway.

Share this post


Link to post
Share on other sites
I'm kind of a n00b when it comes to OpenGL. I am just making this game as a hobby, i never took any courses on OGL and i haven't read all of the red book. I know the basics of OGL and i even made a Windows Media Player visualisation using it but i never understood how to do alpha blending. When i tried the mode that i thought would work, i just got black where it should have been transparent. So when i started making my game i used SOIL for loading images and for reasons i can no longer remember i used GL_ONE, GL_ONE_MINUS_SRC_ALPHA as the blend mode. I just set blend mode to what you said (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA without pre-multiplied alpha) and it works just the same, so i'll use that.
But i still can't get the map to draw properly. I worked out (before i posted this) that i needed to disable textures before drawing pixels but do i also need to turn off alpha blending? Because it still doesn't work if i do. I know it's hard to help me if i don't provide much info but i don't know where the problem is. Here is some of the code:

/// Map.h /////////////////////////
class Map {
public:
static Vector<Map*> maps; // List of all Maps
static void loadMaps(); // Load Maps from Map directory

public:
bool isCreated; // If the map has been created
String name, description;
String dir; // Directory where map data is
Image* preview; // Preview image to show in GUI
int width, height; // Size of map (px)
Rect view; // Area in view on screen
int maxPlayers; // Maximum number of players
Image* background; // Background image
int bgTileMode; // Whether to tile or stretch background
unsigned char* image; // Map image
unsigned char* terrain; // Terrain map

Map() : isCreated(false) {}
~Map();
void load(String &filename);
void create();
void distroy();
void draw();
};

/// Map.cpp ///////////////////////
void Map::draw() {
drawImage(background, 0, 0, 800, 450); // Draws a texture mapped quad with background image on it
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glRasterPos2i(100, 300); // These values are just for debugging
glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, image);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
}


The image is made up of smaller tile images but basically the image data is loaded using SOIL_load_image (see SOIL docs). When i don't use pre-multiplied alpha (and using the blend mode you guys suggested) the transparent areas are white but when i do use it the transparent areas are black, which i assume is transparent because the background is black.

Share this post


Link to post
Share on other sites
Quote:
Original post by Yann L
And premultiplied alpha has even more problems that (conveniently) aren't mentioned in that article. Standard alpha blending is the 'best' in the sense that it is the easiest, most intuitive and most flexible method that is achievable with todays' non-programmable blending stages.



Having to resort to complex, ad-hoc pre-processing in order to avoid weird halos around the edges of transparent textures is what makes the standard method awkward, non-intuitive (especially for beginners) and pretty damn stupid: http://www.robinwood.com/Catalog/Technical/SL-Tuts/SLPages/WhiteHalo2.html

Compression aside, what are the "even more" problems with PM?

Share this post


Link to post
Share on other sites
Quote:
Original post by raigan
Quote:
Original post by Yann L
And premultiplied alpha has even more problems that (conveniently) aren't mentioned in that article. Standard alpha blending is the 'best' in the sense that it is the easiest, most intuitive and most flexible method that is achievable with todays' non-programmable blending stages.



Having to resort to complex, ad-hoc pre-processing in order to avoid weird halos around the edges of transparent textures is what makes the standard method awkward, non-intuitive (especially for beginners) and pretty damn stupid: http://www.robinwood.com/Catalog/Technical/SL-Tuts/SLPages/WhiteHalo2.html

That's a photoshop/content creation issue that has nothing to do with pre-multiplied vs. non-pre-multiplied. PEBKAC.

Share this post


Link to post
Share on other sites
I guess I wasn't clear -- this image on that page specifically shows the sort of thing you have to do (either via the plugin on that page OR your own pre-process) to make sure that fully transparent pixels are colored to avoid haloing: http://www.robinwood.com/Catalog/Technical/SL-Tuts/SLGraphics/Halo/Halo12-Solidified.jpg

Color from fully transparent pixels being "pulled in" by texture filtering isn't a Photoshop/content creation issue, it's a direct result of the non-PM blending formula.

Another way to look at it is that non-PM will always lerp both color and alpha channels at the same time, which is not what you want at the edges of transparencies -- you want the alpha to lerp to 0 while the color stays constant. This is impossible to achieve with non-PM blending, and pre-processing images to "bleed" the correct color into fully transparent texels is a common work-around, not a proper solution.

PM lets you avoid this problem, which is the main reason to use it IMHO; I thought this was a well-known issue, or else I would have been more specific with the example link.. it was just the first one I came across.

Share this post


Link to post
Share on other sites
Quote:
Original post by raigan
Color from fully transparent pixels being "pulled in" by texture filtering isn't a Photoshop/content creation issue, it's a direct result of the non-PM blending formula.

No, it's a direct result of incorrectly using the blending stage (or the whole pixel pipeline).

Quote:
Original post by raigan
Another way to look at it is that non-PM will always lerp both color and alpha channels at the same time, which is not what you want at the edges of transparencies -- you want the alpha to lerp to 0 while the color stays constant. This is impossible to achieve with non-PM blending, and pre-processing images to "bleed" the correct color into fully transparent texels is a common work-around, not a proper solution.

Blatantly incorrect. Infact, flood filling around transparent texel is one correct solution (although others exist). Premultiplied alpha is essentially nothing but a (bad) hack, that will fail in many occasions, and that was developed in a time where fully programmable image manipulation hardware, that operates with full floating point precision and without clamping/saturating, simply didn't exist.

In fact, you asked where else PM alpha would create problems other than with (compressed) textures. Well, actually, nowhere - because premultiplied alpha doesn't even exist anywhere else on modern hardware ! This whole pre- versus post- multiply will only make a difference when information gets lost somewhere in the middle - if it is clamped, saturated, or non-linearily combined without user control. Guess what - the whole graphics pipeline of a modern 3D card is both fully programmable and doesn't lose information (except for numerical rounding errors, but these are irrelevant for image blending). Pre or post alpha, it doesn't make a difference in a floating point pipeline, because they're mathematically the same !

So the only point where you can still use them is with LDR textures. And even there, it doesn't make sense, as I outlined above. Take FP16/32 textures and/or shader filtering, and PM-alpha completely disappears from this plane of existance.

Quote:
Original post by raigan
PM lets you avoid this problem, which is the main reason to use it IMHO; I thought this was a well-known issue,

Yes, this is a well known issue. And PM alpha is an incorrect way of addressing it.

Share this post


Link to post
Share on other sites
Uh, guys, back to my problem. I have already realised that i don't need to use pre-multiplied alpha, my other problem (which at first i thought was related to PM alpha) is that i get black where it should be transparent when i draw the pixels on top of a textured quad. You can see what i am doing in the code i posted for Map::draw, as for creating the image, well, here is the code for loading the "composite images" that contains all of the tiles:

void CompositeImage::load(XMLNode node) {
numTiles = node.numChild("tile");
tiles = new MapTile[numTiles];
int ch; // Number of channels the original image had

// Load image data
String imgFile = node.getAttribute("img");
image = SOIL_load_image(imgFile.c_str(),&width,&height,&ch,SOIL_LOAD_RGBA);
if (image == NULL) // Throw error if load fails
throw ImageError("Could not load map image \"" + imgFile +
"\"\nReason: " + SOIL_last_result());
Image::invertY(image, width, height, 4); // Need to flip image vertically
Image::premultAlpha(image, width, height, 4);

... // Create terrain data then create tiles
}

/// Image.cpp //////////////////////

/* Converts an image with an regular alpha channel to pre-multiplied alpha */
/* Taken from the SOIL code (../soil/soil.c, line 1004) */
void Image::premultAlpha(unsigned char* img, int w, int h, int channels) {
int i;
switch(channels) {
case 2:
for(i = 0; i < 2*w*h; i += 2) {
img[i] = (img[i] * img[i+1] + 128) >> 8;
}
break;
case 4:
for(i = 0; i < 4*w*h; i += 4) {
img[i+0] = (img[i+0] * img[i+3] + 128) >> 8;
img[i+1] = (img[i+1] * img[i+3] + 128) >> 8;
img[i+2] = (img[i+2] * img[i+3] + 128) >> 8;
}
break;
default:
/* no other number of channels contains alpha data */
break;
}
}


There is nothing that i can see in that code that would cause the problem. SOIL_load_image just loads an image in RGBA format. Although i said i wasn't going to use PM alpha, for some reason i still get different results with and without it.

The area that is white in the second pic should be transparent and the whole black background rectangle should also. The coloured squiggly lines are semi-transparent in the original image, you can see that the colours overlap but only show the black background behind it instead of the proper background image (the city, see my first post for a better view of it).

Share this post


Link to post
Share on other sites
can anyone help me? I can't seem to find anything wrong with my code so it must be some sort of OpenGL related thing that i am not aware of, like not being able to use glDrawPixels with textures. And if that is the case then how else can i do it?
thanks

Share this post


Link to post
Share on other sites
Can it be a depth test problem? If you enable depth testing and leave it at the default depth test function (GL_LESS), you cannot draw two images at exactly the same depth becuase the second one will fail the depth test.

Share this post


Link to post
Share on other sites
Ah, thank you. I would have never thought of that. Oh, but i disabled depth testing:
glDisable(GL_DEPTH_TEST);
when i init my graphics routines. I never really thought that i would need depth testing with a 2D game but i didn't know what would happen if i left it on (which i didn't). Well just in case the problem is similar to that i will post the code for initiating GL:

/* Intialise the graphics routines and pixel format */
void graphics::initGraphics() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glShadeModel(GL_FLAT);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_COLOR_LOGIC_OP);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

// Set up orthographic view
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, screenWidth, screenHeight, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

// Create bitmap fonts from images
Font::initFonts();
}


But i doubt that anything there would cause my problem.
Just to make sure, colour (0,0,0,0) is transparent when GL_BLEND is enabled with GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA? I will double-check the image data to make sure it is 0,0,0,0 and not 0,0,0,255 or something.

Share this post


Link to post
Share on other sites
I googled my problem and found this post at gamedev. One of the guys there said that glDrawPixels can be used with alpha but he didn't really go into it any more. One of the quotes he posted mentioned that "you may need to control the current drawing buffers". I don't know that much about OpenGL so maybe someone could explain why that is needed (it sounds like OGL uses two or more buffers where it can write images to and then blend the pixels from each buffer).
Also, does anyone know of any websites that have a tutorial or a "dumbed-down" explanation of some of these OGL functions like blending, working with pixel data, and the framebuffer. Or will i have to read all of the red book [depressed]. I'm sorry but i just can't sit down and read that thing, i prefer a more practical learning approach (that i'm sure many people do).

Share this post


Link to post
Share on other sites
I was just reading the OpenGL red book and i remembered that i am using double buffering, i wondered if i needed to select the correct buffer for reading/writing to. I set the buffer to GL_FRONT and GL_BACK but neither had any affect. I don't think i need double buffering anyway so i might disable it, i should also mention that i am using SDL with OpenGL so i use SDL commands to do it (i.e. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);), still, i don't think that would make any difference.

Share this post


Link to post
Share on other sites
Since OpenGL is designed to be a 3d API, it generally doesnt handle constant-depth (2d) over-draw in a predictable manner. Rather than relying on OpenGL to simulate transparency with alpha blending, you might want to just manipulate the raw image data yourself.

i.e. assuming you have 2 images stored in arrays of RGBA values

for each pixel in image array:
__if overlapping image alpha = 1:
____current pixel red = overlapping red
____current pixel blue = overlapping blue
____current pixel green = overlapping green
__else if overlapping alpha = 0:
____dont change current pixel, the overlapping image is transparent here

draw the resulting texture to the screen


This is probably the complete-noob way to do it, but at least Im fairly confident it will work. If it doesnt, your image's alpha values are probably messed up.

Share this post


Link to post
Share on other sites
Well that would only account for opaque or transparent images (i.e. alpha of 1 or 0), not varying degrees of transparency. I'm also not sure if that will be slower than having OpenGL do it.
Quote:
Since OpenGL is designed to be a 3d API, it generally doesnt handle constant-depth (2d) over-draw in a predictable manner.

I did not know that. But i even tried drawing the map image twice slightly overlapping each other and transparency didn't even work then. i.e. using glDrawPixels to draw a transparent image over another image drawn with glDrawPixels didn't work.

And as i asked before, would double buffering have any affect? EDIT: i just disables double buffering and i didn't notice any difference.

[Edited by - XTAL256 on August 4, 2008 3:16:57 AM]

Share this post


Link to post
Share on other sites
/// Map.cpp ///////////////////////
void Map::draw() {
drawImage(background, 0, 0, 800, 450); // Draws a texture mapped quad with background image on it
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glRasterPos2i(100, 300); // These values are just for debugging
glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, image);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
}



Looks like you are disabling blending(glDisable(GL_BLEND)) right before drawing your pixels. That could be the problem.

Share this post


Link to post
Share on other sites
Na, i think that is for textures only and has no effect on glDrawPixels. I don't know for sure but i have tried with and without GL_BLEND and neither work.
In case anyone wants have a thorough look through my code (i'm not saying you have to, just if you have nothing else to do) i have uploaded my source code here. The code in question is in map/Map.cpp functions Map::create() and Map::draw(). Also look at the other files in that directory.

Share this post


Link to post
Share on other sites
Quote:
Original post by XTAL256
Na, i think that is for textures only and has no effect on glDrawPixels. I don't know for sure but i have tried with and without GL_BLEND and neither work.
In case anyone wants have a thorough look through my code (i'm not saying you have to, just if you have nothing else to do) i have uploaded my source code here. The code in question is in map/Map.cpp functions Map::create() and Map::draw(). Also look at the other files in that directory.


Wrong, Blending is part of the pipeline, and works whatever your fragments come from textures, or from glDrawPixels.

So enable it to achieve transparency. (And setup the blend equation)

Share this post


Link to post
Share on other sites
Oh, ok. For some reason i got the impression that i had to disable GL_BLEND for glDrawPixels, i must have just been thinking of GL_TEXTURE_2D.
But even if i do that it still doesn't have transparency.

Share this post


Link to post
Share on other sites
Now that you've got blend back on i suggest commenting out
"Image::premultAlpha(image, width, height, 4);"
And see if that helps.

If not i cant really guess at why it wouldn't be working. I get alpha blending just fine with glDrawPixels using pretty much just:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

...

glWindowPos2i(50,400);
glDrawPixels(imagewidth, imageheight, 0x1908, GL_UNSIGNED_BYTE, imagedata);

Share this post


Link to post
Share on other sites
Oh yeah, i forgot i still had pre-multiplied alpha on. Actually, i thought i already commented it out, i must have missed it. Oh, i remember why i kept it. For some reason, it doesn't have any transparency at all if i don't pre-multiply alpha. When i use it, the image is blended with the black background but not with the texture behind it.

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