PDA

View Full Version : OpenGL - 2 questions


Ian Jones
01-07-2007, 09:44 AM
Hey all, I'm a fairly inexperienced programmer... totally new to C++ and OpenGL but having had a few years of scripting experience in several languages. So here goes:

1) OpenGL video textures. I'm interested in the methods people use professionally. I'm aware of the NeHe tutorial where he loads an avi frame and uses glTexSubImage2D() to modify an existing texture rather than creating a new texture for every frame. Is this the best method? I can't help wondering if there is a faster or cleaner way, would Mac OsX use this for example in something like Quartz Composer for video textures? Basically, is this in your opinion the professional method?

2) OpenGL Picking. I'm aware of two methods, using the picking system built into OpenGL and the pixel colour comparison method. I'm unsure as to the drawbacks of each system, but lets say I want to develop a complex modern 2d GUI using OpenGL to render. Which would be best? Also, would a simple passing of the mouse coordinates and comparing object positions be even simpler (2d orthographic, so projections aren't neccesary).

If you have any information / links on either of these topics please post them here. ;)

gga
01-08-2007, 07:59 AM
glTexSubImage2D() to modify an existing texture rather than creating a new texture for every frame. Is this the best method?

Pretty much. On Windows you can also use the extremely painful to use DirectX/DirectShow framework for slightly faster conversion from avi's to textures and on Win32/Macs, if you use apple's quicktime, the quicktime api supports feeding a texture for opengl from within its api with its rather convoluted and afaik undocumented syntax.
Both microsoft's directx and apple's quicktime are not only a pain in the neck to use but they are not truly multi-platform apis, so if you do use them, you will constrain your application to not work on other OSes, unlike the case of you using opengl only and doing the conversion yourself.

2) OpenGL Picking. I'm aware of two methods, using the picking system built into OpenGL and the pixel colour comparison method.

Not sure what the pixel colour comparison method is. OpenGL's standard picking system should be fine for all your needs.

Ian Jones
01-08-2007, 08:25 AM
thx, I thought this would be the case with the video api's. I certainly wan't to stay cross platform.

The picking pixel comparison method is to draw an offscreen buffer with unqiuely coloured objects and getPixel on the mouse click, compare this with a lookup table and u have the object u clicked on. Only side affect is that colour can vary on each implementation of opengl so u have to do runtime colour setup checking at initialisation.

I have a new question. Using SDL, I can't seem to get SDL_image to load transparent png's properly. I can't find a concrete answer to the problem. At the moment I'm using NeHe's TGA loader instead, but I'd really rather use png's.

UrbanFuturistic
01-08-2007, 11:23 AM
When you say you can't get it to load transparent pngs properly, what exactly is the manifestation of this problem?

EDIT:

OK, needed to run through some of my own code, been a tad busy lately and I needed a refresher. I did remember that I did have problems with transparency myself. Things you can do include checking if the alpha channel has loadedSDL_Surface *texImage;
texImage = IMG_Load(texFile);
if (texImage->flags & SDL_SRCALPHA)
cout << "alpha loaded\n";If this is the case then there are a couple of things you may need to set.

First, there'sglEnable(GL_ALPHA_TEST); and before you initialise your SDL surface with SDL_SetVideoModeSDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);Don't ask me why this isn't set automatically if you set BPP to 32 but it isn't and it made my brain ache for, ooh, at least a day.

playmesumch00ns
01-08-2007, 12:31 PM
1) OpenGL video textures. I'm interested in the methods people use professionally. I'm aware of the NeHe tutorial where he loads an avi frame and uses glTexSubImage2D() to modify an existing texture rather than creating a new texture for every frame. Is this the best method? I can't help wondering if there is a faster or cleaner way, would Mac OsX use this for example in something like Quartz Composer for video textures? Basically, is this in your opinion the professional method?


The recommended method with opengl 2 is to use pixel buffer objects. There's an example of doing almost exactly what you want in the extension documentation http://www.opengl.org/registry/specs/EXT/pixel_buffer_object.txt

A

Ian Jones
01-08-2007, 12:32 PM
Cheers, will give those suggestions a go. I've been having problems getting the format into OpenGL correctly and having it recognise the alpha channel. I'm not using alpha testing, im using blending.

UrbanFuturistic
01-08-2007, 01:06 PM
You may be doing blending, but without the alpha-test, how will it know what to blend?

