Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I wanna ask something :
I got a program that draw Red Triangle using OpenGL ES 2 and C++ and my question is
"How could I improved my program in order to create Rainbow Triangle?"

Here's the code that I used in my program to create Red Triangle:
C++
#include <stdlib.h>
#include "esUtil.h"
#include <stdio.h>

typedef struct
{
   // Handle to a program object
   GLuint programObject;
} UserData;

///
// Create a shader object, load the shader source, and
// compile the shader.
//
GLuint LoadShader ( GLenum type, const char *shaderSrc )
{
   GLuint shader;
   GLint compiled;
   
   // Create the shader object
   shader = glCreateShader ( type );

   if ( shader == 0 )
   	return 0;

   // Load the shader source
   glShaderSource ( shader, 1, &shaderSrc, NULL );
   
   // Compile the shader
   glCompileShader ( shader );

   // Check the compile status
   glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled );

   if ( !compiled ) 
   {
      GLint infoLen = 0;

      glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen );
      
      if ( infoLen > 1 )
      {
         char* infoLog = malloc (sizeof(char) * infoLen );

         glGetShaderInfoLog ( shader, infoLen, NULL, infoLog );
         esLogMessage ( "Error compiling shader:\n%s\n", infoLog );            
         
         free ( infoLog );
      }

      glDeleteShader ( shader );
      return 0;
   }

   return shader;

}

///
// Initialize the shader and program object
//
int Init ( ESContext *esContext )
{
	UserData *userData = esContext->userData;
	GLbyte vShaderStr[] =
	"attribute vec4 vPosition; \n"
	"void main() \n"
	"{ \n"
	"	gl_Position = vPosition; \n"
	"} \n";
	
	GLbyte fShaderStr[] =
	"precision mediump float; \n"
	"void main() \n"
	"{ \n"
	"	gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0), \n"
	"				 (0.0 , 1.0 , 0.0 , 1.0),\n"
	"				 (0.0 , 0.0 , 0.1 , 1.0); \n"
	"} \n";
GLuint vertexShader;
GLuint fragmentShader;
GLuint programObject;
GLint linked;

   /*UserData *userData = esContext->userData;
   GLbyte vShaderStr[] =  
      "attribute vec4 a_Position;    \n"
	  "attribute vec2 a_texCoord;	 \n"
	  "varying vec2 v_texCoord;		 \n"
      "void main()                   \n"
      "{          int i;             \n"
	  "//for (i=0;i<=1;i++)			 \n"
      " //  gl_Position = vPosition; \n"
	  "gl_Position = a_position;	 \n"
	  "v_texCoord=a_texCoord;		 \n"
      "}                             \n";
   
   GLbyte fShaderStr[] =  
      "precision mediump float;\n"
	  "varying vec2 v_texCoord;\n"
	  "uniform sampler2D s_texture;\n"
	  "void main(){     \n"             
	  "//for (i=0;i<=1;i++)			\n"
      "//gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0);\n"
	  "//gl_FragColor = vec4 (1,1,1,0);\n"
	  "gl_FragColor=texture2D(s_texture, v_texCoord);\n"
      "}        \n";*/

   //GLuint vertexShader;
   //GLuint fragmentShader;
   //GLuint programObject;
   //GLint linked;

   // Load the vertex/fragment shaders
   vertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr );
   fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr );

   // Create the program object
   programObject = glCreateProgram ( );
   
   if ( programObject == 0 )
      return 0;

   glAttachShader ( programObject, vertexShader );
   glAttachShader ( programObject, fragmentShader );

   // Bind vPosition to attribute 0   
   glBindAttribLocation ( programObject, 0, "vPosition" );

   // Link the program
   glLinkProgram ( programObject );

   // Check the link status
   glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );

   if ( !linked ) 
   {
      GLint infoLen = 0;

      glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );
      
      if ( infoLen > 1 )
      {
         char* infoLog = malloc (sizeof(char) * infoLen );

         glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );
         esLogMessage ( "Error linking program:\n%s\n", infoLog );            
         
         free ( infoLog );
      }

      glDeleteProgram ( programObject );
      return FALSE;
   }

   // Store the program object
   userData->programObject = programObject;

   glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );//background
   return TRUE;
}

