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
Related
I've written a very simple OpenGL application. Its goal is to load a texture and draw it on a plane. If I use the function 'glTexSubImage2D' the plane is not textured and the function 'glGetError' returns the error '1281' (invalid value). However if I use the function 'glTexImage2D' my plane plane is textured correctly (and I have no error).
Here's a piece of my code :
void core::RootDevice::LoadTexture(char const *pFileName)
{
SDL_Surface *pSurface = IMG_Load(pFileName);
char *pPixels = reinterpret_cast<char*>(pSurface->pixels);
uint32_t bytePerPixel = pSurface->format->BitsPerPixel;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboID);
{
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_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pSurface->w, //NO ERROR : All is ok
//pSurface->h, 0, GL_RGB, GL_UNSIGNED_BYTE, pPixels);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pSurface->w, //ERROR : 1281
pSurface->h, GL_RGB, GL_UNSIGNED_BYTE, pPixels);
std::cout << glGetError() << std::endl;
getchar();
}
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}
glBindTexture(GL_TEXTURE_2D, 0);
}
And the rendering code :
void core::RootDevice::Render(void)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureId);
{
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
}
glBindTexture(GL_TEXTURE_2D, 0);
}
And the result is the followings:
Does anyone can help me?
glTexSubImage2D() is used to replace parts or all of a texture that already has image data allocated. You have to call glTexImage2D() on the texture at least once before you can use glTexSubImage2D() on it. Unlike glTexSubImage2D(), glTexImage2D() allocates image data. You can use NULL for the last (data) argument to glTexImage2D() if you only want to allocate image data, and later set the data with glTexSubImage2D().
Newer versions of OpenGL (4.4 and ES 3.0) have a new entry point glTexStorage2D() that can be used as an alternative to glTexImage2D() to allocate the image data for a texture without specifying the data. It is similar to calling glTexImage2D() with data = NULL, but also allows specifying ahead of time if space for mipmaps will be needed.
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:
I want to apply gaussian blur in a 256x256 texture but Im having problems passing the first pass to a framebuffer and reusing it actualy result in part of image, that appear to be the texture transformed.
Tanks the attention
vertex shader:
void main()
{
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
}
fragment shader:
I removed all the part of blurring only to tests.
uniform sampler2D texture_0;
uniform float switcher;
vec4 cor;
void main()
{
cor = vec4(0.0,0.0,0.0,0.0);
vec2 position = gl_TexCoord[0].xy;
cor = texture2D(texture_0,position);
gl_FragColor = cor;
}
initializations:
glGenTextures( 1, &tex1 );
glBindTexture( GL_TEXTURE_2D, tex1 );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img1->getWidth(),img1->getHeight(), 0,GL_RGB, GL_UNSIGNED_BYTE, data1);
glBindTexture(GL_TEXTURE_2D, 0);
glGenTextures( 1, &tex2 );
glBindTexture( GL_TEXTURE_2D, tex2 );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img1->getWidth(),img1->getHeight(), 0,GL_RGB, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, tex1 );
glActiveTexture(GL_TEXTURE1);
glBindTexture( GL_TEXTURE_2D, tex2 );
glGenFramebuffers(1,&framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER,framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex2,0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
render:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glUniform1i(loc_u_texture_0, 0);
glUniform1f(loc_switcher_1, 0);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glNormal3f(0, 1, 0);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-2, -2, -10);
glTexCoord2f(1, 0);
glVertex3f(2, -2, -10);
glTexCoord2f(1, 1);
glVertex3f(2, 2, -10);
glTexCoord2f(0, 1);
glVertex3f(-2, 2, -10);
glEnd();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
usleep(50);
glUniform1i(loc_u_texture_0, 1);
glUniform1f(loc_switcher_1, 1);
glNormal3f(0, 1, 0);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-2, -2, -10);
glTexCoord2f(1, 0);
glVertex3f(2, -2, -10);
glTexCoord2f(1, 1);
glVertex3f(2, 2, -10);
glTexCoord2f(0, 1);
glVertex3f(-2, 2, -10);
glEnd();
glutSwapBuffers();
Use the following lines to draw a quad:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
When you reset both the projection and the modelview matrices, your primitives are not transformed and you can directly draw objects in clipping space (which is a cube with corners (-1,-1,-1) and (1,1,1)).
Also, don't use the following:
glActiveTexture(GL_TEXTURE1);
glBindTexture( GL_TEXTURE_2D, tex2 );
if you are not going to use more than one input sampler per shader at the same time, you need only the first texture channel GL_TEXTURE0, which is set by default.
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);
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.