Trying to think about graphical programming using C ++ and OpenGL3 +, I ran into the problem of a bit of specialized understanding of the char type, pointers to it and the potential implicit or explicit conversion to other types of char pointers. I think I managed to find a solution, but I would like to double by asking you to accept this.
Current (October 2014) The main specification of the OpenGL4.5 profile (Table 2.2 in Chapter 2.2 Command Syntax) contains a list of OpenGL data types and explicitly indicates
GL types are not C types. Thus, for example, GL type int is referred to as GLint outside of this document and is not necessarily equivalent to C type int. The implementation must accurately use the number of bits indicated in the table to represent the GL type.
The GLchar type in this table is defined as the bit width type of 8, which is used to represent the characters that make up the string.
To further narrow down what GLchar should provide, we can take a look at the GLSL Specification (OpenGL Shading Language 4.50, July 2014, Chapter 3.1 Character Set and Compilation Steps):
The original character set used for OpenGL shading languages โโis Unicode in the UTF-8 encoding scheme.
Now, as implemented in any OpenGL library header that I needed to look for, this is simple
typedef char GLchar;
, , " GL - C", .
, typedefs , .
.
OpenGL, GLSL GLchar, . (, , . , .)
open.gl :
const GLchar* vertexSource =
"#version 150 core\n"
"in vec2 position;"
"void main() {"
" gl_Position = vec4(position, 0.0, 1.0);"
"}";
:
#define GLSL(src) "#version 150 core\n" #src
const GLchar* vertexShaderSrc = GLSL(
in vec2 pos;
void main() {
gl_Position = vec4(pos, 0.0, 1.0);
}
);
lazyfoo.net( 30 ) ( ) std::string shaderString, GL:
const GLchar* shaderSource = shaderString.c_str();
, , - , , google - ClockworkCoders , SDK OpenGL, - GLchar*, GLubyte* - :
GLchar** ShaderSource;
unsigned long len;
ifstream file;
len = getFileLength(file);
*ShaderSource = (GLubyte*) new char[len+1];
++ . g++ , -fpermissive. , , GLubyte - , , typedef unsigned char, , char. , - . ++, char* signed unsigned char*, - . , :
, , OpenGL typedef . . , GL .
OpenGL - - - , GLchar typedef char, , . , , , , .
, : wiki opengl.org Shader Compilation, ..:
std::string vertexSource =
const GLchar *source = (const GLchar *)vertexSource.c_str();
- const GLchar* . , , , , OpenGL (): 8, UTF-8.
, GLchar2, , :
#include <iostream>
#include <locale> // handle whitespaces
class GLchar2 {
char element;
public:
GLchar2 () {}
GLchar2 (char element) : element(element) {}
GLchar2 (const GLchar2& c) : element(c.element) {}
~GLchar2 () {}
GLchar2& operator= (const GLchar2& c) {element = c; return *this;}
operator char () const {return element;}
};
std::ostream& operator<< (std::ostream& o, const GLchar2* output_string) {
for (const GLchar2* string_it = output_string; *string_it != '\0'; ++string_it) {
o << *string_it;
}
return o;
}
std::istream& operator>> (std::istream& i, GLchar2& input_char) {
char in;
if (i >> in) input_char = in;
return i;
}
std::istream& operator>> (std::istream& i, GLchar2* input_string) {
GLchar2* string_it;
int width = i.width();
std::locale loc;
while (std::isspace((char)i.peek(),loc)) i.ignore();
for (string_it = input_string; (((i.width() == 0 || --width > 0) && !std::isspace((char)i.peek(),loc)) && i >> *string_it); ++string_it);
*string_it = '\0';
i.width(0);
return i;
}
, , , GLchar2 c-string. , , char GLchar2 ( ). char GLchar2 .
, GLchar , . typedef char GLchar1;, , :
#include <iostream>
#include <fstream>
#include <locale> // handle whitespaces
#include "GLchar2.h"
typedef char GLchar1;
int main () {
std::cout << "GLchar1 has a size of " << sizeof(GLchar1) << " byte.\n";
std::cout << "GLchar2 has a size of " << sizeof(GLchar2) << " byte.\n";
const GLchar1 test_char1 = 'o';
const GLchar2 test_char2 = 't';
GLchar2 test_char3;
test_char3 = '3';
GLchar2 test_char4;
GLchar2 test_char5;
test_char5 = test_char4 = 65;
GLchar2 test_char6 = test_char5;
const GLchar1* test_string1 = "test string one";
const GLchar2* test_string2 = (const GLchar2*)"test string two";
std::cout << "A test character of type GLchar1: " << test_char1 << ".\n";
std::cout << "A test character of type GLchar2: " << test_char2 << ".\n";
std::cout << "A test character of type GLchar2: " << test_char3 << ".\n";
std::cout << "A test character of type GLchar2: " << test_char4 << ".\n";
std::cout << "A test character of type GLchar2: " << test_char5 << ".\n";
std::cout << "A test character of type GLchar2: " << test_char6 << ".\n";
std::cout << "A test string of type GLchar1: " << test_string1 << ".\n";
std::cout << "A test string of type GLchar2: " << test_string2 << ".\n";
GLchar1* test_string3;
GLchar2* test_string4;
GLchar1* test_string5;
GLchar2* test_string6;
std::ifstream test_file("test_input_file.vert");
if (test_file) {
test_file.seekg(0, test_file.end);
int length = test_file.tellg();
test_file.seekg(0, test_file.beg);
test_string3 = new GLchar1[length+1];
GLchar1* test_it = test_string3;
std::locale loc;
while (test_file >> *test_it) {
++test_it;
while (std::isspace((char)test_file.peek(),loc)) {
*test_it = test_file.peek();
test_file.ignore();
++test_it;
}
}
*test_it = '\0';
std::cout << test_string3 << "\n";
std::cout << length << " " <<test_it - test_string3 << "\n";
delete[] test_string3;
test_file.close();
}
std::ifstream test_file2("test_input_file.vert");
if (test_file2) {
test_file2.seekg(0, test_file2.end);
int length = test_file2.tellg();
test_file2.seekg(0, test_file2.beg);
test_string4 = new GLchar2[length+1];
GLchar2* test_it = test_string4;
std::locale loc;
while (test_file2 >> *test_it) {
++test_it;
while (std::isspace((char)test_file2.peek(),loc)) {
*test_it = test_file2.peek();
test_file2.ignore();
++test_it;
}
}
*test_it = '\0';
std::cout << test_string4 << "\n";
std::cout << length << " " << test_it - test_string4 << "\n";
delete[] test_string4;
test_file2.close();
}
test_file.open("test_input_file.vert");
if (test_file) {
test_file.seekg(0, test_file.end);
int length = test_file.tellg();
test_file.seekg(0, test_file.beg);
test_string5 = new GLchar1[length+1];
test_file >> test_string5;
std::cout << test_string5 << "\n";
delete[] test_string5;
test_file.close();
}
test_file2.open("test_input_file.vert");
if (test_file2) {
test_file2.seekg(0, test_file2.end);
int length = test_file2.tellg();
test_file2.seekg(0, test_file2.beg);
test_string6 = new GLchar2[length+1];
test_file2 >> test_string6;
std::cout << test_string6 << "\n";
delete[] test_string6;
test_file2.close();
}
test_file.open("test_input_file.vert");
if (test_file) {
test_file.seekg(0, test_file.end);
int length = test_file.tellg();
test_file.seekg(0, test_file.beg);
test_string5 = new GLchar1[length+1];
GLchar1* test_it = test_string5;
std::locale loc;
while (test_file >> test_it) {
while (*test_it != '\0') ++test_it;
while (std::isspace((char)test_file.peek(),loc)) {
*test_it = test_file.peek();
test_file.ignore();
++test_it;
}
}
std::cout << test_string5 << "\n";
delete[] test_string5;
test_file.close();
}
test_file2.open("test_input_file.vert");
if (test_file2) {
test_file2.seekg(0, test_file2.end);
int length = test_file2.tellg();
test_file2.seekg(0, test_file2.beg);
test_string6 = new GLchar2[length+1];
GLchar2* test_it = test_string6;
std::locale loc;
while (test_file2 >> test_it) {
while (*test_it != '\0') ++test_it;
while (std::isspace((char)test_file2.peek(), loc)) {
*test_it = test_file2.peek();
test_file2.ignore();
++test_it;
}
}
std::cout << test_string6 << "\n";
delete[] test_string6;
test_file2.close();
}
test_file.open("test_input_file.vert");
if (test_file) {
test_file.seekg(0, test_file.end);
int length = test_file.tellg();
test_file.seekg(0, test_file.beg);
test_string5 = new GLchar1[length+1];
std::locale loc;
while (std::isspace((char)test_file.peek(),loc)) test_file.ignore();
test_file.getline(test_string5, length, '\0');
std::cout << test_string5 << "\n";
delete[] test_string5;
test_file.close();
}
test_file.open("test_input_file.vert");
if (test_file) {
std::locale loc;
while (std::isspace((char)test_file.peek(),loc)) test_file.ignore();
std::string test_stdstring1;
std::getline(test_file, test_stdstring1, '\0');
test_string5 = (GLchar1*) test_stdstring1.c_str();
std::cout << test_string5 << "\n";
test_file.close();
}
test_file2.open("test_input_file.vert");
if (test_file2) {
std::locale loc;
while (std::isspace((char)test_file2.peek(),loc)) test_file2.ignore();
std::string test_stdstring2;
std::getline(test_file2, test_stdstring2, '\0');
test_string6 = (GLchar2*) test_stdstring2.c_str();
std::cout << test_string6 << "\n";
test_file.close();
}
return 0;
}
, , GLchar , ++:
, , . , OpenGL , . , , - , , , char.
, , c-.
: , , OpenGL, , "" "", ( ) ?
, stackoverflow, , , , .
, , ( ). , , OpenGL , .