This commit is contained in:
Hayden K 2019-03-06 13:58:16 -05:00
parent 375073416a
commit ec602f2d70
15 changed files with 428 additions and 3 deletions

View File

@ -96,6 +96,9 @@ void APIENTRY glKosSwapBuffers() {
PROFILER_PUSH(__func__);
printf("Flush:\n\tOP:%d\n\tPT:%d\n\tTR:%d\n",OP_LIST.vector.size,PT_LIST.vector.size,TR_LIST.vector.size);
printf("Flush: glTexImage mem free:%d\n",pvr_mem_available());
pvr_wait_ready();

View File

@ -109,6 +109,7 @@ void _glMatrixLoadTexture();
void _glApplyRenderMatrix();
matrix_t* _glGetProjectionMatrix();
matrix_t* _glGetModelViewMatrix();
void _glWipeTextureOnFramebuffers(GLuint texture);
GLubyte _glCheckImmediateModeInactive(const char* func);

View File

@ -67,10 +67,14 @@ void _glApplyColorTable() {
pvr_set_pal_format(PVR_PAL_ARGB8888);
GLushort i = 0;
//printf("Palette Entries: %d\n",src->width);
for(; i < src->width; ++i) {
GLubyte* entry = &src->data[i * 4];
//printf("#%02X%02X%02X\t\t %08X\n",entry[0],entry[1],entry[2], PACK_ARGB8888(entry[3], entry[0], entry[1], entry[2]));
pvr_set_pal_entry(i, PACK_ARGB8888(entry[3], entry[0], entry[1], entry[2]));
}
//printf("------END------\n");
}
GLubyte _glGetActiveTexture() {
@ -725,7 +729,7 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
if(format != GL_COLOR_INDEX) {
if(!_isSupportedFormat(format)) {
_glKosThrowError(GL_INVALID_ENUM, "glTexImage2D-color_index");
_glKosThrowError(GL_INVALID_ENUM, "glTexImage2D-invalid_format");
}
/* Abuse determineStride to see if type is valid */
@ -868,8 +872,40 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
assert(data);
assert(bytes);
/* Linear/iterative twiddling algorithm from Marcus' tatest */
#define TWIDTAB(x) ( (x&1)|((x&2)<<1)|((x&4)<<2)|((x&8)<<3)|((x&16)<<4)| \
((x&32)<<5)|((x&64)<<6)|((x&128)<<7)|((x&256)<<8)|((x&512)<<9) )
#define TWIDOUT(x, y) ( TWIDTAB((y)) | (TWIDTAB((x)) << 1) )
#define MIN(a, b) ( (a)<(b)? (a):(b) )
uint32 x, y, yout, min, mask, invert;
min = MIN(w, h);
mask = min - 1;
invert = 0;
uint8 * pixels;
uint16 * vtex;
pixels = (uint8 *) data;
vtex = (uint16*)targetData;
for(y = 0; y < h; y += 2) {
if(!invert)
yout = y;
else
yout = ((h - 1) - y);
for(x = 0; x < w; x++) {
vtex[TWIDOUT((yout & mask) / 2, x & mask) +
(x / min + yout / min)*min * min / 2] =
pixels[y * w + x] | (pixels[(y + 1) * w + x] << 8);
}
}
active->color = PVR_TXRFMT_PAL8BPP | PVR_TXRFMT_TWIDDLED;
/* No conversion? Just copy the data, and the pvr_format is correct */
sq_cpy(targetData, data, bytes);
//sq_cpy(targetData, data, bytes);
return;
} else {
TextureConversionFunc convert = _determineConversion(
@ -906,6 +942,7 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
source += stride;
}
}
printf("GLdc: glTexImage mem free:%d\n",pvr_mem_available());
}
void APIENTRY glTexParameteri(GLenum target, GLenum pname, GLint param) {

View File

@ -0,0 +1,32 @@
TARGET = paletted.elf
OBJS = main.o
LIBS = -lGLdc
all: rm-elf $(TARGET)
include $(KOS_BASE)/Makefile.rules
clean:
-rm -f $(TARGET) $(OBJS) romdisk.* 1ST_READ.BIN paletted.bin
rm-elf:
-rm -f $(TARGET) romdisk.* 1ST_READ.BIN paletted.bin
$(TARGET): $(OBJS) romdisk.o
$(KOS_CC) $(KOS_CFLAGS) $(KOS_LDFLAGS) -o $(TARGET) $(KOS_START) \
$(OBJS) romdisk.o $(OBJEXTRA) $(LIBS) -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: $(TARGET)
rm -f $(OBJS) romdisk.o romdisk.img
$(KOS_STRIP) $(TARGET)
strip-bin paletted
scramble paletted.bin 1ST_READ.BIN

View File

@ -0,0 +1,352 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h>
#include <GL/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;
unsigned int new_palette[1024];
void VID_SetPalette (unsigned char *palette)
{
#if 0
char *pal;
unsigned r,g,b;
unsigned v;
int r1,g1,b1;
int j,k,l,m;
unsigned short i;
unsigned *table;
char s[255];
int dist, bestdist;
//
// 8 8 8 encoding
//
pal = palette;
table = d_8to24table;
for (i=0 ; i<256 ; i++)
{
r = pal[0];
g = pal[1];
b = pal[2];
pal += 3;
v = (255<<24) + (r<<0) + (g<<8) + (b<<16);
*table++ = v;
}
d_8to24table[255] &= 0xffffff; // 255 is transparent
#endif
}
int LoadPalette(Image* image){
FILE* filein = NULL;
filein = fopen("/rd/CONCHARS", "rb");
if(!filein) {
printf("Unable to open file\n");
return 0;
}
image->width = 128;
image->height = 128;
int size = (image->width*image->height);
image->data = (char*) malloc(size);
fread(image->data, size, 1, filein);
fclose(filein);
filein = fopen("/rd/palette.lmp", "rb");
if(!filein) {
printf("Unable to open the palette file\n");
return 0;
}
image->palette = (char*)malloc(256 * 3/*RGB 888*/);
image->palette_width = 256;
fread(image->palette, /*components*/3, image->palette_width, filein);
fclose(filein);
int i;
for(i = 0; i < 256; 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 = (0xFF) >> 24;
new_palette[i * 4] = r;
new_palette[i * 4 + 1] = g;
new_palette[i * 4 + 2] = b;
new_palette[i * 4 + 3] = a;
}
return 1;
}
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() {
#if 1
// 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 (!LoadPalette(image1)) {
printf("Unable to load stuff\n");
exit(1);
}
//VID_SetPalette(image1->palette);
glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB8, image1->palette_width, GL_RGB, GL_UNSIGNED_BYTE, image1->palette);
// 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); // scale linearly when image smalled than texture
// 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);
#else
// 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);
}
glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGBA8, image1->palette_width, GL_RGBA, GL_UNSIGNED_BYTE, image1->palette);
// 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); // scale linearly when image smalled than texture
// 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_TWID_KOS, image1->data);
#endif
}
/* 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.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.