PDA

View Full Version : Scene isn't rendering - probably matrix issues?



WIld Sage
05-22-2011, 06:31 PM
Okay, well. First of all, this is the first time I've tried using vertex buffers with shaders, and for whatever reason, nothing is showing up when I set it to render the object.

Here's my render() function:



float mvMat[16] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 50, 1 };
//mvMat is supposed to be an identity matrix translated 50 units forward

float projMat[16];

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

if (vbo != 0 && ibo != 0)
{
float mvpMat[16];

m3dMatrixMultiply44(mvpMat, mvMat, projMat);

glUseProgram(progFlatColor);
glUniform4f(uniformColor, 1.0, 0.0, 1.0, 1.0);
glUniformMatrix4fv(uniformMVP, 1, 0, mvpMat);

// Draw the main object
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 20, (const GLvoid*)0);

glDrawElements(GL_TRIANGLES, activeHeader.numTris, GL_UNSIGNED_SHORT, 0);


// Draw the wireframe

// Draw normals

// Draw PhysX shapes

// Draw bounding box/sphere

printf("ERROR: %i\n", glGetError());
}

SwapBuffers(g_hDC);
}

projMat is set up like so:

float fov = 70.0f;
float aspect = (rcClient.right/iRenderHeight);
float znear = 1.0f;
float zfar = 100.0f;

const float h = 1.0f/tan(fov*PI_OVER_360);
float neg_depth = znear-zfar;
projMat[0] = h / aspect;
projMat[1] = 0;
projMat[2] = 0;
projMat[3] = 0;

projMat[4] = 0;
projMat[5] = h;
projMat[6] = 0;
projMat[7] = 0;

projMat[8] = 0;
projMat[9] = 0;
projMat[10] = (zfar + znear)/neg_depth;
projMat[11] = -1;

projMat[12] = 0;
projMat[13] = 0;
projMat[14] = 2.0f*(znear*zfar)/neg_depth;
projMat[15] = 0;

And my SetupShaders() function looks like this:

unsigned long progFlatColor, progFlatTexture, progLitColor, progLitTexture;
int uniformColor, uniformMVP = -1;

bool DebugShader(unsigned int shader);
bool DebugProgram(unsigned int program);

const char VertSrcFlatColor[] =
"\
//Flat Shader - Vertex\n\
//Displays unshaded shapes using a uniform color\n\
#version 120\n\
\n\
uniform mat4 MVP;\n\
\n\
attribute vec4 position;\n\
\n\
void main(void)\n\
{\n\
gl_Position = (position * MVP);\n\
}\n\
\0";

const char FragSrcFlatColor[] =
"\
//Flat Shader - Fragment\n\
\n\
uniform vec4 color;\n\
\n\
void main(void)\n\
{\n\
gl_FragColor = color;\n\
}\n\
\0";

enum ShaderMode
{
SM_NONE = 0,
SM_CORE,
SM_ARB
};

void SetupShaders(void)
{
//printf("INFO: vert shader: \n%s", VertSrcFlatColor);
//printf("INFO: frag shader: \n%s", FragSrcFlatColor);

ShaderMode sm = SM_NONE;

if (glewIsSupported("GL_VERSION_2_0"))
{
printf("INFO: Shaders supported natively in GL version 2.0 or higher.\n");
sm = SM_CORE;
}
else if (GLEW_ARB_vertex_shader && GLEW_ARB_vertex_shader)
{
printf("INFO: Shaders supported via extensions.\n");
sm = SM_ARB;
}
else
{
printf("ERROR: Shaders aren't supported.\n");
sm = SM_NONE;
}

unsigned long vertShader, fragShader;

const char* vv = VertSrcFlatColor;
const char* ff = FragSrcFlatColor;

switch (sm)
{
case SM_CORE:
vertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShader, 1, &vv, NULL);
glCompileShader(vertShader);
if (!DebugShader(vertShader))
{
glDeleteShader(vertShader);
return;
}

fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShader, 1, &ff, NULL);
glCompileShader(fragShader);
if (!DebugShader(fragShader))
{
glDeleteShader(vertShader);
glDeleteShader(fragShader);
return;
}

progFlatColor = glCreateProgram();
glAttachShader(progFlatColor, vertShader);
glAttachShader(progFlatColor, fragShader);

glBindAttribLocation(progFlatColor, 0, "position");

glLinkProgram(progFlatColor);
glUseProgram(progFlatColor);
if (!DebugProgram(progFlatColor))
{
glDetachShader(progFlatColor, vertShader);
glDeleteShader(vertShader);
glDetachShader(progFlatColor, fragShader);
glDeleteShader(fragShader);
glDeleteProgram(progFlatColor);
return;
}

uniformColor = glGetUniformLocation(progFlatColor, "color");
uniformMVP = glGetUniformLocation(progFlatColor, "MVP");

if (uniformColor == -1 || uniformMVP == -1)
printf("ERROR: Could not find uniform locations.\n");

glDeleteShader(vertShader);
glDeleteShader(fragShader);
break;

case SM_ARB:
vertShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
glShaderSourceARB(vertShader, 1, &vv, NULL);
glCompileShaderARB(vertShader);
fragShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
glShaderSourceARB(fragShader, 1, &ff, NULL);
glCompileShaderARB(fragShader);

progFlatColor = glCreateProgramObjectARB();
glAttachObjectARB(progFlatColor, vertShader);
glAttachObjectARB(progFlatColor, fragShader);
glLinkProgramARB(progFlatColor);
glUseProgramObjectARB(progFlatColor);
break;

default:
return;
break;
}

printf("INFO: Shaders successfully initialized.\n");
}


I'm about 99% sure that the buffers are getting filled properly - I printf'd the entire array of vertices, and they look correct.

WIld Sage
05-23-2011, 12:51 PM
Bump?

I could really use some help here, I really have no idea what I'm doing wrong. Am I handling matrices all wrong? Or am I doing it right, and the problem is elsewhere?

Dan Bartlett
05-23-2011, 06:05 PM
This line:

gl_Position = (position * MVP);
should most likely be:

gl_Position = (MVP * position);

Also, you should probably be using activeHeader.numTris * 3 in your glDrawElements call, since that argument is the number of vertices, not the number of triangles.

WIld Sage
05-23-2011, 07:01 PM
I changed it as you suggested, but still nothing...

mobeen
05-23-2011, 09:57 PM
Your projection matrix is wrong. Change it to this and see if it helps.



void GetPerspective(float fov, float asp, float zMin, float zMax, float* projMat)
{

float yMax = zMin * tanf(fov*float(M_PI) / 360.0f);
float yMin = -yMax;
float xMin = yMin * asp;
float xMax = -xMin;

projMat[0] = (2.0f * zMin) / (xMax - xMin);
projMat[1] = 0;
projMat[2] = (xMax + xMin) / (xMax - xMin);
projMat[3] = 0;

projMat[4] = 0;
projMat[5] = (2.0f * zMin) / (yMax - yMin);
projMat[6] = (yMax + yMin) / (yMax - yMin);
projMat[7] = 0;

projMat[8] = 0;
projMat[9] = 0;
projMat[10] = -((zMax + zMin) / (zMax - zMin));
projMat[11] = -1;

projMat[12] = 0;
projMat[13] = 0;
projMat[14] = -((2.0f * (zMax*zMin))/(zMax - zMin));
projMat[15] = 0;
}

