Friday, May 27, 2011

glBindAttribLocation example c c++ java objc

Name
glBindAttribLocation — Associates a generic vertex attribute index with a named attribute variable

C Specification

void glBindAttribLocation(GLuint  program,
GLuint  index,
const GLchar * name);

Parameters

program
Specifies the handle of the program object in which the association is to be made.
index
Specifies the index of the generic vertex attribute to be bound.
name
Specifies a null terminated string containing the name of the vertex shader attribute variable to which index is to be bound.

Description

glBindAttribLocation is used to associate a user-defined attribute variable in the program object specified by program with a generic vertex attribute index. The name of the user-defined attribute variable is passed as a null terminated string inname. The generic vertex attribute index to be bound to this variable is specified by index. When program is made part of current state, values provided via the generic vertex attribute index will modify the value of the user-defined attribute variable specified by name.
If name refers to a matrix attribute variable, index refers to the first column of the matrix. Other matrix columns are then automatically bound to locations index+1 for a matrix of type mat2; index+1 and index+2 for a matrix of type mat3; andindex+1index+2, and index+3 for a matrix of type mat4. (glBindAttribLocation)
This command makes it possible for vertex shaders to use descriptive names for attribute variables rather than generic variables that are numbered from 0 to GL_MAX_VERTEX_ATTRIBS -1. The values sent to each generic attribute index are part of current state, just like standard vertex attributes such as color, normal, and vertex position. If a different program object is made current by calling glUseProgram, the generic vertex attributes are tracked in such a way that the same values will be observed by attributes in the new program object that are also bound to index.
Attribute variable name-to-generic attribute index bindings for a program object can be explicitly assigned at any time by calling glBindAttribLocation. Attribute bindings do not go into effect until glLinkProgram is called. After a program object has been linked successfully, the index values for generic attributes remain fixed (and their values can be queried) until the next link command occurs.
Applications are not allowed to bind any of the standard OpenGL vertex attributes using this command, as they are bound automatically when needed. Any attribute binding that occurs after the program object has been linked will not take effect until the next time the program object is linked.

Notes

glBindAttribLocation is available only if the GL version is 2.0 or greater.
glBindAttribLocation can be called before any vertex shader objects are bound to the specified program object. It is also permissible to bind a generic attribute index to an attribute variable name that is never used in a vertex shader.
If name was bound previously, that information is lost. Thus you cannot bind one user-defined attribute variable to multiple indices, but you can bind multiple user-defined attribute variables to the same index.
Applications are allowed to bind more than one user-defined attribute variable to the same generic vertex attribute index. This is called aliasing, and it is allowed only if just one of the aliased attributes is active in the executable program, or if no path through the shader consumes more than one attribute of a set of attributes aliased to the same location. The compiler and linker are allowed to assume that no aliasing is done and are free to employ optimizations that work only in the absence of aliasing. OpenGL implementations are not required to do error checking to detect aliasing. Because there is no way to bind standard attributes, it is not possible to alias generic attributes with conventional ones (except for generic attribute 0).
Active attributes that are not explicitly bound will be bound by the linker when glLinkProgram is called. The locations assigned can be queried by calling glGetAttribLocation.
OpenGL copies the name string when glBindAttribLocation is called, so an application may free its copy of the name string immediately after the function returns.

Errors

GL_INVALID_VALUE is generated if index is greater than or equal to GL_MAX_VERTEX_ATTRIBS.
GL_INVALID_OPERATION is generated if name starts with the reserved prefix "gl_".
GL_INVALID_VALUE is generated if program is not a value generated by OpenGL.
GL_INVALID_OPERATION is generated if program is not a program object.
GL_INVALID_OPERATION is generated if glBindAttribLocation is executed between the execution of glBegin and the corresponding execution of glEnd.

Associated Gets

glGet with argument GL_MAX_VERTEX_ATTRIBS
glGetActiveAttrib with argument program
glGetAttribLocation with arguments program and name
glIsProgram

Copyright

Copyright © 2003-2005 3Dlabs Inc. Ltd. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. http://opencontent.org/openpub/.
Example

1// Create the shader program
2GLuint uiProgramObject = glCreateProgram();34glAttachShader(uiProgramObject, uiFragShader);5glAttachShader(uiProgramObject, uiVertShader);6glBindAttribLocation(uiProgramObject, VERTEX_ARRAY, "myVertex");78// Link the program
9glLinkProgram(uiProgramObject);



Example 2
//
// ES2Renderer.m
// TestIPhone
//
// Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "CMyRenderer.h"

@implementation CMyRenderer
@synthesize mGLMoveY;
@synthesize mGLMoveYValue;