Here's the code I'm using for loading textures:int loadTex(GLuint* texObj)
{
// Status indicator
int status = FALSE;

// Create storage space for the texture
SDL_Surface *texImage[NUM_TEX];

// Set up texture maps
glEnable(GL_TEXTURE_2D);
glEnable(GL_ALPHA_TEST);
glGenTextures(NUM_TEX, texObj);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

//Loading loop
for(int i = 0; i < NUM_TEX; i++)
{
//only runs if texture successfully loads
if ((texImage[i] = IMG_Load( szTextureFiles[i])))
{
status += 1;
//Creation of texture objects and binding to pointers
glBindTexture( GL_TEXTURE_2D, texObj[i] );

// Mipmapped Filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

if ((texImage[i]->flags & SDL_SRCALPHA)) //This part's essential, if you try to put a non-alpha'd image through the first option it tanks.
gluBuild2DMipmaps( GL_TEXTURE_2D, 4, texImage[i]->w, texImage[i]->h, GL_RGBA, GL_UNSIGNED_BYTE, texImage[i]->pixels );
else
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, texImage[i]->w, texImage[i]->h, GL_RGB, GL_UNSIGNED_BYTE, texImage[i]->pixels );
//Destroy SDL surface as no longer needed at this stage
SDL_FreeSurface( texImage[i] );
}
else
{
//Output error on texture load failure
fprintf(stderr, "%s\n", SDL_GetError());
}
}
return status;
}

Ian Jones
01-08-2007, 01:53 PM
wow, thx guys... very helpful cheers!

odubtaig: Alpha is obtained through pixel values in the texture data. AFAIK this makes glEnable(GL_ALPHA_TEST) redundant. I'm loading TGA's successfully with alpha working correctly and not using this switch. I'll give it a try though and see what happens. I just think, from what I understand that blending and alpha testing are two different methods.

playmesumch00ns: Excellent! I knew there must be some sort of improved method. Do u know of any other resources or a tutorial which demonstrates this? Also, forgive my naivety but how do I know if I can use OpenGL 2.0 and if I my GPU or OS supports it? Also, will this work with SDL?

UrbanFuturistic
01-08-2007, 02:19 PM
Hmmm, not sure how I got that mixed up. I have the dumb :cry:

Ian Jones
01-08-2007, 03:46 PM
ahh, well that clears that up... I thought alpha test and blending were different.

I did a bit of digging around about OpenGL 2.0 and my operating system. I'm using a macbookpro with an ATI Mobility Radeon X1600 (which according to the ATI site supports 2.0). I'm running OsX 10.4.8 Tiger, and I can't seem to find a clear answer of what version of OpenGL it supports?

I'm very new to this, and am finding this aspect difficult to understand. I guess I need OpenGL 2.0 because my project hinges specifically around a camera feed being pushed into an OpenGL texture. I'm pretty sure I need bleeding edge speed too, so I assume OpenGL 2.0 is the only way to go? I'd like to be able to do complex pixel operations, gets and sets.

playmesumch00ns
01-08-2007, 05:10 PM
Well for complex pixel operations you'll definitely want opengl 2.0. What version you have depends on your drivers. On linux you can check with:
glxinfo | grep "OpenGL version"
Might work in a terminal on the mac, might not.

If you're new to GL you should probably just use glTexSubImage2d first since it's easier to understand.

Before trying to do everything you want to do you should probably do all of the nehe tutorials

UrbanFuturistic
01-08-2007, 05:13 PM
Have you got the OpenGL SuperBible at all? The third edition does give some useful primers on GLSL.

It also answers the OpenGL "what's my version?" question.mystring = glGetString(GL_VERSION);should get you the version number in, well, string format.

Asides from this, I'm not sure why you want OpenGL 2.0 specifically as any implementation from 1.3 onwards should implement pixel buffers, in fact the text file playmesumch00ns linked to specifies OpenGL 1.5 so it's not that big of a worry for this aspect.

UrbanFuturistic
01-08-2007, 05:16 PM
Might work in a terminal on the mac, might not.Unlikely seeing as glxinfo gives you info about GL on X Windows.

Ian Jones
01-09-2007, 12:57 AM
Alright, thx guys I'll keep reading up... I need to buy some books.

CGTalk Moderation
01-09-2007, 12:57 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.