5

I must draw lots of primitives with OpenGL (3.3, 4.2), I knew with glutSolidTeapot() ; I can draw a teapot primitive with glut.

But it seems there won't be a vertex array generated from this command, and I don't know if this kind of commands are deprecated or not.

And I noticed lots of modern OpenGL tutorials just loads their own primitives and avoided to just use glut, they even loaded simple geometry from 3d mesh format file.

My purpose is to just draw these primitives as quick as possible.And use the new OpenGL as much as possible.

So that how to draw primitives in modern OpenGL?

tomriddle_1234
  • 3,145
  • 6
  • 41
  • 71

2 Answers2

8

As GLUT (which is not part of OpenGL in any way) draws its primitves using immediate mode glBegin/glEnd and using the deprecated fixed-function builtin attributes, you won't be able to use these anymore, if you want to concentrate on non-deprecated, modern core functionality.

Instead of using the builtin attributes (like glVertex, glNormal, ...) you have to use your own generic vertex attributes (in conjunction with an appropriate vertex shader, of course) and instead of glBegin/glEnd calls you have to draw primitives using vertex arrays fed by VBOs and drawn using glDrawArrays/glDrawElements and their derivatives.

Whereas nothing prevents you from storing the vertex data for these objects as variables in your source code or to generate them manually, loading them from files is the easiest and most generic way, at least for such rather complex objects like the Utah teapot. The Wavefront OBJ format is a rather simple ASCII based mesh file format that is quite easy to read and might be a starting point to look into, as it can be exported by virtually any modelling software.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
  • but how was the glut's accuracy on primitives? I noticed that there will be more than 50 lines if load a sphere from a fbx or obj file... – tomriddle_1234 Oct 15 '11 at 06:05
  • 1
    @tomriddle_1234: Those 50 lines deal with file I/O parsing, creating buffers, etc. Actually 50 lines is rather short. Drawing a cube is easy. Drawing a UV sphere with just C++ code, not loading from a file I explained here: http://stackoverflow.com/questions/5988686/how-do-i-create-a-3d-sphere-in-opengl-using-visual-c/5989676#5989676 The GLUT teapot is created in a similar way (evaluation of parametric patches). – datenwolf Oct 15 '11 at 09:37
  • 1
    @tomriddle_1234: So far I just wrote down this sphere generation code for that SO answer. However I was thinking about writing a small, simple primitives library for OpenGL. Anyway, cubes are dead simple, cylinders and cones are straightforard. The teapot is most complex since it requires evaluation parametric surfaces. You can look into the GLUT sourcecode for how it's done. – datenwolf Oct 16 '11 at 06:45
  • I found lots of unit primitive from here https://bitbucket.org/alfonse/gltut/downloads, he wrote some lua script to generate unit primitive xml file. He created his own format, it's complex to load so but not hard, and it is very convinent – tomriddle_1234 Oct 18 '11 at 01:42
2

Rendering with the new (non deprecated) OpenGL standards is done exclusively by using shaders.

Shader attribute can only be buffer objects.

Shortly, instead of having a set of arrays that specify vertex positions, colors, texture coordinates and so on, is client memory, you havfe to upload them in buffer objects.

Luca
  • 11,646
  • 11
  • 70
  • 125
  • any where I can find these unit objects with 3d model file, and load them easily? what if I want different level of tessellation – tomriddle_1234 Oct 15 '11 at 06:09