Using single channel texture (OpenGL 2)? - c++

Short storry:
when I render anything using texture loaded like this
glTexImage2D ( GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, pixels );
I get only black
Long storry:
I can get RGBA texture with alpha channel (e.g. text with transparent backgorund using this code):
This code works:
// === load
#define GL_ABGR 0x8000
SDL_Surface * surf = SDL_LoadBMP( "common_resources/dejvu_sans_mono_RGBA.bmp" );
glGenTextures ( 1, &itex );
glBindTexture ( GL_TEXTURE_2D, itex );
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA, surf->w, surf->h, 0, GL_ABGR, GL_UNSIGNED_BYTE, surf->pixels );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// ....
// === render
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, itex );
glColor3f(1.0f,1.0f,1.0f);
glEnable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
drawString ( caption, xmin, ymin+12, 6 );
renders like
But I'm trying to use just one channel (8-bit; grayscale) images / textures instead of RGBA. These I cannot get to render neither with nor without transparancy. Whatever I do I get only black image.
This doesn't
// === load
#define GL_ABGR 0x8000
SDL_Surface * surf = SDL_LoadBMP( "common_resources/dejvu_sans_mono_Alpha.bmp" );
glGenTextures ( 1, &itex );
glBindTexture ( GL_TEXTURE_2D, itex );
glTexImage2D ( GL_TEXTURE_2D, 0, GL_R8, surf->w, surf->h, 0, GL_RED, GL_UNSIGNED_BYTE, surf->pixels );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// ....
// === render
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, itex );
glColor3f(1.0f,1.0f,1.0f);
//glEnable(GL_BLEND);
//glEnable(GL_ALPHA_TEST);
//glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
drawString ( caption, xmin, ymin+12, 6 );
renders like
Notes:
I know that I should somehow use glTexEnv according to e.g. here but my main problem is that apparently the monochrome texture does not render at all
I tried also other GL_LUMINANCE and GL_INTENSITY instead of GL_RED in glTexImage2D with no difference
there are other questions like here and here but mostly with OpenGL>3.0 and fragment shaders
Also, is it possible that my graphics card or driver does not support this ? I'm on ubuntu 16.04
GL_VENDOR: Intel Open Source Technology Center
GL_RENDERER: Mesa DRI Intel(R) HD Graphics 530 (Skylake GT2)
GL_VERSION: 3.0 Mesa 11.2.0
for completeness - although it is not importaint the drawString looks like this:
drawString ( caption, xmin, ymin+12, 6 ){
const int nchars = 95;
float persprite = 1.0f/nchars;
glBegin(GL_QUADS);
for(int i=0; i<65536; i++){
if( str[i] == 0 ) break; // 0-terminated string
int isprite = str[i] - 33; // 33 is offset of meaningfull ASCII characters
float offset = isprite*persprite+(persprite*0.57);
float xi = i*sz + x;
glTexCoord2f( offset , 1.0f ); glVertex3f( xi, y, 3.0f );
glTexCoord2f( offset+persprite, 1.0f ); glVertex3f( xi+sz, y, 3.0f );
glTexCoord2f( offset+persprite, 0.0f ); glVertex3f( xi+sz, y+sz*2, 3.0f );
glTexCoord2f( offset , 0.0f ); glVertex3f( xi, y+sz*2, 3.0f );
}
glEnd();
}

I want to try to help you. In my projects I am using this arguments for generating textures from grayscale source images:
glTexImage2D(GL_TEXTURE_2D, 0, 1, width, height, 0, GL_RED,
GL_UNSIGNED_BYTE, pixels);
As written in documentation, third argument - number of color components (1 in our case). Need to check integer value of GL_R8 or replace it explicitly.
GL_RED means that you place luminances in red channel (not in each red, green, blue channels as for grayscale image).

Related

OpenGL - FBO black screen when rendering to texture on NVIDIA card

