File: glgdTexture.c

package info (click to toggle)
gauche-gtk 0.6%2Bgit20160927-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,452 kB
  • sloc: ansic: 7,097; lisp: 5,659; sh: 2,829; makefile: 338
file content (129 lines) | stat: -rw-r--r-- 2,781 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * gldgTexture.c
 *
 * OpenGL Graph Display texture utility module implementation
 *
 * Written by: Shawn Taras
 */
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>
#include "glgd.h"

/*
 * Defines
 */
#define _SQR(x)             ((x) * (x))

/*
 * External public functions
 */
glgdTexture
*glgdTextureCreate(void)
{
    glgdTexture *tex;
    
    tex = (glgdTexture *)GLGD_MALLOC(sizeof(glgdTexture));
    if (tex)
    {
        glgdTextureInit(tex);
    }
    
    return tex;
}

glgdTexture
*glgdTextureDestroy(glgdTexture *tex)
{
    if (tex != NULL)
    {
        glgdTextureFini(tex);
        GLGD_FREE(tex);
    }
        
    return (glgdTexture *)NULL;
}

GLboolean
glgdTextureInit(glgdTexture *tex)
{
    if (tex != NULL)
    {
        tex->name = 0;
        tex->width = 0;
        tex->height = 0;
        tex->texels = NULL;
        
        return GL_TRUE;
    }
    
    return GL_FALSE;
}

GLboolean
glgdTextureFini(glgdTexture *tex)
{
    if (tex != NULL)
    {
        glDeleteTextures(1, &tex->name);
        if (tex->texels != NULL)
        {
            GLGD_FREE(tex->texels);
        }
        glgdTextureInit(tex);
        
        return GL_TRUE;
    }
    
    return GL_FALSE;
}

GLboolean
glgdTextureSetup(glgdTexture *tex, int width, int height)
{
    GLint   widthSet;
    GLint   heightSet;
    GLvoid  *texels;

    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &widthSet);
    glgdTrace(1, "GL_MAX_TEXTURE_SIZE = %d\n", widthSet);

    /* Create the texture */
    if (tex)
    {
        glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA,
            width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

        /* Check for valid (width,height) parameters */
        glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
                GL_TEXTURE_WIDTH, &widthSet);
        glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
                GL_TEXTURE_HEIGHT, &heightSet);
        if (widthSet == 0 || heightSet == 0)
        {
            return GL_FALSE;
        }

        texels = GLGD_MALLOC(width * height * 4);
        memset(texels, 0, width * height * 4);

        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

        glGenTextures(1, &tex->name);
        glBindTexture(GL_TEXTURE_2D, tex->name);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
            width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texels);

        tex->width = width;
        tex->height = height;
        tex->texels = texels;
        
        return GL_TRUE;
    }
    
    return GL_FALSE;
}