WIld Sage
05-24-2011, 12:01 AM
Still nothing =(

WIld Sage
05-24-2011, 12:11 AM
Still nothing =(

mobeen
05-24-2011, 01:39 AM
What geometry are u pushing into the vbo? I mean the positions. Are u sure that they are within the near far clip range (1-100) that u have given. Try changing the near clip to 0.1?
And I also dont see any viewing transformation, perhaps add in a translation of vec(0,0,-3) to move the camera three units on z axis.

WIld Sage
05-24-2011, 03:10 AM
Still nothing =(

mobeen
05-24-2011, 03:12 AM
You did not answer me i asked


What geometry are u pushing into the vbo? I mean the positions. Are u sure that they are within the near far clip range (1-100) that u have given?

Can u show us what geometry u have?

WIld Sage
05-24-2011, 03:53 AM
Sorry about that, I triple posted because of lag (Don't ask me how lol)

Anyways, my viewing transform is this:


float mvMat[16] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 50, 1 };
// It might look backwards, but I transposed it to column-major.
// Yes, I've tried it with -50 instead of 50, with each change.


When I print out the vertex positions from the .3ds file I read in, it shows:


0: x = -10, y = -10, z = 15
1: x = 10, y = -10, z = 15
2: x = 10, y = 10, z = 15
3: x = -10, y = 10, z = 15
4: x = -10, y = -10, z = -15
5: x = 10, y = -10, z = -15
6: x = 10, y = 10, z = -15
7: x = -10, y = 10, z = -15


My near and far planes are set to 1 and 100 respectively, and I'm (trying to) translate it 50 units forward.

EDIT: I'm sending the geometry to the buffer as an interleaved array, also consisting of vertex normals (float[3]), and texture coordinates (float[2]). My "vertex" struct looks like:

struct vertex
{
float x, y, z, nx, ny, nz, u, v;
};
The data I send into the vertex buffer is a "vertex[8]", or more specifically, a "void*", malloc'd and memcpy'd with data from my .3ds loader. here's the portion of the code that uploads the data to the buffers:


activeVertData = malloc(sizeof(verts));
if (!activeVertData)
printf("ERROR: Failed to allocate memory for activeVertData. Required bytes: %i\n", sizeof(verts));

memcpy(activeVertData, verts, sizeof(verts));


activeTriData = malloc(sizeof(faces));
if (!activeTriData)
printf("ERROR: Failed to allocate memory for activeTriData. Required bytes: %i\n", sizeof(faces));

memcpy(activeTriData, faces, sizeof(faces));

free(verts);
free(faces);

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(activeVertData), activeVertData, GL_STATIC_DRAW);

glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(activeTriData), activeTriData, GL_STATIC_DRAW);

Alfonse Reinheart
05-24-2011, 05:58 AM
sizeof(activeVertData)

"activeVertData" is a pointer. Its size is four bytes. Same goes for "activeTriData".

The sizeof operator cannot be used to detect the size of dynamically allocated blocks of memory (stuff you get with malloc or new[]). You must keep track of this yourself, or store you data in a std::vector which will keep track of it for you.

WIld Sage
05-24-2011, 11:14 AM
Okay, I changed it so that it now uses (size = numVerts * sizeof(vertex)) instead. Still nothing. I think I'm getting closer, though....

WIld Sage
05-24-2011, 11:56 PM
Bump, I'm really stumped here T_T

mobeen
05-25-2011, 12:13 AM
Could u try to render in immediate mode i.e.


glBegin(GL_TRIANGLES);
for(int i=0;i<indices.size();i++) {
int index = i*3;
glVertex3fv(vertices[indices[index] ]);
glVertex3fv(vertices[indices[index+1] ]);
glVertex3fv(vertices[indices[index+2] ]);
}
glEnd();

WIld Sage
05-25-2011, 01:38 AM
Huh. Surprisingly, still nothing. Here's my new Render() function:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -50.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 1.0);
for (int i = 0; i < activeHeader.numTris; i++)
{
glVertex3f(activeVertData[activeTriData[i].a].x, activeVertData[activeTriData[i].a].y, activeVertData[activeTriData[i].a].z);
glVertex3f(activeVertData[activeTriData[i].b].x, activeVertData[activeTriData[i].b].y, activeVertData[activeTriData[i].b].z);
glVertex3f(activeVertData[activeTriData[i].c].x, activeVertData[activeTriData[i].c].y, activeVertData[activeTriData[i].c].z);
}
glEnd();
The projection matrix is set by gluPerspective in the WM_SIZE message. Perhaps memcpy failed, or something..?

WIld Sage
05-25-2011, 01:47 AM
OOOOkay, this is weird. For the heck of I tested out:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, 50.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-1, -1, 0);
glVertex3f(0, 1, 0);
glVertex3f(1, -1, 0);
glEnd();
and STILL NOTHING. Ultimately, the only thing that really WORKS, is glClearColor. What the heck am I doing wrong?

