Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How do I create a context #58

Open
Elsarques opened this issue Jun 27, 2024 · 5 comments
Open

[Question] How do I create a context #58

Elsarques opened this issue Jun 27, 2024 · 5 comments

Comments

@Elsarques
Copy link

Elsarques commented Jun 27, 2024

I'm trying to run an example OpenGL Project, by porting it to OpenGL ES 2, so that I can run on Termux:GUI and learn how to make 3D graphics, and i'm stuck in this problem that I can't figure out the variable that indicates the display to create a context, basically, I need help, here's the code:

import termuxgui as tg
from OpenGL.GLES2 import *  # pylint: disable=W0614
from OpenGL.GLU import *  # pylint: disable=W0614
from OpenGL.GLUT import *
import glm
from OpenGL.EGL import *

from utils.shaderLoader import Shader

with tg.Connection() as c:
  
  a = tg.Activity(c)
  
  #hdc=a
  #hglrc=eglCreateContext(hdc, None, EGL_NO_CONTEXT, None)
  #eglMakeCurrent(hdc,hglrc)
  
  g_vertex_buffer_data = [
		  -1.0,-1.0,-1.0,
		  -1.0,-1.0, 1.0,
		  -1.0, 1.0, 1.0,
		  1.0, 1.0,-1.0,
		  -1.0,-1.0,-1.0,
		  -1.0, 1.0,-1.0,
		  1.0,-1.0, 1.0,
		  -1.0,-1.0,-1.0,
		  1.0,-1.0,-1.0,
		  1.0, 1.0,-1.0,
		  1.0,-1.0,-1.0,
		  -1.0,-1.0,-1.0,
		  -1.0,-1.0,-1.0,
		  -1.0, 1.0, 1.0,
		  -1.0, 1.0,-1.0,
		  1.0,-1.0, 1.0,
		  -1.0,-1.0, 1.0,
		  -1.0,-1.0,-1.0,
		  -1.0, 1.0, 1.0,
		  -1.0,-1.0, 1.0,
		  1.0,-1.0, 1.0,
		  1.0, 1.0, 1.0,
		  1.0,-1.0,-1.0,
		  1.0, 1.0,-1.0,
		  1.0,-1.0,-1.0,
		  1.0, 1.0, 1.0,
		  1.0,-1.0, 1.0,
		  1.0, 1.0, 1.0,
		  1.0, 1.0,-1.0,
		  -1.0, 1.0,-1.0,
		  1.0, 1.0, 1.0,
		  -1.0, 1.0,-1.0,
		  -1.0, 1.0, 1.0,
		  1.0, 1.0, 1.0,
		  -1.0, 1.0, 1.0,
		  1.0,-1.0, 1.0]
  g_color_buffer_data = [ 
		0.583,  0.771,  0.014,
		0.609,  0.115,  0.436,
		0.327,  0.483,  0.844,
		0.822,  0.569,  0.201,
		0.435,  0.602,  0.223,
		0.310,  0.747,  0.185,
		0.597,  0.770,  0.761,
		0.559,  0.436,  0.730,
		0.359,  0.583,  0.152,
		0.483,  0.596,  0.789,
		0.559,  0.861,  0.639,
		0.195,  0.548,  0.859,
		0.014,  0.184,  0.576,
		0.771,  0.328,  0.970,
		0.406,  0.615,  0.116,
		0.676,  0.977,  0.133,
		0.971,  0.572,  0.833,
		0.140,  0.616,  0.489,
		0.997,  0.513,  0.064,
		0.945,  0.719,  0.592,
		0.543,  0.021,  0.978,
		0.279,  0.317,  0.505,
		0.167,  0.620,  0.077,
		0.347,  0.857,  0.137,
		0.055,  0.953,  0.042,
		0.714,  0.505,  0.345,
		0.783,  0.290,  0.734,
		0.722,  0.645,  0.174,
		0.302,  0.455,  0.848,
		0.225,  0.587,  0.040,
		0.517,  0.713,  0.338,
		0.053,  0.959,  0.120,
		0.393,  0.621,  0.362,
		0.673,  0.211,  0.457,
		0.820,  0.883,  0.371,
		0.982,  0.099,  0.879
  ]        

  glClearColor(0.0,0,0.4,0)
  glDepthFunc(GL_LESS)
  glEnable(GL_DEPTH_TEST)
  
  shader = Shader()
  shader.initShaderFromGLSL(["glsl/tu01/vertex.glsl"],["glsl/tu01/fragment.glsl"])
  MVP_ID   = glGetUniformLocation(shader.program,"MVP")

  Projection = glm.perspective(glm.radians(45.0),800.0/480.0,0.1,100.0)
  View =  glm.lookAt(glm.vec3(4,3,-3), # Camera is at (4,3,-3), in World Space
                glm.vec3(0,0,0), #and looks at the (0.0.0))
                glm.vec3(0,1,0) ) #Head is up (set to 0,-1,0 to look upside-down)

  Model=  glm.mat4(1.0)
  MVP =  Projection * View *Model
  #print context.MVP
  #exit(0)

  vertexbuffer  = glGenBuffers(1)
  glBindBuffer(GL_ARRAY_BUFFER,vertexbuffer)
  glBufferData(GL_ARRAY_BUFFER,len(g_vertex_buffer_data)*4,(GLfloat * len(g_vertex_buffer_data))(*g_vertex_buffer_data),GL_STATIC_DRAW)


  colorbuffer  = glGenBuffers(1)
  glBindBuffer(GL_ARRAY_BUFFER,colorbuffer)
  glBufferData(GL_ARRAY_BUFFER,len(g_color_buffer_data)*4,(GLfloat * len(g_color_buffer_data))(*g_color_buffer_data),GL_STATIC_DRAW)

  while(True):
	  #print self.context.MVP
	  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
	  glUseProgram(shader.program)
	  glUniformMatrix4fv(MVP_ID, 1,GL_FALSE,glm.value_ptr(MVP))

	  glEnableVertexAttribArray(0)
	  glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer)

	  glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,ctypes.c_voidp(0)) # None means ctypes.c_voidp(0)

	  glEnableVertexAttribArray(1)
	  glBindBuffer(GL_ARRAY_BUFFER, colorbuffer)
	  glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,ctypes.c_voidp(0))


	  glDrawArrays(GL_TRIANGLES, 0, 12*3) # 12*3 indices starting at 0 -> 12 triangles

	  glDisableVertexAttribArray(0)
	  glDisableVertexAttribArray(1)
