Major Vertex Array Problem

All,

I am lost…I have a small app that tries to render a long polygon to simulate a horizon from a texture. The vertex array portion is all jacked! Can someone please take a look at the full sample prg below and see if more eyes can see whats wrong…

Basically uses a 256x256 16bit RGB565 texture with four individual images 192x64 stacked on each other that when tiled produce a portion of a horizon. But any image should prodce the strange effect. The program below produces the polygon correclty but the texture coordinates are all messed up.

I’m guessing I’m doing something wrong with the texture coordinate array. Maybe packing (short in int for storage). I tries ints for the tex coords but not the correct results, still messed up.

Any ideas? Please…


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

#define APPNAME “glVertexPointer”

#define WIDTH 640
#define HEIGHT 480
#define FIELDOFVIEW 70.0
#define NEAR_Z 1.0
#define FAR_Z 1000000.0

int Cvertices[] = { 0, -380, 32, 0,
1, -190, 32, 0,
2, 0, 32, 0,
3, 190, 32, 0,
4, 380, 32, 0,
5, -380, -32, 0,
6, -190, -32, 0,
7, 0, -32, 0,
8, 190, -32, 0,
9, 380, -32, 0};

unsigned int Cindices0[] = { 0,1,6,5}; /* -SideA */
short Ctexture0[] = { 1, 1,
190, 1,
190, 63,
1, 63};

unsigned int Cindices1[] = { 1,2,7,6}; /* ASideB */
short Ctexture1[] = { 1, 65,
190, 65,
190, 127,
1, 127};

unsigned int Cindices2[] = { 2,3,8,7}; /* BSideC */
short Ctexture2[] = { 1, 129,
190, 129,
190, 191,
1, 191};

unsigned int Cindices3[] = { 3,4,9,8}; /* CSideD */
short Ctexture3[] = { 1, 193,
190, 193,
190, 255,
1, 255};

void *Load_File(char *FileName)
{
FILE *Work_File;
char *Allocated_Memory_Pointer;
long Current_Pos, Size;

Work_File = fopen(FileName, “rb”);

Current_Pos = ftell(Work_File);
fseek(Work_File, 0, SEEK_END);
Size = ftell(Work_File);
fseek(Work_File, Current_Pos, SEEK_SET);

Allocated_Memory_Pointer = (char*)malloc(Size);

fread(Allocated_Memory_Pointer, 1, Size, Work_File);

fclose(Work_File);

return(Allocated_Memory_Pointer);
}

void Dump(unsigned int *indices, short *texture)
{
int i;

for(i = 0; i < 4; i++) {
printf("Vert %1d: (%3d,%3d)
“, indices[i], texture[i*2], texture[i*2+1]);
}
printf(”
");
}

void Key(unsigned char Key, int X, int Y)
{
switch(Key) {
case 27: exit(0);
}
}

int OpenGL_init(int argc, char **argv)
{
glutInitWindowPosition(0, 0);
glutInitWindowSize(WIDTH, HEIGHT);
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE);

if(!glutCreateWindow(APPNAME)) {
fprintf(stderr,“Error opening a window.
\r”);
return 0;
}

glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(FIELDOFVIEW, ((GLfloat)WIDTH/(GLfloat)HEIGHT), NEAR_Z, FAR_Z);

glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(FAR_Z);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glEnable(GL_TEXTURE_2D);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

return 1;
}

void OpenGL_loadTexture(void data)
{
glBindTexture(GL_TEXTURE_2D, 0);
glTexImage2D(GL_TEXTURE_2D,
0, /
MipMap Level /
GL_RGB5, /
Internal storage format /
256, /
Width of texture /
256, /
Height of texture /
0, /
Border Width /
GL_RGB, /
Source storage format /
GL_UNSIGNED_SHORT_5_6_5, /
Source pixel format /
data); /
Pixel data for texture */
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

// Scale texture to allow integer values
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glScalef(1.0/256.0, 1.0/256.0, 1.0);
}

void Draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, 0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslated(0.0, 0.0, -425.0);

glVertexPointer(3, GL_INT, 16, (GLvoid*)((int)Cvertices + sizeof(int)));

Dump(Cindices0, Ctexture0);
glTexCoordPointer(2, GL_SHORT, 0, Ctexture0);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, Cindices0);
Dump(Cindices1, Ctexture1);
glTexCoordPointer(2, GL_SHORT, 0, Ctexture1);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, Cindices1);
Dump(Cindices2, Ctexture2);
glTexCoordPointer(2, GL_SHORT, 0, Ctexture2);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, Cindices2);
Dump(Cindices3, Ctexture3);
glTexCoordPointer(2, GL_SHORT, 0, Ctexture3);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, Cindices3);

glFlush();
glutSwapBuffers();
}

int main(int argc, char **argv)
{
if(OpenGL_init(argc, argv)) {
OpenGL_loadTexture(Load_File(“horizon.dat”));

glutKeyboardFunc(Key);
glutDisplayFunc(Draw);
glutMainLoop();

}

return 1;
}