mobeen
05-25-2011, 01:53 AM
1) What are u setting the near and far clip planes to? Are u sure the values are fine.
2) Could u post your render function may be u r doing something wrong elsewhere in the function?

WIld Sage
05-25-2011, 02:31 AM
The Near and Far planes are being set to 1 and 100 respectively. Entire Render() function (minus commented code):
#include "Main.h"

const float identityMat[16] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };

float mvMat[16] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 50, 1 };

float projMat[16];

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


// Immediate mode test
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, 50.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-1, -1, 0);
glVertex3f(0, 1, 0);
glVertex3f(1, -1, 0);
glEnd();


SwapBuffers(g_hDC);
}(The matrix definitions at the top don't need to be there as I'm not using them now, but they're not hurting anyone, so I didn't comment them out)

And, for completion, the WM_SIZE message in my WndProc:
RECT rcStatus, rcClient;
HWND hStatus = GetDlgItem(hWnd, MAIN_STATUS);
SendMessage(hStatus, WM_SIZE, 0, 0);
GetWindowRect(hStatus, &amp;rcStatus);
int iStatusHeight = rcStatus.bottom - rcStatus.top;

GetClientRect(hWnd, &amp;rcClient);
int iRenderHeight = rcClient.bottom - iStatusHeight;

glViewport (rcClient.left, rcClient.top, rcClient.right, iRenderHeight);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, (float)rcClient.right/(float)iRenderHeight, 1, 100);(There's a status bar at the bottom, plus I intend to put more child windows, which I don't want GL rendering under)

mobeen
05-25-2011, 02:46 AM
Change this

glTranslatef(0.0, 0.0, 50.0);

to this


glTranslatef(0.0, 0.0, -50.0);

WIld Sage
05-25-2011, 03:01 AM
Progress!! Finally!!! Okay, so this:

glVertex3f(-10, -10, 0);
glVertex3f(0, 10, 0);
glVertex3f(10, -10, 0);
Gives me a magenta triangle. This:

for (int i = 0; i < activeHeader.numTris; i++)
{
glVertex3f(activeVertData[activeTriData[i].a].x, activeVertData[activeTriData[i].a].y, activeVertData[activeTriData[i].a].z);
glVertex3f(activeVertData[activeTriData[i].b].x, activeVertData[activeTriData[i].b].y, activeVertData[activeTriData[i].b].z);
glVertex3f(activeVertData[activeTriData[i].c].x, activeVertData[activeTriData[i].c].y, activeVertData[activeTriData[i].c].z);

}
Gives me nothing whatsoever.

EDIT: Okay, I got the mesh to load. It shows a magenta cube (which is all the mesh was) on the screen. However, when I switch back to using the buffers, nothing shows up again...

mobeen
05-25-2011, 03:10 AM
OK i m glad it worked.
Hmm now could u show the vbo setup and how the data is being input?

WIld Sage
05-25-2011, 03:15 AM
Just to note, this worked:

