I'm learning to use OpenGL ES
#include
#include
#include
Void changemvp (GLfloat MVP [16]);
Int main (int arg c, char * * argv) {
SDL_Init (SDL_INIT_VIDEO);
SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute (SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
SDL_DisplayMode dm;
SDL_GetDesktopDisplayMode (0, & amp; Dm);
SDL_Window * win=SDL_CreateWindow (" Hello ", 0, 0, dm, w, dm, h, SDL_WINDOW_OPENGL);
SDL_GLContext GLC=SDL_GL_CreateContext (win);
Const GLchar * vShaderStr=
"The attribute vec4 vPosition;"
"The attribute vec4 vColor;"
"Uniform mat4 MVP"
"Varying vec4 v_color;"
"Void main ()
""{"
Gl_Position=MVP * vPosition; ""
"V_color=vColor;"
"} ";
Const GLchar * fShaderStr=
"Precision mediump float;"
"Varying vec4 v_color;"
"Void main ()
""{"
"Gl_FragColor=v_color;"
"} ";
GLuint vertexShader;
GLuint fragmentShader;
GLuint programObject;
VertexShader=glCreateShader (GL_VERTEX_SHADER);
FragmentShader=glCreateShader (GL_FRAGMENT_SHADER);
GlShaderSource (vertexShader, 1, & amp; VShaderStr, NULL);
GlShaderSource (fragmentShader, 1, & amp; FShaderStr, NULL);
GlCompileShader (vertexShader);
GlCompileShader (fragmentShader);
ProgramObject=glCreateProgram ();
GlAttachShader (programObject vertexShader);
GlAttachShader (programObject fragmentShader);
GlLinkProgram (programObject);
GlDeleteShader (vertexShader);
GlDeleteShader (fragmentShader);
GlUseProgram (programObject);
GlClearColor (0.0 f, f, 0.0 0.0 f, 0.0 f);
GlClear (GL_COLOR_BUFFER_BIT);
GlViewport (0, 0, dm. W, dm, h);
GLfloat vVertices []={0.5 0.0 f, f, f 0.0, 0.67 f, f 0.4, 0.47 f,
1.0 f to 0.5 f to 0.0 f to 1.0 f to 0.4 f to 0.97 f,
0.5 f to 0.5 f, 0.0 f, f 0.0, 0.4 f, 0.57 f
};
# define stride 6
# define vPosition_offset 0
# define vPosition_size 3
# define vColor_offset 3
# define vPosition_size 3
GLfloat MVP [16]={1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1
};
GlBindAttribLocation (programObject, 0, "vPosition");
GlBindAttribLocation (programObject, 1, "vColor");
GlVertexAttribPointer (0, vPosition_size, GL_FLOAT, GL_FALSE, stride * sizeof (float), vVertices + vPosition_offset);
GlVertexAttribPointer (1, vPosition_size, GL_FLOAT, GL_FALSE, stride * sizeof (float), vVertices + vColor_offset);
Int mvploc=glGetUniformLocation (programObject, "MVP");
GlUniformMatrix4fv (mvploc, 1, GL_FALSE, MVP);
GlEnableVertexAttribArray (0);
GlEnableVertexAttribArray (1);
GlDrawArrays (GL_TRIANGLES, 0, 3);
SDL_GL_SwapWindow (win);
SDL_Event e;
While (1) {
Int r=SDL_PollEvent (& amp; E);
if(! R) continue;
If (e. ype==SDL_KEYDOWN) goto the quit;
If (e. ype==SDL_MOUSEMOTION) {
Changemvp (MVP);
}
}
The quit:
SDL_GL_DeleteContext (GLC);
SDL_DestroyWindow (win);
SDL_Quit ();
}
Void changemvp (GLfloat MVP [16]) {
}
CodePudding user response:
If you declare a variable is not used, then glGetUniformLocation return value is always 1