PolyBoard.vs
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
uniform float near;
uniform float far;
uniform vec3 camPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection*view*model*vec4(aPos, 1.0);
ourColor = aColor;
}
PolyBoard.gs
#version 330 core
layout(lines_adjacency) in;
layout(triangle_strip, max_vertices = 12) out;
out vec4 aColor;
uniform float near;
uniform float far;
uniform vec3 camPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
float r = .5;
vec4 P1 = gl_in[0].gl_Position;
vec4 T1 = P1;
T1 = T1+vec4(0.0,r,0.0,0.0);
vec4 T2 = P1;
T2 = T2+vec4(r,0.0,0.0,0.0);
vec4 T3 = P1;
T3 = T3+vec4(-r,0.0,0.0,0.0);
aColor = vec4(0.0, 1.0, 0.9686, 1.0);
gl_Position = T1;
EmitVertex();
gl_Position = T2;
EmitVertex();
gl_Position = T3;
EmitVertex();
EndPrimitive();
}
So far I do not see a cyan triangle anyplace in my window. I do have the lines of the triangles drawing in 1px width great so far.::::
triangles.py
def render():
global projection
while (not glfwWindowShouldClose(window)):
processInput(window)
glClearColor(0.2, 0.3, 0.3, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
triangle_red.drawLines(VAO,VBO,shader_line,
camera.getView(), camera.getProjection())
triangle_green.drawLines(VAO,VBO,shader_line,
camera.getView(), camera.getProjection())
#triangle_red.drawFill(VAO,VBO,None, shader_fill,view,projection)
#triangle_green.drawFill(VAO,VBO,None, shader_fill,view,projection)
triangle_red.drawPolyboard(VAO,VBO,shader_polyboard,view,projection,camera)
glfwSwapBuffers(window)
glfwPollEvents()
Does anyone know why my attempt to enumerate a point as a triangle with the geometry shader is not showing up anyplace in my window?
P.S. This is how things are being drawn::::
def drawPolyboard(self, VAO, VBO, shader, view, projection, camera):
# TODO:
# implenent line width for the triangle loop with the polyboard
# routines from Mathematics for 3D Game Programming Third Edition
# Eric Lengyel... (search Polyboard keyword...)
shader.use()
shader.setFloat('near', camera.near)
shader.setFloat('far', camera.far)
shader.setVec3('camPos', camera.cam_pos )
shader.setMat4('model', self.model)
shader.setMat4('view', view)
shader.setMat4('projection', projection)
glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glDrawArrays(GL_LINE_STRIP_ADJACENCY, self.getArrayStart(), self.vcount)
#print('Testing...')
triangle.py
from OpenGL.GL import *
from glfw.GLFW import *
from glfw import _GLFWwindow as GLFWwindow
from OpenGL.GL import *
from glfw.GLFW import *
from glfw import _GLFWwindow as GLFWwindow
import platform
import glm
import sys
import os
sys.path.append(os.getcwd())
from includes.Triangle import Triangle
from includes.Shader import Shader
from includes.Util import *
from includes.Camera import Camera
def init_glfw():
global SCR_WIDTH
global SCR_HEIGHT
global window
SCR_WIDTH = 1920
SCR_HEIGHT = 1080
glfwInit()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
if (platform.system() == "Darwin"):
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", None, None)
if (window == None):
print("Failed to create GLFW window")
glfwTerminate()
return -1
glfwMakeContextCurrent(window)
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
def init_shaders():
global shader_line
global shader_fill
global shader_polyboard
shader_line = Shader('shaders/Line.vs', 'shaders/Line.fs')
shader_fill = Shader('shaders/Fill.vs', 'shaders/Fill.fs')
shader_polyboard = Shader('shaders/PolyBoard.vs', 'shaders/PolyBoard.fs', 'shaders/PolyBoard.gs')
def init_objects():
global standalone_v
global standalone_i
global triangle_red
global triangle_green
global camera
camera = Camera(deg2rad(0),deg2rad(0),[0,0,0],1920.)
camera.setOrtho(1920,1080,0.0001,1920*2)
standalone_v = glm.array.zeros(0,glm.float32)
standalone_i = glm.array.zeros(0,glm.uint32)
size = 50.
vertices = []
vertices.append([-size,0.,0.])
vertices.append([size,0.,0.])
vertices.append([0.,size,0.])
indices = None
color = [1,0,0]
triangle_red = Triangle(vertices, indices, color, standalone_v,standalone_i)
standalone_v = standalone_v.concat(triangle_red.vertices)
color = [0,1,0]
vertices = []
vertices.append([-size*2,10,0.])
vertices.append([size*2,10,0.])
vertices.append([0.,size*2+10,0.])
triangle_green = Triangle(vertices, indices, color, standalone_v,standalone_i)
standalone_v = standalone_v.concat(triangle_green.vertices)
print('objects are initialized...')
def init_buffers():
global VAO
global VBO
global EBO
global standalone_v
global standalone_i
VAO = glGenVertexArrays(1)
VBO = glGenBuffers(1)
EBO = glGenBuffers(1)
glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, standalone_v.nbytes, standalone_v.ptr, GL_STATIC_DRAW)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, standalone_i.nbytes, standalone_i.ptr, GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
8 * glm.sizeof(glm.float32), None)
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
8 * glm.sizeof(glm.float32), ctypes.c_void_p(3 * glm.sizeof(glm.float32)))
glEnableVertexAttribArray(1)
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE,
8 * glm.sizeof(glm.float32), ctypes.c_void_p(6 * glm.sizeof(glm.float32)))
glEnableVertexAttribArray(2)
def init_matrices():
#global view
#global projection
#global camera
#view = glm.mat4(1.0)
#view = glm.lookAt(glm.vec3(0.0, 0.0, 10.0), glm.vec3(0.0, 0.0, 0.0), glm.vec3(0.0, 1.0, 0.0))
#projection = glm.mat4(1.0)
#projection = glm.ortho((-SCR_WIDTH/2),(SCR_WIDTH/2),-SCR_HEIGHT/2,SCR_HEIGHT/2,0.1,1000)
print('matrices are initialized...')
def render():
global projection
while (not glfwWindowShouldClose(window)):
processInput(window)
glClearColor(0.2, 0.3, 0.3, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
triangle_red.drawLines(VAO,VBO,shader_line,
camera.getView(), camera.getProjection())
triangle_green.drawLines(VAO,VBO,shader_line,
camera.getView(), camera.getProjection())
#triangle_red.drawFill(VAO,VBO,None, shader_fill,view,projection)
#triangle_green.drawFill(VAO,VBO,None, shader_fill,view,projection)
triangle_red.drawPolyboard(VAO,VBO,shader_polyboard,
camera.getView(),camera.getProjection(),camera)
glfwSwapBuffers(window)
glfwPollEvents()
def processInput(window: GLFWwindow) -> None:
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS):
glfwSetWindowShouldClose(window, True)
def framebuffer_size_callback(window: GLFWwindow, width: int, height: int) -> None:
SCR_WIDTH = width
SCR_HEIGHT = height
glViewport(0, 0, width, height)
##glfwSetWindowAspectRatio(window, int(SCR_WIDTH), int(SCR_HEIGHT))
def main() -> int:
init_glfw()
init_shaders()
init_objects()
init_buffers()
init_matrices()
render()
glfwTerminate()
return 0
main()
Camera.py
import sys
import os
sys.path.append(os.getcwd())
import glm
import math
from includes.Util import *
class ArcBall:
def __init__(self, 𝜑y, θz, O, α):
self.O = O
self.𝜑y = 𝜑y
self.θz = θz
self.α = α
self.near = None
self.far = None
def setPhi(self, 𝜑y):
self.𝜑y = 𝜑y
def setTheta(self, θz):
self.θz = θz
class Camera:
def __init__(self, 𝜑y, θx, O, α):
print('Camera()')
self.projection = glm.mat4(1.0)
self.ArcBall = ArcBall( 𝜑y, θx, O, α)
self.up = glm.vec3(0.0, 1.0, 0.0)
self.degrees = glm.vec3( rad2deg(θx), rad2deg(𝜑y),0.0)
self.radians = [θx, 𝜑y, 0.0]
self.quat = glm.quat(self.radians)
print('degrees:',self.degrees)
print('radians:',self.radians)
print('quaternion:', glm.eulerAngles(self.quat))
self.rmat = glm.mat4(self.quat)
self.x = 0.
self.y = 0.
self.z = α
self.near = None
self.far = None
self.cam_pos = glm.vec3(self.x,self.y,-self.z)
self.cam_pos = self.rmat * self.cam_pos
print('cam_pos:', self.cam_pos)
def printAnglesFromQuat(self):
qvec = glm.eulerAngles(self.quat)
print('quaternion:',qvec)
def setAngles(self, rad_x, rad_y):
self.radians = [rad_x, rad_y, 0]
self.degrees = glm.vec3(rad2deg(rad_x), rad2deg(rad_y), 0)
self.quat = glm.quat(self.radians)
self.x = (self.ArcBall.α) * math.cos(self.radians[0])
self.y = (self.ArcBall.α) * math.sin(self.radians[1])
self.Δx = (self.ArcBall.α) * math.sin(self.radians[0])
self.cam_pos = [self.x,self.y,self.Δx]
def printCamPos(self):
print("camerapos:",self.cam_pos)
def getProjection(self):
return self.projection
def setOrtho(self,width,height,near,far):
self.near = near
self.far = far
self.ArcBall.near = near
self.ArcBall.far = far
self.projection = glm.ortho((-width/2),(width/2),(-height/2),(height/2),near,far)
def getView(self):
view = glm.mat4(1.0)
camx = self.cam_pos[0]
camy = self.cam_pos[1]
camz = self.cam_pos[2]
view = glm.lookAt(glm.vec3(camx, camy, camz),
self.ArcBall.O,
self.up)
return view
print('Camera.getView()')
def getCamPos(self):
return self.cam_pos
Triangle.py:
import glm
import math
import sys
import os
sys.path.append(os.getcwd())
from includes.Shader import Shader
from OpenGL.GL import *
class Triangle:
def __init__(self, vertices = None, indices=None, color=None,
standalone_v = None, standalone_i = None):
self.bufwidth = 8
self.vertices_start = self.getVerticesStart(standalone_v)
self.indices_start = self.getIndicesStart(standalone_i)
self.vertices = glm.array.zeros(len(vertices)*self.bufwidth,glm.float32)
self.defcolor = color
self.vcount = len(vertices)
self.model = glm.mat4(1.0)
self.indices = None
count = 0
for v in vertices:
self.vertices[count] = v[0]
self.vertices[count+1] = v[1]
self.vertices[count+2] = v[2]
self.vertices[count+3] = color[0]
self.vertices[count+4] = color[1]
self.vertices[count+5] = color[2]
self.vertices[count+6] = 0.
self.vertices[count+7] = 0.
count = count + self.bufwidth
print('Triangle()')
def getVerticesStart(self, standalone_v):
if(len(standalone_v)==0):
return 0
else:
return len(standalone_v)
def getIndicesStart(self, standalone_i):
if(len(standalone_i)==0):
return 0
else:
return len(standalone_i)
def getArrayStart(self):
return int(self.vertices_start/self.bufwidth)
def drawPolyboard(self, VAO, VBO, shader, view, projection, camera):
# TODO:
# implenent line width for the triangle loop with the polyboard
# routines from Mathematics for 3D Game Programming Third Edition
# Eric Lengyel... (search Polyboard keyword...)
shader.use()
shader.setFloat('near', camera.near)
shader.setFloat('far', camera.far)
shader.setVec3('camPos', camera.cam_pos )
shader.setMat4('model', self.model)
shader.setMat4('view', view)
shader.setMat4('projection', projection)
glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glDrawArrays(GL_LINE_STRIP_ADJACENCY, self.getArrayStart(), self.vcount)
#print('Testing...')
#
def drawLines(self, VAO, VBO, shader, view, projection):
shader.use()
shader.setMat4('model', self.model)
shader.setMat4('view', view)
shader.setMat4('projection', projection)
glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glDrawArrays(GL_LINE_LOOP, self.getArrayStart(), self.vcount)
#print('Testing...')
def drawFill(self, VAO, VBO, EBO, shader, view, projection):
shader.use()
shader.setMat4('model', self.model)
shader.setMat4('view', view)
shader.setMat4('projection', projection)
glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glDrawArrays(GL_TRIANGLES, self.getArrayStart(), self.vcount)
#print('Testing...')