#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 mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
ourColor = aColor;
}
#version 330 core
in vec4 aColor;
out vec4 FragColor;
void main()
{
FragColor = aColor;
}
#version 330 core
layout (lines_adjacency) in;
layout(points, max_vertices = 32) out;
out vec4 aColor;
vec4 intersection(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
float px = ((x1*y1-y1*x2)*(x3-x4))-((x1-x2)*(x3*y4-y3*x4)) /
((x1-x2)*(y3-y4))-((y1-y2)*(x3-x4));
float py = ((x1*y2-y1*x2)*(y3-y4))-((y1-y2)*(x3*y4-y3*x4)) /
((x1-x2)*(y3-y4))-((y1-y2)*(x3-x4));
vec4 r = vec4(px,py,0.0,1.0);
return r;
}
void main() {
vec4 viewport = vec4(1920,1080,0,1.0);
vec4 p1 = gl_in[0].gl_Position;
vec4 p2 = gl_in[1].gl_Position;
vec4 p3 = gl_in[2].gl_Position;
vec4 p4 = gl_in[3].gl_Position;
vec4 line_forward = p2 - p1;
line_forward = normalize(line_forward);
vec4 line_forward_perpd = vec4(-line_forward.y,line_forward.x,0.0,1.0);
line_forward_perpd = normalize(line_forward_perpd);
line_forward_perpd = line_forward_perpd * .1;
vec4 line_backward = p3 - p2;
line_backward = normalize(line_backward);
vec4 line_backward_perpd = vec4(-line_backward.y,line_backward.x,0.0,1.0);
line_backward_perpd = normalize(line_backward_perpd);
line_backward_perpd = line_backward_perpd * .1;
aColor = vec4(1.0,0.0,0.0,1.0);
gl_Position = p1;
EmitVertex();
aColor = vec4(0.0,1.0,0.0,1.0);
vec4 p1inner = p1 + line_forward_perpd;
gl_Position = p1inner;
EmitVertex();
aColor = vec4(0.0,1.0,1.0,1.0);
vec4 p1outer = p1 - line_forward_perpd;
gl_Position = p1outer;
EmitVertex();
aColor = vec4(1.0,0.0,0.0,1.0);
gl_Position = p2;
EmitVertex();
aColor = vec4(1.0,0.0,0.0,1.0);
gl_Position = p3;
EmitVertex();
aColor = vec4(0.0,1.0,0.0,1.0);
vec4 p3inner = p3 + line_backward_perpd;
gl_Position = p3inner;
EmitVertex();
aColor = vec4(0.0,1.0,1.0,1.0);
vec4 p3outer = p3 - line_backward_perpd;
gl_Position = p3outer;
EmitVertex();
vec4 p1lp = (p1inner + (line_forward * .1));
vec4 p3lp = (p3inner - (line_backward * .1));
aColor = vec4(1.0,1.0,0.0,1.0);
gl_Position = p1lp;
EmitVertex();
aColor = vec4(1.0,0.0,1.0,1.0);
gl_Position = p3lp;
EmitVertex();
//vec4 cx = intersection(p1inner.x,p1inner.y,p1lp.x,p1lp.y,p3inner.x,p3inner.y,p3lp.x,p3lp.y);
//aColor = vec4(1.0,0.0,1.0,1.0);
// gl_Position = cx;
// EmitVertex();
}
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.PointList import PointList
from includes.Circle import Circle
from includes.Rectangle import Rectangle
from includes.Shader import Shader
def main() -> int:
init_glfw()
init_shaders()
init_objects()
init_buffers()
init_matrices()
render()
glfwTerminate()
return 0
def init_matrices():
global view
global projection
view = glm.mat4(1.0)
view = glm.lookAt( glm.vec3(0.0, 0.0, 600.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('debugme')
def init_shaders():
global shader_rect_texture
global shader_rect_fill
global shader_circle_lines
global shader_circle_fill
global shader_circle_thick
global shader_circle_thick_gs
shader_rect_texture = Shader('shaders/Rectangle_Texture.vs', 'shaders/Rectangle_Texture.fs')
shader_rect_fill = Shader('shaders/Rectangle_Fill.vs', 'shaders/Rectangle_Fill.fs')
shader_circle_lines = Shader('shaders/Circle.vs', 'shaders/Circle.fs')
shader_circle_fill = Shader('shaders/Circle_Fill.vs', 'shaders/Circle_Fill.fs')
shader_circle_thick_gs = Shader('shaders/ThickLines.vs', 'shaders/ThickLines.fs',
'shaders/ThickLines.gs')
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:
global SCR_WIDTH
global SCR_HEIGHT
global projection
SCR_WIDTH = width
SCR_HEIGHT = height
glfwSetWindowAspectRatio(window, int(SCR_WIDTH), int(SCR_HEIGHT))
projection = glm.mat4(1.0)
projection = glm.ortho((-SCR_WIDTH/2),(SCR_WIDTH/2),(-SCR_HEIGHT/2),(SCR_HEIGHT/2),0.1,1000)
glViewport(0, 0, width, height)
def render():
while (not glfwWindowShouldClose(window)):
processInput(window)
glClearColor(0.2, 0.3, 0.3, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
#circle001.drawLines(VAO,VBO,shader_circle_lines,view,projection)
#circle002.drawLines(VAO,VBO,shader_circle_lines,view,projection)
#circle003.drawLines(VAO,VBO,shader_circle_lines,view,projection)
circle001.drawLinesThick2(VAO,VBO,shader_circle_thick_gs,view,projection)
#circle002.drawLinesThick2(VAO,VBO,shader_circle_thick_gs,view,projection)
#circle003.drawLinesThick2 (VAO,VBO,shader_circle_thick_gs,view,projection)
glfwSwapBuffers(window)
glfwPollEvents()
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)
glfwSetWindowAspectRatio(window, int(SCR_WIDTH), int(SCR_HEIGHT))
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
aspect = SCR_WIDTH/SCR_HEIGHT
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT)
def init_objects():
global standalone_v
global standalone_i
global standalone_ssbo
standalone_v = glm.array.zeros(0,glm.float32)
standalone_i = glm.array.zeros(0,glm.uint32)
standalone_ssbo = glm.array.zeros(0,glm.vec4)
segments = 46
radius = 600
global circle001
circle001 = Circle(segments, radius, standalone_v, standalone_i, 1)
standalone_v = standalone_v.concat(circle001.vertices)
standalone_i = standalone_i.concat(circle001.indices)
array = circle001.getVec4s(1)
standalone_ssbo = standalone_ssbo.concat(array)
print('debug')
def init_buffers():
global VAO
global VBO
global EBO
global SSBO
global standalone_v
global standalone_i
global standalone_ssbo
VAO = glGenVertexArrays(1)
VBO = glGenBuffers(1)
EBO = glGenBuffers(1)
SSBO = 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)
glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO )
glBufferData(GL_SHADER_STORAGE_BUFFER, glm.sizeof(glm.vec4) * (len(standalone_ssbo)), standalone_ssbo.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)
main()
import glm
import math
import sys
import os
sys.path.append(os.getcwd())
from includes.Shader import Shader
from OpenGL.GL import *
import glm
class Circle:
def __init__(self,segments, radius, standalone_v, standalone_i, ssbo_start = None):
self.bufwidth = 8
self.vertices_start = self.getVerticesStart(standalone_v)
self.indices_start = self.getIndicesStart(standalone_i)
self.ssbo_start = ssbo_start-1
self.ssbo_end = 0
self.ssbo_length = 0
self.segments = segments
self.vertices = glm.array.zeros((self.segments*self.bufwidth)+(self.bufwidth*2),glm.float32)
self.radius = radius
self.ssbosize = 0
self.theta = 0.0
self.gradient = math.pi*2/self.segments
self.defcolor = [1.0,0.0,0.0]
self.defz = 0.0
self.pointsize = 5.0
self.origin = [0.0,0.0,self.defz]
self.count = int((len(self.vertices)/self.bufwidth)) - 1
self.model = glm.mat4(1.0)
self.indices = glm.array.zeros( ((self.segments*self.bufwidth)+self.bufwidth), glm.uint32)
self.N = 0
for i in range(0,(self.segments*self.bufwidth)+self.bufwidth, self.bufwidth):
self.vertices[i] = self.radius * math.cos(self.theta)
self.vertices[i+1] = self.radius * math.sin(self.theta)
self.vertices[i+2] = self.defz
self.vertices[i+3] = self.defcolor[0]
self.vertices[i+4] = self.defcolor[1]
self.vertices[i+5] = self.defcolor[2]
self.vertices[i+6] = 0.0
self.vertices[i+7] = 0.0
self.theta = self.theta + self.gradient
endi = self.segments*self.bufwidth+self.bufwidth
self.vertices[endi] = self.origin[0]
self.vertices[endi+1] = self.origin[1]
self.vertices[endi+2] = self.origin[2]
self.vertices[endi+3] = self.defcolor[0]
self.vertices[endi+4] = self.defcolor[1]
self.vertices[endi+5] = self.defcolor[2]
self.vertices[endi+6] = 0.0
self.vertices[endi+7] = 0.0
count = 0
for i in range(0,(self.count-1)*3, 3):
self.indices[i] = self.count
self.indices[i+1] = count
self.indices[i+2] = count + 1
count = count + 1
print('testing...')
def getVec4s(self, start):
size = self.segments
array = glm.array.zeros(size+3,glm.vec4)
count = 1
u = -8
for i in range(8,(size*8)+(8*3),8):
u = u + 8
a = u*math.pi/180.0
c = math.cos(a) * self.radius
s = math.sin(a) * self.radius
array[count] = glm.vec4(c,s,self.defz,1.0)
count = count + 1
#print(u)
self.ssbo_end = int(start) + len(array) -1
self.ssbo_length = len(array)
return array
def getSSBOStart(self, standalone_ssbo):
if(standalone_ssbo==0):
return 0
else:
self.N = standalone_ssbo.nbytes/8
return standalone_ssbo.nbytes/8
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) + 1
def getArrayStart(self):
return int(self.vertices_start/self.bufwidth)
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)
glPointSize(self.pointsize)
glDrawArrays(GL_LINE_STRIP, self.getArrayStart(), self.count)
def drawLinesThick2(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)
glPointSize(self.pointsize)
glDrawArrays(GL_LINE_STRIP_ADJACENCY, self.getArrayStart(), 4)
def drawTriangles(self, VAO, VBO, EBO, shader, view, projection):
# set up triangles to draw a disc or a filled circle as we may call it....
shader.use()
shader.setMat4('model', self.model)
shader.setMat4('view', view)
shader.setMat4('projection', projection)
glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
#glBindTexture(GL_TEXTURE_2D, self.texture)
glDrawElements(GL_TRIANGLES, len(self.indices), GL_UNSIGNED_INT,
ctypes.c_void_p(self.indices_start * glm.sizeof(glm.uint32)))
print('debug me...')
In regards to:
vec4 p1lp = (p1inner + (line_forward * .1));
vec4 p3lp = (p3inner - (line_backward * .1));
So far what I am trying to accomplish is to project p1inner some distance along line_forward and p3inner some distance along line_backward. I did get the points to come out so I can see them with EmnitVertex and they look OK. I don’t really understand why I am having to multiply by .1 to get them to show up. So far what I would like to do from here is get the intersection of the two inner lines however no luck yet. The intersection function I have seems to be working in cases just not the one I need.
Is there some way to get the intersection of the two lines from p1inner to p1lp and respectively from p3inner to p3lp?
(Also could I get maybe a little explanation if I had missed some transform with the w co-ordinate or by the viewport vector?)