///
// Draw a triangle using the shader pair created in Init()
//
void Draw ( ESContext *esContext )
{
	static float transY = 0.0f;
	GLuint textureID;
   UserData *userData = esContext->userData;
    GLubyte warna [] ={1.0, 0.0, 0.0, 1.0,
					  0.0, 1.0, 0.0, 1.0,
					  0.0, 0.0, 0.1, 1.0};
   GLfloat vVertices[] = {  0.0f,  0.5f, 0.0f, 
                           -0.5f, -0.5f, 0.0f,
                            0.5f, -0.5f, 0.0f };

  

   /*  GLubyte warna [] ={1.0, 0.0, 0.0, 1.0,
					  0.0, 1.0, 0.0, 1.0,
					  0.0, 0.0, 0.1, 1.0};*/
      
  /* glPixelStorei(GL_UNPACK_ALIGNMENT ,1);
   glGenTextures(1, &textureID);
   glBindTexture(GL_TEXTURE_2D, textureID);
   glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,2,2,0,GL_RGB,GL_UNSIGNED_BYTE,warna);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);*/

   // Set the viewport
   glViewport ( 0, 0, esContext->width, esContext->height );
   
   // Clear the color buffer
   glClear ( GL_COLOR_BUFFER_BIT );

   // Use the program object
   glUseProgram ( userData->programObject );

   // Load the vertex data
   glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices );
   //glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors);
  // glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,warna);
	
   glEnableVertexAttribArray(0);
  // glEnableClientState();
   glDrawArrays ( GL_TRIANGLES, 0, 3 );

   eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
}


int main ( int argc, char *argv[] )
{
   ESContext esContext;
   UserData  userData;

   esInitContext ( &esContext );
   esContext.userData = &userData;
   
   esCreateWindow ( &esContext, "Hello Triangle", 320, 240, ES_WINDOW_RGB );
   
   if ( !Init ( &esContext ) )
      return 0;

   esRegisterDrawFunc ( &esContext, Draw );
   
   esMainLoop ( &esContext );
 
}


I am using VS C++ 2008 and OpenGL ES 2.
What should I change or add into that code?

Thanks in advance,

Best regards
Posted
Updated 22-Apr-12 22:09pm
v3
Comments
nv3 23-Apr-12 3:58am    
Your question is too general. Please use Improve Question to narrow your question. What in particular are you having problems with? If you just ask: How could I possible improve my program, responses are likely to be very sparse.
Sandeep Mewara 23-Apr-12 5:11am    
This is not a well framed question! Please be specific on where you stuck and having issues. Tried anything?
Use the "Improve question" link to edit your question and provide better information.
enhzflep 23-Apr-12 6:07am    
Nonsense! The question makes (and did so before the edit) perfect sense.

Haven't used glDrawArrays before, though it seems the problem is that your fragmentShader program contains the following text:

C++
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0), \n"
	"	   (0.0, 1.0 , 0.0 , 1.0),\n"
	"	   (0.0, 0.0 , 0.1 , 1.0);


I can't find a single reference on the net to being able to assign gl_FragColor like this.

I would assume that the triangle is all red because that's the color (1.0, 0.0, 0.0, 1.0). When using glDrawArrays, I would expect to see that you supply arrays holding (a) vert coords (b) vert normals (c) vert-colors (d) tex-coords.

A quick look at the docs suggests this to be the case. You've just used glVertexAttribPointer, while I think you should make another array that holds the colours of each of the verts and let OpenGL know about it through the use of glColorPointer.
 
Share this answer
 
Oh, somehow I solved it last night :
here's how :

Instead of using this :'
C++
"attribute vec4 vPosition; \n"
	"void main() \n"
	"{ \n"
	"	gl_Position = vPosition; \n"
	"} \n";
	
	GLbyte fShaderStr[] =
	"precision mediump float; \n"
	"void main() \n"
	"{ \n"
	"	gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0), \n"
	"				 (0.0 , 1.0 , 0.0 , 1.0),\n"
	"				 (0.0 , 0.0 , 0.1 , 1.0); \n"
	"} \n";


I use this :
C++
"attribute vec4 vPosition;    \n"
    "attribute vec4 a_color;       \n"
    "varying vec4 v_color;         \n"
    "void main()                   \n"
    "{                             \n"
    "   v_color = a_color;		   \n"
    "   gl_Position = vPosition;  \n"
    "}\n";
	
	GLbyte fShaderStr[] =
	"precision mediump float; \n"
	"varying vec4 v_color;         \n"
        "void main (void)              \n"
        "{                             \n"
        "    gl_FragColor = v_color;   \n"
        "}";


And I modify some of my codes :
New variables :
C++
GLint attColor;
GLint attPosition;
..
..
 attPosition = glGetAttribLocation(programObject, "vPosition");
 attColor = glGetAttribLocation(programObject, "a_color");


Updating my draw function :
C++
...
..
glVertexAttribPointer(attPosition, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glVertexAttribPointer(attColor, 4, GL_FLOAT, GL_FALSE, 0, warna);
glEnableVertexAttribArray(attPosition);
glEnableVertexAttribArray(attColor);
..
..


And viola!, I got Rainbow Triangle on my windows.
Anyhow, Thanks for the concern and reply to all of you.

Best Regards,


newmessage

PS: anyone need my source code (probably), just PM me. I'll mail you .
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900