• Content count

  • Joined

  • Last visited

Community Reputation

150 Neutral

About Fugitive

  • Rank
  1. OpenGL OpenGL drawing between pixel

    Look at the third reply (by overlay) here: http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Main=51347&Number=265215#Post265215 It answers the question about what the OpenGL spec says about pixel rasterization in quite detail.
  2. Should I use everything on shaders?

  3. @robertosaget: Try these tutorials: http://www.opengl.org/wiki/Category:Tutorials @karwosts: Unfortunately, I do feel it is a genuine problem. Your experience is actually rare. For the vast majority of beginners, having to learn and then unlearn within a short span of time is not only difficult, its frustrating. Beginners tend to stick to what they have learned. I say this from experience as a lecturer. I agree with maspeir. Old OpenGL should be eliminated. There is very little resemblance between the classic OpenGL and the modern OpenGL to justify a 'transition'. They are altogether different and should be treated as such.
  4. glVertexPointer(3, GL_FLOAT, sizeof(textureStruct), (void*)offsetof(textureStruct,vertices)); Since you are now passing an alpha value, your glVertexPointer should be: glVertexPointer(4,...)
  5. As a Red-book owner, and Graphics person, here is my take on this: The Red book (7th Edition) is primarily dissapointing because its approach to teaching the distiction between modern OpenGL, and classic OpenGL is backwards. In general, people begining with OpenGL should be taught how to program in OpenGL 'the right way'first, and then be taught the older functions for reference. The Red book does it the other way around. After making sure it has taught how to program using the now deprecated 'glBegin glEnd', it notes that these are now deprecated and adds a couple of paragraphs explaining the newer functions, mostly without complete self-contained OpenGL 3 code that doesn't use deprecated features. What this results in, is beginners tend to stick to 'glBegin glEnd' et al. This is a disaster since its hard enough to learn OpenGL, much less the need to figure out what we should remember 'not' to use as well. I do graphics everyday, yet I'm still struggling to understand all the inctricate differences between all the OpenGL versions + extensions, and the differences on each platform. I need a book to make it clearer, rather than muddle it all up even more...
  6. OpenGL OpenGL 3.2 and SDL 1.3

    Could perhaps someone just confirm if they also get a blank screen, if nothing else?
  7. OpenGL OpenGL 3.2 and SDL 1.3

    Some source: tutorialmain.cpp #include <stdlib.h> #include <stdio.h> #include <string.h> #include "MyOpenGLExtensions.h" /* Ensure we are using opengl's core profile only */ //#define GL3_PROTOTYPES 1 //#include <GL3/gl3.h> #include <gl\gl.h> #include<gl\glext.h> #include <SDL\SDL.h> #include <math.h> #include "utils.h" void setupwindow(SDL_WindowID *window, SDL_GLContext *context) { if (SDL_Init(SDL_INIT_VIDEO) < 0) /* Initialize SDL's Video subsystem */ sdldie("Unable to initialize SDL"); /* Or die on error */ /* Request an opengl 3.2 context. * SDL doesn't have the ability to choose which profile at this time of writing, * but it should default to the core profile */ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); /* Enable double buffering with a 24bit Z buffer. * You may need to change this to 16 or 32 for your system */ SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); /* Enable multisampling for a nice antialiased effect */ SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); /* Create our window centered at 512x512 resolution */ *window = SDL_CreateWindow("Tutorial 4", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); if (!*window) /* Die if creation failed */ sdldie("Unable to create window"); /* Create our opengl context and attach it to our window */ *context = SDL_GL_CreateContext(*window); if (initGLExtensions() == false) { sdldie("Unable to load openGL extensions"); } /* This makes our buffer swap syncronized with the monitor's vertical refresh */ SDL_GL_SetSwapInterval(1); /* Enable Z depth testing so objects closest to the viewpoint are in front of objects further away */ glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); } void drawscene(SDL_WindowID window) { int i; /* Simple iterator */ GLuint vao, vbo[3]; /* Create handles for our Vertex Array Object and three Vertex Buffer Objects */ GLfloat projectionmatrix[16]; /* Our projection matrix starts with all 0s */ GLfloat modelmatrix[16]; /* Our model matrix */ /* An identity matrix we use to perform the equivalant of glLoadIdentity */ const GLfloat identitymatrix[16] = IDENTITY_MATRIX4; /* The four vericies of a tetrahedron */ const GLfloat tetrahedron[4][3] = { { 1.0, 1.0, 1.0 }, /* index 0 */ { -1.0, -1.0, 1.0 }, /* index 1 */ { -1.0, 1.0, -1.0 }, /* index 2 */ { 1.0, -1.0, -1.0 } }; /* index 3 */ /* Color information for each vertex */ const GLfloat colors[4][3] = { { 1.0, 0.0, 0.0 }, /* red */ { 0.0, 1.0, 0.0 }, /* green */ { 0.0, 0.0, 1.0 }, /* blue */ { 1.0, 1.0, 1.0 } }; /* white */ const GLubyte tetraindicies[6] = { 0, 1, 2, 3, 0, 1 }; /* These pointers will receive the contents of our shader source code files */ GLchar *vertexsource, *fragmentsource, *geometrysource; /* These are handles used to reference the shaders */ GLuint vertexshader, fragmentshader, geometryshader; /* This is a handle to the shader program */ GLuint shaderprogram; /* Allocate and assign a Vertex Array Object to our handle */ glGenVertexArrays(1, &vao); /* Bind our Vertex Array Object as the current used object */ glBindVertexArray(vao); /* Allocate and assign three Vertex Buffer Objects to our handle */ glGenBuffers(3, vbo); /* Bind our first VBO as being the active buffer and storing vertex attributes (coordinates) */ glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); /* Copy the vertex data from tetrahedron to our buffer */ /* 12 * sizeof(GLfloat) is the size of the tetrahedrom array, since it contains 12 GLfloat values */ glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tetrahedron, GL_STATIC_DRAW); /* Specify that our coordinate data is going into attribute index 0, and contains three floats per vertex */ glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); /* Enable attribute index 0 as being used */ glEnableVertexAttribArray(0); /* Bind our second VBO as being the active buffer and storing vertex attributes (colors) */ glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); /* Copy the color data from colors to our buffer */ /* 12 * sizeof(GLfloat) is the size of the colors array, since it contains 12 GLfloat values */ glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), colors, GL_STATIC_DRAW); /* Specify that our color data is going into attribute index 1, and contains three floats per vertex */ glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0); /* Enable attribute index 1 as being used */ glEnableVertexAttribArray(1); /* Bind our third VBO as being the active buffer and storing vertex array indicies */ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[2]); /* Copy the index data from tetraindicies to our buffer * 6 * sizeof(GLubyte) is the size of the index array, since it contains 6 GLbyte values */ glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLubyte), tetraindicies, GL_STATIC_DRAW); /* Read our shaders into the appropriate buffers */ vertexsource = filetobuf("tutorial4.vert"); geometrysource = filetobuf("tutorial4.geom"); fragmentsource = filetobuf("tutorial4.frag"); /* Assign our handles a "name" to new shader objects */ vertexshader = glCreateShader(GL_VERTEX_SHADER); geometryshader = glCreateShader(GL_GEOMETRY_SHADER); fragmentshader = glCreateShader(GL_FRAGMENT_SHADER); /* Associate the source code buffers with each handle */ glShaderSource(vertexshader, 1, (const GLchar**)&vertexsource, 0); glShaderSource(geometryshader, 1, (const GLchar**)&geometrysource, 0); glShaderSource(fragmentshader, 1, (const GLchar**)&fragmentsource, 0); /* Compile our shader objects */ glCompileShader(vertexshader); glCompileShader(geometryshader); glCompileShader(fragmentshader); /* Assign our program handle a "name" */ shaderprogram = glCreateProgram(); /* Attach our shaders to our program */ glAttachShader(shaderprogram, vertexshader); glAttachShader(shaderprogram, geometryshader); glAttachShader(shaderprogram, fragmentshader); /* Bind attribute 0 (coordinates) to in_Position and attribute 1 (colors) to in_Color */ glBindAttribLocation(shaderprogram, 0, "in_Position"); glBindAttribLocation(shaderprogram, 1, "in_Color"); /* Link our program, and set it as being actively used */ glLinkProgram(shaderprogram); glUseProgram(shaderprogram); /* Create our projection matrix with a 45 degree field of view * a width to height ratio of 1 and view from .1 to 100 infront of us */ perspective(projectionmatrix, 45.0, 1.0, 0.1, 100.0); /* Loop our display rotating our model more each time. */ for (i=0; i < 360; i++) { /* Load the identity matrix into modelmatrix. rotate the model, and move it back 5 */ memcpy(modelmatrix, identitymatrix, sizeof(GLfloat) * 16); rotate(modelmatrix, (GLfloat)i * -1.0, X_AXIS); rotate(modelmatrix, (GLfloat)i * 1.0, Y_AXIS); rotate(modelmatrix, (GLfloat)i * 0.5, Z_AXIS); translate(modelmatrix, 0, 0, -5.0); /* multiply our modelmatrix and our projectionmatrix. Results are stored in modelmatrix */ multiply4x4(modelmatrix, projectionmatrix); /* Bind our modelmatrix variable to be a uniform called mvpmatrix in our shaderprogram */ glUniformMatrix4fv(glGetUniformLocation(shaderprogram, "mvpmatrix"), 1, GL_FALSE, modelmatrix); /* Make our background black */ glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Invoke glDrawElements telling it to draw a triangle strip using 6 indicies */ glDrawElements(GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_BYTE, 0); /* Swap our buffers to make our changes visible */ SDL_GL_SwapWindow(window); /* Sleep for roughly 33 milliseconds between frames */ //SDL_Delay(33); } /* Cleanup all the things we bound and allocated */ glUseProgram(0); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glDetachShader(shaderprogram, vertexshader); glDetachShader(shaderprogram, geometryshader); glDetachShader(shaderprogram, fragmentshader); glDeleteProgram(shaderprogram); glDeleteShader(vertexshader); glDeleteShader(geometryshader); glDeleteShader(fragmentshader); glDeleteBuffers(3, vbo); glDeleteVertexArrays(1, &vao); free(vertexsource); free(geometrysource); free(fragmentsource); } void destroywindow(SDL_WindowID window, SDL_GLContext context) { SDL_GL_DeleteContext(context); SDL_DestroyWindow(window); SDL_Quit(); } /* Our program's entry point */ int main(int argc, char *argv[]) { SDL_WindowID mainwindow; /* Our window handle */ SDL_GLContext maincontext; /* Our opengl context handle */ /* Create our window, opengl context, etc... */ setupwindow(&mainwindow, &maincontext); /* Call our function that performs opengl operations */ drawscene(mainwindow); /* Delete our opengl context, destroy our window, and shutdown SDL */ destroywindow(mainwindow, maincontext); return 0; } myopenglextensions.h //--------------------------------------------------------------------------- #ifndef MyOpenGLExtensionsH #define MyOpenGLExtensionsH #ifndef GLHEADERS #define GLHEADERS #include <windows.h> #include <gl\gl.h> #include <gl\glext.h> #endif //int initGLExtensions(); //function prototype PFNGLCREATESHADERPROC glCreateShader; PFNGLSHADERSOURCEPROC glShaderSource; PFNGLCOMPILESHADERPROC glCompileShader; . . . PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv; int initGLExtensions() { glCreateShader = (PFNGLCREATESHADERPROC) wglGetProcAddress("glCreateShader"); glShaderSource = (PFNGLSHADERSOURCEPROC) wglGetProcAddress("glShaderSource"); glCompileShader = (PFNGLCOMPILESHADERPROC) wglGetProcAddress("glCompileShader"); glCreateProgram = (PFNGLCREATEPROGRAMPROC) wglGetProcAddress("glCreateProgram"); . . . glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSPROC) wglGetProcAddress("glDeleteVertexArrays"); glUniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) wglGetProcAddress("glUniformMatrix4fv"); if(glGenVertexArrays == NULL) { int x =0; } if (glCreateShader == NULL || glShaderSource == NULL || glCompileShader == NULL || glCreateProgram == NULL || glAttachShader == NULL || glLinkProgram == NULL || glUseProgram==NULL || glGetShaderiv == NULL || glDetachShader == NULL || glDeleteShader == NULL || glDeleteProgram == NULL || glIsShader == NULL || glIsProgram == NULL || glGetUniformLocation == NULL || glUniform1f == NULL || glUniform1i == NULL ) { return 0; } else if(glGenBuffers == NULL || glGenFramebuffers == NULL || glBindBuffer == NULL || glBindFramebuffer == NULL || glGenRenderbuffers == NULL || glBindRenderbuffer == NULL || glRenderbufferStorage == NULL || glFramebufferTexture2D == NULL || glFramebufferRenderbuffer == NULL || glCheckFramebufferStatus == NULL || glDeleteFramebuffers == NULL|| glDeleteRenderbuffers == NULL || glActiveTexture == NULL || glBufferData == NULL || glDrawBuffers == NULL || glDeleteBuffers == NULL ) { return 0; } else if(glVertexAttribPointer == NULL || glEnableVertexAttribArray == NULL || glDisableVertexAttribArray == NULL || glBindAttribLocation == NULL || glClampColor == NULL || glMultiTexCoord2f == NULL || glGenVertexArrays == NULL || glBindVertexArray==NULL || glDeleteVertexArrays==NULL) { return 0; } else if(glUniformMatrix4fv == NULL) { return 0; } return 1; } #endif utils.cpp #pragma hdrstop #include <stdio.h> #include <stdlib.h> #include <string.h> #define GL3_PROTOTYPES 1 #include <GL3/gl3.h> #include <SDL/SDL.h> #include <math.h> #include "utils.h" /* Multiply 4x4 matrix m1 by 4x4 matrix m2 and store the result in m1 */ void multiply4x4(GLfloat *m1, GLfloat *m2) { GLfloat temp[16]; int x,y; for (x=0; x < 4; x++) { for(y=0; y < 4; y++) { temp[y + (x*4)] = (m1[x*4] * m2[y]) + (m1[(x*4)+1] * m2[y+4]) + (m1[(x*4)+2] * m2[y+8]) + (m1[(x*4)+3] * m2[y+12]); } } memcpy(m1, temp, sizeof(GLfloat) * 16); } /* Generate a perspective view matrix using a field of view angle fov, * window aspect ratio, near and far clipping planes */ void perspective(GLfloat *matrix, GLfloat fov, GLfloat aspect, GLfloat nearz, GLfloat farz) { GLfloat range; range = tan(fov * 0.00872664625) * nearz; /* 0.00872664625 = PI/360 */ memset(matrix, 0, sizeof(GLfloat) * 16); matrix[0] = (2 * nearz) / ((range * aspect) - (-range * aspect)); matrix[5] = (2 * nearz) / (2 * range); matrix[10] = -(farz + nearz) / (farz - nearz); matrix[11] = -1; matrix[14] = -(2 * farz * nearz) / (farz - nearz); } /* Perform translation operations on a matrix */ void translate(GLfloat *matrix, GLfloat x, GLfloat y, GLfloat z) { GLfloat newmatrix[16] = IDENTITY_MATRIX4; newmatrix[12] = x; newmatrix[13] = y; newmatrix[14] = z; multiply4x4(matrix, newmatrix); } /* Rotate a matrix by an angle on a X, Y, or Z axis */ void rotate(GLfloat *matrix, GLfloat angle, AXIS axis) { const GLfloat d2r = 0.0174532925199; /* PI / 180 */ const int cos1[3] = { 5, 0, 0 }; const int cos2[3] = { 10, 10, 5 }; const int sin1[3] = { 6, 2, 1 }; const int sin2[3] = { 9, 8, 4 }; GLfloat newmatrix[16] = IDENTITY_MATRIX4; newmatrix[cos1[axis]] = cos(d2r * angle); newmatrix[sin1[axis]] = -sin(d2r * angle); newmatrix[sin2[axis]] = -newmatrix[sin1[axis]]; newmatrix[cos2[axis]] = newmatrix[cos1[axis]]; multiply4x4(matrix, newmatrix); } /* A simple function that will read a file into an allocated char pointer buffer */ char* filetobuf(char *file) { FILE *fptr; long length; char *buf; fptr = fopen(file, "r"); /* Open file for reading */ if (!fptr) /* Return NULL on failure */ return NULL; fseek(fptr, 0, SEEK_END); /* Seek to the end of the file */ length = ftell(fptr); /* Find out how many bytes into the file we are */ buf = (char*)malloc(length + 1); /* Allocate a buffer for the entire length of the file plus a null terminator */ fseek(fptr, 0, SEEK_SET); /* Go back to the beginning of the file */ fread(buf, length, 1, fptr); /* Read the contents of the file in to the buffer */ fclose(fptr); /* Close the file */ buf[length] = 0; /* Null terminator */ return buf; /* Return the buffer */ } /* A simple function that prints a message, the error code returned by SDL, and quits the application */ void sdldie(char *msg) { printf("%s: %s\n", msg, SDL_GetError()); SDL_Quit(); exit(1); }
  8. OpenGL OpenGL 3.2 and SDL 1.3

    The rapidshare link has a 'free user' download link, which allows you to download with only a 28 second wait. How about easyshare: http://www.easy-share.com/1909729478/SDL GL test 2.zip In any event, the program is supposed to show some triangles, and a geometry shader that turns it into a closed 3d shape. Instead, the window remains blank until the loop is over, then the window closes without any error or warning. What leads me to believe there is a memory leak is the compiled version on C++ builder. It compiles fine, however, simple things such as char *test="test"; string x; x=test; // <===commenting this line makes it show something again! would cause the program to display a blank screen. No crash nothing. If i comment those lines out, viola, I see the model again. Or something like trying to append more than 2000 bytes would suddenly cause the program to go 'blank' again. Reducing the number of bytes (and recompiling) would show the model. At first I thought there was something weird with the STL string class, but then if I removed all strings, it would give me weird errors further down the line (in code that was working before) related to heap damage when I would try to free memory previously allocated. To me, those things seem like standard memory-leaking symptoms...subtle, hard-to-track, and constantly moving. Whats worrying though, is that I have removed almost all of my own code, so the memory leak is either in the tutorial, or SDL! Kind regards, Fugi
  9. Hi guys, I have been trying to run some simple OpenGL 3.2 tutorials with SDL 1.3, and have been running into some strange problems. My hunch is that its is a memory-leak, but could be something else. The problem, stripped down to its bare minimum is Tutorial 4 (from here: http://www.opengl.org/wiki/Tutorial4:_Using_Indices_and_Geometry_Shaders_(C_/SDL) ) I have only included a header file to initialize opengl extensions (since the original tutorial doesnt do that), and removed gl3.h in favour of gl.h, and glext.h. In addition, here is the init code: if (initGLExtensions() == false) { sdldie("Unable to load openGL extensions"); } I am presented with a blank white screen. Here is the Visual Studio project: http://rapidshare.com/files/371298070/SDL_GL_test_2.zip (Its about 5-6 mb) The strange thing is, it compiles fine with Borland C++ Builder 2009 and 2010 (trial edition), but then causes memory leaks further down the line when I try to expand the code. Any ideas? Kind regards, Fugi
  10. summing pixels (non-clamped)

    If I try passing unclamped colours via glVertexm they get clamped, despite telling the glClampColor function not to. I guess the only way to really get full floating point addition is to use several floating point textures, and avoid adding via blend...
  11. problem with terrain

    Translucent objects require correct depth-sorting to work. Have a look at this: http://www.opengl.org/wiki/Alpha_Blending And this: http://www.opengl.org/resources/faq/technical/transparency.htm I hope that helps. Sajid
  12. @SwiftCoder: I think the OP wanted to maintain a fixed FPS while your code maintains a fixed game-speed (tempo) but variable fps?
  13. In plainest terms, you need a timer. Telling GLUT to call your function after a specific time will not work because, as you pointed out, the function may take longer to execute than the time you allotted it. In such a case, you need to calculate how long your frame is taking to render and update accordingly. I will present a generic example, for illustration purposes only. (actualy usage may vary based on various factors). Say you require a frame-rate of 30fps, each frame should take 1/30 (or 0.033) seconds. Say you call the required frame-rate ReqF. Hence ReqF = 0.33 At the begining of the frame you set the time to T. At the end of the frame, you set the time to NewT. The time your frame took to render is now DeltaT = NewT - T. Supposed you found out that DeltaT is 0.20. In that case you still have ReqF-DeltaT time left before you should call the next frame. You can either do some other calculations meanwhile, or simply put your function to sleep for that amount of time: Sleep(ReqF-DeltaT). though not directly related, have a look here on how to calculate FPS: http://www.gamedev.net/community/forums/topic.asp?topic_id=44929 I hope that helps. kind regards, Sajid
  14. OpenGL Beginning GLSL From Scratch!!

    GLSL only requires formulas because using GLSL means you will be writing graphics routines on your own rather than letting OpenGL do them for you mostly. Obviously, you should try to become familiar with OpenGL and Graphics Technology in general as much as you can before you attempt to write your own routines to do the same things from scratch (which is where GLSL comes in). That said, there is no harm in playing around with GLSL if you feel like you are capable. The best thing to do is start at the following tutorials (the best that I have found on the net so far) and see if you feel like you can dabble: http://www.clockworkcoders.com/oglsl/tutorials.html http://www.lighthouse3d.com/opengl/glsl/ In addition, if you decide to learn GLSL, I would strongly recommend starting out with a shader development tool like RenderMonkey. Those tools will let you focus on shaders alone rather than having to worry about writing full-fledge C/OpenGL code just so you can see shaders in action. I hope that helps. Kind regards, Sajid
  15. summing pixels (non-clamped)

    RPTD, If I create a floating point texture, and assign it values beyond 0-1 (i tried 1.6 and -1.6) and attach it to a quad, render it, and try reading the values in a shader, the values are not clamped. I can, indeed, in the shader read back values beyond 1.0 or less than 0.0. Granted they 'look' totally white or totally black respectively, but using an 'if' statement, i can verify that I do indeed receive floating values in the texture. When I apply the blend, and set the render target to a floating point texture, I expect it to add those values to the texture without clamping. I read the texture back again using exactly the same procedure, i.e, by applying it to a quad, rendering it, and trying to read the values via a shader. The values should not be clamped, no? But they still are. My 'hunch' is that since the 'blend' occurrs in fixed functionality, it will always clamp 0 to 1. Thanks for the suggestion about testing the texture values before rendering. I will try that, even though I have a feeling I will be dissapointed. Kind regards, Sajid