-(void) render
{
HContext *pContect = [self GetContext];
GLfloat vVertices[] =
{  0.0f0.5f0.0f,
-0.5f, -0.5f0.0f,
 0.5f, -0.5f0.0f };
// Set the viewport
glViewport ( 00, pContect->nWidth, pContect->nHeight );
// Clear the color buffer
glClear ( GL_COLOR_BUFFER_BIT );
// Use the program object
glUseProgram ( pContect->unProgram);
//uniform으로 정의  변수를 정의 한다.
glUniform1f(self.mGLMoveY, (GLfloat)mGLMoveYValue);
mGLMoveYValue += 0.075f;

// 0: glBindAttribLocation 정의  인덱스
// 1:점정의 좌표의 크기 ,
// 2:데이터 타입,
// 3:법선벡터 사용유무,
// 4:여러개의 정보를 한개의 버퍼로 사용할때의 Offset, 버퍼
glVertexAttribPointer ( 03GL_FLOATGL_FALSE0, vVertices );
// 위의 인덱스를 사용한다.
glEnableVertexAttribArray ( 0 );
//1: 렌더러의 종류 (삼각형그리기) , 0: EnableArray 인덱스의 시작지점, 3:점정그리기 순서의 갯수.indices
glDrawArrays ( GL_TRIANGLES03 );
return ;
}


//  프로그램의 세이더를 읽어오는데 적합하게 읽어와야 한다.
//  함수가 실행당시에는 HContext *pContect = [Rende GetContext];
//  pContext NULL  상태이다.
// pClass : CMyRenderer 객체의 포인터
// hContext : HContext  포인터
int LoadShader(void* pClass, void* hContext)
{
int nResult = 0;
CMyRenderer * Render = (CMyRenderer*)pClass;
GLbyte vShaderStr[] =
"attribute vec4 vPosition; \n" //정점 attibute 정의 한다.
"uniform float u_fMovey;  \n" //변수를 정의 한다.
"void main() \n"
"{ \n"
" gl_Position = vPosition; \n"
" gl_Position.y += sin(u_fMovey) / 2.0; \n"
"} \n";
GLbyte fShaderStr[] =
"precision mediump float;  \n"
"void main() \n"
"{ \n"
" gl_FragColor = vec4 ( 1.0, 1.0, 0.0, 1.0 );\n"
"} \n";
//1> 정점세이더와 프래그먼트세이더를 컴파일한다.
GLuint programObject;
programObject = hCreateProgram((const char*)vShaderStr,(const char*)fShaderStr);
if(programObject == 0return 0;
//2> 링크하기전에  AttribLocation 바인드해야 한다.
// 0 = vPosition 바인드 인덱스
glBindAttribLocation ( programObject, 0"vPosition" );

//3> 컴파일한 세이더를 링크한다.
nResult = hLinkProgram(programObject);
if(nResult < 0return 0;
//4> 링크 후에 UniformLocation 사용하여 변수의 핸들을 가져온다.
Render.mGLMoveY = glGetUniformLocation( programObject, "u_fMovey" );
return programObject;
}
@end



//
// TerrianGLView.m
// HTerrianMake
//
// Created by 호학  on 10. 9. 27..
// Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "TerrianGLView.h"


@implementation TerrianGLView

- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
//IHRenderer 핸들러를 셋해준다.
[self setMRender:self];
}
return self;
}

- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
}

#define one 1.0f
GLfloat vertices[] = {
-one, -one, -one//Index 0
one, -one, -one//Index 1
oneone, -one//Index 2
-oneone, -one//Index 3
-one, -oneone//Index 4
one, -oneone//Index 5
oneoneone//Index 6
-oneoneone//Index 7
};




//큐브 그리기
#define oneColor 1.0
////RGBA
GLfloat colors[] = {
000oneColor,
oneColor00oneColor,
oneColoroneColor0oneColor,
0oneColor0,oneColor,
00oneColoroneColor,
oneColor0oneColoroneColor,
oneColoroneColoroneColoroneColor,
0oneColoroneColoroneColor,
};

//삼각형 인덱스
unsigned char indices[] = {
045051,
156162,
267273,
374340,
476465,
301312
};



#pragma mark --
#pragma mark IHRenderer
-(void) beginDraw:(HGLContext *)hGLContext
{

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
static float sAngel = 1.0f;
sAngel = sAngel + mDeltaTime * 10.25f;
glRotatef(sAngel, 010); //회전
glFrontFace(GL_CW);
//정점어레이를 Enable해준다.
glEnableClientState(GL_VERTEX_ARRAY);
//Color Array 사용할  있게 선언한다.
glEnableClientState(GL_COLOR_ARRAY);
}

-(void) Draw:(HGLContext *)hGLContext
{
glVertexPointer(3GL_FLOAT0vertices);
glColorPointer(4GL_FLOAT0colors);
glDrawElements(GL_TRIANGLES36GL_UNSIGNED_BYTEindices);

}

-(void) endDraw:(HGLContext *)hGLContext
{
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}

@end