I am using OpenGL to draw a rectangle, and I am doing this with golang.
I am not using red, green, blue argument for now.
var vertices = []float64{
-1.0, -1.0, 0.0, // V1 - bottom left
-1.0, 1.0, 0.0, // V2 - top left
1.0, -1.0, 0.0, // V3 - bottom right
1.0, 1.0, 0.0, // V4 - top right
}
func DisplayWrite(red []byte, green []byte, blue []byte) {
const GL_TRUE = 1
const GL_FALSE = 0
const GL_FLOAT = 0x1406
const GL_VERTEX_ARRAY = 0x8074
const GL_TRIANGLE_STRIP = 0x0005
C.glLoadIdentity();
chkGlErr("glLoadIdentity")
C.glTranslatef(0.0, 0.0, -5.0);
chkGlErr("glTranslatef")
C.glEnableClientState(GL_VERTEX_ARRAY);
chkGlErr("glEnableClientState")
C.glColor4f(1.0, 1.0, 1.0, 1.0);
chkGlErr("glColor4f")
C.glVertexPointer(3, GL_FLOAT, 0, (unsafe.Pointer)(&vertices));
chkGlErr("glVertexPointer")
C.glDrawArrays(GL_TRIANGLE_STRIP, 0, (C.GLsizei)(len(vertices) / 3));
chkGlErr("glDrawArrays")
C.glDisableClientState(GL_VERTEX_ARRAY);
chkGlErr("glDisableClientState")
const GL_UNSIGNED_BYTE = 0x1401
//C.glDrawPixels(Width, Height, GL_RED, GL_UNSIGNED_BYTE, &red[0])
//C.glDrawPixels(Width, Height, GL_GREEN, GL_UNSIGNED_BYTE, &green[0])
//C.glDrawPixels(Width, Height, GL_GREEN, GL_UNSIGNED_BYTE, &blue[0])
}
Problem is when I call glDrawArrays like this:
C.glDrawArrays(GL_TRIANGLE_STRIP, 0, (C.GLsizei)(len(vertices) / 3));
It draws nothing and error is GL_INVALID_OPERATION. Is something wrong with the way I draw something?
This is setup part:
func DisplayInit() DispInitStatus {
//(EGL and OpenGL ES initialization codes...)
C.glViewport(0, 0, (C.GLsizei)(dWidth), (C.GLsizei)(dHeight));
chkGlErr("glViewport")
C.glMatrixMode(GL_PROJECTION);
chkGlErr("glMatrixMode")
C.glLoadIdentity();
chkGlErr("glLoadIdentity")
makeFrustum(45.0, (float64)(dWidth) / (float64)(dHeight), 0.1, 100.0)
C.glMatrixMode(GL_MODELVIEW);
chkGlErr("glMatrixMode")
C.glLoadIdentity();
chkGlErr("glLoadIdentity")
//C.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//chkGlErr("glHint")
C.glClearColor(1.0, 0.0, 0.0, 1.0);
chkGlErr("glClearColor")
C.glClear(GL_COLOR_BUFFER_BIT);
chkGlErr("glClear")
fmt.Println("EGL/GLES is now ready")
return Disp_Ok
//No error with these.
}
And main function
(DisplayUpdate does glFlush and eglSwapBuffers. It works with no problem.)
func main() {
runtime.LockOSThread()
DisplayInit()
red := make([]byte, dWidth*dHeight)
gre := make([]byte, dWidth*dHeight)
blu := make([]byte, dWidth*dHeight)
DisplayWrite(red, gre, blu)
DisplayUpdate()
for {
}
}
I googled and found this:
And I tried using runtime.LockOSThread() like above, but still doesn't work.
ADD: Added makeFrustum.
func makeFrustum(fovY, aspectRatio, front, back float64) {
tangent := math.Tan(fovY / 2 * (3.14159265 / 180.0)); // tangent of half fovY
height := front * tangent; // half height of near plane
width := height * aspectRatio; // half width of near plane
// params: left, right, bottom, top, near, far
C.glFrustumf((C.GLfloat)(-width), (C.GLfloat)(width), (C.GLfloat)(-height), (C.GLfloat)(height), (C.GLfloat)(front), (C.GLfloat)(back));
chkGlErr("glFrustumf")
}
This causes no error, too.
This does same thing as gluPerspective.
https://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml