Skip to content

Commit

Permalink
Simplify test_gl_subdata and test_float_tex. NFC (#21393)
Browse files Browse the repository at this point in the history
Also, remove test_std_cout_new.cpp completely.  This test seems to
be based on the code from these other two tests but doesn't seem to
server any useful purpose anymore.  Its was first added in ad285f6.
  • Loading branch information
sbc100 committed Feb 22, 2024
1 parent bf76ba4 commit b4f224e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 85 deletions.
26 changes: 0 additions & 26 deletions test/core/test_std_cout_new.cpp

This file was deleted.

57 changes: 31 additions & 26 deletions test/float_tex.cpp → test/float_tex.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <assert.h>
#include <math.h>
#include <stdio.h>

#define GL_GLEXT_PROTOTYPES
#define EGL_EGLEXT_PROTOTYPES
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
extern "C" {
#include <GL/gl.h>
#include <GL/glut.h>
}

static const char vertex_shader[] =
"#ifdef GL_ES\n"
"precision lowp float;\n"
Expand All @@ -29,6 +28,7 @@ static const char vertex_shader[] =
" gl_PointSize = v.z;\n"
" color = vec4(0.5 + v.w/2., 0.5 + 0.5 * v.w/2., 0.5, 1);\n"
"}\n";

static const char fragment_shader[] =
"#ifdef GL_ES\n"
"precision lowp float;\n"
Expand All @@ -44,26 +44,29 @@ static const char fragment_shader[] =
"}\n"
"if ( dst > 0.5) discard;\n"
"}";
struct NodeInfo { //structure that we want to transmit to our shaders

typedef struct NodeInfo { //structure that we want to transmit to our shaders
float x;
float y;
float s;
float c;
};
} NodeInfo;

GLuint nodeTexture; //texture id used to bind
GLuint nodeSamplerLocation; //shader sampler address
GLuint indicesAttributeLocation; //shader attribute address
GLuint indicesVBO; //Vertex Buffer Object Id;
const int nbNodes = 512;
NodeInfo * data = new NodeInfo[nbNodes]; //our data that will be transmitted using float texture.
#define NUM_NODES 512
NodeInfo data[NUM_NODES]; //our data that will be transmitted using float texture.
double alpha = 0; //use to make a simple funny effect;
//
static void updateFloatTexture() {
int count = 0;
for (float x=0; x < nbNodes; ++x ) {
data[count].x = 0.2*pow(cos(alpha), 3) + (sin(alpha)*3. + 3.5) * x/nbNodes * cos(alpha + x/nbNodes * 16. * M_PI);
data[count].y = 0.2*pow(sin(alpha), 3) + (sin(alpha)*3. + 3.5) * x/nbNodes * sin(alpha + x/nbNodes * 16. * M_PI);
data[count].s = (16. + 16. * cos(alpha + x/nbNodes * 32. * M_PI)) + 8.;// * fmod(x/nbNodes + alpha, 1.) + 5.;
data[count].c = 0.5 + 0.5 * sin(alpha + x/nbNodes * 32. * M_PI);
for (float x=0; x < NUM_NODES; ++x ) {
data[count].x = 0.2*pow(cos(alpha), 3) + (sin(alpha)*3. + 3.5) * x/NUM_NODES * cos(alpha + x/NUM_NODES * 16. * M_PI);
data[count].y = 0.2*pow(sin(alpha), 3) + (sin(alpha)*3. + 3.5) * x/NUM_NODES * sin(alpha + x/NUM_NODES * 16. * M_PI);
data[count].s = (16. + 16. * cos(alpha + x/NUM_NODES * 32. * M_PI)) + 8.;// * fmod(x/NUM_NODES + alpha, 1.) + 5.;
data[count].c = 0.5 + 0.5 * sin(alpha + x/NUM_NODES * 32. * M_PI);
++count;
}
glBindTexture(GL_TEXTURE_2D, nodeTexture);
Expand All @@ -73,12 +76,13 @@ static void updateFloatTexture() {
// In desktop GL, we can also use sized internal formats.
const GLenum internalFormat = GL_RGBA32F;
#endif
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, nbNodes, 1, 0, GL_RGBA, GL_FLOAT, data);
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, NUM_NODES, 1, 0, GL_RGBA, GL_FLOAT, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
alpha -= 0.001;
}

static void glut_draw_callback(void) {
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
Expand All @@ -93,9 +97,10 @@ static void glut_draw_callback(void) {
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, indicesVBO);
glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, NULL);
glDrawArrays(GL_POINTS, 0, nbNodes);
glDrawArrays(GL_POINTS, 0, NUM_NODES);
glutSwapBuffers();
}

GLuint createShader(const char source[], int type) {
GLint status;
char msg[512];
Expand All @@ -105,11 +110,12 @@ GLuint createShader(const char source[], int type) {
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
glGetShaderInfoLog(shader, sizeof msg, NULL, msg);
std::cout << "Shader info: \"" << msg << "\"" << std::endl;
printf("Shader info: \"%s\"\n", msg);
}
assert(status == GL_TRUE);
return shader;
}

static void gl_init(void) {
GLuint program = glCreateProgram();
glAttachShader(program, createShader(vertex_shader , GL_VERTEX_SHADER));
Expand All @@ -120,13 +126,13 @@ static void gl_init(void) {
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {
glGetProgramInfoLog(program, sizeof msg, NULL, msg);
std::cout << "info: \"" << msg << "\"" << std::endl;
printf("info: \"%s\"\n", msg);
}
assert(status == GL_TRUE);
glUseProgram(program);
std::vector<float> elements(nbNodes);
float elements[NUM_NODES];
int count = 0;
for (float x=0; x < nbNodes; ++x ) {
for (float x=0; x < NUM_NODES; ++x ) {
elements[count] = count;
++count;
}
Expand All @@ -135,26 +141,25 @@ static void gl_init(void) {
/* Store the vertices in a vertex buffer object (VBO) */
glGenBuffers(1, &indicesVBO);
glBindBuffer(GL_ARRAY_BUFFER, indicesVBO);
glBufferData(GL_ARRAY_BUFFER, elements.size() * sizeof(float), &elements[0], GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, NUM_NODES * sizeof(float), &elements[0], GL_STATIC_DRAW);
/* Get the locations of the uniforms so we can access them */
nodeSamplerLocation = glGetUniformLocation(program, "nodeInfo");
nodeSamplerLocation = glGetUniformLocation(program, "nodeInfo");
glBindAttribLocation(program, 0, "indices");
#ifndef __EMSCRIPTEN__ // GLES2 & WebGL do not have these, only pre 3.0 desktop GL and compatibility mode GL3.0+ GL do.
//Enable glPoint size in shader, always enable in Open Gl ES 2.
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
glEnable(GL_POINT_SPRITE);
#endif
}

int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Simple FLOAT Texture Test");
/* Set up glut callback functions */
glutDisplayFunc(glut_draw_callback );
glutDisplayFunc(glut_draw_callback);
gl_init();
glutMainLoop();
return 0;
}


64 changes: 36 additions & 28 deletions test/gl_subdata.cpp → test/gl_subdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <math.h>
#include <stdio.h>
#include <string.h>

#define GL_GLEXT_PROTOTYPES
#define EGL_EGLEXT_PROTOTYPES
#include <cmath>
#include <iostream>
#include <vector>
extern "C" {
#include <GL/gl.h>
#include <GL/glut.h>
}

static const char vertex_shader[] =
"#ifdef GL_ES\n"
"precision lowp float;\n"
Expand All @@ -28,6 +28,7 @@ static const char vertex_shader[] =
" gl_PointSize = v.z;\n"
" color = vec4(0.5 + v.w/2., 0.5 + 0.5 * v.w/2., 0.5, 1);\n"
"}\n";

static const char fragment_shader[] =
"#ifdef GL_ES\n"
"precision lowp float;\n"
Expand All @@ -43,26 +44,29 @@ static const char fragment_shader[] =
"}\n"
"if ( dst > 0.5) discard;\n"
"}";
struct NodeInfo { //structure that we want to transmit to our shaders

typedef struct NodeInfo { //structure that we want to transmit to our shaders
float x;
float y;
float s;
float c;
};
} NodeInfo;

GLuint nodeTexture; //texture id used to bind
GLuint nodeSamplerLocation; //shader sampler address
GLuint indicesAttributeLocation; //shader attribute address
GLuint indicesVBO; //Vertex Buffer Object Id;
const int nbNodes = 512;
NodeInfo data[nbNodes]; //our data that will be transmitted using float texture.
#define NUM_NODES 512
NodeInfo data[NUM_NODES]; //our data that will be transmitted using float texture.
double alpha = 0; //use to make a simple funny effect;

static void updateFloatTexture() {
int count = 0;
for (float x=0; x < nbNodes; ++x ) {
data[count].x = 0.2*pow(cos(alpha), 3) + (sin(alpha)*3. + 3.5) * x/nbNodes * cos(alpha + x/nbNodes * 16. * M_PI);
data[count].y = 0.2*pow(sin(alpha), 3) + (sin(alpha)*3. + 3.5) * x/nbNodes * sin(alpha + x/nbNodes * 16. * M_PI);
data[count].s = (16. + 16. * cos(alpha + x/nbNodes * 32. * M_PI)) + 8.;// * fmod(x/nbNodes + alpha, 1.) + 5.;
data[count].c = 0.5 + 0.5 * sin(alpha + x/nbNodes * 32. * M_PI);
for (float x=0; x < NUM_NODES; ++x ) {
data[count].x = 0.2*pow(cos(alpha), 3) + (sin(alpha)*3. + 3.5) * x/NUM_NODES * cos(alpha + x/NUM_NODES * 16. * M_PI);
data[count].y = 0.2*pow(sin(alpha), 3) + (sin(alpha)*3. + 3.5) * x/NUM_NODES * sin(alpha + x/NUM_NODES * 16. * M_PI);
data[count].s = (16. + 16. * cos(alpha + x/NUM_NODES * 32. * M_PI)) + 8.;// * fmod(x/NUM_NODES + alpha, 1.) + 5.;
data[count].c = 0.5 + 0.5 * sin(alpha + x/NUM_NODES * 32. * M_PI);
++count;
}
glBindTexture(GL_TEXTURE_2D, nodeTexture);
Expand All @@ -72,12 +76,13 @@ static void updateFloatTexture() {
// In desktop GL, we can also use sized internal formats.
const GLenum internalFormat = GL_RGBA32F;
#endif
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, nbNodes, 1, 0, GL_RGBA, GL_FLOAT, data);
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, NUM_NODES, 1, 0, GL_RGBA, GL_FLOAT, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
alpha -= 0.001;
}

static void glut_draw_callback(void) {
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
Expand All @@ -92,30 +97,32 @@ static void glut_draw_callback(void) {
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, indicesVBO);
glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, NULL);
glDrawArrays(GL_POINTS, 0, nbNodes);
glDrawArrays(GL_POINTS, 0, NUM_NODES);
glutSwapBuffers();
}
GLuint createShader(const char source[], int type) {

GLuint createShader(const char* source, int type) {
char msg[512];
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, (const GLchar**)(&source), NULL);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
glGetShaderInfoLog(shader, sizeof msg, NULL, msg);
std::cout << "Shader info: " << msg << std::endl;
printf("Shader info: %s\n", msg);
return shader;
}

static void gl_init(void) {
GLuint program = glCreateProgram();
glAttachShader(program, createShader(vertex_shader , GL_VERTEX_SHADER));
glAttachShader(program, createShader(fragment_shader, GL_FRAGMENT_SHADER));
glLinkProgram(program);
char msg[512];
glGetProgramInfoLog(program, sizeof msg, NULL, msg);
std::cout << "info: " << msg << std::endl;
printf("info: %s\n", msg);
glUseProgram(program);
std::vector<float> elements(nbNodes);
float elements[NUM_NODES];
int count = 0;
for (float x=0; x < nbNodes; ++x ) {
for (float x=0; x < NUM_NODES; ++x ) {
elements[count] = count;
++count;
}
Expand All @@ -124,28 +131,29 @@ static void gl_init(void) {
/* Store the vertices in a vertex buffer object (VBO) */
glGenBuffers(1, &indicesVBO);
glBindBuffer(GL_ARRAY_BUFFER, indicesVBO);
float zeroes[nbNodes];
float zeroes[NUM_NODES];
memset(zeroes, 0, sizeof(zeroes));
glBufferData(GL_ARRAY_BUFFER, elements.size() * sizeof(float), zeroes, GL_STATIC_DRAW);
for (int x = 0; x < nbNodes; x++) {
glBufferData(GL_ARRAY_BUFFER, NUM_NODES * sizeof(float), zeroes, GL_STATIC_DRAW);
for (int x = 0; x < NUM_NODES; x++) {
glBufferSubData(GL_ARRAY_BUFFER, x * sizeof(float), sizeof(float), &elements[x]);
}
/* Get the locations of the uniforms so we can access them */
nodeSamplerLocation = glGetUniformLocation(program, "nodeInfo");
nodeSamplerLocation = glGetUniformLocation(program, "nodeInfo");
glBindAttribLocation(program, 0, "indices");
#ifndef __EMSCRIPTEN__ // GLES2 & WebGL do not have these, only pre 3.0 desktop GL and compatibility mode GL3.0+ GL do.
//Enable glPoint size in shader, always enable in Open Gl ES 2.
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
glEnable(GL_POINT_SPRITE);
#endif
}

int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Simple FLOAT Texture Test");
int w = glutCreateWindow("Simple FLOAT Texture Test");
/* Set up glut callback functions */
glutDisplayFunc(glut_draw_callback );
glutDisplayFunc(glut_draw_callback);
gl_init();
glutMainLoop();
return 0;
Expand Down
4 changes: 2 additions & 2 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2320,11 +2320,11 @@ def test_tex_nonbyte(self):

@requires_graphics_hardware
def test_float_tex(self):
self.btest('float_tex.cpp', reference='float_tex.png', args=['-lGL', '-lglut'])
self.btest('float_tex.c', reference='float_tex.png', args=['-lGL', '-lglut'])

@requires_graphics_hardware
def test_subdata(self):
self.btest('gl_subdata.cpp', reference='float_tex.png', args=['-lGL', '-lglut'])
self.btest('gl_subdata.c', reference='float_tex.png', args=['-lGL', '-lglut'])

@requires_graphics_hardware
def test_perspective(self):
Expand Down
3 changes: 0 additions & 3 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5703,9 +5703,6 @@ def test_direct_string_constant_usage(self):
self.set_setting('EXIT_RUNTIME')
self.do_core_test('test_direct_string_constant_usage.cpp')

def test_std_cout_new(self):
self.do_core_test('test_std_cout_new.cpp')

def test_std_function_incomplete_return(self):
self.do_core_test('test_std_function_incomplete_return.cpp')

Expand Down

0 comments on commit b4f224e

Please sign in to comment.