132001f49Smrg/** 232001f49Smrg * Test GLSL 1.20 gl_PointCoord fragment program attribute. 332001f49Smrg * Brian Paul 432001f49Smrg * 11 Aug 2007 532001f49Smrg */ 632001f49Smrg 732001f49Smrg 832001f49Smrg#include <assert.h> 932001f49Smrg#include <string.h> 1032001f49Smrg#include <stdio.h> 1132001f49Smrg#include <stdlib.h> 1232001f49Smrg#include <math.h> 1332001f49Smrg#include <GL/glew.h> 1432001f49Smrg#include "glut_wrap.h" 1532001f49Smrg#include "shaderutil.h" 1632001f49Smrg 1732001f49Smrg 1832001f49Smrgstatic GLint WinWidth = 300, WinHeight = 300; 1932001f49Smrgstatic char *FragProgFile = NULL; 2032001f49Smrgstatic char *VertProgFile = NULL; 2132001f49Smrgstatic GLuint fragShader; 2232001f49Smrgstatic GLuint vertShader; 2332001f49Smrgstatic GLuint program; 2432001f49Smrgstatic GLint win = 0; 2532001f49Smrgstatic GLint tex0; 2632001f49Smrgstatic GLenum Filter = GL_NEAREST; 2732001f49Smrgstatic GLenum Origin = GL_UPPER_LEFT; 2832001f49Smrg 2932001f49Smrg 3032001f49Smrgstatic void 3132001f49SmrgRedisplay(void) 3232001f49Smrg{ 3332001f49Smrg printf("GL_POINT_SPRITE_COORD_ORIGIN = %s\n", 3432001f49Smrg (Origin == GL_UPPER_LEFT ? "GL_UPPER_LEFT" : "GL_LOWER_LEFT")); 3532001f49Smrg 3632001f49Smrg glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, Origin); 3732001f49Smrg 3832001f49Smrg glClear(GL_COLOR_BUFFER_BIT); 3932001f49Smrg 4032001f49Smrg /* draw one point/sprite */ 4132001f49Smrg glPushMatrix(); 4232001f49Smrg glPointSize(60); 4332001f49Smrg glBegin(GL_POINTS); 4432001f49Smrg glVertex2f(WinWidth / 2.0f, WinHeight / 2.0f); 4532001f49Smrg glEnd(); 4632001f49Smrg glPopMatrix(); 4732001f49Smrg 4832001f49Smrg glutSwapBuffers(); 4932001f49Smrg} 5032001f49Smrg 5132001f49Smrg 5232001f49Smrgstatic void 5332001f49SmrgReshape(int width, int height) 5432001f49Smrg{ 5532001f49Smrg glViewport(0, 0, width, height); 5632001f49Smrg glMatrixMode(GL_PROJECTION); 5732001f49Smrg glLoadIdentity(); 5832001f49Smrg glOrtho(0, width, 0, height, -1, 1); 5932001f49Smrg 6032001f49Smrg glMatrixMode(GL_MODELVIEW); 6132001f49Smrg glLoadIdentity(); 6232001f49Smrg 6332001f49Smrg WinWidth = width; 6432001f49Smrg WinHeight = height; 6532001f49Smrg} 6632001f49Smrg 6732001f49Smrg 6832001f49Smrgstatic void 6932001f49SmrgCleanUp(void) 7032001f49Smrg{ 7132001f49Smrg glDeleteShader(fragShader); 7232001f49Smrg glDeleteShader(vertShader); 7332001f49Smrg glDeleteProgram(program); 7432001f49Smrg glutDestroyWindow(win); 7532001f49Smrg} 7632001f49Smrg 7732001f49Smrg 7832001f49Smrgstatic void 7932001f49SmrgKey(unsigned char key, int x, int y) 8032001f49Smrg{ 8132001f49Smrg (void) x; 8232001f49Smrg (void) y; 8332001f49Smrg 8432001f49Smrg switch(key) { 8532001f49Smrg case 'o': 8632001f49Smrg case 'O': 8732001f49Smrg if (Origin == GL_UPPER_LEFT) 8832001f49Smrg Origin = GL_LOWER_LEFT; 8932001f49Smrg else 9032001f49Smrg Origin = GL_UPPER_LEFT; 9132001f49Smrg break; 9232001f49Smrg case 27: 9332001f49Smrg CleanUp(); 9432001f49Smrg exit(0); 9532001f49Smrg break; 9632001f49Smrg } 9732001f49Smrg glutPostRedisplay(); 9832001f49Smrg} 9932001f49Smrg 10032001f49Smrg 10132001f49Smrg 10232001f49Smrgstatic void 10332001f49SmrgMakeTexture(void) 10432001f49Smrg{ 10532001f49Smrg#define SZ 16 10632001f49Smrg GLubyte image[SZ][SZ][4]; 10732001f49Smrg GLuint i, j; 10832001f49Smrg 10932001f49Smrg for (i = 0; i < SZ; i++) { 11032001f49Smrg for (j = 0; j < SZ; j++) { 11132001f49Smrg if ((i + j) & 1) { 11232001f49Smrg image[i][j][0] = 0; 11332001f49Smrg image[i][j][1] = 0; 11432001f49Smrg image[i][j][2] = 0; 11532001f49Smrg image[i][j][3] = 255; 11632001f49Smrg } 11732001f49Smrg else { 11832001f49Smrg image[i][j][0] = j * 255 / (SZ-1); 11932001f49Smrg image[i][j][1] = i * 255 / (SZ-1); 12032001f49Smrg image[i][j][2] = 0; 12132001f49Smrg image[i][j][3] = 255; 12232001f49Smrg } 12332001f49Smrg } 12432001f49Smrg } 12532001f49Smrg 12632001f49Smrg glActiveTexture(GL_TEXTURE0); /* unit 0 */ 12732001f49Smrg glBindTexture(GL_TEXTURE_2D, 42); 12832001f49Smrg glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SZ, SZ, 0, 12932001f49Smrg GL_RGBA, GL_UNSIGNED_BYTE, image); 13032001f49Smrg glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter); 13132001f49Smrg glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter); 13232001f49Smrg glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 13332001f49Smrg glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 13432001f49Smrg#undef SZ 13532001f49Smrg} 13632001f49Smrg 13732001f49Smrg 13832001f49Smrgstatic void 13932001f49SmrgInit(void) 14032001f49Smrg{ 14132001f49Smrg static const char *fragShaderText = 14232001f49Smrg "#version 120 \n" 14332001f49Smrg "uniform sampler2D tex0; \n" 14432001f49Smrg "void main() { \n" 14532001f49Smrg " gl_FragColor = texture2D(tex0, gl_PointCoord.xy, 0.0); \n" 14632001f49Smrg "}\n"; 14732001f49Smrg static const char *vertShaderText = 14832001f49Smrg "void main() {\n" 14932001f49Smrg " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" 15032001f49Smrg "}\n"; 15132001f49Smrg 15232001f49Smrg if (!ShadersSupported()) 15332001f49Smrg exit(1); 15432001f49Smrg 15532001f49Smrg vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText); 15632001f49Smrg fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText); 15732001f49Smrg program = LinkShaders(vertShader, fragShader); 15832001f49Smrg 15932001f49Smrg glUseProgram(program); 16032001f49Smrg 16132001f49Smrg tex0 = glGetUniformLocation(program, "tex0"); 16232001f49Smrg printf("Uniforms: tex0: %d\n", tex0); 16332001f49Smrg 16432001f49Smrg glUniform1i(tex0, 0); /* tex unit 0 */ 16532001f49Smrg 16632001f49Smrg /*assert(glGetError() == 0);*/ 16732001f49Smrg 16832001f49Smrg glClearColor(0.3f, 0.3f, 0.3f, 0.0f); 16932001f49Smrg 17032001f49Smrg printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); 17132001f49Smrg 17232001f49Smrg assert(glIsProgram(program)); 17332001f49Smrg assert(glIsShader(fragShader)); 17432001f49Smrg assert(glIsShader(vertShader)); 17532001f49Smrg 17632001f49Smrg MakeTexture(); 17732001f49Smrg 17832001f49Smrg glEnable(GL_POINT_SPRITE); 17932001f49Smrg 18032001f49Smrg glColor3f(1, 0, 0); 18132001f49Smrg} 18232001f49Smrg 18332001f49Smrg 18432001f49Smrgstatic void 18532001f49SmrgParseOptions(int argc, char *argv[]) 18632001f49Smrg{ 18732001f49Smrg int i; 18832001f49Smrg for (i = 1; i < argc; i++) { 18932001f49Smrg if (strcmp(argv[i], "-fs") == 0) { 19032001f49Smrg FragProgFile = argv[i+1]; 19132001f49Smrg } 19232001f49Smrg else if (strcmp(argv[i], "-vs") == 0) { 19332001f49Smrg VertProgFile = argv[i+1]; 19432001f49Smrg } 19532001f49Smrg } 19632001f49Smrg} 19732001f49Smrg 19832001f49Smrg 19932001f49Smrgint 20032001f49Smrgmain(int argc, char *argv[]) 20132001f49Smrg{ 20232001f49Smrg glutInit(&argc, argv); 20332001f49Smrg glutInitWindowSize(WinWidth, WinHeight); 20432001f49Smrg glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 20532001f49Smrg win = glutCreateWindow(argv[0]); 20632001f49Smrg glewInit(); 20732001f49Smrg glutReshapeFunc(Reshape); 20832001f49Smrg glutKeyboardFunc(Key); 20932001f49Smrg glutDisplayFunc(Redisplay); 21032001f49Smrg ParseOptions(argc, argv); 21132001f49Smrg Init(); 21232001f49Smrg glutMainLoop(); 21332001f49Smrg return 0; 21432001f49Smrg} 215