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           
"
	"uniform vec3 uColor;"
    "void main(void) {        "
	"  gl_FragColor = vec4(uColor, 1.0);"
    "}";
const char *vs_source = 
	#ifdef GL_ES_VERSION_2_0
		"#version 100
"  // OpenGL ES 2.0
	#else
		"#version 120
"  // 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
");
    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
");
    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
", aPosition);
		return 0;
	}
	if (colorHandle == -1) {
		fprintf(stderr, "Could not show color %s
", 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_DEPTH);
  glutInitWindowSize(640, 480);
  glutCreateWindow("My First Triangle");
 
  /* Extension wrangler initialising */
  GLenum glew_status = glewInit();
  if (glew_status != GLEW_OK)
  {
    fprintf(stderr, "Error: %s
", 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.

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.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.