I've followed some tutorials on how to setup and render an FBO. At first it worked and the scene rendered fine, but it turns out the program was using the integrated GPU. (I'm using my laptop)
Then, out of curiosity, I ran it with the "higher-performance" GPU (an Nvidia GeForce GT540M) and the screen was all black. At this point I tried saving the FBO's color texture into a file and the scene was actually being drawn there.
But eventually I found a solution. Previously I would only clear the FBO (color and depth buffers), but now I clear both the FBO and the default framebuffer and the scene is rendered again.
So the question is, is it bad that I have to call glClear twice? Do I really have to call glClear twice? Why would the one clear work on the integrated card?
I can show some code if it helps.
Framebuffer initialization
bool FrameBufferObject::initializeFBO( int width, int height ) {
glActiveTexture( GL_TEXTURE0 );
if ( !_colorTexture.createEmptyTexture( width, height ) ) {
return false;
}
_textureId = _colorTexture.getTextureId();
if ( !_depthTexture.createDepthTexture( width, height ) ) {
return false;
}
_depthTextureId = _depthTexture.getTextureId();
glGenFramebuffers( 1, &_frameBufferID );
glBindFramebuffer( GL_FRAMEBUFFER, _frameBufferID );
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _textureId, 0 );
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _depthTextureId, 0 );
if ( glCheckFramebufferStatus( GL_FRAMEBUFFER ) != GL_FRAMEBUFFER_COMPLETE ) {
return false;
}
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
_width = width;
_height = height;
_isInitialized = true;
return true;
}
Color Texture
bool Texture::createEmptyTexture( int width, int height ) {
if ( isLoaded() ) {
closeTexture();
}
GLuint textureId = 0;
glGenTextures( 1, &textureId );
if ( getOpenGLError( "Unable to generate TextureID." ) ) {
return false;
}
glBindTexture( GL_TEXTURE_2D, textureId );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glBindTexture( GL_TEXTURE_2D, 0 );
if ( getOpenGLError( "Error creating empty texture." ) ) {
glDeleteTextures( 1, &textureId );
return false;
}
_isLoaded = true;
_textureWidth = _imageWidth = width;
_textureHeight = _imageHeight = height;
_textureId = textureId;
return true;
}
The Depth Texture is the same, except it uses the GL_DEPTH_COMPONENT format,
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL );
Post Processing Shader
Vertex shader
#version 330 core
in vec2 inVertex;
in vec2 inTexture;
out vec2 texCoords;
void main() {
texCoords = inTexture;
gl_Position = vec4( inVertex.x, inVertex.y, 0, 1 );
}
Fragment shader
#version 330 core
in vec2 texCoords;
uniform sampler2D texture0;
layout(location = 0) out vec4 outColor;
void main() {
vec4 color = texture( texture0, texCoords );
outColor = color;
}
The rendering code looks like this,
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
fbo.bindFBO();
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// .. render scene ..
fbo.unbindFBO();
// This is a namespace
PostProcShader::shader.useShader();
PostProcShader::render( fbo.getColorTexture() );
PostProcShader::shader.disableShader();
SDL_GL_SwapWindow( window );
Where the post processing shader simply renders the texture on a screen-sized quad. The scene uses 3 shaders: one for 3D objects, the second for the skybox and last one for fonts.
I'm using C++, SDL2 and (of course) OpenGL/Glew in VS 2015.
Edit:
Depth Test initialization
glEnable( GL_DEPTH_TEST );
glDepthMask( GL_TRUE );
glDepthFunc( GL_LEQUAL );
glDepthRange( 0.0f, 1.0f );
glClearDepth( 1.0f );
I got a similar problem when I went from Intel HUD to Nvidia. However my solution was simple. I had to ensure I called glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) before rendering to the new framebuffer (on Nvidia) otherwise I would see nothing rendered. I suspect that on Nvidia the depth buffer is not set to the same defaults as on Intel so clearing it sets it to the opengl default clear value.

Alpha channel being rendered black. How to make it transparent?