for (int i = 0; i < activeHeader.numTris; i++)
{
glVertex3f(activeVertData[activeTriData[i].a].x, activeVertData[activeTriData[i].a].y, activeVertData[activeTriData[i].a].z);
glVertex3f(activeVertData[activeTriData[i].b].x, activeVertData[activeTriData[i].b].y, activeVertData[activeTriData[i].b].z);
glVertex3f(activeVertData[activeTriData[i].c].x, activeVertData[activeTriData[i].c].y, activeVertData[activeTriData[i].c].z);

} It's my fault for saying it didn't, I forgot to actually load the mesh lol. Anyhow. Here's my CURRENT render():

#include "Main.h"

const float identityMat[16] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };

float mvMat[16] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, -30,
0, 0, 0, 1 };

float projMat[16];

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

if (vbo != 0 &amp;&amp; ibo != 0)
{

float mvpMat[16];

m3dMatrixMultiply44(mvMat, mvMat, identityMat);
m3dMatrixMultiply44(mvpMat, projMat, identityMat);

glUseProgram(progFlatColor);
glUniform4f(uniformColor, 1.0, 0.0, 1.0, 1.0);
glUniformMatrix4fv(uniformMVP, 1, 0, mvpMat);

// Draw the main object
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 20, NULL);

glDrawElements(GL_TRIANGLES, activeHeader.numTris * 3, GL_UNSIGNED_SHORT, 0);

}

SwapBuffers(g_hDC);
}

As a note, I've tried that modelview matrix both as-is and transposed, and no good either way. Here's my WM_SIZE message:



RECT rcStatus, rcClient;
HWND hStatus = GetDlgItem(hWnd, MAIN_STATUS);
SendMessage(hStatus, WM_SIZE, 0, 0);
GetWindowRect(hStatus, &amp;rcStatus);
int iStatusHeight = rcStatus.bottom - rcStatus.top;

GetClientRect(hWnd, &amp;rcClient);
int iRenderHeight = rcClient.bottom - iStatusHeight;

glViewport (rcClient.left, rcClient.top, rcClient.right, iRenderHeight);


float fov = 90.0f;
float aspect = ((float)rcClient.right/(float)iRenderHeight);
float zMin = 1.0f;
float zMax = 100.0f;

float yMax = zMin * tanf(fov*PI_OVER_360);
float yMin = -yMax;
float xMin = yMin * aspect;
float xMax = -xMin;

projMat[0] = (2.0f * zMin) / (xMax - xMin);
projMat[1] = 0;
projMat[2] = (xMax + xMin) / (xMax - xMin);
projMat[3] = 0;

projMat[4] = 0;
projMat[5] = (2.0f * zMin) / (yMax - yMin);
projMat[6] = (yMax + yMin) / (yMax - yMin);
projMat[7] = 0;

projMat[8] = 0;
projMat[9] = 0;
projMat[10] = -((zMax + zMin) / (zMax - zMin));
projMat[11] = -1;

projMat[12] = 0;
projMat[13] = 0;
projMat[14] = -((2.0f * (zMax*zMin))/(zMax - zMin));
projMat[15] = 0;

EDIT: I'm just gonna post my entire Load3DS function here:
#include "Main.h"

GLuint vbo, ibo = 0;

struct Vertex
{
float x, y, z, nx, ny, nz, u, v;
};

struct Normal
{
float x, y, z;
};

struct Face
{
unsigned short a, b, c;
};

