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: In OpenGL2.0 Very basic shader for beginners

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    5

    Cool In OpenGL2.0 Very basic shader for beginners

    hi guys, i have made a sample shading program for running a simple OpenGL2.0 proram in windows.
    Hope this will be helpful for beginners in OpenGL.


    /* Using the standard output for fprintf */
    #include <stdio.h>
    #include <stdlib.h>
    /* Use glew.h instead of gl.h to get all the GL prototypes declared */
    #include <GL/glew.h>
    #include <GL/GL.h>
    /* Using the GLUT library for the base windowing setup */
    #include <GL/glut.h>
    #pragma comment(lib, "glew32.lib")

    GLuint program;
    GLint positionHandle;
    GLint colorHandle;

    GLfloat triangle_vertices[] = {
    0.0f, 0.8f,
    -0.8f, -0.8f,
    0.8f, -0.8f
    };
    const char *fs_source =
    "#version 120 \n"
    "uniform vec3 uColor;"
    "void main(void) { "
    " gl_FragColor = vec4(uColor, 1.0);"
    "}";
    const char *vs_source =
    #ifdef GL_ES_VERSION_2_0
    "#version 100\n" // OpenGL ES 2.0
    #else
    "#version 120\n" // OpenGL 2.1
    #endif
    "attribute vec2 aPosition; "
    "void main(void) { "
    " gl_Position = vec4(aPosition, 0.0, 1.0); "
    "}";

    int showError(const char*, const char*);

    //Returns 1 when all is ok, 0 with a displayed error
    int init_resources(void)
    {
    GLint compile_ok = GL_FALSE;
    GLint link_ok = GL_FALSE;

    GLuint vertexHandle = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexHandle, 1, &vs_source, NULL);
    glCompileShader(vertexHandle);
    glGetShaderiv(vertexHandle, GL_COMPILE_STATUS, &compile_ok);
    if (0 == compile_ok)
    {
    fprintf(stderr, "Error in vertex shader\n");
    return 0;
    }

    GLuint fragHandle = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragHandle, 1, &fs_source, NULL);
    glCompileShader(fragHandle);
    glGetShaderiv(fragHandle, GL_COMPILE_STATUS, &compile_ok);
    if (!compile_ok) {
    fprintf(stderr, "Error in fragment shader\n");
    return 0;
    }

    program = glCreateProgram();
    glAttachShader(program, vertexHandle);
    glAttachShader(program, fragHandle);
    glLinkProgram(program);
    glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
    if (!link_ok) {
    fprintf(stderr, "glLinkProgram:");
    return 0;
    }

    const char* aPosition = "aPosition";
    const char* uColor = "uColor";
    positionHandle = glGetAttribLocation(program, aPosition);
    colorHandle = glGetUniformLocation(program, uColor);

    showError(aPosition, uColor);
    return 1;
    }

    int showError(const char* aPosition, const char* uColor)
    {
    if (positionHandle == -1) {
    fprintf(stderr, "Could not bind attribute %s\n", aPosition);
    return 0;
    }
    if (colorHandle == -1) {
    fprintf(stderr, "Could not show color %s\n", uColor);
    return 0;
    }
    }

    void onDisplay()
    {
    /* Clear the background as white */
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glUseProgram(program);
    glUniform3f(colorHandle, 1.0f, 0.0f, 0.0f);

    glEnableVertexAttribArray(positionHandle);
    glVertexAttribPointer(positionHandle, 2, GL_FLOAT,GL_FALSE, 0, triangle_vertices);
    /* Push each element in buffer_vertices to the vertex shader */
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(positionHandle);
    /* Display the result */
    glutSwapBuffers();
    }

    void free_resources()
    {
    glDeleteProgram(program);
    }

    int main(int argc, char* argv[])
    {
    /* Glut-related initialising functions */
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEP TH);
    glutInitWindowSize(640, 480);
    glutCreateWindow("My First Triangle");

    /* Extension wrangler initialising */
    GLenum glew_status = glewInit();
    if (glew_status != GLEW_OK)
    {
    fprintf(stderr, "Error: %s\n", glewGetErrorString(glew_status));
    return EXIT_FAILURE;
    }

    /* When all init functions run without errors,
    the program can initialise the resources */
    if (1 == init_resources())
    {
    /* We can display it if everything goes OK */
    glutDisplayFunc(onDisplay);
    glutMainLoop();
    }

    /* If the program exits in the usual way,
    free resources and exit with a success */
    free_resources();
    return EXIT_SUCCESS;
    }


    Do ask any question what so ever is bothering you going through this code.

  2. #2
    Member Regular Contributor
    Join Date
    Jan 2012
    Location
    Germany
    Posts
    325
    I'm not sure OpenGL beginners want to start with OpenGL 2.0 from nearly 10 years ago, I would highly recommend OpenGL 3.2 and later. You could update your example and use better formatting for code blocks.

Posting Permissions

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