Implement glColorTableEXT

This commit is contained in:
Luke Benstead 2019-02-21 20:35:18 +00:00
parent c84f211ad0
commit 571438ef4e
9 changed files with 334 additions and 14 deletions

View File

@ -45,7 +45,7 @@ typedef struct {
/* Palette data is always stored in RAM as RGBA8888 and packed as ARGB8888
* when uploaded to the PVR */
GLubyte* data;
GLsizei width;
GLushort width;
GLenum format;
} TexturePalette;

View File

@ -3,6 +3,7 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "../include/glext.h"
#include "../include/glkos.h"
@ -18,6 +19,28 @@ static GLubyte ACTIVE_TEXTURE = 0;
static GLuint _determinePVRFormat(GLint internalFormat, GLenum type);
#define PACK_ARGB8888(a,r,g,b) ( ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF) )
static void _glApplyColorTable() {
TextureObject* active = getBoundTexture();
if(!active) {
return; //? Unload the palette? Make White?
}
if(!active->palette || !active->palette->data) {
return;
}
pvr_set_pal_format(PVR_PAL_ARGB8888);
GLushort i = 0;
for(; i < active->palette->width; ++i) {
GLubyte* entry = &active->palette->data[i * 4];
pvr_set_pal_entry(i, PACK_ARGB8888(entry[3], entry[1], entry[2], entry[0]));
}
}
GLubyte _glGetActiveTexture() {
return ACTIVE_TEXTURE;
}
@ -195,6 +218,9 @@ void APIENTRY glBindTexture(GLenum target, GLuint texture) {
if(texture) {
TEXTURE_UNITS[ACTIVE_TEXTURE] = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, texture);
// If this is a paletted texture, we need to reapply the color table
_glApplyColorTable();
} else {
TEXTURE_UNITS[ACTIVE_TEXTURE] = NULL;
}
@ -633,18 +659,24 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
_glKosThrowError(GL_INVALID_ENUM, "glTexImage2D");
}
if(!_isSupportedFormat(format)) {
_glKosThrowError(GL_INVALID_ENUM, "glTexImage2D");
}
if(format != GL_COLOR_INDEX) {
if(!_isSupportedFormat(format)) {
_glKosThrowError(GL_INVALID_ENUM, "glTexImage2D");
}
/* Abuse determineStride to see if type is valid */
if(_determineStride(GL_RGBA, type) == -1) {
_glKosThrowError(GL_INVALID_ENUM, "glTexImage2D");
}
/* Abuse determineStride to see if type is valid */
if(_determineStride(GL_RGBA, type) == -1) {
_glKosThrowError(GL_INVALID_ENUM, "glTexImage2D");
}
internalFormat = _cleanInternalFormat(internalFormat);
if(internalFormat == -1) {
_glKosThrowError(GL_INVALID_VALUE, "glTexImage2D");
internalFormat = _cleanInternalFormat(internalFormat);
if(internalFormat == -1) {
_glKosThrowError(GL_INVALID_VALUE, "glTexImage2D");
}
} else {
if(internalFormat != GL_COLOR_INDEX8_EXT) {
_glKosThrowError(GL_INVALID_ENUM, __func__);
}
}
GLint w = width;
@ -671,6 +703,13 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
_glKosThrowError(GL_INVALID_OPERATION, "glTexImage2D");
}
GLboolean isPaletted = (internalFormat == GL_COLOR_INDEX8_EXT) ? GL_TRUE : GL_FALSE;
if(isPaletted && level > 0) {
/* Paletted textures can't have mipmaps */
_glKosThrowError(GL_INVALID_OPERATION, __func__);
}
if(_glKosHasError()) {
_glKosPrintError();
return;
@ -698,11 +737,15 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
/* All colour formats are represented as shorts internally. Paletted textures
* are represented by byte indexes (which look up into a color table)
*/
GLboolean isPaletted = (internalFormat == GL_COLOR_INDEX8_EXT) ? GL_TRUE : GL_FALSE;
GLint destStride = isPaletted ? 1 : 2;
GLuint bytes = (width * height * destStride);
if(!active->data) {
assert(active);
assert(width);
assert(height);
assert(destStride);
/* need texture memory */
active->width = width;
active->height = height;
@ -712,7 +755,11 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
active->dataStride = destStride;
GLuint size = _glGetMipmapDataSize(active);
assert(size);
active->data = pvr_mem_malloc(size);
assert(active->data);
active->isCompressed = GL_FALSE;
active->isPaletted = isPaletted;
}
@ -727,7 +774,10 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
* These are the only formats where the source format passed in matches the pvr format.
* Note the REV formats + GL_BGRA will reverse to ARGB which is what the PVR supports
*/
if(format == GL_BGRA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && internalFormat == GL_RGBA) {
if(format == GL_COLOR_INDEX) {
/* Don't convert color indexes */
needsConversion = GL_FALSE;
} else if(format == GL_BGRA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && internalFormat == GL_RGBA) {
needsConversion = GL_FALSE;
} else if(format == GL_BGRA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV && internalFormat == GL_RGBA) {
needsConversion = GL_FALSE;
@ -742,6 +792,7 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
}
GLubyte* targetData = _glGetMipmapLocation(active, level);
assert(targetData);
if(!data) {
/* No data? Do nothing! */
@ -876,7 +927,7 @@ GLAPI void APIENTRY glColorTableEXT(GLenum target, GLenum internalFormat, GLsize
}
/* Only allow up to 256 colours in a palette */
if(width > 256) {
if(width > 256 || width == 0) {
_glKosThrowError(GL_INVALID_VALUE, __func__);
_glKosPrintError();
return;
@ -919,6 +970,9 @@ GLAPI void APIENTRY glColorTableEXT(GLenum target, GLenum internalFormat, GLsize
src += sourceStride;
dst += 4;
}
/* Apply the palette immediately, we'll also do this when binding the texture */
_glApplyColorTable();
}
GLAPI void APIENTRY glColorSubTableEXT(GLenum target, GLsizei start, GLsizei count, GLenum format, GLenum type, const GLvoid *data) {

View File

@ -20,3 +20,4 @@ all:
$(KOS_MAKE) -C terrain all
$(KOS_MAKE) -C quadmark all
$(KOS_MAKE) -C multitexture_arrays all
$(KOS_MAKE) -C paletted all

29
samples/paletted/Makefile Normal file
View File

@ -0,0 +1,29 @@
TARGET = paletted.elf
OBJS = main.o
all: rm-elf $(TARGET)
include $(KOS_BASE)/Makefile.rules
clean:
-rm -f $(TARGET) $(OBJS) romdisk.*
rm-elf:
-rm -f $(TARGET) romdisk.*
$(TARGET): $(OBJS) romdisk.o
$(KOS_CC) $(KOS_CFLAGS) $(KOS_LDFLAGS) -o $(TARGET) $(KOS_START) \
$(OBJS) romdisk.o $(OBJEXTRA) -lm -lkosutils $(KOS_LIBS)
romdisk.img:
$(KOS_GENROMFS) -f romdisk.img -d romdisk -v
romdisk.o: romdisk.img
$(KOS_BASE)/utils/bin2o/bin2o romdisk.img romdisk romdisk.o
run: $(TARGET)
$(KOS_LOADER) $(TARGET)
dist:
rm -f $(OBJS) romdisk.o romdisk.img
$(KOS_STRIP) $(TARGET)

236
samples/paletted/main.c Normal file
View File

@ -0,0 +1,236 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gl.h"
#include "glext.h"
#include "glu.h"
#include "glkos.h"
extern uint8 romdisk[];
KOS_INIT_ROMDISK(romdisk);
/* floats for x rotation, y rotation, z rotation */
float xrot, yrot, zrot;
/* storage for one texture */
int texture[1];
typedef struct {
unsigned int height;
unsigned int width;
unsigned int palette_width;
char* palette;
char* data;
} Image;
int LoadPalettedTex(const char* filename, Image* image) {
struct {
char id[4]; // 'DTEX'
short width;
short height;
int type;
int size;
} header;
FILE* filein = NULL;
filein = fopen(filename, "rb");
if(!filein) {
printf("Unable to open file\n");
return 0;
}
fread(&header, sizeof(header), 1, filein);
if((header.type & (7 << 27)) >> 27 != 6) {
printf("Not PAL8BPP format\n");
return 0;
}
image->width = header.width;
image->height = header.height;
image->data = (char*) malloc(sizeof(char) * header.size);
fread(image->data, header.size, sizeof(char), filein);
fclose(filein);
char palette_filename[100];
strcpy(palette_filename, filename);
strcat(palette_filename, ".pal");
filein = fopen(palette_filename, "rb");
if(!filename) {
printf("Unable to open the palette file\n");
return 0;
}
struct {
char id[4];
int numcolors;
} palette_header;
fread(&palette_header, sizeof(palette_header), 1, filein);
image->palette = (unsigned int*) malloc(sizeof(unsigned int) * palette_header.numcolors);
image->palette_width = palette_header.numcolors;
fread(image->palette, sizeof(unsigned int), palette_header.numcolors, filein);
unsigned int i = 0;
for(; i < palette_header.numcolors; ++i) {
unsigned int* p = (unsigned int*) image->palette;
// Swap the colours around from ARGB to RGBA
char r = (p[i] & 0x00FF0000) >> 16;
char g = (p[i] & 0x0000FF00) >> 8;
char b = (p[i] & 0x000000FF);
char a = (p[i] & 0xFF000000) >> 24;
image->palette[i * 4] = r;
image->palette[i * 4 + 1] = g;
image->palette[i * 4 + 2] = b;
image->palette[i * 4 + 3] = a;
}
return 1;
}
// Load Bitmaps And Convert To Textures
void LoadGLTextures() {
// Load Texture
Image *image1;
// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}
if (!LoadPalettedTex("/rd/NeHe.tex", image1)) {
exit(1);
}
// Create Texture
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); // scale linearly when image smalled than texture
glColorTableEXT(GL_TEXTURE_2D, GL_RGBA8, image1->palette_width, GL_RGBA, GL_UNSIGNED_BYTE, image1->palette);
// 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
// border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
glTexImage2D(GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, image1->width, image1->height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, image1->data);
};
/* A general OpenGL initialization function. Sets all of the initial parameters. */
void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{
LoadGLTextures();
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW);
}
/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
void ReSizeGLScene(int Width, int Height)
{
if (Height == 0) // Prevent A Divide By Zero If The Window Is Too Small
Height = 1;
glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}
/* The main drawing function. */
void DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-5.0f); // move 5 units into the screen.
glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate On The X Axis
glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate On The Y Axis
glRotatef(zrot,0.0f,0.0f,1.0f); // Rotate On The Z Axis
glBindTexture(GL_TEXTURE_2D, texture[0]); // choose the texture to use.
glBegin(GL_QUADS); // begin drawing a cube
// Front Face (note that the texture's corners have to match the quad's corners)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glEnd(); // done with the polygon.
xrot+=1.5f; // X Axis Rotation
yrot+=1.5f; // Y Axis Rotation
zrot+=1.5f; // Z Axis Rotation
//
// swap buffers to display, since we're double buffered.
glKosSwapBuffers();
}
int main(int argc, char **argv)
{
glKosInit();
InitGL(640, 480);
ReSizeGLScene(640, 480);
while(1) {
DrawGLScene();
}
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

Binary file not shown.

View File