How to compile with DevIL? - g++

I'm trying to get a proper setup for 3D programs, and the next step is a texture loader. I am trying to use DevIL, but I can't seem to get it working, and no explanatory text seems to be up to date.
To start with, I'm just going to include a dump of my project: (Keep in mind that it just happens to be in this state, it's not all relevant; Also note that 'IOUtil.cpp' just defines 'Print', which prints to 'cout'. It didn't seem relevant, so I excluded it.)
Project Structure:
+- bin
+- include
| +- GLFW
| | +- glfw3.h
| | +- glfw3native.h
| +- IL
| | +- config.h
| | +- config.h.win
| | +- devil_internal_exports.h
| | +- il.h
| | +- il_wrap.h
| | +- ilu.h
| | +- ilu_region.h
| | +- ilut.h
| | +- ilut_config.h
+- lib
| +- unicode
| | +- DevIL.dll
| | +- DevIL.lib
| | +- ILU.dll
| | +- ILU.lib
| | +- ILUT.dll
| | +- ILUT.lib
| +- DevIL.dll
| +- DevIL.lib
| +- ILU.dll
| +- ILU.lib
| +- ILUT.dll
| +- ILUT.lib
| +- libglfw3dll.a
+- logs
+- src
| +- Main.exe
+- build.bat
+- run.bat
Main.exe:
#include <GLFW/glfw3.h>
#include <IL/il.h>
#include <IL/ilu.h>
#include <IL/ilut.h>
#include <stdlib.h>
#include <algorithm>
#include "util/IOUtil.cpp"
float SENSITIVITY = 0.25;
float cursorX = 0;
float cursorY = 0;
float camX = 0;
float camY = 0;
static void error_callback(int error, const char* description) {
Print("GLFW ERROR: " + (std::string)description);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
float dx = xpos - cursorX;
float dy = -ypos - cursorY;
cursorX = xpos;
cursorY = -ypos;
camX += dx * SENSITIVITY;
camY += dy * SENSITIVITY;
if (camX < 0) {
camX += 360;
}
if (camX > 360) {
camX -= 360;
}
camY = std::max(-90.0f, std::min(90.0f, camY));
}
int main(void) {
glfwSetErrorCallback(error_callback); //Set the error callback
Print("Initializing GLFW");
if (!glfwInit()) { //If initialization fails, crash
Print("GLFW initialization failed!");
exit(EXIT_FAILURE);
}
Print("Initializing window");
GLFWwindow* window = glfwCreateWindow(600, 600, "FPS", NULL, NULL); //Initialize the window
if (!window) { //If window initialization fails, crash
Print("Window initialization failed!");
glfwTerminate(); //Deinitialize
exit(EXIT_FAILURE);
}
ilutRenderer(ILUT_OPENGL); //Bind Texture Loader to Renderer
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); //Capture the cursor
glfwMakeContextCurrent(window); //Create the context
glfwSwapInterval(1); //Set the swap interval
glfwSetKeyCallback(window, key_callback); //Set the key callback
glfwSetCursorPosCallback(window, cursor_position_callback); //Set the cursor callback
//ilInit();
//ILuint image;
//ilGenImages(1, &image);
//ilBindImage(image);
//ilLoadImage("data/image.png");
glEnable(GL_TEXTURE_2D);
GLuint tex;
//tex = ilutGLBindTexImage();
Print("Beginning main loop");
while (!glfwWindowShouldClose(window)) { //Main loop, ends when window close is requested
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height); //Load the window dimensions
ratio = width / (float) height; //Calculate the aspect ratio
glViewport(0, 0, width, height); //Configure the viewport
glClear(GL_COLOR_BUFFER_BIT); //Clear the buffer
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float near = 1; //Near Clipping Plane
float far = 1000; //Far Clipping Plane
float fov = 1; //tan(fovAngle)/2.
glFrustum(-ratio * near * fov, ratio * near * fov, -fov, fov, near, far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Enable depth testing
//glEnable(GL_DEPTH_TEST);
//Set background color
//glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glPushMatrix();
glRotatef(camY, -1.0, 0.0, 0.0);
glRotatef(camX, 0.0, 1.0, 0.0);
//glTranslatef(0,0,-10);
glBegin(GL_QUADS);
//glColor3f(1.0, 0.0, 0.0);
glVertex3f(-1.0, -1.0, -10.0);
//glColor3f(1.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, -10.0);
//glColor3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -10.0);
//glColor3f(1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, -10.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-1.0, -1.0, 10.0);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, 10.0);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 10.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 10.0);
glEnd();
glPopMatrix();
glfwSwapBuffers(window); //Swap buffers
glfwPollEvents(); //Poll events
}
Print("Window close requested");
glfwDestroyWindow(window); //Close the window
Print("Deinitializing");
glfwTerminate(); //Deinitialize
exit(EXIT_SUCCESS); //End the program
}
build.bat:
g++ -c -v -Iinclude src/Main.cpp 2>logs/build.txt
g++ -v -o bin/main Main.o -Llib -lglfw3dll -lopengl32 -lgdi32 -lDevIL -lILU -lILUT 2>logs/link.txt
del Main.o
pause
Here is the log from line one of the batch file:
Using built-in specs.
COLLECT_GCC=g++
Target: mingw32
Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=mingw32 --without-pic --enable-shared --enable-static --with-gnu-ld --enable-lto --enable-libssp --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --with-gmp=/usr/src/pkg/gmp-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --with-mpfr= --with-system-zlib --with-gnu-as --enable-decimal-float=yes --enable-libgomp --enable-threads --with-libiconv-prefix=/mingw32 --with-libintl-prefix=/mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIME_T
Thread model: win32
gcc version 4.8.1 (GCC)
COLLECT_GCC_OPTIONS='-c' '-v' '-I' 'include' '-shared-libgcc' '-mtune=generic' '-march=pentiumpro'
c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/cc1plus.exe -quiet -v -I include -iprefix c:\mingw\bin\../lib/gcc/mingw32/4.8.1/ src/Main.cpp -quiet -dumpbase Main.cpp -mtune=generic -march=pentiumpro -auxbase Main -version -o C:\Users\MyUsername\AppData\Local\Temp\cccPMwk6.s
GNU C++ (GCC) version 4.8.1 (mingw32)
compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1
warning: MPC header version 1.0.1 differs from library version 1.0.2.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include/c++"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include/c++/mingw32"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include/c++/backward"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../../../../include"
ignoring duplicate directory "/mingw/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include-fixed"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../../../../mingw32/include"
ignoring duplicate directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
include
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include/c++
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include/c++/mingw32
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include/c++/backward
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include-fixed
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../mingw32/include
End of search list.
GNU C++ (GCC) version 4.8.1 (mingw32)
compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1
warning: MPC header version 1.0.1 differs from library version 1.0.2.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 1ebc2a6f92fbd3aadc367a20a63fdf9f
src/Main.cpp: In function 'int main()':
src/Main.cpp:95:14: error: expected unqualified-id before '=' token
float near = 1; //Near Clipping Plane
^
src/Main.cpp:96:13: error: expected unqualified-id before '=' token
float far = 1000; //Far Clipping Plane
^
src/Main.cpp:98:29: error: invalid type argument of unary '*' (have 'float')
glFrustum(-ratio * near * fov, ratio * near * fov, -fov, fov, near, far);
^
src/Main.cpp:98:49: error: invalid type argument of unary '*' (have 'float')
glFrustum(-ratio * near * fov, ratio * near * fov, -fov, fov, near, far);
^
src/Main.cpp:98:69: error: expected primary-expression before ',' token
glFrustum(-ratio * near * fov, ratio * near * fov, -fov, fov, near, far);
^
src/Main.cpp:98:74: error: expected primary-expression before ')' token
glFrustum(-ratio * near * fov, ratio * near * fov, -fov, fov, near, far);
^
Sorry for just dumping everything, but I don't know what else to do. I don't see why the IL includes would cause syntax errors in syntactically correct code.
Anyone know what's going wrong?

