OpenGL/Utah teapot: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Kotlin)
m (→‎{{header|C}}: Remove vanity tags)
Line 10: Line 10:
This implementation sticks to the task requirements and only shows the teapot, it's rotating, so you can enjoy it's magnificence from all possible viewpoints and perspectives.
This implementation sticks to the task requirements and only shows the teapot, it's rotating, so you can enjoy it's magnificence from all possible viewpoints and perspectives.
<lang C>
<lang C>
/*Abhishek Ghosh, MahaShoshti, 26th September 2017*/

#include<gl/freeglut.h>
#include<gl/freeglut.h>



Revision as of 15:28, 29 July 2018

OpenGL/Utah teapot is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

This is way to render classic Utah Teapot using OpenGL library.

See also: Utah Teapot wikipage.

C

As well explained on the Wikipedia page ( link above ), the teapot played such an important role in the development of Computer Graphics, that Mark Kilgard honoured it by giving it it's own primitive drawing functions. freeglut did even better and implemented the rest of the tea set, yes, the API also provides a cup and a spoon.

This implementation sticks to the task requirements and only shows the teapot, it's rotating, so you can enjoy it's magnificence from all possible viewpoints and perspectives. <lang C>

  1. include<gl/freeglut.h>

double rot = 0; float matCol[] = {1,0,0,0};

void display(){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(30,1,1,0); glRotatef(rot,0,1,1); glMaterialfv(GL_FRONT,GL_DIFFUSE,matCol); glutWireTeapot(0.5); glPopMatrix(); glFlush(); }


void onIdle(){ rot += 0.01; glutPostRedisplay(); }

void init(){ float pos[] = {1,1,1,0}; float white[] = {1,1,1,0}; float shini[] = {70};

glClearColor(.5,.5,.5,0); glShadeModel(GL_SMOOTH); glLightfv(GL_LIGHT0,GL_AMBIENT,white); glLightfv(GL_LIGHT0,GL_DIFFUSE,white); glMaterialfv(GL_FRONT,GL_SHININESS,shini); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); }

int main(int argC, char* argV[]) { glutInit(&argC,argV); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(900,700); glutCreateWindow("The Amazing, Rotating Utah Teapot brought to you in OpenGL via freeglut."); init(); glutDisplayFunc(display); glutIdleFunc(onIdle); glutMainLoop(); return 0; } </lang>

Kotlin

Translation of: C
Library: freeglut
Works with: Ubuntu 14.04

Assuming that freeglut is already installed on your system in the default location(s), you first need to build opengl.klib using the following .def file and the cinterop tool:

// opengl.def
headers = /usr/include/GL/glut.h
compilerOpts = -I/usr/include
linkerOpts = -L/usr/lib/x86_64-linux-gnu -lglut -lGL -lGLU

You then need to compile the following Kotlin program, linking against opengl.klib, and run the resulting .kexe file to view the rotating teapot. <lang scala>// Kotlin Native v0.6

import kotlinx.cinterop.* import opengl.*

var rot = 0f val matCol = floatArrayOf(1f, 0f, 0f, 0f)

fun display() {

   glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
   glPushMatrix()
   glRotatef(30f, 1f, 1f, 0f)
   glRotatef(rot, 0f, 1f, 1f)
   glMaterialfv(GL_FRONT, GL_DIFFUSE, matCol.toCValues())
   glutWireTeapot(0.5)
   glPopMatrix()
   glFlush()

}

fun onIdle() {

   rot += 0.1f
   glutPostRedisplay()

}

fun init() {

   val white = floatArrayOf(1f, 1f, 1f, 0f)
   val shini = floatArrayOf(70f)
   glClearColor(0.5f, 0.5f, 0.5f, 0f);
   glShadeModel(GL_SMOOTH)
   glLightfv(GL_LIGHT0, GL_AMBIENT, white.toCValues())
   glLightfv(GL_LIGHT0, GL_DIFFUSE, white.toCValues())
   glMaterialfv(GL_FRONT, GL_SHININESS, shini.toCValues())
   glEnable(GL_LIGHTING)
   glEnable(GL_LIGHT0)
   glEnable(GL_DEPTH_TEST)

}

