glUniform3fv not working OpenGL - c++

I'm working on a piece of code in OpenGL.
I'm getting the following error message while trying to compile:
MyGLWidget.cpp: In member function ‘virtual void MyGLWidget::initializeGL()’:
MyGLWidget.cpp:30:38: error: cannot convert ‘glm::vec3 {aka glm::tvec3<float, (glm::precision)0u>}’ to ‘const GLfloat* {aka const float*}’ in argument passing
MyGLWidget.cpp:31:39: error: cannot convert ‘glm::vec3 {aka glm::tvec3<float, (glm::precision)0u>}’ to ‘const GLfloat* {aka const float*}’ in argument passing
I have declared these locations in MyGLWidget.h as follows:
GLuint llumLoc, focusLoc;
And have initialize them in MyGLWidget.cpp as:
llumLoc = glGetUniformLocation (program->programId(), "llumAmbient");
focusLoc = glGetUniformLocation (program->programId(), "posFocus");
"llumAmbient" and "posFocus" are uniforms in my vertex shader:
uniform vec3 llumAmbient;
uniform vec3 posFocus;
I get the mentioned error message while trying to call the following code inside of MyGLWidget::initializeGL
glUniform3fv(llumLoc, 1, glm::vec3(0.2));
glUniform3fv(focusLoc, 1, glm::vec3(1.0));
Obviously, I've tried to follow the documentation at www.opengl.org and glm.g-truc.net/0.9.2/api/a00001.html, but I just can't see what's wrong with this code...

Actually, I just found out today that there's a cleaner and more straight forward way. Simply:
glUniform3fv(focusLoc, 1, &v[0]);
No need to import value_ptr.

You need to give a pointer to the vector, not the vector itself. For example:
glm::vec3 v(1.0f);
glUniform3fv(focusLoc, 1, glm::value_ptr(&v[0]));

Related

In C++'s Eigen library, how do I solve: invalid use of incomplete type ‘const class Eigen::MatrixSquareRootReturnValue<Eigen::Matrix<float, -1, -1> >’

I'm using the Eigen library in C++ to obtain the square root of a float square matrix:
MatrixXf gPrime(QUAD_EKF_NUM_STATES, QUAD_EKF_NUM_STATES);
gPrime.setIdentity();
auto sqrtG = gPrime.sqrt();
when I compile it I got the following error:
.../src/QuadEstimatorEKF.cpp:255:31: error: invalid use of incomplete type ‘const class Eigen::MatrixSquareRootReturnValue<Eigen::Matrix<float, -1, -1> >’
auto sqrtG = gPrime.sqrt()
^
In file included from /.../lib/Eigen/Core:346,
from /.../lib/Eigen/Dense:1,
from /.../src/QuadEstimatorEKF.h:11,
from /.../src/QuadEstimatorEKF.cpp:2:
/.../lib/Eigen/src/Core/util/ForwardDeclarations.h:286:34: note: declaration of ‘class Eigen::MatrixSquareRootReturnValue<Eigen::Matrix<float, -1, -1> >’
template<typename Derived> class MatrixSquareRootReturnValue;
^~~~~~~~~~~~~~~~~~~~~~~~~~~
What does 'incomplete type' mean and what should I do to fix it?
I'm using C++ 11 and g++ 8.1.1.
Searching for the documentation of that function reveals that it is part of Eigen-unsupported and (at the top)
To use this module, add
#include <unsupported/Eigen/MatrixFunctions>
at the start of your source file.
invalid use of incomplete type
means that the compiler only finds a declaration but a definition is required at the point of use.
Looks like its looking for something more than the P_old.sqrt(), you will need to have a look at what this object looks like, it might have getters/setters or atomic types you might need to use.
Read up about forward declarations to understand why you got this error, fixing it should be straightforward.

Missing GLSL functionality

I've moved a Visual Studio project to a different computer and now GLSL won't compile shaders which previously worked fine. It's getting stuck implicitly converting vec4s to vec3s and it tells me the 'dot' function is undefined, for example.
I'm using the GLSDK and the project builds correctly, and glGetString(GL_SHADING_LANGUAGE_VERSION) tells me 4.40. It's obviously something I don't have installed but used to, but I've searched around and cannot work out what.
Sorry, it sounds like your old drivers were being a bit too permissive. Your new drivers are correct in rejecting the shaders.
vec4 a = vec4(1.0, 2.0, 3.0, 4.0);
vec3 x = vec3(a); // Ok
// vec3 y = a; error
Indeed, if I run the implicit conversion through the reference compiler, I get the following error message:
ERROR: 0:4: '=' : cannot convert from '4-component vector of float' to '3-component vector of float'
ERROR: 1 compilation errors. No code generated.
Try validating your scripts with the reference compiler, it may catch some portability issues like these. Your only real option here is to fix the broken shaders.
What about dot()?
Try this:
#version 330
void main() {
vec4 x = vec4(1.0);
vec3 y = vec3(2.0);
float z = dot(x, y);
}
When I run the validator, I get:
ERROR: 0:5: 'dot' : no matching overloaded function found
ERROR: 1 compilation errors. No code generated.
The error here is that my arguments to dot() are the wrong type. Again, the problem is in my shader.

