Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 448|回复: 0

【学习】SDL 2.0 OpenGL纹理

[复制链接]

535

主题

552

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
19009
发表于 2020-4-2 23:34:29 | 显示全部楼层 |阅读模式
OpenGL初始化
[C++] syntaxhighlighter_viewsource syntaxhighlighter_copycode
bool SDLWindow::InitOpenGL()
{
        this->glcontext = SDL_GL_CreateContext(this->window);
        int w, h;
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        SDL_GetWindowSize(this->window, &w, &h);
        glViewport(0, 0, w, h);
        glOrtho(0.0f, w, h, 0.0f, -1.0f, 1.0f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        GLenum error = glGetError();
        if(error != GL_NO_ERROR)
        {
                return false;
        }

        return true;
}


绘图功能
[C++] syntaxhighlighter_viewsource syntaxhighlighter_copycode
void Graphics::Draw(Vector2* position, Texture2D* texture)
{
        SDL_GL_BindTexture(texture->texture, (float*)texture->GetWidth(), (float*)texture->GetHeight());
        glBegin(GL_QUADS);
                glTexCoord2f(0, 0);
                glVertex2f(position->x, position->y);
                glTexCoord2f(1, 0);
                glVertex2f(position->x + texture->GetWidth(), position->y);
                glTexCoord2f(1, 1);
                glVertex2f(position->x + texture->GetWidth(), position->y + texture->GetHeight());
                glTexCoord2f(0, 1);
                glVertex2f(position->x, position->y + texture->GetHeight());
        glEnd();
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|246SA 石器论坛

GMT+8, 2020-8-15 19:14 , Processed in 1.079227 second(s), 13 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表