Hey folks,
Not really a Nim question, I suppose, but since the code is Nim, I figured this was a good place to start.
I've wanted to learn a bit about how games are made, so I started to play with opengl this weekend. More specifically, I'm trying to use OpenGL and SDL2 together. Initially, I'm just trying to render a texture onto a quad. However, some vertical artifacts are showing up in the rendered content:
It's obviously supposed to be a checkerboard, but you can see the noise.
Here is the code I'm running, pruned down to a repro case:
import sdl2, opengl, glu
const WINDOW_WIDTH = 640
const WINDOW_HEIGHT = 480
proc loadTexture(path: string): GLuint =
## Loads a texture and returns the texture identifier
# Load a texture
let surface = loadBMP(path)
defer: surface.destroy
# Allocate a texture Id
glGenTextures(1, addr result)
glBindTexture(GL_TEXTURE_2D, result)
# Set the texture’s stretching properties
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
# Load the texture into opengl
let mode = if surface.format.BytesPerPixel == 4: GL_RGB else: GL_RGBA
glTexImage2D(
target = GL_TEXTURE_2D,
level = 0,
internalformat = GLint(mode.ord),
width = GLsizei(surface.w),
height = GLsizei(surface.h),
border = 0,
format = mode,
type = GL_UNSIGNED_BYTE,
pixels = surface.pixels)
proc quad[T: SomeNumber](texture: GLuint; bottomLeft, bottomRight, topLeft, topRight: tuple[x, y: T]): void =
## Draw a quad
glBindTexture(GL_TEXTURE_2D, texture)
glBegin(GL_QUADS)
glTexCoord2f(0, 0); glVertex2f(GLfloat(bottomLeft.x), GLfloat(bottomLeft.y))
glTexCoord2f(1, 0); glVertex2f(GLfloat(bottomRight.x), GLfloat(bottomRight.y))
glTexCoord2f(1, 1); glVertex2f(GLfloat(topRight.x), GLfloat(topRight.y))
glTexCoord2f(0, 1); glVertex2f(GLfloat(topLeft.x), GLfloat(topLeft.y))
glEnd()
proc main(): void =
sdl2.init(INIT_EVERYTHING)
defer: sdl2.quit()
let window = createWindow(
"test",
100, 100, WINDOW_WIDTH, WINDOW_HEIGHT,
SDL_WINDOW_SHOWN or SDL_WINDOW_OPENGL)
let context = window.glCreateContext()
# Initialize OpenGL
loadExtensions()
glEnable(GL_TEXTURE2D)
# Set up the viewport to fill the window
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
# Configure the camera
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, WINDOW_WIDTH / WINDOW_HEIGHT, 0.1, 100.0)
# Clear color and depth buffers
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
# Shift around the camera by moving all the things
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0, 0, -20.0)
# Load a single texture
let texture = loadTexture("resources/test.bmp")
# Draw a rectangle with that texture
quad(texture,
bottomLeft = (0, 0),
bottomRight = (10, 0),
topRight = (10, 10),
topLeft = (0, 10))
# Swap in the texture
window.glSwapWindow()
# Pause for 2 seconds
sdl2.delay(2000)
main()
I was surprised at how hard it was to find documentation around this. There is a lot about OpenGL, but nothing great about OpenGL with SDL2.
Anyway, thanks!