Skip to content

Commit

Permalink
[loader] "assets/cube.obj" was corrected in loading (previous loader …
Browse files Browse the repository at this point in the history
…was conflicting vertex normals, super-imposed, re-assigned)
  • Loading branch information
arkenidar committed Jan 13, 2024
1 parent 8952ffd commit 098fa98
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 100 deletions.
36 changes: 17 additions & 19 deletions loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,28 @@ function load_obj_file(file_path)
else
lines_iterator = io.lines(file_path)
end
local index_vertex = {}
local index_vertex_normal = {}
local indexable_vertex_position_xyz = {}
local indexable_vertex_normal_xyz = {}
for line in lines_iterator do
local type = line:match"(%S+) .+"
if type=="v" then
local type = line:match "(%S+) .+"
if type == "v" then
-- vertex
local x,y,z = line:match "v (%S+) (%S+) (%S+)"
local vertex={ x=tonumber(x), y=tonumber(y), z=tonumber(z) }
table.insert(index_vertex, vertex)
elseif type=="vn" then
local x, y, z = line:match "v (%S+) (%S+) (%S+)"
local vertex_position_xyz = { tonumber(x), tonumber(y), tonumber(z) }
table.insert(indexable_vertex_position_xyz, vertex_position_xyz)
elseif type == "vn" then
-- vertex normal
local x,y,z = line:match "vn (%S+) (%S+) (%S+)"
local vertex_normal={ x=tonumber(x), y=tonumber(y), z=tonumber(z) }
table.insert(index_vertex_normal, vertex_normal)
elseif type=="f" then
local x, y, z = line:match "vn (%S+) (%S+) (%S+)"
local vertex_normal_xyz = { tonumber(x), tonumber(y), tonumber(z) }
table.insert(indexable_vertex_normal_xyz, vertex_normal_xyz)
elseif type == "f" then
-- face (it's specific for triangulated-mesh: exactly 3 vertices)
local v1,vn1,v2,vn2,v3,vn3 = line:match "f (%d+)//(%d+) (%d+)//(%d+) (%d+)//(%d+)"
local triangle = { index_vertex[tonumber(v1)],
index_vertex[tonumber(v2)], index_vertex[tonumber(v3)] }
triangle[1].normal = index_vertex_normal[tonumber(vn1)]
triangle[2].normal = index_vertex_normal[tonumber(vn2)]
triangle[3].normal = index_vertex_normal[tonumber(vn3)]
local v1, vn1, v2, vn2, v3, vn3 = line:match "f (%d+)//(%d+) (%d+)//(%d+) (%d+)//(%d+)"
local triangle = {}
triangle.vertex_position_indices = { tonumber(v1), tonumber(v2), tonumber(v3) }
triangle.vertex_normal_indices = { tonumber(vn1), tonumber(vn2), tonumber(vn3) }
table.insert(triangles, triangle)
end
end
return triangles
return { triangles, indexable_vertex_position_xyz, indexable_vertex_normal_xyz }
end
165 changes: 84 additions & 81 deletions opengl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ gcc -E includes.c | grep -v '^#' > ffi_defs_gl.h
#include <GL/glu.h>
]]
ffi.cdef( io.open('ffi_defs_gl.h','r'):read('*a') )
ffi.cdef(io.open('ffi_defs_gl.h', 'r'):read('*a'))

local SDL = ffi.load('SDL2')

local libraries={}
libraries.GL={Linux='GL',Windows='openGL32'}
libraries.GLU={Linux='GLU',Windows='GLU32'}
local libraries = {}
libraries.GL = { Linux = 'GL', Windows = 'openGL32' }
libraries.GLU = { Linux = 'GLU', Windows = 'GLU32' }
local GL = ffi.load(libraries.GL[ffi.os])
local GLU = ffi.load(libraries.GLU[ffi.os])

--require("sdl-defs")(SDL)
require("opengl-defs")(GL,GLU,SDL)
require("opengl-defs")(GL, GLU, SDL)
--====================================

SDL_Init(0)

local window_width, window_height = 400, 300
local window = SDL_CreateWindow("opengl in sdl", 50,50, window_width, window_height, SDL_WINDOW_OPENGL)
local window = SDL_CreateWindow("opengl in sdl", 50, 50, window_width, window_height, SDL_WINDOW_OPENGL)

context = SDL_GL_CreateContext(window)

function update(mouse_position,mouse_down)
-- to be defined
function update(mouse_position, mouse_down)
-- to be defined
end

