Results 1 to 4 of 4

Thread: OpenGL 3.0 textured quad

  1. #1

    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 \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?

  2. #2
    Junior Member Newbie
    Join Date
    Jan 2009
    Location
    Poland
    Posts
    26

    Re: OpenGL 3.0 textured quad

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

  3. #3

    Re: OpenGL 3.0 textured quad

    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\

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2009
    Location
    Poland
    Posts
    26

    Re: OpenGL 3.0 textured quad

    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

Similar Threads

  1. textured quad will not draw
    By advorak in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-19-2015, 10:40 AM
  2. Fade in-out a textured quad ?
    By in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 07-31-2005, 07:01 AM
  3. How a textured quad lighter?
    By johannesprix in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 02-23-2004, 11:13 PM
  4. Warping Textured Quad
    By Sebaot in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 03-17-2001, 10:38 AM
  5. Subdividing a textured quad
    By overloadnn in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 11-15-2000, 02:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean