unfortunately I missed the brief chance to get a copy of the code including necessaray structures, etc.
Following the OpenGL tutorial recommended in other forum posts (from DarkDragon), I made it all the way through creating VAOs, VBOs, EBOs as well as textures using the afore-mentioned and uniform values in shaders.
Trying to get into matrix calculation (rotate and translate) I'm kind of lost and am happy to have found this thread. Is there a chance to receive a complete copy of the code to fill the gaps, please? I'd appreciate it very much!
That's how far I've come in applying textures to a simple rectangle (made up of 2 triangles) and changing transparency (up/down) as well as size (left/right).
Code: Select all
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
; structures
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Global points_vbo.i, colors_vbo.i, vao.i, ebo.i, texture.i, transparenz.f
Structure s_positions
x.f
y.f
z.f
EndStructure
Structure s_colors
r.f
g.f
b.f
EndStructure
Structure s_texture
x.f
y.f
EndStructure
Structure s_obj
Pos.s_positions
Col.s_colors
Tex.s_texture
EndStructure
Structure Tindex
a.l
b.l
c.l
EndStructure
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
; Identity Matrix
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Dim ourTransform.f(3,3)
ourTransform(0,0)=1 : ourTransform(0,1)=0 : ourTransform(0,2)=0 : ourTransform(0,3)=0 ;// first column
ourTransform(1,0)=0 : ourTransform(1,1)=1 : ourTransform(1,2)=0 : ourTransform(1,3)=0 ;// second column
ourTransform(2,0)=0 : ourTransform(2,1)=0 : ourTransform(2,2)=1 : ourTransform(2,3)=0 ; // third column
ourTransform(3,0)=0 : ourTransform(3,1)=0 : ourTransform(3,2)=0 : ourTransform(3,3)=1 ;// fourth column
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
; vertex and index Arrays (Koordianten, Colors und Texture-Location)
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Global Dim Vertex.s_obj(4)
;// top right
Vertex(0)\Pos\x = 0.5 : Vertex(0)\Pos\y = 0.5 : Vertex(0)\Pos\z = 0.0
Vertex(0)\Col\r = 1.0 : Vertex(0)\Col\g = 0.0 : Vertex(0)\Col\b = 0.0
Vertex(0)\Tex\x = 1.0 : Vertex(0)\Tex\y = 1.0
; // bottom right
Vertex(1)\Pos\x = 0.5 : Vertex(1)\Pos\y = -0.5 : Vertex(1)\Pos\z = 0.0
Vertex(1)\Col\r = 0.0 : Vertex(1)\Col\g = 1.0 : Vertex(1)\Col\b = 0.0
Vertex(1)\Tex\x = 1.0 : Vertex(1)\Tex\y = 0.0
; // bottom left
Vertex(2)\Pos\x = -0.5 : Vertex(2)\Pos\y = -0.5 : Vertex(2)\Pos\z = 0.0
Vertex(2)\Col\r = 0.0 : Vertex(2)\Col\g = 0.0 : Vertex(2)\Col\b = 1.0
Vertex(2)\Tex\x = 0.0 : Vertex(2)\Tex\y = 0.0
; // top left
Vertex(3)\Pos\x = -0.5 : Vertex(3)\Pos\y = 0.5 : Vertex(3)\Pos\z = 0.0
Vertex(3)\Col\r = 1.0 : Vertex(3)\Col\g = 1.0 : Vertex(3)\Col\b = 0.0
Vertex(3)\Tex\x = 0.0 : Vertex(3)\Tex\y = 1.0
Global Dim Index.Tindex(2)
Index(0)\a = 0 : Index(0)\b = 1 : Index(0)\c = 3 ; // first triangle
Index(1)\a = 1 : Index(1)\b = 2 : Index(1)\c = 3 ; // second triangle
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
; pics for textures
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
UseJPEGImageDecoder()
UsePNGImageDecoder()
#ImagePath1 = #PB_Compiler_Home + "examples/3d/Data/Textures/"
LoadImage(1, #ImagePath1+"ValetCoeur.jpg") ;ValetCoeur.jpg")
*Buffer1 = EncodeImage(1)
#ImagePath2 = #PB_Compiler_Home + "examples/3d/Data/Textures/"
LoadImage(2, #ImagePath1+"smoke2.png")
*Buffer2 = EncodeImage(2)
transparenz.f = 0.0
Global minTransparenz.f = 0.0
Global maxTransparenz.f = 1.0
;**************************************************************************************
OpenWindow(0, 0, 0, 800, 600, "Tutorial Phase 7 - mehrere Texturen ")
OpenGLGadget(0, 10, 10, WindowWidth(0) , WindowHeight(0) , #PB_OpenGL_Keyboard)
;**************************************************************************************
IncludeFile "mypath\OpenGL_INCLUDE\Functions.pbi"
IncludeFile "mypath\OpenGL_INCLUDE\GLEXT.pbi"
;**************************************************************************************
glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
glEnable_(#GL_DEPTH_TEST); // enable depth-testing
glGenVertexArrays (1, @vao)
glGenBuffers(1, @points_vbo )
glGenBuffers(1, @ebo)
glBindVertexArray(vao)
glBindBuffer(#GL_ARRAY_BUFFER, points_vbo)
glBufferData(#GL_ARRAY_BUFFER,32 * ArraySize(Vertex()),@Vertex(0), #GL_STATIC_DRAW)
glBindBuffer(#GL_ELEMENT_ARRAY_BUFFER, ebo)
glBufferData(#GL_ELEMENT_ARRAY_BUFFER, 12*ArraySize(Index()), @Index(0), #GL_STATIC_DRAW)
; position attribute
glVertexAttribPointer(0, 3, #GL_FLOAT, #GL_FALSE, 8 * SizeOf(float), 0)
glEnableVertexAttribArray(0)
; color attribute
glVertexAttribPointer(1, 3, #GL_FLOAT, #GL_FALSE, 8 * SizeOf(float), 3 * SizeOf(float))
glEnableVertexAttribArray(1)
; Texture Coords attribute
glVertexAttribPointer(2, 2, #GL_FLOAT, #GL_FALSE, 8 * SizeOf(float), 6 * SizeOf(float))
glEnableVertexAttribArray(2);
; Textur 1
glGenTextures_(1, @texture1)
glBindTexture_(#GL_TEXTURE_2D, texture1)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_WRAP_S, #GL_CLAMP_TO_EDGE)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_WRAP_T, #GL_CLAMP_TO_EDGE)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)
glTexImage2D_(#GL_TEXTURE_2D, 0, #GL_RGB, ImageWidth(1), ImageHeight(1), 0, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, *Buffer1)
;glGenerateMipmap_(#GL_TEXTURE_2D)
FreeMemory(*Buffer1)
; Textur 2
glGenTextures_(2, @texture2)
glBindTexture_(#GL_TEXTURE_2D, texture2)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_WRAP_S, #GL_REPEAT)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_WRAP_T, #GL_REPEAT)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)
glTexImage2D_(#GL_TEXTURE_2D, 0, #GL_RGB, ImageWidth(2), ImageHeight(2), 0, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, *Buffer2)
;glGenerateMipmap_(#GL_TEXTURE_2D)
FreeMemory(*Buffer2)
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
; prepare shaders
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Define vertex_shader.s
Define fragment_shader.s
Define *fbuff
Define *vbuff
vertex_shader = "#version 330 core"+#CRLF$
vertex_shader + "layout(location = 0) in vec3 aPos;"+#CRLF$ ; the position variable has attribute position 0
vertex_shader + "layout(location = 1) in vec3 aColor;"+#CRLF$ ; the color variable has attribute position 1
vertex_shader + "layout(location = 2) in vec2 aTexCoord;"+#CRLF$ ; the color variable has attribute position 1
vertex_shader + "out vec3 ourColor;"+#CRLF$
vertex_shader + "out vec2 TexCoord;"+#CRLF$
vertex_shader + "uniform mat4 ourTransform;"+#CRLF$
vertex_shader + "void main() {"+#CRLF$
vertex_shader + "gl_Position = vec4(aPos, 1.0);"+#CRLF$
vertex_shader + "gl_Position = ourTransform * vec4(aPos, 1.0);"+#CRLF$
vertex_shader + "ourColor = aColor;"+#CRLF$
vertex_shader + "TexCoord = aTexCoord;"+#CRLF$
vertex_shader + "}"
fragment_shader = "#version 330 core"+#CRLF$
fragment_shader + "out vec4 FragColor;"+#CRLF$
fragment_shader + "in vec3 ourColor;"+#CRLF$
fragment_shader + "in vec2 TexCoord;"+#CRLF$
fragment_shader + "uniform float transparenz;"+#CRLF$
fragment_shader + "uniform sampler2D texture1;"+#CRLF$
fragment_shader + "uniform sampler2D texture2;"+#CRLF$
fragment_shader + "void main()"+#CRLF$
fragment_shader + "{"+#CRLF$
fragment_shader + " FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), transparenz);"+#CRLF$
;fragment_shader + " FragColor = texture(texture2, TexCoord);"+#CRLF$
fragment_shader + "}"+#CRLF$
*vbuff = Ascii(vertex_shader)
*fbuff = Ascii(fragment_shader)
Global vs = glCreateShader(#GL_VERTEX_SHADER);
glShaderSource(vs, 1, @*vbuff, #Null) ;
glCompileShader(vs)
Global fs = glCreateShader(#GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, @*fbuff, #Null);
glCompileShader(fs)
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
; create shader program
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Global shader_programme = glCreateProgram();
glAttachShader(shader_programme, vs)
glAttachShader(shader_programme, fs)
glLinkProgram(shader_programme)
glDeleteShader(vs)
glDeleteShader(fs)
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
; fixed uniform values for shader
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
glUseProgram(shader_programme) ; // don't forget to activate/use the shader before setting uniforms!
glUniform1i(glGetUniformLocation(shader_programme, "texture1"), 0) ; muss nur einmal festgelegt werden
glUniform1i(glGetUniformLocation(shader_programme, "texture2"), 1) ; die 1 hinten ist wichtig, sonst wird nur die erste Textur genommen!
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
; render loop
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
SetActiveGadget(0) ; make the openGLgadget active so the keyboard can be used with it
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget And EventGadget() = 0
If EventType() = #PB_EventType_KeyDown
key = GetGadgetAttribute(0,#PB_OpenGL_Key )
Select key
Case #PB_Shortcut_Up
If transparenz < 1
transparenz = transparenz + 0.01
EndIf
Case #PB_Shortcut_Down
If transparenz > 0
transparenz = transparenz - 0.01
EndIf
Case #PB_Shortcut_Left
ourTransform(3,3) + 0.01
Case #PB_Shortcut_Right
ourTransform(3,3) - 0.01
Case #PB_Shortcut_W; display wire Frame or solid frame
If fill = 0
glPolygonMode_(#GL_FRONT_AND_BACK, #GL_LINE )
fill = 1
Else
glPolygonMode_(#GL_FRONT_AND_BACK, #GL_FILL)
fill = 0
EndIf
Case #PB_Shortcut_Escape ; Esc key to exit
quit = 1
EndSelect
EndIf
EndIf
;**************************************************************************************
glClearColor_(0.5, 0.5, 0.3, 1.0)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glViewport_(0, 0, 800, 600)
glActiveTexture(#GL_TEXTURE0);
glBindTexture_(#GL_TEXTURE_2D, texture1)
glActiveTexture(#GL_TEXTURE1)
glBindTexture_(#GL_TEXTURE_2D, texture2)
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
; dynamic uniform values für shader
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
glUseProgram(shader_programme)
glUniform1f(glGetUniformLocation(shader_programme, "transparenz"), transparenz)
glUniformMatrix4fv(glGetUniformLocation(shader_programme, "ourTransform"), 1, #GL_FALSE, @ourTransform(0,0));
;**************************************************************************************
glBindVertexArray(vao)
glDrawElements_(#GL_TRIANGLES, 6, #GL_UNSIGNED_INT, 0 )
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Delay(16)
Until Event = #PB_Event_CloseWindow Or quit = 1