Converting Cocos2d code to ARC issues

I am trying to ARC enable a project and I am having a few issues when selecting files for ARC.
In the Ball class, the following line,
ballBody->SetUserData(self);
gives the error,
Cannot initialize a parameter of type 'void *' with an Ivalue of type 'Ball *const__strong'
In the Enemy.mm class, the following line,
enemyBody->SetUserData(enemySprite);
gives the error,
Cannot initialize a parameter of type 'void *' with an Ivalue of type 'CCPhysicsSprite*__strong'
In Enemy.h I have defined the above as:
b2Body* enemyBody;
CCPhysicsSprite* enemySprite; (in Enemy.m)
How can I solve these issues?
Bridge casting:
ballBody->SetUserData((__bridge void*)self);
enemyBody->SetUserData((__bridge void*)enemySprite);
and the reverse:
CCPhysicsSprite* enemySprite = (__bridge CCPhysicsSprite*)enemyBody->GetUserData();

Compilation error when registering callbacks in openGL

I've just started learning openGL a couple of hours ago for my work and have been tasked with rendering a concave polygon using tessellation. I'm trying to compile the following code:
#ifndef CALLBACK
#define CALLBACK
#endif
#include "GL/gl.h"
#include "GL/glu.h"
void CALLBACK beginCallback(GLenum which);
void drawHook()
{
GLUtesselator* tessObj = gluNewTess();
gluTessCallback(tessObj, GLU_TESS_BEGIN, beginCallback);
}
void CALLBACK beginCallback(GLenum which)
{
glBegin(which);
}
which I've gotten from the OpenGL Programming Guide, Seventh Edition, with the relevant chapter also being available online. But the following error is being returned:
hook.cc:28: error: invalid conversion from ‘void (*)(GLenum)’ to ‘void (*)()’
hook.cc:28: error: initializing argument 3 of ‘void gluTessCallback(GLUtesselator*, GLenum, void (*)())’
This error leads me to believe that the third argument of gluTessCallback should be a function that takes no arguments, yet the 'official' openGL reference states otherwise.
Am I missing something here or is the book incorrect?
The book is correct. You just have to cast the function pointer to void(*)(). It's needed because the type is not (and cannot be) precise.
gluTessCallback(tessObj, GLU_TESS_BEGIN, (void(*)())&beginCallback);
The documentation you linked states that the third argument of gluTessCallback is a parameterless function. (right after the heading Tessellation Callback Routines)
However, Example 11-1 casts the actual function pointer to a parameterless one:
gluTessCallback(tobj, GLU_TESS_ERROR, (GLvoid (*) ()) &errorCallback);
^^^^^^^^^^^^^^^

glutBitmapString/glutStrokeString seem to require const unsigned char* - strings don't work

Ubuntu 11.04, G++, freeglut and GLUT.
I don't understand this at all. Here's the error I get:
whatever.cc:315:59: error: cannot convert ‘std::string’ to ‘const unsigned char*’ for argument ‘2’ to ‘void glutStrokeString(void*, const unsigned char*)’
and if I try glutBitmapString:
whatever.cc:315:59: error: cannot convert ‘std::string’ to ‘const unsigned char*’ for argument ‘2’ to ‘void glutBitmapString(void*, const unsigned char*)’
Here's the relevant code (I think).
scoreStream << "Score: " << score << "\0";
scoreString = scoreStream.str();
// ...in another method:
glRasterPos2i(0, 0);
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, scoreString);
This answer tells me it should work, but it just doesn't.
Quote for those who don't want to jump:
// Draw blue text at screen coordinates (100, 120), where (0, 0) is the top-left of the
// screen in an 18-point Helvetica font
glRasterPos2i(100, 120);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, "text to render");
(Also, I have tried leaving a direct string like "text to render" in there. No dice.)
whatever.cc:315:64: error: invalid conversion from ‘const char*’ to ‘const unsigned char*’
I am confused. This is my first question on SO, as far as I remember, so apologies if it isn't too well put together. I'll provide any extra information I can.
There is no automatic conversion of an std::string to a char const*. However, you can use std::string::c_str() to get the char const* out of an std::string.
e.g. glutBitmapString(GLUT_BITMAP_HELVETICA_18, scoreString.c_str());
Note: The answer you linked to doesn't say that you can use std::string. The example it gives (glutBitmapString(GLUT_BITMAP_HELVETICA_18, "text to render");) uses a string literal, which is an array of char, not an std::string.
Remember that OpenGL is a C library, and std::string doesn't exist in C.