0

I am try draw a rectangle in OpenGL with texture. before I'm writing texture to rectangle I draw one OpenGL primitive with appropriate color. Then I write texture to rectangle using code given below where variable buffer contains RGB data for 800x600 pixels. But perform execution a piece of code leads to what color inside rectangle remains filled with color from the primitive. What am I doing wrong?

Update code from 07.11.19

a piece of code:

    //glDisable(GL_COLOR_MATERIAL);

    glScalef(1,1,1);
    glPixelstorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures (1, @texID);
    glBindTexture (GL_TEXTURE_2D,texID);

    if schglobal=1 then
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_BGR, GL_UNSIGNED_BYTE, buffer)
                   else
                   begin
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_BGR, GL_UNSIGNED_BYTE, buffer)
                   end;

    glcolor3i(255,255,255);
    //glcolor3i(0,0,0);
    //glcolor3i(0,0,0);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    //glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
    glBindTexture (GL_TEXTURE_2D,texID);
    glEnable(GL_TEXTURE_2D);
    glBegin (GL_QUADS);
    glTexCoord2f (0, 0);
    glVertex3f (vertexes[4,1], vertexes[4,2], vertexes[4,3]);
    glTexCoord2f (1, 0);
    glVertex3f (vertexes[2,1], vertexes[2,2], vertexes[2,3]);
    glTexCoord2f (1, 1);
    glVertex3f (vertexes[1,1], vertexes[1,2], vertexes[1,3]);
    glTexCoord2f (0, 1);
    glVertex3f (vertexes[3,1], vertexes[3,2], vertexes[3,3]);
    glEnd;
    ans:=glGetError;
    //glFlush();
    //glBindTexture (GL_TEXTURE_2D,0);
    glDisable(GL_TEXTURE_2D);

Update code from 08.11.19 (work but don't work if glTexSubImage2D used error 1281)

    glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
    glScalef(1,1,1);
    glPixelstorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures (1, @texID);
    glBindTexture (GL_TEXTURE_2D,texID);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    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);
    if use_gltexsubimage=false then
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_BGR, GL_UNSIGNED_BYTE, buffer)
                               else begin
    if use_glteximage=true then begin
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_BGR, GL_UNSIGNED_BYTE, buffer);
    use_glteximage:=false;
    end
                           else begin
    glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, 800, 600, GL_RGB, GL_UNSIGNED_BYTE, buffer);
            ans:=glGetError;
                        end;
                                    end;

    glcolor3f(1,1,1);

    glBindTexture (GL_TEXTURE_2D,texID);
    glEnable(GL_TEXTURE_2D);
    glBegin (GL_QUADS);
    glTexCoord2f (0, 0);
    glVertex3f (vertexes[4,1], vertexes[4,2], vertexes[4,3]);
    glTexCoord2f (1, 0);
    glVertex3f (vertexes[2,1], vertexes[2,2], vertexes[2,3]);
    glTexCoord2f (1, 1);
    glVertex3f (vertexes[1,1], vertexes[1,2], vertexes[1,3]);
    glTexCoord2f (0, 1);
    glVertex3f (vertexes[3,1], vertexes[3,2], vertexes[3,3]);
    glEnd;
    glBindTexture (GL_TEXTURE_2D,0);

                     end;
  • 2
    Since you use a rectangle texture (not sure why though), the texture coordinates shouldn't be normalized but in the range [0,0] to [800, 600]. – BDL Nov 05 '19 at 09:34
  • @ Rabbid76 @BDL I try also glEnable(GL_TEXTURE_2D) and so on but does not matter. If I try use glcolor3f(1,1,1) or glcolor3f(0,0,0) OpenGL draw white or black rectangle respectively. In version glEnable(GL_TEXTURE_2D) I use glBindTexture (GL_TEXTURE_2D,texID) and glTexImage2D(GL_TEXTURE_2D, 0, GL_BGR, 800, 600, 0, GL_BGR, GL_UNSIGNED_BYTE, buffer) – RobertKazan Nov 05 '19 at 10:09
  • 2
    What I mean is: `glTexCoord2f (1, 0);` should be `glTexCoord2f (800, 0);`. `glTexCoord2f (1, 1);` should be `glTexCoord2f (800, 600);`, and so on. What you currently draw is just the upper left texel of the texture. – BDL Nov 05 '19 at 10:15
  • @BDL I try glTexCoord2f (800, 0) instead glTexCoord2f (1, 0) and so on but does not matter. – RobertKazan Nov 05 '19 at 10:27
  • @RobertKazan Did you change texture environment parameters? Try `glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);` – Rabbid76 Nov 05 '19 at 16:00
  • @Rabbid76 Yes, I try it but does not matter. I myself can’t understand the reason. I downloaded the data, but why it is not displayed. I tried to programmatically change part of the array of pixels to white, but this does not change the display on the screen – RobertKazan Nov 05 '19 at 18:43
  • @RobertKazan you need also refresh and swap buffers so you can see the oputput ... check here [Applying map of the earth texture a Sphere](https://stackoverflow.com/a/31804515/2521214) the texture code in `planet::init` (however its VCL/C++ not delphi) and this for the swapping and refreshing: [How to render an openGL frame in C++ builder?](https://stackoverflow.com/a/20679773/2521214) check all events especially init/draw/resize – Spektre Nov 06 '19 at 09:31
  • @Spektre I perform swap buffer. Thank you for link. – RobertKazan Nov 06 '19 at 17:56
  • I update code to the newest version and added some comments – RobertKazan Nov 07 '19 at 10:49
  • I try use glGetTexImage and this function return identically values as buffer. Why when draw texture to rectangle I see black background in OpenGL panel although the variable buffer contains nonzero values e.g. 30,30,30 and so on. – RobertKazan Nov 07 '19 at 11:55
  • I solve problem with draw texture to rectangle but not completely. Later I will write the code. But I try using also gltexsubimage2d but while I get in response error 1281 and wrong colors. – RobertKazan Nov 07 '19 at 13:30
  • New version of code added – RobertKazan Nov 08 '19 at 07:41
  • It is normal that you get an error when you try to update a sub-region of a texture object for which you never cretaed the actual texture storage. – derhass Nov 08 '19 at 15:45
  • @derhass I must use gltexstorage2d? – RobertKazan Nov 09 '19 at 21:50
  • No, `glTexImage2D` will also create the texture storage, `glTexStorage2D` is just a more modern alternative, which some different semantics, and which you better not use since the rest of your code is all legacy GL. – derhass Nov 10 '19 at 14:06
  • @derhass If I try use vertex and fragment shaders I can get a big performance boost? – RobertKazan Nov 10 '19 at 14:31
  • No, not by doing just that - currently, the driver is using shaders behind your back anyways. Your code is just completely outdated. – derhass Nov 10 '19 at 14:44
  • @derhass Thank you. How I use a modern OpenGL? Outdated You mean block glBegin..glEnd or something else? – RobertKazan Nov 10 '19 at 20:41
  • If I use glTexImage2d why error 1281 in after perform glTexSubImage2d? Which commands may delete texture storage? If I don't use glDeleteTextures I get memory leak. – RobertKazan Nov 11 '19 at 04:05

1 Answers1

0

I solve my problem with glTexSubImage2d. I transferred texture generation to the form creation procedure and deleting the texture to the form closing procedure.