PDA

View Full Version : OpenGL 3.0 textured quad



MrKaktus
08-13-2009, 11:04 AM
Hi, can someone please show me some basic code to display textured quad on screen in OGL 3.0? I'm fighting with that for long time and still it is not working.

Here is some code of mine:

data declaration:


// Dummy textured quad (traingle strip)
float quad[20] = { 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f };
uint8 quad_row[] = { 2, FLOAT3, FLOAT2 };


used shaders :


// Test of rendering using RC
string nullVertexShader = "#version 140 \n\
in vec4 mPosition; \n\
in vec2 mTexCoord0; \n\
out vec2 fTexCoord0; \n\
uniform mat4 matMPV; \n\
uniform mat4 matTri; \n\
void main(void) \n\
{ \n\
gl_Position = matMPV * matTri * mPosition; //gl_Position = matMPV * vec4(normalize( vec3(matTri* mPosition) ),1.0); \n\
fTexCoord0 = mTexCoord0; \n\
}; \n\
";

string nullFragmentShader = "#version 140 \n\
in vec2 fTexCoord0; \n\
out vec4 outColor; \n\
uniform sampler2D tex; \n\
void main(void) \n\
{ \n\
outColor = texture(tex,vec2(0.5,0.5)) + vec4(0.1,0.1,0.0,1.0); // fTexCoord0 \n\
}; \n\
";


code to load texture:



ifstream *sfile;
uint32 size;
ILubyte *buffer;
ILuint tex;

// Otwarcie pliku
sfile = new ifstream(name.c_str(),ios::in | ios::binary | ios::ate);
if (sfile->fail())
{
delete sfile;
return 0;
}

// Okreslenie rozmiaru pliku
sfile->seekg(0,ios::end);
size = sfile->tellg();
sfile->seekg(0,ios::beg);

// Utworzenie buforu tymczasowego i odczyt pliku
buffer = new ILubyte[size];
sfile->read((char*)buffer,size);
sfile->close();
delete sfile;

// Rozkodowanie PNG do OpenGL
ilGenImages(1,&tex);
ilBindImage(tex);
ilLoadL(IL_PNG,buffer,size);
GLuint id = 0;
glGenTextures(1,&id);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,id);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,ilGetInteger( IL_IMAGE_WIDTH),ilGetInteger(IL_IMAGE_HEIGHT),0,GL _RGB,GL_UNSIGNED_BYTE,ilGetData());

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _REPEAT);
ilDeleteImages(1, &tex);



linking shaders:



//Create a program
programId = glCreateProgram();
glAttachShader(programId, vertexShaderId);
glAttachShader(programId, fragmentShaderId);
glBindAttribLocation(programId, 0, "mPosition");
glBindAttribLocation(programId, 1, "mTexCoord0");

//glBindAttribLocation(programId, 2, "mColor");
glLinkProgram(programId);


and code to draw in loop:


glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D); // <- it returns opengl error Invalid enum in loop ?
glBindTexture(GL_TEXTURE_2D,dummy_tex);

float4x4 quadMatrix = mul(camera->projectionMatrix(),camera->matrix());
glUniformMatrix4fv(g_matId, 1, false, (const GLfloat *)&amp;quadMatrix.m); // set the uniform matrix
glUniformMatrix4fv(g_matId2, 1, false, (const GLfloat *)&amp;identity.m); // set the uniform matrix
enGpu.render.input.draw(quad_id,EN_TRIANGLE_STRIP) ; // my routine that calls draw function


I got error when trying to enable TEXTURE_2D and i see quad drawn only with color that is added in pixel shader to sampled one.

Do anyone have some clue what could be wrong?

Jacek Nowak
08-13-2009, 12:17 PM
First of all, glEnable(GL_TEXTURE_2D) isn't necessary in OpenGL 3.0.

Secondly, try binding sampler to texture, something like this:

glUseProgram(programID);
glUniform1i( glGetUniformLocation(programID, "tex"), 0);

MrKaktus
08-13-2009, 12:58 PM
I didn't bind sampler to texture, but i found that stupid error was here:

outColor = texture(tex,vec2(0.5,0.5)) + vec4(0.1,0.1,0.0,1.0); // fTexCoord0 \n\

should be:

outColor = texture(tex,ftexCoord0) + vec4(0.1,0.1,0.0,1.0); // fTexCoord0 \n\

Jacek Nowak
08-13-2009, 01:00 PM
I can't believe I missed that, I checked whether you have in/out texcoords but I forgot to check if you are using them properly :P