void Load3DS(char* filename)
{
printf("\nINFO: Opening %s as a .3ds model.\n\n", filename);

unsigned long i;
FILE *file;
unsigned short chunkID;
unsigned long chunkLength;
unsigned char Char;
unsigned short qty;
unsigned short faceFlags;
unsigned long fileLength;
char name[21] = {0};

unsigned short numVerts, numFaces;
Vertex* verts;
Face* faces;

file = fopen(filename, "rb");
if (!file)
{
printf("ERROR: Failed to open %s.\n", filename);
return;
}

fseek(file, 0, SEEK_END);
fileLength = ftell(file);
fseek(file, 0, SEEK_SET);

while (ftell(file) < fileLength)
{
fread(&amp;chunkID, 2, 1, file);
fread(&amp;chunkLength, 4, 1, file);

switch (chunkID)
{
case 0x4d4d: // Main chunk
break;

case 0x3d3d: // 3d editor chunk
break;

case 0x4000: // Contains the name of the object
i = 0;
do
{
fread(&amp;Char, 1, 1, file);
name[i] = Char;
i++;
} while (Char != '\0' &amp;&amp; i < 20);
printf("INFO: read object '%s'.\n", name);
break;

case 0x4100: // Another empty parent node...
break;

case 0x4110: // Vertices! yay!
fread(&amp;numVerts, 2, 1, file);
printf("INFO: Found %i vertices.\n", numVerts);
verts = (Vertex*)malloc(sizeof(Vertex) * numVerts);
if (!verts)
{
printf("ERROR: Failed to allocate memory for vertices.\n");
if (faces)
free(faces);
return;
}
for (i = 0; i < numVerts; i++)
{
fread(&amp;verts[i].x, 4, 1, file);
fread(&amp;verts[i].y, 4, 1, file);
fread(&amp;verts[i].z, 4, 1, file);
}
break;

case 0x4120: // Faces
fread(&amp;numFaces, 2, 1, file);
printf("INFO: found %i faces.\n", numFaces);
faces = (Face*)malloc(sizeof(Face) * numFaces);
if (!faces)
{
printf("ERROR: Failed to allocate memory for faces.\n");
if (verts)
free(verts);
return;
}
for (i = 0; i < numFaces; i++)
{
fread(&amp;faces[i].a, 2, 1, file);
fread(&amp;faces[i].b, 2, 1, file);
fread(&amp;faces[i].c, 2, 1, file);
fread(&amp;faceFlags, 2, 1, file);
}
break;

case 0x4140:
fread(&amp;qty, 2, 1, file);
printf("INFO: found %i texture coordinates.\n", qty);
for (i = 0; i < qty; i++)
{
fread(&amp;verts[i].u, 4, 1, file);
fread(&amp;verts[i].v, 4, 1, file);
}
break;

default:
fseek(file, chunkLength - 6, SEEK_CUR);
break;
}
}
fclose(file);

//////////////////////////////
// Calculate vertex normals //
//////////////////////////////
for (i = 0; i < numFaces; i++)
{
Normal tempNorm;

// Calculate the surface normal
Normal U;
U.x = (verts[faces[i].b].x - verts[faces[i].a].x);
U.y = (verts[faces[i].b].y - verts[faces[i].a].y);
U.z = (verts[faces[i].b].z - verts[faces[i].a].z);

Normal V;
V.x = (verts[faces[i].c].x - verts[faces[i].a].x);
V.y = (verts[faces[i].c].y - verts[faces[i].a].y);
V.z = (verts[faces[i].c].z - verts[faces[i].a].z);

tempNorm.x = (U.y * V.z) - (U.z * V.y);
tempNorm.y = (U.z * V.x) - (U.x * V.z);
tempNorm.z = (U.x * V.y) - (U.y * V.x);

// Add the surface normal to every connected vertex
verts[faces[i].a].nx += tempNorm.x;
verts[faces[i].a].ny += tempNorm.y;
verts[faces[i].a].nz += tempNorm.z;

verts[faces[i].b].nx += tempNorm.x;
verts[faces[i].b].ny += tempNorm.y;
verts[faces[i].b].nz += tempNorm.z;

verts[faces[i].b].nx += tempNorm.x;
verts[faces[i].b].ny += tempNorm.y;
verts[faces[i].b].nz += tempNorm.z;
}

for (i = 0; i < numVerts; i++)
{
// Normalize vertex normals
float length = sqrt((verts[i].nx*verts[i].nx) + (verts[i].ny*verts[i].ny) + (verts[i].nz*verts[i].nz));

verts[i].nx /= length;
verts[i].ny /= length;
verts[i].nz /= length;
}

// Print vertex info

for (i = 0; i < numVerts; i++)
{
printf("INFO: Vertex #%i: x: %f, y: %f, z: %f,\n nx: %f, ny: %f, nz: %f,\n u: %f, v: %f\n",
i, verts[i].x, verts[i].y, verts[i].z,
verts[i].nx, verts[i].ny, verts[i].nz,
verts[i].u, verts[i].v );
}


activeFilename = filename;
activeHeader.magic[1] = 'A';
activeHeader.magic[2] = 'M';
activeHeader.magic[3] = 'L';
activeHeader.magic[4] = ' ';
activeHeader.versionNumber = 1;
activeHeader.numVerts = numVerts;
activeHeader.numTris = numFaces;
activeHeader.vertFormat = VF_PNT;
activeHeader.triFormat = TF_16;

//// Copy vertices to the array and upload them to the buffer
long size = activeHeader.numVerts * activeHeader.vertFormat;

activeVertData = (vert_PNT*)malloc(size);
if (!activeVertData)
printf("ERROR: Failed to allocate memory for activeVertData. Required bytes: %i\n", sizeof(verts));

memcpy(activeVertData, verts, size);

glGenBuffers(1, &amp;vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, size, activeVertData, GL_STATIC_DRAW);

//// Do the same with the triangles
size = activeHeader.numTris * activeHeader.triFormat;

activeTriData = (triangle_16*)malloc(size);
if (!activeTriData)
printf("ERROR: Failed to allocate memory for activeTriData. Required bytes: %i\n", sizeof(faces));

memcpy(activeTriData, faces, size);

free(verts);
free(faces);

glGenBuffers(1, &amp;ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, activeTriData, GL_STATIC_DRAW);

// Check for errors
if (vbo == 0 || ibo == 0)
{
printf("ERROR: Failed to create vertex/index buffers.\n");
}

return;
}