fun main(args: Array<String>) {

   memScoped {
       val argc = alloc<IntVar>().apply { value = 0 }
       glutInit(argc.ptr, null) 
   }
   glutInitDisplayMode(GLUT_SINGLE or GLUT_RGB or GLUT_DEPTH)
   glutInitWindowSize(900, 700)
   val title = "The Amazing, Rotating Utah Teapot brought to you in OpenGL via freeglut."
   glutCreateWindow(title)
   init()
   glutDisplayFunc(staticCFunction(::display))
   glutIdleFunc(staticCFunction(::onIdle))
   glutMainLoop()

}</lang>

Ol

Library: GL
Library: GLU

<lang lisp>

initial data

(define vertices '(

   (  0.2000  0.0000 2.70000 ) (  0.2000 -0.1120 2.70000 )
   (  0.1120 -0.2000 2.70000 ) (  0.0000 -0.2000 2.70000 )
   (  1.3375  0.0000 2.53125 ) (  1.3375 -0.7490 2.53125 )
   (  0.7490 -1.3375 2.53125 ) (  0.0000 -1.3375 2.53125 )
   (  1.4375  0.0000 2.53125 ) (  1.4375 -0.8050 2.53125 )
   (  0.8050 -1.4375 2.53125 ) (  0.0000 -1.4375 2.53125 )
   (  1.5000  0.0000 2.40000 ) (  1.5000 -0.8400 2.40000 )
   (  0.8400 -1.5000 2.40000 ) (  0.0000 -1.5000 2.40000 )
   (  1.7500  0.0000 1.87500 ) (  1.7500 -0.9800 1.87500 )
   (  0.9800 -1.7500 1.87500 ) (  0.0000 -1.7500 1.87500 )
   (  2.0000  0.0000 1.35000 ) (  2.0000 -1.1200 1.35000 )
   (  1.1200 -2.0000 1.35000 ) (  0.0000 -2.0000 1.35000 )
   (  2.0000  0.0000 0.90000 ) (  2.0000 -1.1200 0.90000 )
   (  1.1200 -2.0000 0.90000 ) (  0.0000 -2.0000 0.90000 )
   ( -2.0000  0.0000 0.90000 ) (  2.0000  0.0000 0.45000 )
   (  2.0000 -1.1200 0.45000 ) (  1.1200 -2.0000 0.45000 )
   (  0.0000 -2.0000 0.45000 ) (  1.5000  0.0000 0.22500 )
   (  1.5000 -0.8400 0.22500 ) (  0.8400 -1.5000 0.22500 )
   (  0.0000 -1.5000 0.22500 ) (  1.5000  0.0000 0.15000 )
   (  1.5000 -0.8400 0.15000 ) (  0.8400 -1.5000 0.15000 )
   (  0.0000 -1.5000 0.15000 ) ( -1.6000  0.0000 2.02500 )
   ( -1.6000 -0.3000 2.02500 ) ( -1.5000 -0.3000 2.25000 )
   ( -1.5000  0.0000 2.25000 ) ( -2.3000  0.0000 2.02500 )
   ( -2.3000 -0.3000 2.02500 ) ( -2.5000 -0.3000 2.25000 )
   ( -2.5000  0.0000 2.25000 ) ( -2.7000  0.0000 2.02500 )
   ( -2.7000 -0.3000 2.02500 ) ( -3.0000 -0.3000 2.25000 )
   ( -3.0000  0.0000 2.25000 ) ( -2.7000  0.0000 1.80000 )
   ( -2.7000 -0.3000 1.80000 ) ( -3.0000 -0.3000 1.80000 )
   ( -3.0000  0.0000 1.80000 ) ( -2.7000  0.0000 1.57500 )
   ( -2.7000 -0.3000 1.57500 ) ( -3.0000 -0.3000 1.35000 )
   ( -3.0000  0.0000 1.35000 ) ( -2.5000  0.0000 1.12500 )
   ( -2.5000 -0.3000 1.12500 ) ( -2.6500 -0.3000 0.93750 )
   ( -2.6500  0.0000 0.93750 ) ( -2.0000 -0.3000 0.90000 )
   ( -1.9000 -0.3000 0.60000 ) ( -1.9000  0.0000 0.60000 )
   (  1.7000  0.0000 1.42500 ) (  1.7000 -0.6600 1.42500 )
   (  1.7000 -0.6600 0.60000 ) (  1.7000  0.0000 0.60000 )
   (  2.6000  0.0000 1.42500 ) (  2.6000 -0.6600 1.42500 )
   (  3.1000 -0.6600 0.82500 ) (  3.1000  0.0000 0.82500 )
   (  2.3000  0.0000 2.10000 ) (  2.3000 -0.2500 2.10000 )
   (  2.4000 -0.2500 2.02500 ) (  2.4000  0.0000 2.02500 )
   (  2.7000  0.0000 2.40000 ) (  2.7000 -0.2500 2.40000 )
   (  3.3000 -0.2500 2.40000 ) (  3.3000  0.0000 2.40000 )
   (  2.8000  0.0000 2.47500 ) (  2.8000 -0.2500 2.47500 )
   (  3.5250 -0.2500 2.49375 ) (  3.5250  0.0000 2.49375 )
   (  2.9000  0.0000 2.47500 ) (  2.9000 -0.1500 2.47500 )
   (  3.4500 -0.1500 2.51250 ) (  3.4500  0.0000 2.51250 )
   (  2.8000  0.0000 2.40000 ) (  2.8000 -0.1500 2.40000 )
   (  3.2000 -0.1500 2.40000 ) (  3.2000  0.0000 2.40000 )
   (  0.0000  0.0000 3.15000 ) (  0.8000  0.0000 3.15000 )
   (  0.8000 -0.4500 3.15000 ) (  0.4500 -0.8000 3.15000 )
   (  0.0000 -0.8000 3.15000 ) (  0.0000  0.0000 2.85000 )
   (  1.4000  0.0000 2.40000 ) (  1.4000 -0.7840 2.40000 )
   (  0.7840 -1.4000 2.40000 ) (  0.0000 -1.4000 2.40000 )
   (  0.4000  0.0000 2.55000 ) (  0.4000 -0.2240 2.55000 )
   (  0.2240 -0.4000 2.55000 ) (  0.0000 -0.4000 2.55000 )
   (  1.3000  0.0000 2.55000 ) (  1.3000 -0.7280 2.55000 )
   (  0.7280 -1.3000 2.55000 ) (  0.0000 -1.3000 2.55000 )
   (  1.3000  0.0000 2.40000 ) (  1.3000 -0.7280 2.40000 )
   (  0.7280 -1.3000 2.40000 ) (  0.0000 -1.3000 2.40000 )))
   

(define Rim: '(

   ( 102 103 104 105   4   5   6   7
       8   9  10  11  12  13  14  15 )))

(define Body: '(

   (  12  13  14  15  16  17  18  19
      20  21  22  23  24  25  26  27 )
   (  24  25  26  27  29  30  31  32
      33  34  35  36  37  38  39  40 )))

(define Lid: '(

   (  96  96  96  96  97  98  99 100
     101 101 101 101   0   1   2   3 )
   (   0   1   2   3 106 107 108 109
     110 111 112 113 114 115 116 117 )))

(define Handle: '(

   (  41  42  43  44  45  46  47  48
      49  50  51  52  53  54  55  56 )
   (  53  54  55  56  57  58  59  60
      61  62  63  64  28  65  66  67 )))

(define Spout: '(

   (  68  69  70  71  72  73  74  75
      76  77  78  79  80  81  82  83 )
   (  80  81  82  83  84  85  86  87
      88  89  90  91  92  93  94  95 )))
render pass

(define knots '(0 0 0 0 1 1 1 1))

  (let ((render (lambda (surface)
                    (gluBeginSurface teapot)
                    (gluNurbsSurface teapot 8 knots 8 knots (* 4 3) 3  (fold append '() (map (lambda (n) (nth vertices n)) surface))  4 4 GL_MAP2_VERTEX_3)
                    (gluEndSurface teapot))))
     (for-each render Rim:)
     (for-each render Body:)
     (for-each render Lid:)
     (glScalef -1 1  1)
     (for-each render Rim:)
     (for-each render Body:)
     (for-each render Lid:)
     (glScalef -1 -1 1)
     (for-each render Rim:)
     (for-each render Body:)
     (for-each render Lid:)
     (glScalef -1 1  1)
     (for-each render Rim:)
     (for-each render Body:)
     (for-each render Lid:)
     (for-each render Handle:)
     (for-each render Spout:)
     (glScalef 1 -1 1)
     (for-each render Handle:)
     (for-each render Spout:))

</lang>