I was working on a simple OpenGL ES 2.0 program (along with SDL 2 to make things a bit easier) and decided to try out point sprites. I was able to get them to draw successfully, but I wasn't able to change their size by outputting gl_PointSize from the vertex shader. Theoretically, that should be all that I have to do.
The following code snippet is a very stripped-down version of my barely-C++ code (no error-checking at all, but that is because nothing should go wrong with it) that demonstrates how I am trying to change the size of my point sprites. It has been tested on two rather different computers with similar results (Linux, but 32-bit/software rendering vs 64-bit/discrete GPU), and may be compiled using g++ with g++ main.cpp -lSDL2 -Wall -D_REENTRANT -lGLESv2
.
#include <GLES2/gl2.h>
#include <SDL2/SDL.h>
struct myData {
SDL_Window *window;
SDL_GLContext context;
};
const GLchar vertex[] =
"#version 100\n"
"precision mediump float;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n"
" gl_PointSize = 128.0;\n"
"}\0";
const GLchar fragment[] =
"#version 100\n"
"precision mediump float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\0";
GLuint loadShader(GLuint program, GLenum type, const GLchar *shaderSrc) {
GLuint shader;
shader = glCreateShader(type);
glShaderSource(shader, 1, &shaderSrc, NULL);
glCompileShader(shader);
glAttachShader(program, shader);
return 0;
}
int sdlInit(myData *data) {
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
data->window = SDL_CreateWindow("Demo", 0, 0, 512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
data->context = SDL_GL_CreateContext(data->window);
return 0;
}
int glInit(myData *data) {
GLuint programObject;
programObject = glCreateProgram();
loadShader(programObject, GL_VERTEX_SHADER, vertex);
loadShader(programObject, GL_FRAGMENT_SHADER, fragment);
glLinkProgram(programObject);
glUseProgram(programObject);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glViewport(0, 0, 512, 512);
return 0;
}
int loopFunc(myData *data) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return 1;
}
}
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_POINTS, 0, 1);
SDL_GL_SwapWindow(data->window);
return 0;
}
void sdlQuit(myData *data) {
SDL_GL_DeleteContext(data->context);
SDL_DestroyWindow(data->window);
SDL_Quit();
return;
}
int main() {
myData data;
sdlInit(&data);
glInit(&data);
while (!loopFunc(&data));
sdlQuit(&data);
return 0;
}
When ran, the program should produce a point sprite with a size of 128 pixels, per the value that I set in the vertex shader. However, when actually executed, the size of the points sprite in the center of the window is exactly one pixel. What am I doing wrong?
A couple things:
#version 100
GLSL code; desktop OpenGL requires that you turn ongl_PointSize
viaglEnable(GL_PROGRAM_POINT_SIZE)
.gl_PointSize
of128.0
by checkingGL_ALIASED_POINT_SIZE_RANGE
; the spec only requires implementations support a range of 1.0 to 1.0, anything above that is optional.Workin' fine on this Debian Buster box:
SDL/OpenGL ES info:
Code: