summaryrefslogtreecommitdiff
path: root/examples/rlgl_hello_triangle/main.lua
blob: 571acb0abfd0d0b86d6bca762793e9965e20896b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"

Util = require( "utillib" )
Vec2 = require( "vector2" )

local res = Vec2:new( 1024, 720 )
local winScale = 1
local winSize = res:scale( winScale )
local monitor = 0

local triangle = {
	texture = {
		id = -1,
		data = nil,
		size = Vec2:new(),
		mipmaps = 0,
		format = 0,
	},
	vao = -1,
	vbos = {
		positions = -1,
		texcoords = -1,
		colors = -1,
	},
}

local vertexShader = [[
	#version 330 core
	layout (location = 0) in vec3 aPos;
	layout (location = 1) in vec4 aColor;
	layout (location = 2) in vec2 aTexCoord;
	
	out vec4 flagColor;
	out vec2 fragTexCoord;
	
	void main() {
		flagColor = aColor;
		fragTexCoord = aTexCoord;
		gl_Position = vec4( aPos, 1.0 );
	}
]]
local fragmentShader = [[
	#version 330 core
	out vec4 FragColor;
	
	in vec4 flagColor;
	in vec2 fragTexCoord;

	uniform sampler2D ourTexture;

	void main() {
		FragColor = texture( ourTexture, fragTexCoord ) * flagColor;
	}
]]

local shaderProgram = -1

-- Let's make our own custom texture.
local function loadTexture( path )
	local image = RL.LoadImage( path )

	triangle.texture.data = RL.GetImageData( image )
	triangle.texture.size = Vec2:new( RL.GetImageSize( image ) )
	triangle.texture.format = RL.GetImageFormat( image )
	triangle.texture.mipmaps = RL.GetImageMipmaps( image )

	triangle.texture.id = RL.rlLoadTexture(
		triangle.texture.data,
		triangle.texture.size,
		triangle.texture.format,
		triangle.texture.mipmaps
	)
end

local function createTriangle()
	loadTexture( RL.GetBasePath().."../resources/images/monkey_tex.png" )
	-- Set texture to shader uniform.
	local shaderLoc = RL.rlGetLocationUniform( shaderProgram, "ourTexture" )

	RL.rlEnableShader( shaderProgram )
	-- NOTE: Default texture is always activated as GL_TEXTURE0 so our slot will be 1.
	RL.rlSetUniformSampler( shaderLoc, triangle.texture.id )
	RL.rlDisableShader()

	-- Setup triangle buffers.
	triangle.vao = RL.rlLoadVertexArray()
	RL.rlEnableVertexArray( triangle.vao )

	-- Positions.
	local vertexBuffer = RL.LoadBuffer(
		{
			-0.5, -0.5, 0.0,
			0.5, -0.5, 0.0,
			0.0,  0.5, 0.0
		},
		RL.BUFFER_FLOAT
	)
	triangle.vbos.positions = RL.rlLoadVertexBuffer( vertexBuffer, false )
	RL.rlSetVertexAttribute( 0, 3, RL.RL_FLOAT, false, 0, 0 )
	RL.rlEnableVertexAttribute( 0 )

	-- Colors.
	local colors = RL.LoadBuffer(
		{
			1, 0, 0, 1,
			0, 1, 0, 1,
			0, 0, 1, 1
		},
		RL.BUFFER_FLOAT
	)
	triangle.vbos.colors = RL.rlLoadVertexBuffer( colors, false )
	RL.rlSetVertexAttribute( 1, 4, RL.RL_FLOAT, false, 0, 0 )
	RL.rlEnableVertexAttribute( 1 )

	-- Texcoords.
	local texcoors = RL.LoadBuffer(
		{
			0, 0,
			1, 0,
			0.5, 1,
		},
		RL.BUFFER_FLOAT
	)
	triangle.vbos.texcoors = RL.rlLoadVertexBuffer( texcoors, false )
	RL.rlSetVertexAttribute( 2, 2, RL.RL_FLOAT, false, 0, 0 )
	RL.rlEnableVertexAttribute( 2 )

	-- Disable.
	RL.rlDisableVertexBuffer()
	RL.rlDisableVertexArray()
end

function RL.init()
	local monitorPos = Vec2:new( RL.GetMonitorPosition( monitor ) )
	local monitorSize = Vec2:new( RL.GetMonitorSize( monitor ) )

	RL.SetWindowTitle( "RLGL Hello Triangle" )
	RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )
	RL.SetWindowState( RL.FLAG_VSYNC_HINT )
	RL.SetWindowSize( winSize )
	RL.SetWindowPosition( { monitorPos.x + monitorSize.x / 2 - winSize.x / 2, monitorPos.y + monitorSize.y / 2 - winSize.y / 2 } )

	RL.rlViewport( { 0, 0, res.x ,res.y } )
	shaderProgram = RL.rlLoadShaderCode( vertexShader, fragmentShader )

	createTriangle()
end

local function drawTriangle()
	-- Texture slot 0 is the default texture.
	RL.rlActiveTextureSlot( 1 )
	RL.rlEnableTexture( triangle.texture.id )

	RL.rlEnableShader( shaderProgram )

	RL.rlEnableVertexArray( triangle.vao )
	RL.rlDrawVertexArray( 0, 3 )

	-- Disable.
	RL.rlDisableVertexArray()
	RL.rlDisableVertexBuffer()
	RL.rlDisableTexture()
	RL.rlDisableShader()
end

function RL.draw()
	RL.ClearBackground( RL.BLACK )
	drawTriangle()
end

function RL.exit()
	RL.rlUnloadVertexArray( triangle.vao )
	RL.rlUnloadVertexBuffer( triangle.vbos.positions )
	RL.rlUnloadVertexBuffer( triangle.vbos.colors )
	RL.rlUnloadVertexBuffer( triangle.vbos.texcoords )
	RL.rlUnloadTexture( triangle.texture.id )
end