OpengGL - SOIL texture all black - c++

I linked, included, and did everything necessary to use SOIL and it's functions.
I load the texture like so in init():
void initRendering() {
std::string vertexInputString="exampleVS.txt";
std::string fragmentInputString="exampleFS.txt";
//create new shader
InitializeProgram(shaderOne, readFiletoString(vertexInputString), readFiletoString(fragmentInputString));
//enables/disables
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable (GL_BLEND);
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
//states
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
//load textures
image = SOIL_load_OGL_texture(
"resources/wood.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
if( 0 == image ){printf( "SOIL loading error: '%s'\n", SOIL_last_result());}
//start shader
glUseProgram(shaderOne);
}
For simplicity the wood.png is a 256x256 png texture and loads without an error message.
Here is where i draw the textured quad:
glPushMatrix();
glColor4f(0.0, 0.0, 0.0,1.0f);
glBindTexture(GL_TEXTURE_2D, image);
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_DECAL);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_DECAL);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); // when texture area is small, bilinear filter the closest mipmap
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // when texture area is large, bilinear filter the first mipmap
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // texture should tile
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f,0.0f); glVertex3f(-50.0f, 10.0f, 50.0f);
glTexCoord2f(0.0f,1.0f); glVertex3f(-50.0f, 10.0f, -50.0f);
glTexCoord2f(1.0f,1.0f); glVertex3f(50.0f, 10.0f, -50.0f);
glTexCoord2f(1.0f,0.0f); glVertex3f(50.0f, 10.0f, 50.0f);
glEnd();
glPopMatrix();
The result is simply an all black texture instead of a wood texture:

Related

glutpostredisplay() equivalent in winapi

I am following this article to render a video onto a texture using OpenGL and winforms using C++.
I have changed the code in the renderer as follows. But the glutPostRedisplay(); is not working. The same logic works well when I am creating a OpenGL window and rendering over there. But does not seem to work well in winforms.
As of what I understood is that the glutPostRedisplay is trying to refresh my main winforms window and not the OpenGL viewport. I am not sure how to Refresh my viewport.
void OpenGLForm::COpenGL::Render(System::Void)
{
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
// These are necessary if using glTexImage2D instead of gluBuild2DMipmaps
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
// Draw a textured quad
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(frame_width, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(frame_width, frame_height);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, frame_height);
glEnd();
glFlush();
//glutSwapBuffers();
OpenGLForm::COpenGL::SwapOpenGLBuffers();
//Get data from the camera
uint32_t* buffer = new uint32_t[frame_width * frame_height * 4];
if (display_mode == DISPLAY_ARGB) {
// Pass a pointer to the texture directly into Thermal_GetImage for maximum performance
status = Thermal_GetDisplayImage(camera, buffer, (uint32_t)frame_pixels);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA8,
frame_width,
frame_height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
buffer);
// Clean up buffer
delete[] buffer;
// Update display
glutPostRedisplay();
}
void OpenGLForm::COpenGL::SwapOpenGLBuffers(System::Void)
{
SwapBuffers(m_hDC);
}
glutPostRedisplay only works with a "glut"-window. (glutCreateWindow). You have to use a Win-API function to invalidate the client area of the window (e.g. InvalidateRect):
InvalidateRect(HWND, NULL, TRUE);

Open GL texture flickering with GL_TEXTURE_MIN_FILTER at GL_NEAREST

I have a simple OpenGL program built with GLUT that draws a few buildings, have a keyboard controlled moving camera, and I am trying to texture the ground. The animation is smooth.
The problem I have is with texturing mapping the ground. Two separate issues with two separate approaches
1) If I use glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); then I have no
flickering when I zoom out to far away. But the issue is when I zoom out from far, it appears as a single solid color.
From far:
From close:
2) If I use, glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); then I have a lot of flickering
when I zoom out to far away as I move, almost look like a TV noise when you are out of tune.
But stationary, it looks more natural.
From far:
From close:
Questions: What would be a simple way from my existing code to get the best of both worlds? Looking natural from far, and no flickering of the texture as I move. Thank you.
Here is the relevant code:
GLuint grassTextureId;
GLfloat GROUND_PLANE_WIDTH = 1000.0f;
void Display()
{
glLoadIdentity();
camera.Update();
...
FlatGroundPlane_Draw(void);
...
glutSwapBuffers();
}
void FlatGroundPlane_Draw(void)
{
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, grassTextureId); // call glBindTexture before glBegin
glBegin(GL_QUADS);
glNormal3f(0, 1, 0);
glTexCoord2d(0, 0);
GLdouble textCoord = GROUND_PLANE_WIDTH;
glVertex3f( -GROUND_PLANE_WIDTH, 0, -GROUND_PLANE_WIDTH);
// go beyond 1 for texture coordinate so it repeats and tiles
glTexCoord2d(0, textCoord);
glVertex3f( -GROUND_PLANE_WIDTH, 0, GROUND_PLANE_WIDTH);
glTexCoord2d(textCoord, textCoord);
glVertex3f( GROUND_PLANE_WIDTH, 0, GROUND_PLANE_WIDTH);
glTexCoord2d(textCoord, 0);
glVertex3f( GROUND_PLANE_WIDTH, 0, -GROUND_PLANE_WIDTH);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void Initialise()
{
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
modelParser = new ModelParser();
// grass texture is 1024x1024
// from http://seamless-pixels.blogspot.ca/2012_10_01_archive.html
grassTextureId = modelParser->LoadTiledTextureFromFile("./Grass/Grass_1024.ppm");
}
void ModelParser::UploadTiledTexture(unsigned int &iTexture, const RGBImage &img)
{
glGenTextures(1, &iTexture); // create the texture
glBindTexture(GL_TEXTURE_2D, iTexture);
// Issue 1: no flickering, but appear solid color from far away
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Issue 2: flickering noise during movement but appear realistic from far away
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// the texture would wrap 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 );
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img.Width(), img.Height(), GL_RGB, GL_UNSIGNED_BYTE, img.Data());
}
int main(int argc, char** argv)
{
...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
...
glutDisplayFunc(&GLUT::Display);
...
glutMainLoop();
}

Drawing single quad texture openGL ES 2.0

I'm pretty new at ES 2.0, I'm trying to draw a single texture in openGL ES 2.0.
Here is my code:
data : a pointer to the texture (it is working, I already could test it with ES 1.0)
w : texture width
h : texture height
texture is 256x256 pixels
glGetError does return GL_NO_ERROR on every lineā€¦
Hope someone can help ! All I can see is a black screen !
Thanks,
Wise
{
{
enum {
ATTRIB_VERTEX=0,
ATTRIB_TEXTURE_POSITION,
NUM_ATTRIB
};
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const GLfloat vertices[] = { -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f };
const GLfloat texture_coord[] = { 0.0f, 1.0f,0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f };
GLuint texName;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texName);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, vertices);
glEnableVertexAttribArray(ATTRIB_TEXTURE_POSITION);
glVertexAttribPointer(ATTRIB_TEXTURE_POSITION, 2, GL_FLOAT, 0, 0, texture_coord);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,w,h,
0, GL_RGBA,
GL_UNSIGNED_BYTE, data);
glDrawArrays (GL_TRIANGLES, 0, 4);
}
}
Is That full source ?? If so , you have to do these but you must do setting shader before these .
Setting hadles
ATTRIB_VERTEX = glGetAttribLocation(m_paintProgram, "position");
ATTRIB_TEXTURE_POSITION = glGetAttribLocation(m_paintProgram, "inputTextureCoordinate");
maintextureHandle= glGetUniformLocation(m_EffectProgram[displayMode], "texture0");
giving the value through handles
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texName);
glUniform1i(maintextureHandle, 0); // add
And also you don't have to call this code every time , just call it once
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,w,h,0, GL_RGBA,GL_UNSIGNED_BYTE, data);
Actually , your code doesn't have so many required codes such as Shader load, Shader compile, glUseProgram() , glBindFramebuffer() , glviewport(), glBindRenderbuffer things like that. but I can't teach all of them so
I want you to refer to this link and learn more information of opengl es 2.0.
http://examples.oreilly.com/9780596804831/readme.html#WireframeSkeleton

Is there a way to put in a buffer FBO texture output format GL_ALPHA?

I am using ftgl font library.
ftgl function to create image has this code:
glBindTexture(GL_TEXTURE_2D, textID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, textureWidth, textureHeight,0, GL_ALPHA, GL_UNSIGNED_BYTE, textureMemory);
Format GL_ALPHA.
When I render to FBO images(GL_RGBA) they look ok.
But when I render fonts to fbo texture, there is some problems with colour and alpha.
This image should open in editor that don't touch pixels(browser uses anti-aliasing).
Code:
/// 1 step create FBO
// Create the color buffer
glGenTextures(1, &id_tex);
glBindTexture(GL_TEXTURE_2D, id_tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, wid, hei, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); // GL_RGBA GL_ALPHA
// Create the frame buffer
glGenFramebuffersEXT(1, &id_buf);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id_buf);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, id_tex, 0);
GLenum err = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
assert(err == GL_FRAMEBUFFER_COMPLETE_EXT);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
/// 2 step render to fbo
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id_buf);
glEnable(GL_TEXTURE_2D);
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0, 1920 , 1080);
ftgl::render();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glDisable(GL_TEXTURE_2D);
glFlush ();
glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
glViewport (0, 0, 1920, 1080);
glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
/// 3 step render texture
glEnable(GL_TEXTURE_2D);
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
glBindTexture(GL_TEXTURE_2D, id_tex);
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex3f(point4.X , point4.Y , point4.Z);
glTexCoord2d(0,1); glVertex3f(point1.X , point1.Y , point1.Z);
glTexCoord2d(1,1); glVertex3f(point2.X , point2.Y , point2.Z);
glTexCoord2d(1,0); glVertex3f(point3.X , point3.Y , point3.Z);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
glDisable(GL_TEXTURE_2D);

sdl surface to opengl texture

How can I convert a .png image to an OpenGL surface, with SDL? what I have now:
typedef GLuint texture;
texture load_texture(std::string fname){
SDL_Surface *tex_surf = IMG_Load(fname.c_str());
if(!tex_surf){
return 0;
}
texture ret;
glGenTextures(1, &ret);
glBindTexture(GL_TEXTURE_2D, ret);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tex_surf->w, tex_surf->h, 0, GL_RGB, GL_UNSIGNED_BYTE, tex_surf->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SDL_FreeSurface(tex_surf);
return ret;
}
and my code to draw the thing:
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
//Use blurry texture mapping (replace GL_LINEAR with GL_NEAREST for blocky)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor4f( 1.0, 1.0, 1.0, 1.0 ); //Don't use special coloring
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(128.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(128.0f, 128.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, 128.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
The problem is that it only works with .bmp files, and they turn bluish, so what is wrong?
Also, when I try to load a .png, it shows up really weird.
Wrong colors can be caused by getting the channel order wrong. The code I have lying around for loading .bmp's uses GL_BGR instead of GL_RGB so I think that will solve your problem with bmp's.
The problem with your png image is more likely caused by the png being 32-bits per pixel. Probably the best solution for you is to inspect the format field of the SDL surface to determine to appropriate flags/values to pass to glTexImage2D.