Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: glViewPort (java - lwjgl)

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    19

    Smile glViewPort (java - lwjgl)

    here's my code :
    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);
        }
    }

    Code :
    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 \n" +
    			"\n" +
    			"layout(location = 0) in vec4 position;\n" +
    			"void main()\n" +
    			"{\n" +
    			"    gl_Position = position;\n" +
    			"}";
     
        private final String strFragmentShader = 
    			"#version 330\n" +
    			"\n" +
    			"out vec4 outputColor;\n" +
    			"void main()\n" +
    			"{\n" +
                            "   float lerpValue = gl_FragCoord.y / 500.0f;\n" + 
                            "   outputColor = mix(vec4(1.0f, 1.0f, 1.0f, 1.0f), vec4(0.2f, 0.2f, 0.2f, 1.0f), lerpValue);\n" +
    			"}";
     
        /**
         * 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\n", 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:\n%s\n", 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)?

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    491
    Quote Originally Posted by kululu View Post
    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 ?
    When a context is first attached to a window, the viewport will be set to the dimensions of the window, i.e. the equivalent of glViewport(0,0,width,height). So it's only necessary to call glViewport() when the window size changes or if you want to set the viewport to something other than the entire window.

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    19
    Quote Originally Posted by GClements View Post
    When a context is first attached to a window, the viewport will be set to the dimensions of the window, i.e. the equivalent of glViewport(0,0,width,height). So it's only necessary to call glViewport() when the window size changes or if you want to set the viewport to something other than the entire window.
    ok so its called after the context has been created
    but in my vertex shader i transform my coords to clip coords (even tho they already are if u look they have the 4th componet) and i dont need to transform them to ndcs because they already have the same w so in what part of the rendering proccess the ndcs will translate to wcs ?

  4. #4
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    491
    Quote Originally Posted by kululu View Post
    but in my vertex shader i transform my coords to clip coords (even tho they already are if u look they have the 4th componet) and i dont need to transform them to ndcs because they already have the same w so in what part of the rendering proccess the ndcs will translate to wcs ?
    This occurs in the "Vertex Post-Processing" stage, after the vertex/tessellation/geometry shaders and before the fragment shader.

  5. #5
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    19
    Quote Originally Posted by GClements View Post
    This occurs in the "Vertex Post-Processing" stage, after the vertex/tessellation/geometry shaders and before the fragment shader.
    so after the vertices transformed to clip vertices they're being transformed (we're assuming they ndcs already) to wc ?
    and how is it done exacly ? the shader call some glViewPort to sort this out ? (also glViewport transform points from ndcs to wcs / from wcs to new wcs beside changing the area we can render on right?)

  6. #6
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    491
    Quote Originally Posted by kululu View Post
    so after the vertices transformed to clip vertices they're being transformed (we're assuming they ndcs already) to wc ?
    and how is it done exacly ? the shader call some glViewPort to sort this out ? (also glViewport transform points from ndcs to wcs / from wcs to new wcs beside changing the area we can render on right?)
    The values written to gl_Position are in clip coordinates. Clipping, conversion to NDC, conversion to window coordinates and interpolation of attributes all happen automatically.

    glViewport() defines the transformation from NDC to window coordinates. It doesn't directly affect the area which can be rendered to. Geometry won't extend outside the viewport, because it's clipped to the unit cube (in NDC), which is mapped to the viewport., but the set of fragments which are rendered for e.g. wide lines and points can extend outside of the viewport. If you need to restrict rendering to a 2D rectangle, use glScissor().

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •