OpenGL 3.0 textured quad

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

in vec4 mPosition;

in vec2 mTexCoord0;

out vec2 fTexCoord0;

uniform mat4 matMPV;

uniform mat4 matTri;

void main(void)

{

gl_Position = matMPV * matTri * mPosition; //gl_Position = matMPV * vec4(normalize( vec3(matTri* mPosition) ),1.0);

fTexCoord0 = mTexCoord0;

};

";

string nullFragmentShader = "#version 140

in vec2 fTexCoord0;

out vec4 outColor;

uniform sampler2D tex;

void main(void)

{

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

};

";

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_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,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 *)&quadMatrix.m); // set the uniform matrix
glUniformMatrix4fv(g_matId2, 1, false, (const GLfloat *)&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?

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);

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
\

should be:

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

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 :stuck_out_tongue: