forked from go-gl-legacy/glu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglu.go
153 lines (129 loc) · 3.57 KB
/
glu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package glu
// #cgo darwin LDFLAGS: -framework Carbon -framework OpenGL -framework GLUT
// #cgo linux LDFLAGS: -lGLU
// #cgo windows LDFLAGS: -lglu32
//
// #ifdef __APPLE__
// #include <OpenGL/glu.h>
// #else
// #include <GL/glu.h>
// #endif
import "C"
import (
"errors"
"reflect"
"unsafe"
"github.com/go-gl/gl"
)
func ptr(v interface{}) unsafe.Pointer {
if v == nil {
return unsafe.Pointer(nil)
}
rv := reflect.ValueOf(v)
var et reflect.Value
switch rv.Type().Kind() {
case reflect.Uintptr:
offset, _ := v.(uintptr)
return unsafe.Pointer(offset)
case reflect.Ptr:
et = rv.Elem()
case reflect.Slice:
et = rv.Index(0)
default:
panic("type must be a pointer, a slice, uintptr or nil")
}
return unsafe.Pointer(et.UnsafeAddr())
}
func ErrorString(error gl.GLenum) (string, error) {
e := unsafe.Pointer(C.gluErrorString(C.GLenum(error)))
if e == nil {
return "", errors.New("Invalid GL error code")
}
return C.GoString((*C.char)(e)), nil
}
func Build2DMipmaps(target gl.GLenum, internalFormat int, width, height int, format, typ gl.GLenum, data interface{}) int {
return int(C.gluBuild2DMipmaps(
C.GLenum(target),
C.GLint(internalFormat),
C.GLsizei(width),
C.GLsizei(height),
C.GLenum(format),
C.GLenum(typ),
ptr(data),
))
}
func Perspective(fovy, aspect, zNear, zFar float64) {
C.gluPerspective(
C.GLdouble(fovy),
C.GLdouble(aspect),
C.GLdouble(zNear),
C.GLdouble(zFar),
)
}
func LookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ float64) {
C.gluLookAt(
C.GLdouble(eyeX),
C.GLdouble(eyeY),
C.GLdouble(eyeZ),
C.GLdouble(centerX),
C.GLdouble(centerY),
C.GLdouble(centerZ),
C.GLdouble(upX),
C.GLdouble(upY),
C.GLdouble(upZ),
)
}
func UnProject(winX, winY, winZ float64, model, proj *[16]float64, view *[4]int32) (float64, float64, float64) {
var ox, oy, oz C.GLdouble
m := (*C.GLdouble)(unsafe.Pointer(model))
p := (*C.GLdouble)(unsafe.Pointer(proj))
v := (*C.GLint)(unsafe.Pointer(view))
C.gluUnProject(
C.GLdouble(winX),
C.GLdouble(winY),
C.GLdouble(winZ),
m,
p,
v,
&ox,
&oy,
&oz,
)
return float64(ox), float64(oy), float64(oz)
}
func Project(projX, projY, projZ float64, model, proj *[16]float64, view *[4]int32) (float64, float64, float64) {
var ox, oy, oz C.GLdouble
m := (*C.GLdouble)(unsafe.Pointer(model))
p := (*C.GLdouble)(unsafe.Pointer(proj))
v := (*C.GLint)(unsafe.Pointer(view))
C.gluProject(
C.GLdouble(projX),
C.GLdouble(projY),
C.GLdouble(projZ),
m,
p,
v,
&ox,
&oy,
&oz,
)
return float64(ox), float64(oy), float64(oz)
}
func NewQuadric() unsafe.Pointer {
return unsafe.Pointer(C.gluNewQuadric())
}
func Sphere(q unsafe.Pointer, radius float32, slices, stacks int) {
C.gluSphere((*C.GLUquadric)(q), C.GLdouble(radius), C.GLint(slices), C.GLint(stacks))
}
func Cylinder(q unsafe.Pointer, base, top, height float32, slices, stacks int) {
C.gluCylinder((*C.GLUquadric)(q), C.GLdouble(base), C.GLdouble(top), C.GLdouble(height), C.GLint(slices), C.GLint(stacks))
}
func Disk(q unsafe.Pointer, inner, outer float32, slices, loops int) {
C.gluDisk((*C.GLUquadric)(q), C.GLdouble(inner), C.GLdouble(outer), C.GLint(slices), C.GLint(loops))
}
func PartialDisk(q unsafe.Pointer, inner, outer float32, slices, loops int, startAngle, sweepAngle float32) {
C.gluPartialDisk((*C.GLUquadric)(q), C.GLdouble(inner), C.GLdouble(outer), C.GLint(slices), C.GLint(loops), C.GLdouble(startAngle), C.GLdouble(sweepAngle))
}