In OpenGL ES 2 you can set an array of strings as the shader source
void ShaderSource( uint shader, sizei count, const char **string, const int *length );
Which can be used to "inject" macros into the shader source. The corresponding function in WebGL only takes a single string as it seems.
void shaderSource(WebGLShader shader, DOMString source)
Is the only possibility to use macros to manually insert them in the source string?
I'm not sure what you mean by inject macros
const char* str1 = "foo";
const char* str2 = "bar";
const char* strings[] = { str1, str2, }
glShaderSource(shader, 2, strings, NULL);
is functionally equivalent to
var str1 = "foo";
var str2 = "bar";
var strings = [str1, str2];
gl.shaderSource(shader, strings.join(""));
So, in answer to you question. If you want to insert macros prepend them to the string you supply to gl.shaderSource. There's nothing special about macros and which string they come. As far as GL is concerned it's just one big string.
Related
I would like to pass a String vector into an external C function.
In a minimal example I just want to pass the String vectors (or 1D array) through the C function.
My Modelica function looks like:
function testreadstri
input String instri[2];
output String outstri[2];
external "C" test_stri(instri,, size(instri, 1), outstri);
annotation (Include="#include <ebcmysql.cpp>", Library="libmysql");
end testreadstri;
My C fucntion looks like:
void test_stri(const char* thestring, size_t nLines, const char **testresult)
{
//bout = 12.3;
size_t iLines;
//size_t nLines;
iLines = 0;
//nLines = 1;
while ( iLines <= nLines ) {
<LINE_OF_INTEREST>
iLines++;
}
}
I tried for <LINE_OF_INTEREST> the following lines:
testresult[iLines] = thestring[iLines];
strcpy(testresult[iLines], thestring[iLines]);
What works, but of course does not pass the input through as an output, is:
testresult[iLines] = "aTestString";
Is there any possibility to handle Modelica input String vectors in the external C function?
Thanks in advance!
Here's a short, self-contained and compilable example demonstrating both input string and output string handling of a pure external function in Modelica
model Model
function testreadstri
input String instri[2];
output String outstri[2];
external "C" test_stri(instri, size(instri, 1), outstri, size(outstri, 1));
annotation(Include="
#include \"ModelicaUtilities.h\"
#include <stdlib.h>
#include <string.h>
void test_stri(const char** thestring, size_t nLinesIn, const char** testresult, size_t nLinesOut)
{
size_t iLines;
// example for input string handling
for (iLines = 0; iLines < nLinesIn; iLines++) {
ModelicaFormatMessage(\"%s\\n\", thestring[iLines]);
}
// example for output string handling
for (iLines = 0; iLines < nLinesOut; iLines++) {
char* line = ModelicaAllocateStringWithErrorReturn(6);
if (line != NULL) {
strcpy(line, \"result\");
testresult[iLines] = line;
}
}
}");
end testreadstri;
String s[:] = testreadstri({"first", "second"});
end Model;
Yes, this is supported by the Modelica specification, see https://specification.modelica.org/v3.4/Ch12.html#argument-type-mapping.
I'm trying to program a chatting program.
I ask you guys for help since I have little problem.
when I try to send a CString formatted strings, it only receives first letter of the string.
I'm using CAsyncSocket for sockets.
I tried it with char* format string, it worked.
Can you guys tell me what is wrong?
My code is like below:
worked.
char* buf = new char[m_strMsg.GetLength()];
buf = "helloworld!";
m_ClientSocket.Send("sended", m_strMsg.GetLength());
m_ClientSocket.Send(buf, 10);
not worked.
CString a = _T("helloworld!");
m_ClientSocket.Send(a,10);
I've also tried:
CString a = _T("helloworld!");
char* buf = new char[a.GetLength()];
buf = (LPSTR)(LPCTSTR)a;
m_ClientSocket.Send(buf,a.GetLength()];
Here is the proper UNICODE-compliant way of doing it:
CStringW sMessage = L"Hello World";
// convert from UTF-16 (UCS-2) to UTF-8
CStringA sMessageA = CW2A(sMessage, CP_UTF8);
const size_t nBytes = sizeof(CStringA::XCHAR) * sMessageA.GetLength();
CByteArray Message;
Message.SetSize( nBytes );
std::memcpy( Message.GetData(), (const BYTE*)(LPCSTR)sMessageA, nBytes );
m_ClientSocket.Send(Message.GetData(), Message.GetSize());
I m trying to convert QString with special character to const char but I did not succeed.
my function is:
void class::func(const QString& fileName) // fileName = "â.tmp"
{
qDebug()<< fileName; // display "â.tmp"
const char* cfileName = fileName.toAscii().data();
qDebug() << cfileName; // display "a?.tmp"
}
qDebug()<< fileName display the true value that is "â.tmp" but after converting it to const a char*, I do not succeed to have the right value.
In the second time I try to use
const char* cfileName = QString::fromUtf8(fileName.toAscii().data());
but I did not still have the right value, it display the same thing: a?.tmp.
How can I fix this?
ASCII character set does not have the character â, so what you are trying to do is impossible.
You could try this:
const char* cfileName = = fileName.toUtf8().data();
I first store the 3 value into a pair of map like this:
void AddMenuAtlasTexture( int tag, const char* filename, const char* textureName )
{
map<const char*, const char*> _item;
_item.insert(pair<const char*, const char*>(filename, textureName));
m_texturesToLoad.insert(pair<int, map<const char*, const char*> >(tag, _item));
};
then I pass the value to another function like this:
map<const char*, const char*>::iterator _content;
int _tag = (*m_texturesToLoadIterator).first;
_content = (*m_texturesToLoadIterator).second.begin();
AtlasManagerSingleton->AddAtlas((*_content).first, (*_content).second, _tag);
the "textureName" is an absolute path like this kind: "/Users/eddy/Library/Application Support/iPhone Simulator/User/Applications/5FDE0091-2E93-42FE-BB62-05A16429551D/Ranch.app/../Documents/shop_tex.png"
my problem is the first function can get the "textureName" right, but the second function "AddAtlas" can not get the path, the "(*_content).second" is NULL.
and the "AddAtlas" prototype is:
void AtlasManager :: AddAtlas( const char *a_configFile, const char *a_spriteName, int a_nKey )
I develop this in iPhone dev using XCode.
use make_pair instead of pair<int, map<const char*, const char*> >.
use -> instead of *.
the texture loader is a map int -> const char* -> const char*. I don't see where you used the second index.
Probably this:
AtlasManagerSingleton->AddAtlas((*_content).first, (*_content).second, _tag)
Should be:
AtlasManagerSingleton->AddAtlas(_content->first, _content->second.begin()->second, _tag)
As I understand, SQLite doesn't have the math functions to properly implement the Haversine formula in straight SQL. I'm thinking this should be possible using an external function, with the implementation being in C.
The goal is to have a SQLite database in an iPhone, and to be able to sort by the distance to the user's current location. I've searched, but I can't find an example of any examples of this being done. I think the difficult parts would be getting the function declarations correct. The end result I'm hoping for, is to be able to execute a SQL statement like:
SELECT * FROM LOCATION loc ORDER BY distance(loc.lat, loc.long, ?, ?)
I have a C Haversine formula. The function definition is as follows:
float distance( float nLat1, float nLon1, float nLat2, float nLon2 );
Does anyone know if this is possible and/or have some example code to start from?
I just had good luck with this post: http://www.thismuchiknow.co.uk/?p=71
This demonstrates a sqlite function that takes in one string parameter and returns a string result.
In your case you would need a function that reads four floats and returns a float but the principle is the same (you would replace sqlite3_value_text with sqlite3_value_double and sqlite3_result_text with sqlite3_result_double):
#include <stdlib.h>
#include <sqlite3.h>
#include <stdio.h>
void haver(sqlite3_context* ctx,int cnt,sqlite3_value** val)
{
printf("In SQLite haver implementation, called for value: %s\n", sqlite3_value_text(*val));
char * resultOfCall = "Result of function call"; //this would call the distance function
sqlite3_result_text(ctx, resultOfCall, strlen(resultOfCall), NULL);
}
int cback (void* udata,int ncol,char** value,char** colname)
{
int i=0;
for(;i<ncol;i++)
printf("Result column: %s value: %s \n", colname[i], value[i]);
return 0;
}
int main()
{
sqlite3 * handle;
int res = sqlite3_open("./test.sql", &handle);
res = sqlite3_create_function(handle, "haver", 1, SQLITE_UTF8, NULL, &haver, NULL, NULL);
char * errmsg = NULL;
res = sqlite3_exec(handle, "select haver(w) from t", &cback, NULL, &errmsg);
printf("sqlite3_exec result: %d %s\n", res, errmsg != NULL ? errmsg : "No error");
sqlite3_close(handle);
}