I'm loading a png texture with 32bit and some transparent regions. I have setted this code in my initialization function:
// OpenGL
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable( GL_ALPHA_TEST );
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glClearColor( 0.0, 0.0, 0.0, 0.0 );
This is how I load a texture:
// Load textures
glGenTextures( 1, &this->texture );
int width, height;
unsigned char* image;
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, this->texture );
std::string path = "../assets/textures/" + name;
image = SOIL_load_image( path.c_str(), &width, &height, 0, SOIL_LOAD_RGBA );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image );
SOIL_free_image_data( image );
glUniform1i( glGetUniformLocation( shader->shader, "materialTex" ), 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_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
This is the effect I get:
I'm trying to make the grass background go from black to transparent.
Can you help me?
There are a couple of problems here, but they all boil down to the fact that alpha transparency is order dependent.
It is not clear why you are using a depth buffer in the first place. The application appears to be 2D and furthermore you seem to be drawing every one of your 2D layers with the same depth. Nevertheless, the red background you are trying to draw needs to be drawn first.
If you drew the background with depth testing enabled, it would write its depth value to the depth buffer and then the other sprites you tried to draw on top of it would fail because the default depth test is GL_LESS (this rejects parts of objects with the same depth). If you disable depth testing when you draw the background, that also disables depth writes, and that is one possible way to fix your problem.
Realistically, you could just eliminate depth testing altogether and follow the golden rule for alpha blending:
Draw opaque geometry first, and then sort translucent objects by depth

nVidia openGL fails to display simple COLOR_INDEX texture

I have my first simple OpenGL program to display 2D images using OpenGL. I'm using an index-based image, calling glTexImage2D(.. GL_RGB, ... GL_COLOR_IMAGE...)
This is working as expected on an ATI card.
Having swapped to an nVidia card, I see a black quad instead of my image. Given that it works on the ATI I guess the code is basically correct - but maybe I have missed a setting - or maybe the card doesn't support what I'm doing (?!)
First the Setup code (I'm using Qt btw, so there's probably some context calls I'm missing):-
glClearColor( 0.1, 0.1, 0.25, 0); // background color
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // 4-byte pixel alignment
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
Here's the code to set the texture :-
GLfloat Greys[256];
GLfloat Ones[256];
for( int I(0); I < 256; ++I )
{
Greys[I] = (GLfloat)I/256;
Ones[I] = 1.0;
}
makeCurrent();
glPixelMapfv( GL_PIXEL_MAP_I_TO_R, 256, Greys );
glPixelMapfv( GL_PIXEL_MAP_I_TO_G, 256, Greys );
glPixelMapfv( GL_PIXEL_MAP_I_TO_A, 256, Ones );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_pImage->size().width(), m_pImage->size().height(), 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, m_pImage->bits() );
Here's the display code
glLoadIdentity();
// Get the camera in the right place
glRotatef( 180, 1, 0, 0 );
// Apply the Pan(Offset), and Zoom
glTranslatef( m_Offset.x(), m_Offset.y(), 0);
glScalef( m_Zoom, m_Zoom, 1 );
// Display the image texture mapped to a rectangle
glColor3f( 1,1,0 );
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f( 0, 0 ); glVertex3f( 0, 0, 10 );
glTexCoord2f( 1, 0 ); glVertex3f( ImSize.width(), 0, 10 );
glTexCoord2f( 1, 1 ); glVertex3f( ImSize.width(), ImSize.height(), 10 );
glTexCoord2f( 0, 1 ); glVertex3f( 0, ImSize.height(), 10 );
glEnd();
glDisable(GL_TEXTURE_2D);
I also display the same image in full colour, in a separate window, using a straight RGB - RGB call to glTexImage2D. So I'm confident the dimensions are acceptable.
If I remove the call to glTexImage2D then I get a yellow quad as expected. Thus I suspect I have a problem with my calls to set the colour LUTs.
Board is an ASUS GeForce 210 silent
Windows XP 32 bit.
nVidia Drivers 6.14.13.681 (9-23-2012), R306.81 (branch: r304_70-122)
Did you test for OpenGL error codes? You may use this code: https://gist.github.com/4144988 – regarding color index formats? I wouldn't be surprised if it simply wasn't supported by the driver. Nobody uses color index formats these days. If you want to draw a palleted texture, upload the pallete into a 1D RGB texture and the color indexed image into a single channel (GL_RED or GL_LUMINANCE, depending on the OpenGL version) 2D texture and use the value as index into the pallete texture.

How can i use a texture .jpg image for an openGL background window in mac?

I want to set a background for an openGL window for mac. background will take a jpg or png file.
Here is my code..
GLuint texture; //the array for our texture
GLfloat angle = 0.0;
GLuint LoadTexture (const char * filename, int width, int height ){
// GLuint texture;
unsigned char * data;
FILE * file;
//The following code will read in our RAW file
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// // when texture area is small, bilinear filter the closest mipmap
// glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
// GL_LINEAR_MIPMAP_NEAREST );
// // when texture area is large, bilinear filter the original
// glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
//
// // the texture wraps over at the edges (repeat)
// glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
// glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
//
// //Generate the texture
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,GL_RGB, GL_UNSIGNED_BYTE, data);
// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// // the texture wraps over at the edges (repeat)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
GL_RGB, GL_UNSIGNED_BYTE, data );
free(data);
return texture; //return whether it was successful
}
void FreeTexture( GLuint texture ){
glDeleteTextures( 1, &texture );
}
void cube () {
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture
glPushMatrix();
glRotatef( angle, 0.0f, 0.0f, 1.0f );
glBegin( GL_QUADS );
glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
glEnd();
glPopMatrix();
glutSwapBuffers();
//glutSolidCube(2);
}
void display () {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
texture = LoadTexture( "/Users/macbook/MatrixEngineClientSample/Fighters/Sunset03.jpg", 256, 256 ); //load the texture
glEnable( GL_TEXTURE_2D ); //enable 2D texturing
// glEnable(GL_TEXTURE_GEN_S); //enable texture coordinate generation
// glEnable(GL_TEXTURE_GEN_T);
cube();
FreeTexture( texture );
//glutSwapBuffers();
//angle ++;
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
//glLoadIdentity ();
gluPerspective (50, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("A basic OpenGL Window");
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutMainLoop ();
return 0;
}
EDIT
I want to see this image as a background of the openGL window...image is below..
but it showing this...
I consulted this Apple guide.
The problem is that the file is compressed AND has a header, and with your code you are even using part of the file header as an image source.
I would replace this code:
// GLuint texture;
unsigned char * data;
FILE * file;
//The following code will read in our RAW file
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );
With something more similar to this:
CFURLRef urlRef = (CFURLRef)[NSURL fileURLWithPath:#"/Users/macbook/MatrixEngineClientSample/Fighters/Sunset03.jpg"];
CGImageSourceRef myImageSourceRef = CGImageSourceCreateWithURL(url, NULL);
CGImageRef myImageRef = CGImageSourceCreateImageAtIndex(myImageSourceRef, 0, NULL);
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(myImageRef));
unsigned char *data = CFDataGetBytePtr(data);
You may need to add Core Graphics and Core Foundation to the list of your linked frameworks.
PNG and JPG images (as with most other image formats) require some form of decompression/decoding, so loading them raw from a file wont produce expected results. It should work with bmp or uncompressed tga images after reading the file header though :/ . Anyway, here are a few image loading libraries that should make loading images easy:
SOIL
DevIL
FreeImage
GLI

I'm seeing artifacts when I attempt to rotate an image

This is the before:
http://img22.imageshack.us/img22/5310/beforedes.jpg
znd after:
http://img189.imageshack.us/img189/8890/afterr.jpg
EDIT:: Now that I look at imageshack's upload, the artifacts are diminished a great deal.. but trust me, they are more pronounced than that.
I don't understand why this is happening. Imageshack uploads them to jpg, but in my program they are in the image folder as .tif (The reason for .tif is because I couldn't get ANY other image to maintain their transparent parts).
But anyways, these artifacts follow the original top of the image as it rotates anywhere except the original.
Here's part of my code that loads the image
GLuint texture;
GLenum texture_format;
GLint nofcolors;
GLfloat spin;
bool Game::loadImage()
{
SDL_Surface * surface; // this surface will tell us the details of the image
if ( surface = SM.load_image("Images/tri2.tif") )
{
//get number of channels in the SDL surface
nofcolors = surface->format->BytesPerPixel;
//contains an alpha channel
if ( nofcolors == 4 )
{
if ( surface->format->Rmask == 0x000000ff )
texture_format = GL_RGBA;
else texture_format = GL_BGRA;
}
else if ( nofcolors == 3 ) //no alpha channel
{
if ( surface->format->Rmask == 0x000000ff )
texture_format = GL_RGB;
else texture_format = GL_BGR;
}
// Have OpenGL generate a texture object handle for us
glGenTextures( 1, &texture );
// Bind the texture object
glBindTexture( GL_TEXTURE_2D, texture );
// Set the texture’s stretching properties
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, nofcolors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels );
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
else
{
SDL_Quit();
return false;
}
// Free the SDL_Surface only if it was successfully created
if ( surface )
{
SDL_FreeSurface( surface );
return true;
}
else return false;
}
void Game::drawImage()
{
// Clear the screen before drawing
glClear( GL_COLOR_BUFFER_BIT );
glTranslatef( float(S_WIDTH/2), float(S_HEIGHT/2), 0.0f );
glRotatef( spin, 0.0, 0.0, 1.0 );
// Bind the texture to which subsequent calls refer to
glBindTexture( GL_TEXTURE_2D, texture );
glBegin( GL_QUADS );
{
// Top-left vertex (corner)
glTexCoord2i( 0, 0 );
glVertex3f( -64, 0, 0 );
// Top-right vertex (corner)
glTexCoord2i( 1, 0 );
glVertex3f( 64, 0, 0 );
// Bottom-right vertex (corner)
glTexCoord2i( 1, 1 );
glVertex3f( 64, 128, 0 );
// Bottom-left vertex (corner)
glTexCoord2i( 0, 1 );
glVertex3f( -64, 128, 0 );
}
glEnd();
glLoadIdentity();
SDL_GL_SwapBuffers();
}
Looks like the texture is set to GL_WRAP. Try GL_CLAMP_TO_EDGE instead.
In Game::loadImage, after your glBindTexture call:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Your current setting is GL_REPEAT, which is the OpenGL default.