here’s my code :
public class GameDisplay {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GameDisplay jurassicIslandDisplay = new GameDisplay();
jurassicIslandDisplay.start();
}
public void start() {
try {
Display.setDisplayMode(new DisplayMode(500, 500));
Display.setTitle("Jurassic Island");
Display.setResizable(true);
Display.create();
} catch(LWJGLException e) {
e.printStackTrace();
System.exit(-1);
}
Triangle t1 = new Triangle();
while(!Display.isCloseRequested())
{
Display.update();
Display.sync(60);
t1.display();
if(Display.wasResized())
{
reshape(Display.getWidth(), Display.getHeight());
}
}
Display.destroy();
}
protected void display()
{
// glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// glClear(GL_COLOR_BUFFER_BIT);
}
private void reshape(int width, int height) {
glViewport(0, 0, width, height);
}
}
public class Triangle{
private final float verticesArray[] = {
0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f};
private int glVerticesBufferObject;
private int theProgram;
private final String strVertexShader =
"#version 330
" +
"
" +
"layout(location = 0) in vec4 position;
" +
"void main()
" +
"{
" +
" gl_Position = position;
" +
"}";
private final String strFragmentShader =
"#version 330
" +
"
" +
"out vec4 outputColor;
" +
"void main()
" +
"{
" +
" float lerpValue = gl_FragCoord.y / 500.0f;
" +
" outputColor = mix(vec4(1.0f, 1.0f, 1.0f, 1.0f), vec4(0.2f, 0.2f, 0.2f, 1.0f), lerpValue);
" +
"}";
/**
* Constructor.
*/
public Triangle() {
initializeVertexBuffer();
initializeProgram();
}
/**
* Initializing the openGL vertex buffer.
*/
private void initializeVertexBuffer() {
FloatBuffer verticesBufferObject = BufferUtils.createFloatBuffer(verticesArray.length); // creates a float buffer with a size identical to the vertex array size
verticesBufferObject.put(verticesArray); // put the data from the vertex array into the float buffer
verticesBufferObject.flip(); // change the buffer from write-able to read-able
glVerticesBufferObject = glGenBuffers(); // creating the new opengl vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, glVerticesBufferObject); // binding the opengl vertex buffer to the context field GL_ARRAY_BUFFER
glBufferData(GL_ARRAY_BUFFER, verticesBufferObject, GL_STATIC_DRAW); // copying the size of the float buffer to the opengl vertex buffer and the data
glBindBuffer(GL_ARRAY_BUFFER, 0); // unbinding the opengl vertex buffer
}
/**
* Initializing the shader program.
*/
private void initializeProgram() {
ArrayList <Integer> shaderList = new ArrayList <> (); // Making a new shader arraylist.
shaderList.add(createShader(GL_VERTEX_SHADER, strVertexShader)); // Adding to the shaderlist new shaders.
shaderList.add(createShader(GL_FRAGMENT_SHADER, strFragmentShader));
theProgram = createProgram(shaderList); //
}
/**
*
* @param shaderList
* @return's a shader program.
*/
private int createProgram(ArrayList <Integer> shaderList) {
int program = glCreateProgram(); // Creating a new program object to serve us later as our program object.
for (Integer shader : shaderList) { // Attaching all shaders in the shader list to the program object.
glAttachShader(program, shader);
}
glLinkProgram(program); // Linking the attached shaders to each other to try and produce a shader program.
int status = glGetProgrami(program, GL_LINK_STATUS); // Checking if the linking of the program object was a success or a failure.
if (status == GL_FALSE) // If the linking of the program object to the shaders was a failure print and create and error log.
{
int infoLogLength = glGetProgrami(program, GL_INFO_LOG_LENGTH);
String infoLog = glGetProgramInfoLog(program, infoLogLength);
System.err.printf("Linker failure: %s
", infoLog);
}
for (Integer shader : shaderList) // Detaching all shaders from the program object.
{
glDetachShader(program, shader);
}
return program;
}
private int createShader(int shaderType, String shaderFile) {
int shader = glCreateShader(shaderType); // Creating a new shader object to serve us later as our new shader and setting it's shader type.
glShaderSource(shader, shaderFile); // Feeding the new shader object we just created with the desired shader string.
glCompileShader(shader); // Compiling the new shader object.
int status = glGetShaderi(shader, GL_COMPILE_STATUS); // Checking if the compilation of the shader object was a success or a failure.
if (status == GL_FALSE) { // If the compilation of the shader object was a failure then print and create an error log.
int infoLogLength = glGetShaderi(shader, GL_INFO_LOG_LENGTH); // Getting the error log length.
String infoLog = glGetShaderInfoLog(shader, infoLogLength); // Getting the error log info.
String strShaderType = null; // Getting the shader type of the shader object to be printed later in the error log.
switch (shaderType) {
case GL_VERTEX_SHADER:
strShaderType = "vertex";
break;
case GL_GEOMETRY_SHADER:
strShaderType = "geometry";
break;
case GL_FRAGMENT_SHADER:
strShaderType = "fragment";
break;
}
System.err.printf("Compile failure in %s shader:
%s
", strShaderType, infoLog); // Printing the error log.
}
return shader;
}
protected void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // setting the color that will appear when openGL clearing the display.
glClear(GL_COLOR_BUFFER_BIT); // telling openGL to clear the display to the color set previously by glClearColor.
glUseProgram(theProgram); // the way of rendering based on the shaders linked to this program object.
glBindBuffer(GL_ARRAY_BUFFER, glVerticesBufferObject); // Binding the glVerticesBufferObject to the context field GL_ARRAY_BUFFER so glVertexAttribPointer would know where to get it's data from.
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0); // Telling openGL in what format our vertex array data in the buffer object (the currently buffer object that is bound to the context field GL_ARRAY_BUFFER) is stored in.
// Rendering the vertices
glDrawArrays(GL_TRIANGLES, 0, 3); // Telling openGL to render and create an independent triangle from every 3 vertices it gets where 0 is the first index of the vertices and 3 is the number of indices.
glDisableVertexAttribArray(0);
glUseProgram(0);
}
}
My question : does glViewPort is being called at some part of the rendering process or it doesn’t being called unless the window has been resized ? and if so (if it doesn’t being called unless window has been resized) how does my NDCs will be transformed to WCs ? or they won’t unless i resize (and I dont think they wont because in my fragment shader i use a lerp value with 500 which is my window height so if it wasnt being transformed in somepoint to WCs that would make something look like it doesn’t right but it’s right since the output is as suppose to be)?