how do i properly use shaders with blitzmax + opengl?
👉 BlitzCoder will be building a new platform and other plans to preserve and continue the Blitz legacy.
To be able to achieve this goal, we need your support by becoming a Patreon Paid Member 👈
Tweet blitzmax opengl shaders
title describes my issue, i don't know how to properly compile shaders in blitzmax
every single time i do it, it doesn't work
here's my code, it's probably not the best, but that isn't really important to the question (other than the code for compiling shaders)
SuperStrict
Type TModel
Field vbuf:Int
Field cbuf:Int
Field tbuf:Int
Field vsz:Int
Field tsz:Int
Field pos:Float[]
Field rot:Float[]
Method New(varr:Float[],tarr:Float[])
Local carr:Float[] = New Float[Len(varr)]
Local i:Int
For i = 0 To Len(carr) -1
carr[i] = RndFloat()
Next
glGenBuffers(1,Varptr Self.vbuf)
glGenBuffers(1,Varptr Self.cbuf)
glGenBuffers(1,Varptr Self.tbuf)
glBindBuffer(GL_ARRAY_BUFFER,Self.vbuf)
glBufferData(GL_ARRAY_BUFFER,Int(SizeOf(varr)),varr,GL_DYNAMIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER,Self.cbuf)
glBufferData(GL_ARRAY_BUFFER,Int(SizeOf(carr)),carr,GL_DYNAMIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER,Self.tbuf)
glBufferData(GL_ARRAY_BUFFER,Int(SizeOf(tarr)),tarr,GL_DYNAMIC_DRAW)
Self.vsz = Int(SizeOf(varr)/4/3)
Self.tsz = Int(SizeOf(tarr)/4/2)
Self.pos = New Float[3]
Self.rot = New Float[3]
End Method
Method Draw()
glLoadIdentity
glTranslatef pos[0],pos[1],pos[2]
glRotatef rot[0],1,0,0
glRotatef rot[1],0,1,0
glRotatef rot[2],0,0,1
glBindBuffer(GL_ARRAY_BUFFER,Self.vbuf)
glVertexPointer(3,GL_FLOAT,0,Null)
glEnableClientState(GL_VERTEX_ARRAY)
'glBindBuffer(GL_ARRAY_BUFFER,Self.cbuf)
'glColorPointer(3,GL_FLOAT,0,Null)
glBindBuffer(GL_ARRAY_BUFFER,Self.tbuf)
glTexcoordPointer(2,GL_FLOAT,0,Null)
glEnableClientState(GL_VERTEX_ARRAY)
'glEnableClientState(GL_COLOR_ARRAY)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glDrawArrays(GL_TRIANGLES, 0, vsz)
glDisableClientState(GL_VERTEX_ARRAY)
'glDisableClientState(GL_COLOR_ARRAY)
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
glFlush()
End Method
End Type
Function LoadModel:TModel(path:String)
Local stream:TStream,str:String
Local vlst:Float[] = New Float[0]
Local tlst:Float[] = New Float[0]
Local finaltlst:Float[] = New Float[0]
Local faces:String[] = New String[0]
Local finalvlst:Float[] = New Float[0]
stream = ReadStream(path)
While Not Eof(stream)
str = ReadLine(stream)
Select Left(str,Instr(str," ",1)-1)
Case "v"
Print "vert cmd"
vlst = vlst[..Len(vlst)+3]
vlst[Len(vlst)-3] = Float(Mid(str,Instr(str," ",1),Instr(str," ",Instr(str," ",1)+1)-1))
vlst[Len(vlst)-2] = Float(Mid(str,Instr(str," ",Instr(str," ",1)+1),Instr(str," ",Instr(str," ",Instr(str," ",1)+1)-1)-2))
vlst[Len(vlst)-1] = Float(Right(str,Instr(str," ",Instr(str," ",Instr(str," ",1)+1)-1)-2))
Case "vt"
Print "texcoord cmd"
tlst = tlst[..Len(tlst)+2]
tlst[Len(tlst)-2]=Float(Mid(str,Instr(str," ",1),Instr(str," ",Instr(str," ",1)+1)-1))
tlst[Len(tlst)-1]=Float(Right(str,Instr(str," ",Instr(str," ",1)+1)-4))
Print Right(str,Instr(str," ",Instr(str," ",1)+1)-4)
Case "f"
Print "face cmd"
faces = faces[..Len(faces)+3]
faces[Len(faces)-3] = Mid(str,Instr(str," ",1),Instr(str," ",Instr(str," ",1)+1)-1)
faces[Len(faces)-2] = Mid(str,Instr(str," ",Instr(str," ",1)+1),Instr(str," ",Instr(str," ",Instr(str," ",1)+1)-1)-2)
faces[Len(faces)-1] = Right(str,Instr(str," ",Instr(str," ",Instr(str," ",1)+1)-1)-2)
Default
End Select
Wend
Local i:Int
For i = 0 To Len(vlst)-1
If i Mod 3 = 0 Then Print i / 3
Print vlst[i]
Next
For i = 0 To Len(tlst)-1
If i Mod 2 = 0 Then Print i / 2
Print tlst[i]
Next
For i = 0 To Len(faces)-1
finalvlst = finalvlst[..Len(finalvlst)+3]
finaltlst = finaltlst[..Len(finaltlst)+2]
Print Trim(faces[i])
Local idx:Int = Int(Trim(Left(faces[i],Instr(faces[i],"/")-1)))
Local idx2:Int = Int(Trim(Left(faces[i],Instr(faces[i],"/",Instr(faces[i],"/")+1)-1)))
finalvlst[Len(finalvlst)-3] = vlst[(idx*3)-3]
finalvlst[Len(finalvlst)-2] = vlst[(idx*3)-2]
finalvlst[Len(finalvlst)-1] = vlst[(idx*3)-1]
finaltlst[Len(finaltlst)-2] = tlst[(idx2*2)-2]
finaltlst[Len(finaltlst)-1] = tlst[(idx2*2)-1]
Next
Return New TModel(finalvlst,finaltlst)
End Function
SetGraphicsDriver GLGraphicsDriver()
Graphics 640,400
SeedRnd MilliSecs()
glewinit() ' inits glew (usually unlisted command required for gl vbos)
glEnable GL_DEPTH_TEST
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 45.0,Float(640)/Float(400),1.0,100.0
glMatrixMode GL_MODELVIEW
glLoadIdentity
Local vertShader:String = LoadText("a.vert")
Local fragShader:String = LoadText("a.frag")
Print vertShader
Print fragShader
Local j:Int
Local vertSource:Byte Ptr = MemAlloc(SizeOf(vertShader))
Local fragSource:Byte Ptr = MemAlloc(SizeOf(fragShader))
For j = 0 To Len(vertShader)-1
vertSource[j] = vertShader[j]
Next
For j = 0 To Len(fragShader)-1
fragSource[j] = fragShader[j]
Next
Local vertID:Int = glCreateShader(GL_VERTEX_SHADER)
Local fragID:Int = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(vertID, 1, Varptr vertSource, Null)
glCompileShader(vertID)
glShaderSource(fragID, 1, Varptr fragSource, Null)
glCompileShader(fragID)
Global shaderProg:Int = glCreateProgram()
glAttachShader(shaderprog, vertID)
glAttachShader(shaderProg, fragID)
glLinkProgram(shaderProg)
Local result:Int
glGetProgramiv(shaderProg, GL_LINK_STATUS, Varptr result)
Print result
Global mdl2:TModel = LoadModel("./cube.obj")
Global tex:Int
Global texpixmap:TPixmap = LoadPixmapPNG("./flesheye.png")
Global texpixels:Byte[texpixmap.width*texpixmap.height]
For j = 0 To (texpixmap.width * texpixmap.height)-1
texpixels[j] = texpixmap.pixels[j]
Next
glGenTextures(1,Varptr tex)
glBindTexture(GL_TEXTURE_2D,tex)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, texpixels)
glGenerateMipmap(GL_TEXTURE_2D)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D,tex)
Global frames:Float = 0.0
While Not KeyHit( KEY_ESCAPE ) Or AppTerminate()
frames = frames + 3.0
glClear GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT 'clear screen and depth
'glUseProgram(shaderProg)
mdl2.pos = [0.0,0.0,-4.0]
mdl2.rot = [0.0,frames,0.0]
mdl2.Draw()
Flip
Wend
and here's the vert and frag shaders
//a.vert
#version 330 core
layout(location = 0) in vec3 vertPos;
void main(){
gl_Position.xyz = vertPos;
gl_Position.w = 1.0;
}
//a.frag
#version 330 core
out vec3 color;
void main(){
color = vec3(1,1,1);
}
nevermind, it was just the way i was converting the strings to byte pointers.
i was using a for loop to manually convert it, but i should've used string.ToCSTring()
to convert it. (for the people who stumble across this in the future.
Reply To Topic (minimum 10 characters)
Please log in to reply