PDA

View Full Version : GL_POINTS to mesh please



dimmoro
11-04-2012, 12:14 AM
Hi,
Think i am missing something very simple.

Have vertex points that render correctly a human torso (with arms and hands) using glDrawArrays' GL_POINTS. When trying the "other" modes, such as GL_POLYGON or GL_TRIANGLES, there are lines, or surfaces coming from the hands to the stomach.

Am I missing a step that tells OpenGL to render it as a mesh? Perhaps, have to render each part separately (i.e. fingers, hands, arms, etc?)

Thank you.

tonyo_au
11-04-2012, 12:49 AM
Please supply a little code. Does each set of 3 vertices make a valid triangle when you change to GL_TRIANGLES and each pair make a valid line for GL_LINES

dimmoro
11-04-2012, 01:54 AM
Appreciate the quick reply.

Don't believe they make a valid triangle nor valid line. But for the code:


- (void)drawRect:(NSRect)dirtyRect
{
//NSLog(@"Redraw");
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEP TH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);

glShadeModel(GL_SMOOTH);

glEnable(GL_POLYGON_SMOOTH);
glPolygonMode(GL_FRONT, GL_POINTS);

// clear our drawable
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);

Vector3 color = {1.0f,0.85f,0.35f};
[self drawAnObject:hair color:color imageName:nil];
color.x = 1.0f; color.y = 0.0f; color.z = 0.0f;
[self drawAnObject:head color:color imageName:@"face.png"];
// color.x = 1.0f; color.y = 0.85f; color.z = 0.35f;
// [self drawAnObject:eye color:color imageName:nil];
color.x = 0.0f; color.y = 1.0f; color.z = 0.0f;
[self drawAnObject:upper_body color:color imageName:@"upper.png"];
color.x = 0.0f; color.y = 0.0f; color.z = 1.0f;
[self drawAnObject:lower_body color:color imageName:@"lower.png"];
// color.x = 1.0f; color.y = 0.85f; color.z = 0.35f;
// [self drawAnObject:skirt color:color imageName:nil];

if ([self inLiveResize] && !fAnimate)
glFlush ();
else
[[self openGLContext] flushBuffer];
//glReportError ();
}

// ---------------------------------

- (void) drawAnObject:(PolyMesh *)polyMesh color:(Vector3)color imageName:(NSString *)imageName
{
if (imageName != nil) {
// allocate a texture name
glGenTextures( 1, &textureid );

// blending
glEnable( GL_TEXTURE_2D );

// load the texture
NSString *filePath = [[NSBundle mainBundle] pathForResource:[imageName stringByDeletingPathExtension] ofType:[imageName pathExtension]];
textureid = SOIL_load_OGL_texture([filePath UTF8String], SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
if (0 == textureid) {
NSLog(@"Failed to load texture %@!", imageName);
} else {
NSLog(@"Loaded texture %@!", imageName);

glBindTexture(GL_TEXTURE_2D, textureid);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
//glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer(2, GL_FLOAT, 0, polyMesh.single_texCoords);
}
} else {
NSLog(@"Texture passed as nil.");
}

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

glNormalPointer(GL_FLOAT, 0, polyMesh.single_normals);
glVertexPointer(3, GL_FLOAT, 0, polyMesh.single_vertices);
glColor3f(color.x,color.y,color.z);
glDrawArrays(GL_LINES, 0, polyMesh.numVertices);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);

if (imageName != nil) {
if (0 != textureid) {
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDeleteTextures(1, &textureid);
}
}
}


Thank you.

Dark Photon
11-04-2012, 06:21 AM
Please use
... or ... blocks for code. Inserted them for you.

Dark Photon
11-04-2012, 06:58 AM
You said it renders fine as GL_POINTS, but that's not what you want as you're trying other primitive types. And I don't think you really stated what you want to see (you need to tell us that).

Is this a joint tree? Do you want to render lines between each parent and child joint pair? If so, then GL_LINES will do what you want. But you of course need to know which point pairs represent parent and child pairs. glDrawArrays will work, but even simpler is glDrawElements.

dimmoro
11-05-2012, 09:45 PM
Hi, reposting.

Not 100% sure what order the vertices are in. Thought as long as the shape was (hopefully) convex, that OpenGL would magically display the surfaces lol.


- (void)drawRect:(NSRect)dirtyRect
{
//NSLog(@"Redraw");
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEP TH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);

glShadeModel(GL_SMOOTH);

glEnable(GL_POLYGON_SMOOTH);
glPolygonMode(GL_FRONT, GL_POINTS);

// clear our drawable
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);

Vector3 color = {1.0f,0.85f,0.35f};
[self drawAnObject:hair color:color imageName:nil];
color.x = 1.0f; color.y = 0.0f; color.z = 0.0f;
[self drawAnObject:head color:color imageName:@"face.png"];
// color.x = 1.0f; color.y = 0.85f; color.z = 0.35f;
// [self drawAnObject:eye color:color imageName:nil];
color.x = 0.0f; color.y = 1.0f; color.z = 0.0f;
[self drawAnObject:upper_body color:color imageName:@"upper.png"];
color.x = 0.0f; color.y = 0.0f; color.z = 1.0f;
[self drawAnObject:lower_body color:color imageName:@"lower.png"];
// color.x = 1.0f; color.y = 0.85f; color.z = 0.35f;
// [self drawAnObject:skirt color:color imageName:nil];

if ([self inLiveResize] && !fAnimate)
glFlush ();
else
[[self openGLContext] flushBuffer];
//glReportError ();
}

// ---------------------------------

- (void) drawAnObject:(PolyMesh *)polyMesh color:(Vector3)color imageName:(NSString *)imageName
{
if (imageName != nil) {
// allocate a texture name
glGenTextures( 1, &textureid );

// blending
glEnable( GL_TEXTURE_2D );

// load the texture
NSString *filePath = [[NSBundle mainBundle] pathForResource:[imageName stringByDeletingPathExtension] ofType:[imageName pathExtension]];
textureid = SOIL_load_OGL_texture([filePath UTF8String], SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
if (0 == textureid) {
NSLog(@"Failed to load texture %@!", imageName);
} else {
NSLog(@"Loaded texture %@!", imageName);

glBindTexture(GL_TEXTURE_2D, textureid);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
//glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer(2, GL_FLOAT, 0, polyMesh.single_texCoords);
}
} else {
NSLog(@"Texture passed as nil.");
}

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

glNormalPointer(GL_FLOAT, 0, polyMesh.single_normals);
glVertexPointer(3, GL_FLOAT, 0, polyMesh.single_vertices);
glColor3f(color.x,color.y,color.z);
glDrawArrays(GL_LINES, 0, polyMesh.numVertices);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);

if (imageName != nil) {
if (0 != textureid) {
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDeleteTextures(1, &textureid);
}
}
}