java - OpenGL only 1 triangle is drawn -
i relatively new @ opengl lwjgl stuff, , have thoroughly checked code, 1 triangle drawn, despite me putting in coords 2. prove point, 6 points drawn gl_points.
main.scala
import org.lwjgl._ import org.lwjgl.glfw.callbacks._ import org.lwjgl.glfw.glfw._ import org.lwjgl.opengl.gl11._ import org.lwjgl.system.memoryutil._ class main { import org.lwjgl.glfw.glfwerrorcallback import org.lwjgl.opengl.gl private var window = 0l def run(): unit = { system.out.println("version: " + version.getversion + "!") init() run_loop() glfwfreecallbacks(window) glfwdestroywindow(window) glfwterminate() glfwseterrorcallback(null).free() } private def init() = { glfwerrorcallback.createprint(system.err).set if (!glfwinit) throw new illegalstateexception("unable initialize glfw") glfwdefaultwindowhints() glfwwindowhint(glfw_visible, glfw_false) glfwwindowhint(glfw_resizable, glfw_true) window = glfwcreatewindow(640, 480, "window", 0, 0) if (window == null) throw new runtimeexception("failed create glfw window") glfwmakecontextcurrent(window) glfwswapinterval(1) glfwshowwindow(window) } def run_loop() { gl.createcapabilities glenable(gl_texture_2d) val vertices: array[float] = array( -0.5f, 0.5f, 0, //top left 0.5f, 0.5f, 0, //top right 0.5f, -0.5f, 0, //bottom right -0.5f, 0.5f, 0, //bottom right -0.5f, -0.5f, 0,//bottom left -0.5f, 0.5f, 0 //top left ) val model: model = model(vertices) while (!glfwwindowshouldclose(window)) { glfwpollevents() glclear(gl_color_buffer_bit | gl_depth_buffer_bit) if (glfwgetkey(window, glfw_key_escape) == gl_true) { glfwsetwindowshouldclose(window, true) } model.render() glfwswapbuffers(window) } } } object main { def main(args: array[string]): unit = { val main: main = new main main.run() } }
model.scala
import java.nio.floatbuffer import org.lwjgl.bufferutils import org.lwjgl.opengl.gl11._ import org.lwjgl.opengl.gl15._ case class model (vertices: array[float]){ private val draw_count: int = vertices.length / 3 private val v_id: int = glgenbuffers val buffer: floatbuffer = bufferutils.createfloatbuffer(vertices.length) buffer.put(vertices) buffer.flip() glbindbuffer(gl_array_buffer, v_id) glbufferdata(gl_array_buffer, buffer, gl_dynamic_draw) glbindbuffer(gl_array_buffer, 0) def render(): unit = { glenableclientstate(gl_vertex_array) glbindbuffer(gl_array_buffer, v_id) glvertexpointer(3, gl_float, 0, 0) gldrawarrays(gl_quads, 0, draw_count) gldrawarrays(gl_points, 0, draw_count) glbindbuffer(gl_array_buffer, 0) gldisableclientstate(gl_vertex_array) glpopmatrix() } }
your vertices
incorrect. if 0.5f, -0.5f
bottom right, how can -0.5f, 0.5f
bottom right?
val vertices: array[float] = array( -0.5f, 0.5f, 0, // top left 0.5f, 0.5f, 0, // top right 0.5f, -0.5f, 0, // bottom right 0.5f, -0.5f, 0, // bottom right -0.5f, -0.5f, 0, // bottom left -0.5f, 0.5f, 0 // top left )
you use gl_triangle_strip
along with:
val vertices: array[float] = array( -0.5f, 0.5f, 0, // top left 0.5f, 0.5f, 0, // top right -0.5f, -0.5f, 0, // bottom left 0.5f, -0.5f, 0, // bottom right )
or flip last 2 vertices , equally use gl_triangle_fan
.
also note every glpushmatrix()
there must glpopmatrix()
. since noticed have glpopmatrix()
no glpushmatrix()
.
Comments
Post a Comment