There is no simple way to explain this but basically my program once in a while gets a triangle drawn on screen but many times I load it up and it doesn’t.
Right now I am trying to learn the basics of on screen coordinates and just basic primitive drawing. I am starting in 3.3 OpenGL just so I don’t learn out of date things with 2.0 or lower programs.
My thinking of the problem lies with the fact that I am not using transforms at the moment BUT I am trying to just spit things out on screen with base OpenGL screen coordinates which I think is possible? AKA Make a triangle onscreen that won’t move like I see in many tutorials. I don’t have a link but this is basically a slightly modified tutorial. Here is my code
Apologies ahead of time because it is in JAVA and in LWJGL but it matches up with c and c++ about 99%. It will look slightly different but it uses the exact same commands. Just wondering what I am doing wrong and if what I am trying to do is not possible without transforms which I am still learning.
testvertex[0] = 0.0f;
testvertex[1] = 0.0f;
testvertex[2] = -1.0f;
testvertex[3] = -1.0f;
testvertex[4] = 0.75f;
testvertex[5] = 0.75f;
ByteBuffer bytevertex = ByteBuffer.allocateDirect(24);
FloatBuffer vertexbuff = bytevertex.asFloatBuffer();
vertexbuff.put(testvertex);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glClear(GL_DEPTH_BUFFER_BIT);
glDisable(GL_CULL_FACE);
glDepthRange(0, 1);
glDepthFunc(GL_ALWAYS);
glEnable(GL_DEPTH_TEST);
programid = glCreateProgram();
vertshader = glCreateShader(GL_VERTEX_SHADER);
fragshader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertshader,
"#version 330 core
in vec2 inPosition;
void main() { gl_Position =
vec4(inPosition, 0.0, 1.0); }");
glShaderSource(fragshader,
"#version 330 core
out vec4 fragcolor;
void main() { fragcolor =
vec4(0.0, 1.0, 0.0, 1.0); }");
glCompileShader(vertshader);
glCompileShader(fragshader);
glAttachShader(programid, vertshader);
glAttachShader(programid, fragshader);
glBindAttribLocation(programid, 0, "inPosition");
glLinkProgram(programid);
glUseProgram(programid);
idVAO[0] = glGenVertexArrays();
glBindVertexArray(idVAO[0]);
idVBO[0] = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, idVBO[0]);
glBufferData(GL_ARRAY_BUFFER, vertexbuff, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
int frames = 0;
//Draw code here in loop the frames == 20,000 is to make
a simple quick way of not drawing super fast for no reason.
while (!Display.isCloseRequested())
{
if(frames == 20000)
{
glBindVertexArray(idVAO[0]);
glDrawArrays(GL_TRIANGLES, 0, 3);
Display.update();
checkGLErrorRender();
glBindVertexArray(0);
frames = frames - 20000;
}
frames++;
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glUseProgram(0);
glDetachShader(programid, vertshader);
glDetachShader(programid, fragshader);
glDeleteShader(vertshader);
glDeleteShader(fragshader);
glDeleteProgram(programid);
glDeleteBuffers(idVBO[0]);
glDeleteVertexArrays(idVAO[0]);
checkGLError();
Display.destroy();
For the shader i had issues loading it as a file because my loader picks up random junk so I have them as a raw String to get put into. They compile fine no errors.
They just take two in coordinates x and y and auto set w and z.
Frag shader just makes it a solid green triangle because i dont need fancy shaders for a long time =D
Basicly I start the display and setup basic stuff. I make compile ect the shaders and add to program. They all compile fine and it DOES draw sometimes.
Other funny things like it draws fine always with GL_INT even when I feed it floats but with GL_FLOAT it only sometimes displays. Does it prove that my viewpoint changes randomly between closing and opening?
I am just trying to output the primitive to actual screen coordinates right now because I am just learning the basics of drawing a primitive. Later I will get into transformations ect.
Mini edit – the while loop is just to slow down drawing and I don’t need help making it run at x fps I know how to do that. I already have a working platformer in Java2D but I am trying to learn OpenGL to eventually make my own 2D engine for fun but also to learn the basics and maybe move onto 3D once I learn a lot more. I just need help with the OpenGL side
Thanks for any help in advance and sorry for a wall of text.