mobeen
05-25-2011, 03:19 AM
Your get the size using

long size = activeHeader.numVerts * activeHeader.vertFormat;
Does activeHeader.vertFormat contain the total number of elements per vertex?
Usually, i would give the second parameters as


sizeof(float)*numElementsPerVertex*total_vertices
so in your case it should be


sizeof(float)*3*total_vertices;

the same thing goes for indices where sizeof float will be replace by either sizeof(GLushort) or GLuint depending on the type u have given.

Another thing, you need to enable the vertex attribute too like this,


glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, NULL);

The second last parameter is the stride the value of which depends on how the interleaving is done so for your case since you are putting the normals and uvs together with positions, it should be


GLsizei stride = sizeof(GLfloat)*(3+3+2);

since 3 floats for positions+3 floats for normal and 2 floats for uv coordinates.
See if this helps.

WIld Sage
05-25-2011, 03:31 AM
vertFormat and triFormat are defined as sizeof's of the vert_PNT and triangle_16 structs, respectively, so numVerts * vertFormat should give the exact size in bytes of the vertex array. "glBufferData(GL_ARRAY_BUFFER, size, activeVertData, GL_STATIC_DRAW)" uploads the vertex data, or is supposed to...

WIld Sage
05-25-2011, 03:35 AM
GLsizei stride = sizeof(GLfloat)*(3+3+2);

since 3 floats for positions+3 floats for normal and 2 floats for uv coordinates.
See if this helps.

Wait, I thought stride was the number of bytes *between* vertices, not the size of the vertex struct entirely..?


EDIT: I added "glEnableVertexAttribArray(0);", and I now get a purple... shape on the screen. It's *definately* not a cube, however. I'm guessing it's not seeing the indices properly now..?

mobeen
05-25-2011, 03:36 AM
NO since u r interleaving the normals and uvs it should give the offset to the next vertex.

WIld Sage
05-25-2011, 03:42 AM
Okay, yay, I can see a magenta square! Progress has been made! :D

Thank you so much!!

mobeen
05-25-2011, 03:51 AM
Great. I am glad it worked out ok.