Drawing 2D Textures in OpenGL

I had a drawing function called DrawImage, but it really confuses and only works with a specific form of the reshape function, so I have 2 questions:

  • How to draw texture in OpenGL? I just want to create a function that gets the texture, x, y, width, height, and possibly an angle, and draw it, and draw it according to the arguments. I want to draw it as GL_QUAD regularly, but I'm not sure how to do this anymore. People say that I have to use SDL or SFML to do this, is this recommended? If so, can you give me a simple function that loads a texture and one that paints it? I am currently using SOIL to load textures.

function here:

void DrawImage(char filename, int xx, int yy, int ww, int hh, int angle) 
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, filename);

  glLoadIdentity();
  glTranslatef(xx,yy,0.0);
  glRotatef(angle,0.0,0.0,1.0);
  glTranslatef(-xx,-yy,0.0);

// Draw a textured quad
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(xx,yy);
glTexCoord2f(0, 1); glVertex2f(xx,yy + hh);
glTexCoord2f(1, 1); glVertex2f(xx + ww,yy + hh);
glTexCoord2f(1, 0); glVertex2f(xx + ww,yy);

glDisable(GL_TEXTURE_2D);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glMatrixMode(GL_MODELVIEW);
glEnd();
}

- , glDisable, glPopMatrix glMatrixMode glBegin glEnd. , . , ?  2. glutReshapeFunc, , , - ( ) , - , , , .

: ++, C, , OpenGL? , , - , , ( OpenGL ).

- 1 : P

+4
2
  • DrawImage . , , glMatrixMode .. Befor glEnd, . , , , , . glutReshapeFunc , , .

  • SDL , . GLFW . , , . libs GL . SOIL .

  • OpenGL - API 3D-, . GUI, OpenGL, .

  • , , . ++ C, , . , C, ++. , , . , , , . imo, .

, ...

3D 2D-, "" 3D- . , :

enter image description here

. 2D . , , . , , .

( " " ) - -1 . ,

glMatrixMode(GL_PROJECTION); //from now on all glOrtho, glTranslate etc affect projection
glOrtho(0, widthInPixels, 0, heightInPixels, -1, 1);
glMatrixMode(GL_MODELVIEW); //good to leave in edit-modelview mode

- , / , , , , , :

void reshape(int x, int y) {... do stuff with x/y ...}
...
glutReshapeFunc(reshape); //give glut the callback

, , , glVertex, .

: glTranslatef(-xx,-yy,0.0); glVertex2f(0,0) . Push/Pop , .

:

#include <GL/glut.h>
#include <GL/gl.h>
#include <stdio.h>

int main(int argc, char** argv)
{
    //create GL context
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(800, 600);
    glutCreateWindow("windowname");

    //create test checker image
    unsigned char texDat[64];
    for (int i = 0; i < 64; ++i)
        texDat[i] = ((i + (i / 8)) % 2) * 128 + 127;

    //upload to GPU texture
    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 8, 8, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texDat);
    glBindTexture(GL_TEXTURE_2D, 0);

    //match projection to window resolution (could be in reshape callback)
    glMatrixMode(GL_PROJECTION);
    glOrtho(0, 800, 0, 600, -1, 1);
    glMatrixMode(GL_MODELVIEW);

    //clear and draw quad with texture (could be in display callback)
    glClear(GL_COLOR_BUFFER_BIT);
    glBindTexture(GL_TEXTURE_2D, tex);
    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
    glTexCoord2i(0, 0); glVertex2i(100, 100);
    glTexCoord2i(0, 1); glVertex2i(100, 500);
    glTexCoord2i(1, 1); glVertex2i(500, 500);
    glTexCoord2i(1, 0); glVertex2i(500, 100);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);
    glFlush(); //don't need this with GLUT_DOUBLE and glutSwapBuffers

    getchar(); //pause so you can see what just happened
    //System("pause"); //I think this works on windows

    return 0;
}
+6

OpenGL 3.0 , - glBlitFramebuffer(). , , , .

, : tex :

GLuint readFboId = 0;
glGenFramebuffers(1, &readFboId);
glBindFramebuffer(GL_READ_FRAMEBUFFER, readFboId);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                       GL_TEXTURE_2D, tex, 0);
glBlitFramebuffer(0, 0, texWidth, texHeight,
                  0, 0, winWidth, winHeight,
                  GL_COLOR_BUFFER_BIT, GL_LINEAR);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &readFboId);

, , FBO, . / , .

+2

Source: https://habr.com/ru/post/1544894/


All Articles