带有VS C ++ Rainbow Triangle的OpenGL ES 2 [英] OpenGL ES 2 with VS C++ Rainbow Triangle

查看:76
本文介绍了带有VS C ++ Rainbow Triangle的OpenGL ES 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一件事:
我有一个使用OpenGL ES 2和C ++绘制Red Triangle的程序,我的问题是
如何改善程序以创建Rainbow Triangle?"

这是我在程序中用来创建Red Triangle的代码:

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:

#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 );
 
}



我正在使用VS C ++ 2008和OpenGL ES2.
我应该在该代码中更改或添加什么?

预先感谢,

最好的问候



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

Thanks in advance,

Best regards

推荐答案

Have n't以前没有使用glDrawArrays,尽管看起来问题是您的fragmentShader程序包含以下文本:

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

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);



我在网上找不到能够像这样分配gl_FragColor的单个引用.

我会假设三角形都是red,因为那是颜色(1.0、0.0、0.0、1.0).使用glDrawArrays时,我希望看到您提供的数组包含(a)垂直坐标(b)垂直法线(c)垂直颜色(d)tex坐标.

快速浏览一下文档表明情况确实如此.您刚刚使用过glVertexAttribPointer,而我认为您应该制作另一个数组来保存每个顶点的颜色,并通过使用glColorPointer来让OpenGL知道它.



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.


哦,我昨晚以某种方式解决了问题:
这里是方法:

而不是使用这个:''
Oh, somehow I solved it last night :
here''s how :

Instead of using this :''
"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 :

"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 :

GLint attColor;
GLint attPosition;
..
..
 attPosition = glGetAttribLocation(programObject, "vPosition");
 attColor = glGetAttribLocation(programObject, "a_color");



更新我的绘图功能:



Updating my draw function :

...
..
glVertexAttribPointer(attPosition, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glVertexAttribPointer(attColor, 4, GL_FLOAT, GL_FALSE, 0, warna);
glEnableVertexAttribArray(attPosition);
glEnableVertexAttribArray(attColor);
..
..



和中提琴!,我的窗户上有彩虹三角.
无论如何,感谢您的关注并回复大家.

最好的问候,


newmessage

PS:任何人都需要我的源代码(可能),只是PM我.我会寄给您.



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 .


这篇关于带有VS C ++ Rainbow Triangle的OpenGL ES 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