QtOpenGL & Additive Manufacturing File Format
-
Hi guys! I have a problem: I've never worked with OpenGL before, but now I must understand it in short time for university task.
Task is quietly simple: Open simple "AMF":http://en.wikipedia.org/wiki/Additive_Manufacturing_File_Format file with OpenGL. It's XML based file. To open and parse it there is no problem.Ok, for test I took a simple example AMF from wiki:
"Sphere20Face direct link":http://amf.wikispaces.com/file/view/Sphere20Face.amf/392196556/Sphere20Face.amfWhich looks like sphere:
!http://pastexen.com/i/v6XGDgy.png(Sphere)!
and tried to manually set vertices, here is my code:
@#include "firstopengl.h"FirstOpenGL::FirstOpenGL()
{}
FirstOpenGL::~FirstOpenGL()
{}
void FirstOpenGL::initializeGL(){
qglClearColor(Qt::white);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
//paintGL();}
void FirstOpenGL::resizeGL(int nWidth, int nHeight){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, (GLint)nWidth, (GLint)nHeight);
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);}
void FirstOpenGL::paintGL(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -7.0);glBegin(GL_TRIANGLES);
qglColor(Qt::red);glVertex3f(-2.62866, 4.25325, 0); glVertex3f(-0.525731, 0.850651, 0); glVertex3f(2.62866, 4.25325, 0); glVertex3f(-2.62866, -4.25325, 0); glVertex3f(2.62866, -4.25325, 0); glVertex3f(0, -2.62866, 4.25325); glVertex3f(0, 2.62866, 4.25325); glVertex3f(0, -2.62866, -4.25325); glVertex3f(0, 2.62866, -4.25325); glVertex3f(4.25325, 0, -2.62866); glVertex3f(4.25325, 0, 2.62866); glVertex3f(-4.25325, 0, -2.62866); glVertex3f(-4.25325, 0, 2.62866);
glEnd();
}@Ofcourse I got trash:
!http://pastexen.com/i/CfndSLl.png(First try)!
Because I still don't know what to do with normals and set's of triangles (in AMF file in tag <triangle></triangle>).
Please help me! :)
And sorry for bad english. -
Ok, now I understand, sets in <triangle></triangle> is indexies of vertex to create triangles:
@void FirstOpenGL::paintGL(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -7.0);Vertex verts[12] = {
Vertex(-2.62866, 4.25325, 0),
Vertex(2.62866, 4.25325, 0),
Vertex(-2.62866, -4.25325, 0),
Vertex(2.62866, -4.25325, 0),
Vertex(0, -2.62866, 4.25325),
Vertex(0, 2.62866, 4.25325),
Vertex(0, -2.62866, -4.25325),
Vertex(0, 2.62866, -4.25325),
Vertex(4.25325, 0, -2.62866),
Vertex(4.25325, 0, 2.62866),
Vertex(-4.25325, 0, -2.62866),
Vertex(-4.25325, 0, 2.62866) };int inds[] = {
0,11,5, 0,5,1, 0,1,7, 0,7,10, 0,10,11, 1,5,9, 5,11,4, 11,10,2, 10,7,6, 7,1,8, 3,9,4,
3,4,2, 3,2,6, 3,6,8, 3,8,9, 4,9,5, 2,4,11, 6,2,10, 8,6,7, 9,8,1 };glBegin(GL_TRIANGLES);//GL_TRIANGLE_FAN
qglColor(Qt::gray);int currentVertex;
for( int i = 0; i < 60; i += 3 )
for( int j = 0; j < 3; j++ ){currentVertex = inds[i + j];
glVertex3f( verts[ currentVertex ].getX(), verts[ currentVertex ].getY(), verts[ currentVertex ].getZ() );}
glEnd();
}@
But I still don't know what I must do with normals. As I understand I need them for shaders. Without shaders and shadows there is no sense of volumetric shape.
Okaaaay, I've found function for normals, but image is still without shaders, why?
@void FirstOpenGL::paintGL(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -7.0);glRotatef(m_xRotate, 1.0, 0.0, 0.0);
glRotatef(m_yRotate, 0.0, 2.0, 0.0);Vertex verts[12] = {
Vertex(-2.62866, 4.25325, 0),
Vertex(2.62866, 4.25325, 0),
Vertex(-2.62866, -4.25325, 0),
Vertex(2.62866, -4.25325, 0),
Vertex(0, -2.62866, 4.25325),
Vertex(0, 2.62866, 4.25325),
Vertex(0, -2.62866, -4.25325),
Vertex(0, 2.62866, -4.25325),
Vertex(4.25325, 0, -2.62866),
Vertex(4.25325, 0, 2.62866),
Vertex(-4.25325, 0, -2.62866),
Vertex(-4.25325, 0, 2.62866) };Vertex norms[12] = {
Vertex(-0.525731, 0.850651, 0),
Vertex(0.525731, 0.850651, 0),
Vertex(-0.525731, -0.850651, 0),
Vertex(0.525731, -0.850651, 0),
Vertex(0, -0.525731, 0.850651),
Vertex(0, 0.525731, 0.850651),
Vertex(0, -0.525731, -0.850651),
Vertex(0, 0.525731, -0.850651),
Vertex(0.850651, 0, -0.525731),
Vertex(0.850651, 0, 0.525731),
Vertex(-0.850651, 0, -0.525731),
Vertex(-0.850651, 0, 0.525731)
};int inds[] = {
0,11,5, 0,5,1, 0,1,7, 0,7,10, 0,10,11, 1,5,9, 5,11,4, 11,10,2, 10,7,6, 7,1,8, 3,9,4,
3,4,2, 3,2,6, 3,6,8, 3,8,9, 4,9,5, 2,4,11, 6,2,10, 8,6,7, 9,8,1 };glBegin(GL_TRIANGLES);//GL_TRIANGLE_FAN
qglColor(Qt::gray);int currentVertex;
for( int i = 0; i < 60; i += 3 )
for( int j = 0; j < 3; j++ ){currentVertex = inds[i + j];
glVertex3f( verts[ currentVertex ].getX(), verts[ currentVertex ].getY(), verts[ currentVertex ].getZ() );
glNormal3f( norms[ currentVertex ].getX(), norms[ currentVertex ].getY(), norms[ currentVertex ].getZ() );}
glEnd();
}@
!http://pastexen.com/i/Gjd1ZiC.png(Last result)! -
Take a look at choosing the correct shading model here:
void FirstOpenGL::initializeGL(){
qglClearColor(Qt::white);
glEnable(GL_DEPTH_TEST);- glShadeModel(GL_FLAT);
//paintGL();
}
You have it as GL_FLAT. You're probably looking for something more like GL_SMOOTH. You'll also need to specify lighting as well.
It looks like you're using OpenGL's "immediate" mode, which is how things were done prior to OpenGL 3. This is fine if you're just starting to learn the mechanics of 3D modelling, but you should probably look at making the leap to the more modern conventions of using vertex buffers. I've found this wiki "here":http://www.opengl.org/wiki/Main_Pagehttp:// to be invaluable for programming OpenGL. Please note that if you're using "immediate" mode, you'll more than likely be using the "fixed function" pipeline which has been deprecated as of OpenGL 3.
- glShadeModel(GL_FLAT);
-
[quote author="joechamm" date="1364068141"]Take a look at choosing the correct shading model here:
void FirstOpenGL::initializeGL(){
qglClearColor(Qt::white);
glEnable(GL_DEPTH_TEST);- glShadeModel(GL_FLAT);
//paintGL();
}
You have it as GL_FLAT. You're probably looking for something more like GL_SMOOTH. You'll also need to specify lighting as well.
It looks like you're using OpenGL's "immediate" mode, which is how things were done prior to OpenGL 3. This is fine if you're just starting to learn the mechanics of 3D modelling, but you should probably look at making the leap to the more modern conventions of using vertex buffers. I've found this wiki "here":http://www.opengl.org/wiki/Main_Pagehttp:// to be invaluable for programming OpenGL. Please note that if you're using "immediate" mode, you'll more than likely be using the "fixed function" pipeline which has been deprecated as of OpenGL 3. [/quote]
Thank you for your answer! But I can't understand the difference between OpenGL 3.0 style and OpenGL higher very well. As I know “immediate” mode - that's glBegin and glEnd, but I dont know alternative way to do that =
Light is ready now:) - glShadeModel(GL_FLAT);