As it turns out, the issue was being caused by the names of the variables, which I realized when I noticed that 'fov' was not affected. Changing 'near' and 'far' to 'near_plane' and 'far_plane' (along with all references) fixed the issue.
Out of interest, what do 'near' and 'far' define in DevIL?

Related

Eigen on STM32 works only until a certain size

I am trying to use Eigen C++ library on STM32F4 Discovery embedded board to perform some matrix operations in the future, specifically to do some kalman filtering on sensor data.
I tried linking against the standard c++ library and even tried to compile the program using g++ arm compiler.
typedef Eigen::Matrix<float, 10, 10> Matrix10d;
Matrix10d mat1 = Matrix10d::Constant(10, 10, 1);
Matrix10d mat2 = Matrix10d::Constant(10, 10, 2);
Matrix10d result;
result = mat1 * mat2;
I can compile the same code if the matrix size as been set to 7. If I cross that then the code wont compile and the eigen gives me a warning that
warning: argument 1 value '4294967295' exceeds maximum object size 2147483647
These are the partial error messages I am getting
n function 'throw_std_bad_alloc,
inlined from 'check_size_for_overflow at bla/bla/Eigen/src/Core/util/Memory.h:289:24
Here is the memory allocation in Linker script I am using
/*
* STM32F407xG memory setup.
* Note: Use of ram1 and ram2 is mutually exclusive with use of ram0.
*/
MEMORY
{
flash0 : org = 0x08000000, len = 1M
flash1 : org = 0x00000000, len = 0
flash2 : org = 0x00000000, len = 0
flash3 : org = 0x00000000, len = 0
flash4 : org = 0x00000000, len = 0
flash5 : org = 0x00000000, len = 0
flash6 : org = 0x00000000, len = 0
flash7 : org = 0x00000000, len = 0
ram0 : org = 0x20000000, len = 128k /* SRAM1 + SRAM2 */
ram1 : org = 0x20000000, len = 112k /* SRAM1 */
ram2 : org = 0x2001C000, len = 16k /* SRAM2 */
ram3 : org = 0x00000000, len = 0
ram4 : org = 0x10000000, len = 64k /* CCM SRAM */
ram5 : org = 0x40024000, len = 4k /* BCKP SRAM */
ram6 : org = 0x00000000, len = 0
ram7 : org = 0x00000000, len = 0
}
I am just running STM32F4 discovery board with unchanged Chibios configuration
# Stack size to be allocated to the Cortex-M process stack. This stack is
# the stack used by the main() thread.
ifeq ($(USE_PROCESS_STACKSIZE),)
USE_PROCESS_STACKSIZE = 0x400
endif
Update
I was not able to reproduce this error anymore. The sad thing is that I didn't do anything to solve the issue.
arm-none-eabi-gcc -c -mcpu=cortex-m4 -O3 -Os -ggdb -fomit-frame-pointer -falign-functions=16 -ffunction-sections -fdata-sections -fno-common -flto -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -Wall -Wextra -Wundef -Wstrict-prototypes -Wa,-alms=build/lst/ -DCORTEX_USE_FPU=TRUE -DCHPRINTF_USE_FLOAT=TRUE -DTHUMB_PRESENT -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -MD -MP -MF .dep/build.d -I.
The above are the compiler options that I am using if anyone is interested.
Now I can multiply even 20x20 matrices with out any problem.
Matrix20d mat1 = Matrix20d::Constant(20, 20, 2);
// Multiply the matrix with a vector.
Vector20d vec = Vector20d::Constant(20, 1, 2);
Vector20d result;
systime_t startTime = chVTGetSystemTimeX();
result = mat1 * vec;
// Calculate the timedifference
systime_t endTime = chVTGetSystemTimeX();
systime_t timeDifference = chTimeDiffX(startTime, endTime);
chprintf(chp,"Time taken for the multiplication in milliseconds : %d\n", (int)timeDifference);
chprintf(chp, "System time : %d \n", startTime);
chprintf(chp, "Systime end : %d \n", endTime);
chprintf(chp, "Values in the vector : \n [");
for(Eigen::Index i=0; i < result.size();i++)
{
chprintf(chp, "%0.3f, ", result(i));
}
chprintf(chp, "] \n");
chThdSleepMilliseconds(1000);
It took about ~1ms to do the above computation.
I thought that there might be some problem with my compiler. So I tried with two versions of compilers
Version - 1
arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2017-q4-major) 7.2.1 20170904 (release) [ARM/embedded-7-branch revision 255204]
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Version-2
arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors 6-2017-q2-update) 6.3.1 20170620 (release) [ARM/embedded-6-branch revision 249437]
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

GLSL variables not storing?

I am learning GLSL through Unity and I recently came across a problem involving the storing of variables.
Shader "Shader" {
Properties{
_Hole_Position("hole_Position", Vector) = (0., 0., 0., 1.0)
_Hole_EventHorizonDistance("hole_EventHorizonDistance", Float) = 1.0
_DebugValue("debugValue", Float) = 0.0
}
SubShader{
Pass{
GLSLPROGRAM
uniform mat4 _Object2World;
//Variables
varying float debugValue;
varying vec4 pos;
varying vec4 hole_Position;
varying float hole_EventHorizonDistance = 1;
#ifdef VERTEX
void main()
{
pos = _Object2World * gl_Vertex;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#endif
#ifdef FRAGMENT
void main()
{
float dist = distance(vec4(pos.x, 0.0,pos.z, 1.0), vec4(hole_Position.x, 0.0, hole_Position.z, 1.0));
debugValue = dist;
if (dist < hole_EventHorizonDistance)
{
gl_FragColor = vec4(0.3, 0.3, 0.3, 1.0);
}
else
{
gl_FragColor = vec4(0.4, 0.6, 1.0, 1.0);
}
//gl_FragColor = vec4(hole_EventHorizonDistance, 0, 0, 1.0);
}
#endif
ENDGLSL
}
}
}
Now Hole_Position and EventHorizonDistance are changed from an outside C#-script with:
g.GetComponent<Renderer>().sharedMaterial.SetVector("_Hole_Position", new Vector4(transform.position.x, transform.position.y, transform.position.z, 1));
g.GetComponent<Renderer>().sharedMaterial.SetFloat("_Hole_EventHorizonDistance", 2);
this does not work as I intend it too (by changing the fragments color if its position is within 2 units from Hole_Position. However debugging with:
gl_FragColor = vec4(hole_EventHorizonDistance, 0, 0, 1.0);
seemingly suggests that EventHorizon is 0 at all times (the mesh tested on remains completely black), however debugging by getting and printing the variable from an outside (via
print(g.GetComponent<Renderer>().sharedMaterial.GetFloat("_Hole_EventHorizonDistance"));
) tells me EventHorizonDistance = 2. I cannot wrap my head around why this is the case, why is it so?

Clang, link time optimization fails for AVX horizontal add

I have a small piece of testing code which calculates the dot products of two vectors with a third vector using AVX instructions (A dot C and B dot C below). It also adds the two products, but that is just to make the function return something for this example.
#include <iostream>
#include <immintrin.h>
double compute(const double *x)
{
__m256d A = _mm256_loadu_pd(x);
__m256d B = _mm256_loadu_pd(x + 4);
__m256d C = _mm256_loadu_pd(x + 8);
__m256d c1 = _mm256_mul_pd(A, C);
__m256d c2 = _mm256_mul_pd(B, C);
__m256d tmp = _mm256_hadd_pd(c1, c2);
__m128d lo = _mm256_extractf128_pd(tmp, 0);
__m128d hi = _mm256_extractf128_pd(tmp, 1);
__m128d dotp = _mm_add_pd(lo, hi);
double y[2];
_mm_store_pd(y, dotp);
return y[0] + y[1];
}
int main(int argc, char *argv[])
{
const double v[12] = {0.3, 2.9, 1.3, 4.0, -1.0, -2.1, -3.0, -4.0, 0.0, 2.0, 1.3, 1.2};
double x = 0;
std::cout << "AVX" << std::endl;
x = compute(v);
std::cout << "x = " << x << std::endl;
return 0;
}
When I compile as
clang++ -O3 -mavx main.cc -o main
everything works fine. If I enable link time optimization:
clang++ -flto -O3 -mavx main.cc -o main
I get the following error "LLVM ERROR: Do not know how to split the result of this operator!". I have narrowed the culprit to the _mm256_hadd_pd statement. If this is exchanged with e.g. _m256_add_pd link time optimization works again. I realize that this is a silly example to use link-time optimization for, but the error ocurred in a different context where it link-time optimization is extremely helpful.
Can anyone explain what is going on here?

CGAL Solves a Quadratic Programming

I have a qp problem:
Minimize: -5x0 - x1 - 4x2 - 5x5 + 1000x0x2 + 1000x1x2 + 1000x0x3
+ 1000x1x3 + 1000x0x4 +1000x1x4
Subject to: x0>=0 x1>=0 x2>=0 x3>=0 x4>=0 x5>=0
x0+x1+x5<=5
x2+x3+x4<=5
The answer should be X0=0 X1=0 X2=5 X3=0 X4=0 X5=5 and obj=-45.
But CGAL gives me X0=5 X1=0 X2=0 X3=0 X4=0 X5=0 and obj=-25.
The code is pasted as follows:
Any suggestion would be appreciated.
Kelly
#include <iostream>
#include <climits>
#include <cassert>
#include <CGAL/basic.h>
#include <CGAL/QP_models.h>
#include <CGAL/QP_functions.h>
// choose exact integral type
#ifdef CGAL_USE_GMP
#include <CGAL/Gmpz.h>
typedef CGAL::Gmpz ET;
#else
#include <CGAL/MP_Float.h>
typedef CGAL::MP_Float ET;
#endif
using namespace std;
// program and solution types
typedef CGAL::Quadratic_program<int> Program;
typedef CGAL::Quadratic_program_solution<ET> Solution;
int
main(){
Program qp (CGAL::SMALLER, true, 0.0, false, 0.0);
qp.set_c(0, -5);
qp.set_c(1, -1);
qp.set_c(2, -4);
qp.set_c(5, -5);
int g = 1000;
qp.set_d(2, 0, g);
qp.set_d(2, 1, g);
qp.set_d(3, 0, g);
qp.set_d(3, 1, g);
qp.set_d(4, 0, g);
qp.set_d(4, 1, g);
int nRow = 0;
qp.set_a(0, nRow, 1.0);
qp.set_a(1, nRow, 1.0);
qp.set_a(5, nRow, 1.0);
qp.set_b(nRow, 5);
nRow++;
qp.set_a(2, nRow, 1.0);
qp.set_a(3, nRow, 1.0);
qp.set_a(4, nRow, 1.0);
qp.set_b(nRow, 5);
Solution s = CGAL::solve_quadratic_program(qp, ET());
assert (s.solves_quadratic_program(qp));
CGAL::print_nonnegative_quadratic_program(std::cout, qp, "first_qp");
std::cout << s;
return 0;
}
Since you matrix D (quadratic objective function) is not positive semi-definite, your result isn't so surprising. CGAL does not guarantee convergence towards a global minimum but towards a local one. What you obtain is a local minimum respecting the constraints you imposed.
If you set minimum bounds for x2 and x5 at 1 by writing qp.set_l(2,true,1); qp.set_l(5,true,1);, you will see that you converge towards the solution that you computed.

GLSL mat cannot access with variable index?

I declare a mat(3*3) in shader, based on OpenGL ES2.0 on Android.
Then I want to use two loops to go through this mat
mat3 GX = mat3( -1.0, 0.0, 1.0,
-2.0, 0.0, 2.0,
-1.0, 0.0, 1.0 );
for(int I=-1; I<=1; I = I + 1)
{
for(int J=-1; J<=1; J = J + 1)
{
GX[I+1][J+1]; // cannot get the value here!!!
}
}
Implementations of ES 2.0 are not required to support this use of array indices. In section "Appendix A: Limitations for ES 2.0" of the spec document, under "Variables", it says:
Support for indexing with a constant-index-expression is mandated.
Support for indexing with other values is not mandated.