local bit = require("bit")
Expand All @@ -43,36 +43,35 @@ local binary_or = bit.bor
local angle = 0

function draw()

-- update scene

angle = angle + 1

-- draw scene

local lightPosition = ffi.new("float[4]",15,10,5,1)
local lightAmbient = ffi.new("float[4]",0.1,0.1,0.1,1)
local lightDiffuse = ffi.new("float[4]",0.9,0.9,0.9,1)
local lightPosition = ffi.new("float[4]", 15, 10, 5, 1)
local lightAmbient = ffi.new("float[4]", 0.1, 0.1, 0.1, 1)
local lightDiffuse = ffi.new("float[4]", 0.9, 0.9, 0.9, 1)

local redMaterial = ffi.new("float[4]",1,0,0,1)
local blueMaterial = ffi.new("float[4]",0,0,1,1)
local redMaterial = ffi.new("float[4]", 1, 0, 0, 1)
local blueMaterial = ffi.new("float[4]", 0, 0, 1, 1)

local greyMaterial = ffi.new("float[4]",0.5,0.5,0.5,1)
local greenMaterial = ffi.new("float[4]",0,1,0,1)
local greyMaterial = ffi.new("float[4]", 0.5, 0.5, 0.5, 1)
local greenMaterial = ffi.new("float[4]", 0, 1, 0, 1)

glViewport(0, 0, window_width, window_height);

glClearColor(1,1,1,0);
glClear( binary_or(GL_COLOR_BUFFER_BIT ,GL_DEPTH_BUFFER_BIT) );
glClearColor(1, 1, 1, 0);
glClear(binary_or(GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT));
glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30,window_width/window_height,1,200);
gluPerspective(30, window_width / window_height, 1, 200);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 20,5,40, 0,0,0, 0,1,0);
gluLookAt(20, 5, 40, 0, 0, 0, 0, 1, 0);

glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
Expand All @@ -90,7 +89,7 @@ function draw()

glPushMatrix();
local factor = 5
glScalef( factor, factor, factor );
glScalef(factor, factor, factor);
draw_model(model)
glPopMatrix();

Expand Down Expand Up @@ -137,36 +136,45 @@ function draw()
glRotated(angle, 0., 1., 0.);
drawBox(-.5,-.5,-.5,.5,.5,.5);
glPopMatrix();
--]]

glPopMatrix();

end

----------------------------------------------
require("loader")
-- "assets/cube.obj" was corrected in loading (previous loader was conflicting vertex normals, super-imposed, re-assigned)
-- "assets/head.obj" more complex and more memory intensive also (memory use improvement)
model = load_obj_file("assets/head.obj")

function draw_model(model)
--[[
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0.,0.,-1.);
glVertex3f(xmin, ymin, zmin);
glVertex3f(xmin, ymax, zmin);
glVertex3f(xmax, ymin, zmin);
glVertex3f(xmax, ymax, zmin);
glEnd();
--]]
local triangles, indexable_vertex_position_xyz, indexable_vertex_normal_xyz = model[1], model[2], model[3]
glBegin(GL_TRIANGLES)
for _, triangle in ipairs(triangles) do
local position_indices = triangle.vertex_position_indices
local normal_indices = triangle.vertex_normal_indices

-- vertex 1
local normal1 = indexable_vertex_normal_xyz[normal_indices[1]]
glNormal3f(normal1[1], normal1[2], normal1[3])

-- glNormal3f, glVertex3f
for _,triangle in ipairs(model) do
local function normal(xyz) glNormal3f(xyz.x, xyz.y, xyz.z) end
local function vertex(xyz) glVertex3f(xyz.x, xyz.y, xyz.z) end
local function vertex_and_normal(xyz) normal(xyz.normal); vertex(xyz) end
vertex_and_normal(triangle[1])
vertex_and_normal(triangle[2])
vertex_and_normal(triangle[3])
local position1 = indexable_vertex_position_xyz[position_indices[1]]
glVertex3f(position1[1], position1[2], position1[3])

-- vertex 2
local normal2 = indexable_vertex_normal_xyz[normal_indices[2]]
glNormal3f(normal2[1], normal2[2], normal2[3])

local position2 = indexable_vertex_position_xyz[position_indices[2]]
glVertex3f(position2[1], position2[2], position2[3])

-- vertex 3
local normal3 = indexable_vertex_normal_xyz[normal_indices[3]]
glNormal3f(normal3[1], normal3[2], normal3[3])

local position3 = indexable_vertex_position_xyz[position_indices[3]]
glVertex3f(position3[1], position3[2], position3[3])
end

glEnd()
Expand All @@ -176,53 +184,52 @@ end

-- // Draws a simple box using the given corners
function drawBox(xmin, ymin, zmin, xmax, ymax, zmax)

glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0.,0.,-1.);
glVertex3f(xmin, ymin, zmin);
glVertex3f(xmin, ymax, zmin);
glVertex3f(xmax, ymin, zmin);
glVertex3f(xmax, ymax, zmin);
glNormal3f(0., 0., -1.);
glVertex3f(xmin, ymin, zmin);
glVertex3f(xmin, ymax, zmin);
glVertex3f(xmax, ymin, zmin);
glVertex3f(xmax, ymax, zmin);
glEnd();

glBegin(GL_TRIANGLE_STRIP);
glNormal3f(1.,0.,0.);
glVertex3f(xmax, ymin, zmin);
glVertex3f(xmax, ymax, zmin);
glVertex3f(xmax, ymin, zmax);
glVertex3f(xmax, ymax, zmax);
glNormal3f(1., 0., 0.);
glVertex3f(xmax, ymin, zmin);
glVertex3f(xmax, ymax, zmin);
glVertex3f(xmax, ymin, zmax);
glVertex3f(xmax, ymax, zmax);
glEnd();

glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0.,0.,1.);
glVertex3f(xmax, ymin, zmax);
glVertex3f(xmax, ymax, zmax);
glVertex3f(xmin, ymin, zmax);
glVertex3f(xmin, ymax, zmax);
glNormal3f(0., 0., 1.);
glVertex3f(xmax, ymin, zmax);
glVertex3f(xmax, ymax, zmax);
glVertex3f(xmin, ymin, zmax);
glVertex3f(xmin, ymax, zmax);
glEnd();

glBegin(GL_TRIANGLE_STRIP);
glNormal3f(-1.,0.,0.);
glVertex3f(xmin, ymin, zmax);
glVertex3f(xmin, ymax, zmax);
glVertex3f(xmin, ymin, zmin);
glVertex3f(xmin, ymax, zmin);
glNormal3f(-1., 0., 0.);
glVertex3f(xmin, ymin, zmax);
glVertex3f(xmin, ymax, zmax);
glVertex3f(xmin, ymin, zmin);
glVertex3f(xmin, ymax, zmin);
glEnd();

glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0.,1.,0.);
glVertex3f(xmin, ymax, zmin);
glVertex3f(xmin, ymax, zmax);
glVertex3f(xmax, ymax, zmin);
glVertex3f(xmax, ymax, zmax);
glNormal3f(0., 1., 0.);
glVertex3f(xmin, ymax, zmin);
glVertex3f(xmin, ymax, zmax);
glVertex3f(xmax, ymax, zmin);
glVertex3f(xmax, ymax, zmax);
glEnd();

glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0.,-1.,0.);
glVertex3f(xmax, ymin, zmax);
glVertex3f(xmax, ymin, zmin);
glVertex3f(xmin, ymin, zmax);
glVertex3f(xmin, ymin, zmin);
glNormal3f(0., -1., 0.);
glVertex3f(xmax, ymin, zmax);
glVertex3f(xmax, ymin, zmin);
glVertex3f(xmin, ymin, zmax);
glVertex3f(xmin, ymin, zmin);
glEnd();
end

Expand All @@ -231,29 +238,25 @@ end
local event = ffi.new("SDL_Event")
local looping = true

local mouse_position={0,0}
local mouse_down=false
local mouse_position = { 0, 0 }
local mouse_down = false

while looping do

while SDL_PollEvent(event) ~= 0 do
if event.type == SDL_QUIT or
( event.type == SDL_KEYDOWN and event.key.keysym.sym == SDLK_ESCAPE )
(event.type == SDL_KEYDOWN and event.key.keysym.sym == SDLK_ESCAPE)
then
looping = false

looping = false
elseif event.type == SDL_MOUSEBUTTONDOWN then
mouse_down = true

elseif event.type == SDL_MOUSEBUTTONUP then
mouse_down = false

elseif event.type == SDL_MOUSEMOTION then
mouse_position = {event.button.x, event.button.y}
mouse_position = { event.button.x, event.button.y }
end
end

update(mouse_position,mouse_down)
update(mouse_position, mouse_down)

draw()

Expand Down

0 comments on commit 098fa98

Please sign in to comment.