Jump to content

  • Log In with Google      Sign In   
  • Create Account


Madd

Member Since 28 Jun 2012
Offline Last Active May 12 2014 04:19 PM

Topics I've Started

OpenGL doesn't draw triangle

06 May 2014 - 04:50 PM

I have done a lot of programming in OpenGL before, but somehow I am now unable to draw a triangle...

 

Here's the code (this is a one-file program). I have no idea which part of the code is failing so I'm posting all of it. All help appreaciated.

 

This code displays a blank, black window.

#include <MaddCompute.h>
#include <SDL2/SDL.h>
#include <iostream>
#include <GL/glew.h>
#include <string>
#include <fstream>
#include <stdlib.h>

using namespace std;

struct Vertex
{
	float pos[2];
	float color[3];
};

Vertex vertices[3] = {
	{{0.0, -1.0}, {1.0, 0.0, 0.0}},
	{{-1.0, 1.0}, {0.0, 1.0, 0.0}},
	{{1.0, 1.0}, {0.0, 0.0, 1.0}}
};

GLuint CompileShader(GLuint type, string filename)
{
	string code = "";
	ifstream ifs(filename.c_str());
	string line;
	while (getline(ifs, line))
	{
		code += line + '\n';
	};
	ifs.close();

	//cout << code << endl;
	//exit(1);

	GLuint shader = glCreateShader(type);
	char *str = (char*) code.c_str();
	glShaderSource(shader, 1, (const GLchar**)&str, NULL);
	glCompileShader(shader);

#if 0
	int len, ignore;
	glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
	char *buffer = new char[len+1];
	glGetShaderInfoLog(shader, len, &ignore, buffer);
	string log = buffer;
	delete buffer;

	ofstream ofs((filename + ".log").c_str());
	ofs << log << endl;
	ofs.close();
#endif

	return shader;
};

int main()
{
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		cerr << "SDL_Init: " << SDL_GetError() << endl;
		return 1;
	};

	SDL_Window *win = SDL_CreateWindow("MaddCompute OpenGL Shared Context",
						SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
						640, 480,
						SDL_WINDOW_OPENGL);
	if (win == NULL)
	{
		cerr << "SDL_CreateWindow: " << SDL_GetError() << endl;
		return 1;
	};

	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);

	SDL_GLContext glcontext = SDL_GL_CreateContext(win);
	if (glcontext == NULL)
	{
		cerr << "SDL_GL_CreateContext: " << SDL_GetError() << endl;
		return 1;
	};

	if (glewInit() != GLEW_OK)
	{
		cerr << "glewInit fail" << endl;
		return 1;
	};
	if (!glewIsSupported("GL_VERSION_3_0"))
	{
		cerr << "OpenGL 3.0 not supported!" << endl;
		return 1;
	};

	while (glGetError() != GL_NO_ERROR);

	GLuint vshader = CompileShader(GL_VERTEX_SHADER, "vertex.glsl");
	GLuint fshader = CompileShader(GL_FRAGMENT_SHADER, "fragment.glsl");
	
	GLuint prog = glCreateProgram();
	glAttachShader(prog, vshader);
	glAttachShader(prog, fshader);
	glLinkProgram(prog);

#if 0
	int len, ignore;
	glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
	char *buffer = new char[len+1];
	glGetProgramInfoLog(prog, len, &ignore, buffer);
	string log = buffer;
	delete buffer;

	ofstream ofs("link.log");
	ofs << log << endl;
	ofs.close();