time.sleep(5)

Here's the command that set everything:

pkg install termux-gui-package python-opengl numpy

pip install PyOpenGL

if i remember correctly that's all you need to install, but I'm not certain if it's actually just these

almost forgot, here's the repo that i downloaded the examples:

https://github.com/jcteng/python-opengl-tutorial

@tareksander
Copy link
Member

Currently the python library has no support for shared hardware buffers. If you want to use OpenGL, you'll need to use the C library. Here is the OpenGL example.

I likely won't have time to implement this for the python library for a few months.

@Elsarques
Copy link
Author

Currently the python library has no support for shared hardware buffers. If you want to use OpenGL, you'll need to use the C library. Here is the OpenGL example.

I likely won't have time to implement this for the python library for a few months.

I see, i'm trying to compile the example and it's throwing and EGL error:

buffer_gl.c:10:10: fatal error: EGL/egl.h: No such file or directory
10 | #include <EGL/egl.h>
| ^~~~~~~~~~~
compilation terminated.

How do I install EGL with C?

@tareksander
Copy link
Member

I think the headers are in the mesa-dev package?

@Elsarques
Copy link
Author

Elsarques commented Jun 28, 2024

I think the headers are in the mesa-dev package?

The header worked now, but I still can't compile it, I have little to no knowledge in C, so I don't know what happened here

gcc -o buffer buffer_gl.c
/data/data/com.termux/files/usr/bin/ld: /data/data/com.termux/files/usr/tmp/ccPQigwS.o: in function checkGLError': buffer_gl.c:(.text+0x10): undefined reference to glGetError'
/data/data/com.termux/files/usr/bin/ld: /data/data/com.termux/files/usr/tmp/ccPQigwS.o: in function createShader': buffer_gl.c:(.text+0x70): undefined reference to glCreateShader'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x98): undefined reference to glShaderSource' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xb0): undefined reference to glCompileShader'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xdc): undefined reference to glGetShaderiv' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x10c): undefined reference to glGetShaderiv'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x148): undefined reference to glGetShaderInfoLog' /data/data/com.termux/files/usr/bin/ld: /data/data/com.termux/files/usr/tmp/ccPQigwS.o: in function main':
buffer_gl.c:(.text+0x1c0): undefined reference to tgui_connection_create' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x200): undefined reference to tgui_activity_create'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x258): undefined reference to tgui_hardware_buffer_create' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x2a8): undefined reference to tgui_hardware_buffer_create'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x2d4): undefined reference to eglGetDisplay' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x310): undefined reference to eglInitialize'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x320): undefined reference to eglGetError' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x388): undefined reference to eglQueryString'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x39c): undefined reference to eglGetError' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x474): undefined reference to eglGetProcAddress'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x488): undefined reference to eglGetProcAddress' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x49c): undefined reference to eglGetProcAddress'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x4b0): undefined reference to eglGetProcAddress' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x500): undefined reference to eglChooseConfig'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x510): undefined reference to eglGetError' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x55c): undefined reference to eglBindAPI'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x56c): undefined reference to eglGetError' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x5b4): undefined reference to eglCreateContext'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x5c8): undefined reference to eglGetError' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x61c): undefined reference to eglCreatePbufferSurface'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x630): undefined reference to eglGetError' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x664): undefined reference to eglMakeCurrent'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x674): undefined reference to eglGetError' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x69c): undefined reference to glGetString'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x714): undefined reference to glClearColor' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x738): undefined reference to glViewport'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x7b8): undefined reference to eglGetError' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x80c): undefined reference to eglGetError'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x838): undefined reference to glBindFramebuffer' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x854): undefined reference to glBindRenderbuffer'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x898): undefined reference to glFramebufferRenderbuffer' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x8b4): undefined reference to glBindFramebuffer'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x8d0): undefined reference to glBindRenderbuffer' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x914): undefined reference to glFramebufferRenderbuffer'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x930): undefined reference to glCreateProgram' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x980): undefined reference to glAttachShader'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x99c): undefined reference to glAttachShader' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x9b4): undefined reference to glLinkProgram'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0x9d0): undefined reference to glGetProgramiv' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xa00): undefined reference to glGetProgramiv'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xa3c): undefined reference to glGetProgramInfoLog' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xa70): undefined reference to glUseProgram'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xa94): undefined reference to glGetAttribLocation' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xab4): undefined reference to glEnableVertexAttribArray'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xaf8): undefined reference to tgui_poll_event' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xb64): undefined reference to tgui_create_surface_view'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xbb8): undefined reference to tgui_surface_view_config' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xbf0): undefined reference to tgui_send_touch_event'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xc04): undefined reference to tgui_focus' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xcac): undefined reference to tgui_focus'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xcfc): undefined reference to tgui_event_destroy' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xe18): undefined reference to glClear'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xe4c): undefined reference to glVertexAttribPointer' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xe6c): undefined reference to glDrawArrays'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xebc): undefined reference to glBindFramebuffer' /data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xed0): undefined reference to glFlush'
/data/data/com.termux/files/usr/bin/ld: buffer_gl.c:(.text+0xef4): undefined reference to `tgui_surface_view_set_buffer'
collect2: error: ld returned 1 exit status

@lualvsil
Copy link

you need to link the libraries, try like this:
gcc buffer_gl.c -o buffer -ltermuxgui -lEGL -lGLESv2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants