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 2 of 2

Thread: OpenGL Perspective Vertex Shader

  1. #1
    Newbie Newbie
    Join Date
    Apr 2013
    Posts
    1

    OpenGL Perspective Vertex Shader

    Hey guys. I am very new to OpenGL. I am trying to write a program that just makes a square, with RGB and Black in each corner. I got it to work without using the Z axis at all. Now I am trying to put it into to -10 on the Z axis. My code compiles with no errors, but I get a nice plain green screen as my image instead of a smaller square. Any ideas as to what I am doing wrong?

    Code :
    #include <GL/glfw.h>
    //#include <GLES2/gl2.h>
     
    typedef struct {
      float position[3];
      float color[4];
    } Vertex;
     
     
    const Vertex Vertices[] = {
      {{1, -1, -10}, {1, 0, 0, 1}},
      {{1, 1, -10}, {0, 1, 0, 1}},
      {{-1, 1, -10}, {0, 0, 1, 1}},
      {{-1, -1, -10}, {0, 0, 0, 1}}
    };
     
     
    const GLubyte Indices[] = {
      0, 1, 2,
      2, 3, 0
    };
     
     
    char* vertex_shader_src =
      "attribute vec4 Position;\n"
      "attribute vec4 SourceColor;\n"
      "uniform mat4 modelView;\n"
      "uniform mat4 projection;\n"
      "\n"
      "varying vec4 DestinationColor;\n"
      "\n"
      "void main(void) {\n"
      "    DestinationColor = SourceColor;\n"
      "    gl_Position = projection * modelView * Position;\n"
      "}\n";
     
     
    char* fragment_shader_src =
      "varying lowp vec4 DestinationColor;\n"
      "\n"
      "void main(void) {\n"
      "    gl_FragColor = DestinationColor;\n"
      "}\n";
     
     
    GLint simple_shader(GLint shader_type, char* shader_src)
    {
      GLint compile_success = 0;
     
      int shader_id = glCreateShader(shader_type);
     
      glShaderSource(shader_id, 1, &shader_src, 0);
     
      glCompileShader(shader_id);
     
      glGetShaderiv(shader_id, GL_COMPILE_STATUS, &compile_success);
     
      if (compile_success == GL_FALSE) {
        GLchar message[256];
        glGetShaderInfoLog(shader_id, sizeof(message), 0, &message[0]);
        printf("glCompileShader Error: %s\n", message);
        exit(1);
      }
     
      return shader_id;
    }
     
     
    int simple_program()
    {
     
      GLint link_success = 0;
     
      GLint program_id = glCreateProgram();
      GLint vertex_shader = simple_shader(GL_VERTEX_SHADER, vertex_shader_src);
      GLint fragment_shader = simple_shader(GL_FRAGMENT_SHADER, fragment_shader_src);
     
      glAttachShader(program_id, vertex_shader);
      glAttachShader(program_id, fragment_shader);
     
      glLinkProgram(program_id);
     
      glGetProgramiv(program_id, GL_LINK_STATUS, &link_success);
     
      if (link_success == GL_FALSE) {
        GLchar message[256];
        glGetProgramInfoLog(program_id, sizeof(message), 0, &message[0]);
        printf("glLinkProgram Error: %s\n", message);
        exit(1);
      }
     
      return program_id;
    }
     
    GLfloat* make_identity(void){
    	int i;
    	int j;
    	GLfloat* m = malloc(sizeof(float)*4*4);
    	for(i = 0; i <4; i++){
    		for(j = 0; j<4;j++){
    			m[4+i+j] = 0.0;
    			if(i == j){
    				m[4+i+j] = 1.0;
    			}
     
    		}
    	}
    	return m;
    }
    GLfloat* make_perspective(GLfloat w, GLfloat h, GLfloat n, GLfloat f){
    	GLfloat* m = make_identity();
    	m[0] = (2*n)/w;
    	m[5] = (2*n)/h;
    	m[10] = (n+f)/(n-f);
    	m[11] = (2*n*f)/(n-f);
    	m[14] = -1;
    	m[15] = 0;
    	return m;
    }
     
    int main(void)
    {
     
      GLint program_id, position_slot, color_slot;
      GLuint vertex_buffer;
      GLuint index_buffer;
      GLuint projection_slot;
      GLuint model_slot;
      GLfloat* modelView_matrix;
      GLfloat* projection_matrix;
     
      // Initialize GLFW library
      if (!glfwInit())
        return -1;
     
      // Create and open a window
      if (!glfwOpenWindow(640, // width
                          480, // height
                          8,   // red
                          8,   // green
                          8,   // blue
                          0,   // alpha
                          24,  // depth
                          0,   // stencil
                          GLFW_WINDOW)) //mode
        return -1;
     
      program_id = simple_program();
     
      glUseProgram(program_id);
     
      position_slot = glGetAttribLocation(program_id, "Position");
      color_slot = glGetAttribLocation(program_id, "SourceColor");
      model_slot = glGetUniformLocation(program_id, "modelView");
      projection_slot = glGetUniformLocation(program_id, "projection");
     
     
      glEnableVertexAttribArray(position_slot);
      glEnableVertexAttribArray(color_slot);
     
      modelView_matrix = make_perspective(640/640, 480/640, 1, 100);
      projection_matrix = make_perspective(640/640, 480/640, 1, 100);
      glUniformMatrix4fv(model_slot, 1,0,modelView_matrix);
      glUniformMatrix4fv(projection_slot, 1,0,projection_matrix);
     
      // Create Buffer
      glGenBuffers(1, &vertex_buffer);
      // Map GL_ARRAY_BUFFER to this buffer
      glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
      // Send the data
      glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
     
      // Repeat
      glGenBuffers(1, &index_buffer);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
     
      while (glfwGetWindowParam(GLFW_OPENED)) {
     
        glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
     
        glViewport(0, 0, 640, 480);
     
        glVertexAttribPointer(position_slot,
                              3,
                              GL_FLOAT,
                              GL_FALSE,
                              sizeof(Vertex),
                              0);
     
        glVertexAttribPointer(color_slot,
                              4,
                              GL_FLOAT,
                              GL_FALSE,
                              sizeof(Vertex),
                              (GLvoid*) (sizeof(float) * 3));
     
        glDrawElements(GL_TRIANGLES,
                       sizeof(Indices) / sizeof(GLubyte),
                       GL_UNSIGNED_BYTE, 0);
     
        glfwSwapBuffers();
      }
     
      return 0;
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Code :
     
      modelView_matrix = make_perspective(640/640, 480/640, 1, 100);   projection_matrix = make_perspective(640/640, 480/640, 1, 100);

    It seems unlikely that these 2 matrices are the same

Posting Permissions

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