#endif

	glUseProgram(prog);

	if (glGetError() != GL_NO_ERROR)
	{
		cerr << "Something went wrong in shader compilation." << endl;
		return 1;
	};

	GLint attrPos = glGetAttribLocation(prog, "pos");
	if (attrPos == -1)
	{
		cerr << "couldn't find 'pos' !" << endl;
		return 1;
	};
	GLint attrColor = glGetAttribLocation(prog, "color");
	if (attrColor == -1)
	{
		cerr << "couldn't find 'color' !" << endl;
		return 1;
	};

	GLuint vbo;
	glEnableVertexAttribArray(attrPos);
	glEnableVertexAttribArray(attrColor);

	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, 3*sizeof(Vertex), vertices, GL_STATIC_DRAW);
	glVertexAttribPointer(attrPos, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) 0);
	glVertexAttribPointer(attrColor, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, color));

	MC::Context *context = NULL;
	try
	{
		context = new MC::Context(MC::Context::OPENGL);
	}
	catch (MC::Error e)
	{
		cerr << "MC::Error: " << e.what() << endl;
		return 1;
	};

	bool quit = false;
	unsigned long time = SDL_GetTicks();
	while (!quit)
	{
		SDL_Event ev;
		while (SDL_PollEvent(&ev))
		{
			if (ev.type == SDL_QUIT)
			{
				quit = true;
			};
		};

		if ((SDL_GetTicks()-time) >= 33)
		{
			time = SDL_GetTicks();

			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glDrawArrays(GL_TRIANGLES, 0, 3);

			SDL_GL_SwapWindow(win);
		};
	};

	delete context;
	SDL_GL_DeleteContext(glcontext);
	SDL_DestroyWindow(win);
	SDL_Quit();

	return 0;
};

GLSL 1.5 and Sending Vertices

15 July 2013 - 08:46 AM

Hello.

 

In my old game, I used GLSL 1.2 in my shaders, and I could still call glColor*(), glTexCoord*(), etc. to pass info about vertices, and my vertex shader was still able to read it.

 

Now I'm trying to use GLSL 1.5, and I know that I cannot use glColor*() etc to pass such information any more, but it doesn't matter since I can use vertex attributes. However, gl_Vertex was also removed. This means that calling glVertex*() does not make sense anymore, so how do I send the vertices to the GPU?

 

Do I have to use VBOs? If so, is there any good tutorial with a nice example on how to do this?

 

Thank you.


Embedding Python in Code::Blocks

13 July 2013 - 12:56 PM

Hello.

 

I am writing a game engine and wanted to use Python for game scripts. My Main.cpp file is as follows:

// Main.cpp
// This software, including the source code, is release under the terms of
// the License.

#include "Common.h"
#include <sstream>
#include <iostream>
#include "Util/Vector.h"
#include <Python.h>

using namespace std;

int main(int argc, char *argv[])
{
    Py_SetProgramName(argv[0]);
    Py_Initialize();

    Py_Finalize();

    while (1);

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        stringstream ss;
        ss << "SDL failed to initialise: " << SDL_GetError();
        abortEngine(ss.str());
    };
    wasInitSDL = true;

    if (SDL_SetVideoMode(640, 480, 32, SDL_OPENGL) == NULL)
    {
        stringstream ss;
        ss << "Failed to open window: " << SDL_GetError();
        abortEngine(ss.str());
    };

    SDL_WM_SetCaption("Rage Engine", "Rage Engine");

    bool quitEngine = false;

    while (!quitEngine)
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
            {
                quitEngine = true;
            };
        };

        SDL_GL_SwapBuffers();
    };

    SDL_Quit();
    return 0;
};

As you can see, I include Python.h and call Py_Initialize() etc. I specified in the settings the path to Python libs and the include directory, and passed the -lpython26 flag. However, when I compile, I get the following linking errors:

Linking console executable: bin\RageEngine.exe
obj\Release\Main.o:Main.cpp:(.text+0x4c): undefined reference to `_imp__Py_SetProgramName'
obj\Release\Main.o:Main.cpp:(.text+0x52): undefined reference to `_imp__Py_Initialize'
obj\Release\Main.o:Main.cpp:(.text+0x58): undefined reference to `_imp__Py_Finalize'
collect2: ld returned 1 exit status

When I opened python26.lib with notepad I could see that there were symbol names like __imp_Py_Initialize (so the number of underscores disagrees with what the IDE is trying to link with). Does anyone know what may be causing this, and how I could fix it?

 

Thank you.


OpenGL shaders in Windows

14 January 2013 - 11:31 AM

Hello,

 

I was recently trying to compile my game engine on Windows, but I ran into a serious problem. I am using SDL and OpenGL, and I link with the following options:

 

-lmingw32 -lSDLmain -lSDL -lopengl32 -lglu32
 

I use the Code::Blocks IDE. When I compile the engine, I get the following errors:

 

obj\Debug\specfx\specfx.o||In function `meCreateEffect':|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|61|undefined reference to `glCreateProgram@0'|
obj\Debug\specfx\specfx.o||In function `meAttachShaderFromMemory':|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|97|undefined reference to `glCreateShader@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|98|undefined reference to `glCreateShader@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|99|undefined reference to `glShaderSource@16'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|100|undefined reference to `glCompileShader@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|102|undefined reference to `glGetShaderiv@12'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|106|undefined reference to `glGetShaderiv@12'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|108|undefined reference to `glGetShaderInfoLog@16'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|122|undefined reference to `glAttachShader@8'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|138|undefined reference to `glCreateShader@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|140|undefined reference to `glShaderSource@16'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|141|undefined reference to `glCompileShader@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|143|undefined reference to `glGetShaderiv@12'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|147|undefined reference to `glGetShaderiv@12'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|149|undefined reference to `glGetShaderInfoLog@16'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|162|undefined reference to `glAttachShader@8'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|172|undefined reference to `glCreateShader@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|174|undefined reference to `glShaderSource@16'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|175|undefined reference to `glCompileShader@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|177|undefined reference to `glGetShaderiv@12'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|181|undefined reference to `glGetShaderiv@12'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|183|undefined reference to `glGetShaderInfoLog@16'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|196|undefined reference to `glAttachShader@8'|
obj\Debug\specfx\specfx.o||In function `meLinkShaders':|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|210|undefined reference to `glLinkProgram@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|212|undefined reference to `glGetProgramiv@12'|
obj\Debug\specfx\specfx.o||In function `meApplyEffect':|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|228|undefined reference to `glUseProgram@4'|
obj\Debug\specfx\specfx.o||In function `meDeleteEffect':|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\specfx\specfx.c|266|undefined reference to `glDeleteProgram@4'|
obj\Debug\video\shader.o||In function `me_init_shader':|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|69|undefined reference to `glCreateShader@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|70|undefined reference to `glShaderSource@16'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|71|undefined reference to `glCompileShader@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|73|undefined reference to `glCreateShader@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|74|undefined reference to `glShaderSource@16'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|75|undefined reference to `glCompileShader@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|77|undefined reference to `glCreateProgram@0'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|78|undefined reference to `glAttachShader@8'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|79|undefined reference to `glAttachShader@8'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|80|undefined reference to `glLinkProgram@4'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|81|undefined reference to `glUseProgram@4'|
obj\Debug\video\shader.o||In function `meSaveShaderInfoLog':|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|101|undefined reference to `glGetProgramiv@12'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|105|undefined reference to `glGetProgramInfoLog@16'|
obj\Debug\video\shader.o||In function `me_shader_variablei':|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|126|undefined reference to `glGetUniformLocation@8'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|127|undefined reference to `glUniform1i@8'|
obj\Debug\video\shader.o||In function `me_shader_variabled':|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|140|undefined reference to `glGetUniformLocation@8'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|141|undefined reference to `glUniform1d'|
obj\Debug\video\shader.o||In function `me_shader_variablef':|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|154|undefined reference to `glGetUniformLocation@8'|
C:\Age of Prisoners stuff\Madd Engine (ME)\me-1.0.0\video\shader.c|155|undefined reference to `glUniform1f@8'|
||=== Build finished: 46 errors, 0 warnings ===|

Which looks like the shader API is not there :(.

 

Has anyone ran into this problem? Do you know how to solve it?

 

All help is appreciated.


Spherical to Cartesian conversion

05 January 2013 - 03:18 PM

Hello,

 

I am writing a 3D first person game, and i came to a problem with converting spherical coordinates to Cartesian coordinates. The problem is with how I use my axis (which i don't want to change because i got used to them). I know that even Wikipedia gives an example of the conversion, however, I have a slight problem with it.

 

The example on Wikipedia has two angles:

*phi = on the XY plane

*theta = on the ZY plane.

 

Also, the Wikipedia example assumes the X axis is positive backwards, Y axis is positive to the right, and the Z axis is positive up.

 

Reference: http://en.wikipedia.org/wiki/Spherical_coordinates

 

My problem is that my coordinates are as follows:

 

+X = left

+Y = up

+Z = forward

 

And I have two angles:

 

*phi = on the XZ plane

*theta = on the YZ plane.

 

I know it should be simple to figure out how that conversion goes based on the Wikipedia example, but I simply can't visualize it :(

 

Does anyone know how to convert that into Cartesian coordinates?

 

All help is appreciated.


PARTNERS