Sign in to follow this  
zacaj

OpenGL Trouble with Vertex Arrays

Recommended Posts

After reading all of the Opengl super bible and opengl game programming and searching through gamedev and NeHe, I still cant get my program to display the model i load using vertex arrays(or immediate mode), and ive verified that my model loading works by comparing the loaded data with the .obj file. THis is my whole rendering function: GLfloat vect[1000][2]; static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f; unsigned ind[3000]; GLfloat x,y,z,angle; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glPushMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3,GL_FLOAT,0,vect); glColor3f(0.0f, 1.0f, 0.0f); glDrawElements(GL_TRIANGLES, 36,GL_UNSIGNED_BYTE,&ind); glPopMatrix(); // Flush drawing commands glutSwapBuffers(); When I run it nothing appears unless i tell it to do lines, then it draws some of it messed up.

Share this post


Link to post
Share on other sites
What's in your 'ind' array. It's not shown in the listing.
EDIT - Just spotted it. But can you explain what you have in ind and array.
And is your compiler ok with just 'unsigned' in-front of 'ind'?

What .obj file are you loading, and what are you doing with it?

And it's a small point, but why is your vect array [1000] by [2]?

In theory (at first glance) what you have there may just draw something, but the data you are supplying it is obviously not formatted as OpenGL expects..

To help I, or anyone, would need more info...

I would suggest you start with a hand made non-Element Array cube of some sort, and then go from there in steps.

Share this post


Link to post
Share on other sites
The ind array is my indices. The obj file im loading is just a cube exported from milkshape. The vect is by 2 for the x,y,z of each vertex. Heres the full code:
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glut.h>
#include <math.h>
#include <fstream>
#include <string>
#include <sstream>

#include <iostream>
using namespace std;
// Define a constant for the value of PI
#define GL_PI 3.1415f

// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
unsigned ind[3000];
struct GLTVector3
{
float x,y,z;
};
GLfloat vect[1000][2];
GLTVector3 vec[1000];
GLTVector3 nor[1000];
void RenderScene(void)
{
GLfloat x,y,z,angle;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,vect);
glColor3f(0.0f, 1.0f, 0.0f);
glDrawElements(GL_TRIANGLES, 36,GL_UNSIGNED_BYTE,&ind);
/*
glBegin(GL_TRIANGLES);
int i; for(i=0;i<indat;i++)
{
glVertex3f(vec[ind[i]].x,vec[ind[i]].y,vec[ind[i]].z);
}
glEnd();*/
glPopMatrix();
// Flush drawing commands
glutSwapBuffers();
}

// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

// Set drawing color to green
glColor3f(0.0f, 1.0f, 0.0f);
}

void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;

if(key == GLUT_KEY_DOWN)
xRot += 5.0f;

if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;

if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;

if(key > 356.0f)
xRot = 0.0f;

if(key < -1.0f)
xRot = 355.0f;

if(key > 356.0f)
yRot = 0.0f;

if(key < -1.0f)
yRot = 355.0f;

// Refresh the Window
glutPostRedisplay();
}


void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;

// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);

// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void extract_indices_from_string(string str, unsigned &vi, unsigned &ni, unsigned &ti)
{
string v;
string t;
string n;

string *tmp[3] = {&v, &t, &n};

int tidx = 0;
string *target = tmp[0];

for (size_t i = 0; i < str.size(); ++i) {
if (str[i] == '/') target = tmp[++tidx];
else *target += str[i];
}

istringstream iss1(v);
vi = 0;
iss1 >> vi;

istringstream iss2(t);
ti = 0;
iss2 >> ti;

istringstream iss3(n);
ni = 0;
iss3 >> ni;
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Points Example");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
ifstream in("box.obj", ios::in);
int vecat=0;
int norat=0;
int indat=0;
unsigned temp;
while (in) {
string line;
getline(in, line);
istringstream iss;
iss.str(line);
string cmd;
iss >> cmd;

if (cmd[0] == '#') continue;
else if (cmd == "v") {
iss >> vec[vecat].x >> vec[vecat].y >> vec[vecat].z;
printf("%f,%f,%f\n",vec[vecat].x,vec[vecat].y,vec[vecat].z);
vec[vecat].x*=10;
vec[vecat].y*=10;
vec[vecat].z*=10;
//vect[vecat][0]=vec[vecat].x;
//vect[vecat][1]=vec[vecat].y;
//vect[vecat][2]=vec[vecat].z;
vecat++;
} else if (cmd == "vn") {
iss >> nor[norat].x >> nor[norat].y >> vec[norat].z;
norat++;
} else if (cmd == "vt") {
} else if (cmd == "f") {
//iss >> ind[indat] >> "/" >> temp >> "/" >> temp;
//ind[indat]--;
//indat++;
while (iss) {
string word;
iss >> word;
if (word == "") continue;
extract_indices_from_string(word, ind[indat], temp, temp);
ind[indat]--;
printf("%i ",ind[indat]);
indat++;
}
}
}

glutMainLoop();

return 0;
}

and heres the obj file
# Wavefront OBJ exported by MilkShape 3D

v -3.000000 3.083333 3.041667
v -3.000000 -3.000000 3.041667
v 3.000000 3.083333 3.041667
v 3.000000 -3.000000 3.041667
v 3.000000 3.083333 -3.041667
v 3.000000 -3.000000 -3.041667
v -3.000000 3.083333 -3.041667
v -3.000000 -3.000000 -3.041667
# 8 vertices

vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 1.000000
vt 1.000000 0.000000
# 4 texture coordinates

vn 0.000000 0.000000 1.000000
vn 1.000000 0.000000 0.000000
vn 0.000000 0.000000 -1.000000
vn -1.000000 0.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 -1.000000 0.000000
# 6 normals

g Box02
s 1
f 1/1/1 2/2/1 3/3/1
f 2/2/1 4/4/1 3/3/1
s 2
f 3/1/2 4/2/2 5/3/2
f 4/2/2 6/4/2 5/3/2
s 1
f 5/1/3 6/2/3 7/3/3
f 6/2/3 8/4/3 7/3/3
s 2
f 7/1/4 8/2/4 1/3/4
f 8/2/4 2/4/4 1/3/4
s 3
f 7/1/5 1/2/5 5/3/5
f 1/2/5 3/4/5 5/3/5
f 2/1/6 8/2/6 4/3/6
f 8/2/6 6/4/6 4/3/6
# 12 triangles in group

# 12 triangles total


Share this post


Link to post
Share on other sites
Shouldn't it be

glDrawElements(GL_TRIANGLES, 36,GL_UNSIGNED_BYTE, ind);

or even

glDrawElements(GL_TRIANGLES, 36,GL_UNSIGNED_BYTE, &ind[0]);

instead of

glDrawElements(GL_TRIANGLES, 36,GL_UNSIGNED_BYTE,&ind);

You're passing an address to a pointer instead of the pointer itself.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jaska
Shouldn't it be

glDrawElements(GL_TRIANGLES, 36,GL_UNSIGNED_BYTE, ind);

or even

glDrawElements(GL_TRIANGLES, 36,GL_UNSIGNED_BYTE, &ind[0]);

instead of

glDrawElements(GL_TRIANGLES, 36,GL_UNSIGNED_BYTE,&ind);

You're passing an address to a pointer instead of the pointer itself.

He's passing the address of an array, not the address of a pointer. All three of your suggestions will give you the same pointer after array-to-pointer decay.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this