forked from richa-batra/ParticleRobotSimulations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.cpp
167 lines (136 loc) · 4.9 KB
/
render.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 1993-2015 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
#include <math.h>
#include <assert.h>
#include <stdio.h>
// OpenGL Graphics includes
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "render.h"
#include "shaders.h"
#ifndef PI
#define PI 3.1415926535897932384626433832795
#endif
ParticlebotRenderer::ParticlebotRenderer()
: pos(0),
nCells(0),
pointSize(1.0f),
program(0),
vbo(0),
colorVBO(0),
centroid_steps(500)
{
_initGL();
}
ParticlebotRenderer::~ParticlebotRenderer()
{
pos = 0;
}
void ParticlebotRenderer::setPositions(float *p, int n)
{
pos = p;
nCells = n;
}
void ParticlebotRenderer::setVertexBuffer(unsigned int v, int n)
{
vbo = v;
nCells = n;
}
void ParticlebotRenderer::_drawPoints()
{
glEnableVertexAttribArray(glGetAttribLocation(program, "pos"));
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(
glGetAttribLocation(program, "pos"), // attribute
2, // number of elements per vertex
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is (no normalization)
0, // no extra data between each position
0 // offset of first element
);
glEnableVertexAttribArray(glGetAttribLocation(program, "rad"));
glBindBuffer(GL_ARRAY_BUFFER, radVbo);
glVertexAttribPointer(
glGetAttribLocation(program, "rad"), // attribute
1, // number of elements per vertex
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is (no normalization)
0, // no extra data between each position
0 // offset of first element
);
if (colorVBO)
{
glEnableVertexAttribArray(glGetAttribLocation(program, "color"));
glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
glVertexAttribPointer(
glGetAttribLocation(program, "color"), // attribute
4, // number of elements per vertex
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is (no normalization)
0, // no extra data between each position
0 // offset of first element
);
}
glDrawArrays(GL_POINTS, 0, nCells+centroid_steps);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(glGetAttribLocation(program, "pos"));
glDisableVertexAttribArray(glGetAttribLocation(program, "rad"));
glDisableVertexAttribArray(glGetAttribLocation(program, "color"));
}
void ParticlebotRenderer::display()
{
glEnable(GL_POINT_SPRITE_ARB);
glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glUseProgram(program);
glUniform1f(glGetUniformLocation(program, "pointScale"), window_h / tanf(fov*0.5f*(float)PI/180.0f));
glUniform3f(glGetUniformLocation(program, "lightDir"), light_x, light_y, light_z);
glColor3f(1, 1, 1);
_drawPoints();
glUseProgram(0);
glDisable(GL_POINT_SPRITE_ARB);
}
GLuint
ParticlebotRenderer::_compileProgram(const char *vsource, const char *fsource)
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertexShader, 1, &vsource, 0);
glShaderSource(fragmentShader, 1, &fsource, 0);
glCompileShader(vertexShader);
glCompileShader(fragmentShader);
GLuint glprogram = glCreateProgram();
glAttachShader(glprogram, vertexShader);
glAttachShader(glprogram, fragmentShader);
glLinkProgram(glprogram);
// check if program linked
GLint success = 0;
glGetProgramiv(glprogram, GL_LINK_STATUS, &success);
if (!success)
{
char temp[256];
glGetProgramInfoLog(glprogram, 256, 0, temp);
printf("Failed to link program:\n%s\n", temp);
glDeleteProgram(glprogram);
glprogram = 0;
}
return glprogram;
}
void ParticlebotRenderer::_initGL()
{
program = _compileProgram(vertexShader, spherePixelShader);
#if !defined(__APPLE__) && !defined(MACOSX)
glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE);
glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE);
#endif
}