opengl: more funcs

This commit is contained in:
Mitchell Hashimoto
2022-04-02 20:35:39 -07:00
parent 63ce7114a4
commit 049004e6de
3 changed files with 46 additions and 8 deletions

View File

@ -22,7 +22,7 @@ pub fn main() !void {
window.setSizeCallback((struct { window.setSizeCallback((struct {
fn callback(_: glfw.Window, width: i32, height: i32) void { fn callback(_: glfw.Window, width: i32, height: i32) void {
std.log.info("set viewport {} {}", .{ width, height }); std.log.info("set viewport {} {}", .{ width, height });
gl.c.glViewport(0, 0, width, height); try gl.viewport(0, 0, width, height);
} }
}).callback); }).callback);
@ -46,12 +46,11 @@ pub fn main() !void {
defer tex.destroy(); defer tex.destroy();
var texbind = try tex.bind(gl.c.GL_TEXTURE_2D); var texbind = try tex.bind(gl.c.GL_TEXTURE_2D);
defer texbind.unbind(); defer texbind.unbind();
gl.c.glTexParameteri(gl.c.GL_TEXTURE_2D, gl.c.GL_TEXTURE_WRAP_S, gl.c.GL_REPEAT); try texbind.parameter(gl.c.GL_TEXTURE_WRAP_S, gl.c.GL_REPEAT);
gl.c.glTexParameteri(gl.c.GL_TEXTURE_2D, gl.c.GL_TEXTURE_WRAP_T, gl.c.GL_REPEAT); try texbind.parameter(gl.c.GL_TEXTURE_WRAP_T, gl.c.GL_REPEAT);
gl.c.glTexParameteri(gl.c.GL_TEXTURE_2D, gl.c.GL_TEXTURE_MIN_FILTER, gl.c.GL_LINEAR); try texbind.parameter(gl.c.GL_TEXTURE_MIN_FILTER, gl.c.GL_LINEAR);
gl.c.glTexParameteri(gl.c.GL_TEXTURE_2D, gl.c.GL_TEXTURE_MAG_FILTER, gl.c.GL_LINEAR); try texbind.parameter(gl.c.GL_TEXTURE_MAG_FILTER, gl.c.GL_LINEAR);
gl.c.glTexImage2D( try texbind.image2D(
gl.c.GL_TEXTURE_2D,
0, 0,
gl.c.GL_RGB, gl.c.GL_RGB,
imgwidth, imgwidth,
@ -61,7 +60,7 @@ pub fn main() !void {
gl.c.GL_UNSIGNED_BYTE, gl.c.GL_UNSIGNED_BYTE,
data, data,
); );
gl.c.glGenerateMipmap(gl.c.GL_TEXTURE_2D); texbind.generateMipmap();
// Create our vertex shader // Create our vertex shader
const vs = try gl.Shader.create(gl.c.GL_VERTEX_SHADER); const vs = try gl.Shader.create(gl.c.GL_VERTEX_SHADER);

View File

@ -13,6 +13,41 @@ pub const Binding = struct {
c.glBindTexture(b.target, 0); c.glBindTexture(b.target, 0);
b.* = undefined; b.* = undefined;
} }
pub fn generateMipmap(b: Binding) void {
c.glGenerateMipmap(b.target);
}
pub fn parameter(b: Binding, name: c.GLenum, value: anytype) !void {
switch (@TypeOf(value)) {
c.GLint => c.glTexParameteri(b.target, name, value),
else => unreachable,
}
}
pub fn image2D(
b: Binding,
level: c.GLint,
internal_format: c.GLint,
width: c.GLsizei,
height: c.GLsizei,
border: c.GLint,
format: c.GLenum,
typ: c.GLenum,
data: *const anyopaque,
) !void {
c.glTexImage2D(
b.target,
level,
internal_format,
width,
height,
border,
format,
typ,
data,
);
}
}; };
/// Create a single texture. /// Create a single texture.

View File

@ -13,3 +13,7 @@ pub fn drawArrays(mode: c.GLenum, first: c.GLint, count: c.GLsizei) !void {
c.glDrawArrays(mode, first, count); c.glDrawArrays(mode, first, count);
try errors.getError(); try errors.getError();
} }
pub fn viewport(x: c.GLint, y: c.GLint, width: c.GLsizei, height: c.GLsizei) !void {
c.glViewport(x, y, width, height);
}