Sign in to follow this  
Zomgbie

OpenGL OpenGLBook.com sample - I'm not getting it to work!

Recommended Posts

Zomgbie    238
Hello fellows!

So, i've been updating my OpenGL knowledge and managed to get stuck. I've followed the guides over at [url="http://openglbook.com"]OpenGLBook.com[/url] and all was going well, untill i hit [url="http://openglbook.com/the-book/chapter-4-entering-the-third-dimension/"]Chapter 4[/url].
The code i've written is next to identical to their sample code, yet their code works and mine doesn't. I have even copy pasted my code into theirs, replacing every function with mine, and that still works. But my code alone is not up to the task sadly. There must be something i'm missing. Please, help me out!

My code
[code]

/*
* Free Glut test.
*/
#include "utils.h"

/* === DEFINITIONS === */
#define WINDOW_TITLE "Chapter 4: 3D"
#define SHADER_PROGRAM 0
#define SHADER_FRAGMENT 1
#define SHADER_VERTEX 2
#define BUFFER_VAO 0
#define BUFFER_VBO 1
#define BUFFER_IBO 2

/* === GLOBAL VARIABLES === */
GLuint projMatUniLoc, viewMatUniLoc, modelMatUniLoc;
GLuint bufferIds[3] = {0}, shaderIds[3] = {0};
int windowWidth, windowHeight, windowHandle;
float cubeRotation;
clock_t lastTime;
unsigned frames;

/* === MATRIXES === */
Matrix projectionMatrix, viewMatrix, modelMatrix;

/* === FUNCTION PROTOTYPES === */
void init(int, char **);
void initWindow(int, char **);
void resize(int, int);
void render(void);
void timer(int);
void idle(void);
void createCube(void);
void destroyCube(void);
void drawCube(void);

/*
* main():
* Main entry point of the application.
*/
int main(int argc, char **argv) {
/* Initialize everything we need */
init(argc, argv);

/* Start the glut loop */
glutMainLoop();

exit(EXIT_SUCCESS);
}

/*
* init(argc, argv):
* Initializes everything we need to start running.
*/
void init(int argc, char **argv) {
GLenum ret;
/* Create our openGL window */
windowWidth = 1024;
windowHeight = 768;
initWindow(argc, argv);

/* Initialize glew */
ret = glewInit();
if (GLEW_OK != ret) {
fprintf(stderr, "ERROR: %s\n", glewGetErrorString(ret));
exit(EXIT_FAILURE);
}

/* Get the running openGL version */
fprintf(stdout, "INFO: OpenGL Version: %s\n", glGetString(GL_VERSION));

/* Empty potential errors */
glGetError();

/* Clear the screen */
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

/* Enable depth test */
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
ExitOnGLError("ERROR: Could not set OpenGL depth testing");

/* Enable culling of faces */
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
ExitOnGLError("Error: Could not set OpenGL culling");

/* Setup out default matrices */
modelMatrix = IDENTITY_MATRIX;
projectionMatrix = IDENTITY_MATRIX;
viewMatrix = IDENTITY_MATRIX;
TranslateMatrix(&viewMatrix, 0, 0, -2);

/* Create our initial cube */
createCube();
}

/*
* initWindow(argc, argv):
* Create the openGL window for us.
*/
void initWindow(int argc, char **argv) {
/* Initialize glut */
glutInit(&argc, argv);
glutInitContextVersion(4, 0);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glutInitWindowSize(windowWidth, windowHeight);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

/* Create the window */
windowHandle = glutCreateWindow(WINDOW_TITLE);
if (windowHandle < 1) {
fprintf(stderr, "ERROR: Could not create window.\n");
exit(EXIT_FAILURE);
}

/* Set glut callback functions */
glutReshapeFunc(resize);
glutDisplayFunc(render);
glutIdleFunc(idle);
glutTimerFunc(0, timer, 0);
glutCloseFunc(destroyCube);
}

/*
* rezie(width, height):
* Resize the openGL window.
*/
void resize(int width, int height) {
/* Save dimensions */
windowWidth = width;
windowHeight = height;

/* Set viewport */
glViewport(0, 0, width, height);

/* Change our projection matrix */
projectionMatrix = CreateProjectionMatrix(60,
(float)windowWidth / windowHeight, 1.0f, 100.0f);
glUseProgram(shaderIds[0]);
glUniformMatrix4fv(projMatUniLoc, 1, GL_FALSE, projectionMatrix.m);
glUseProgram(0);
}

/*
* render()
* Render our openGL scene.
*/
void render(void) {
++frames;

/* Clear screen */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* Draw our cube */
drawCube();

/* Swap buffers and update display */
glutSwapBuffers();
glutPostRedisplay();
}

/*
* idle():
* Logic function.
*/
void idle(void) {
glutPostRedisplay();
}

/*
* timer(val):
* Timer to count FPS.
*/
void timer(int val) {
char fpsString[512];

if (val != 0) {
/* Allocate memory and copy string */
sprintf(fpsString, "%s: %d FPS @ %d x %d", WINDOW_TITLE,
frames * 4, windowWidth, windowHeight);

/* Set it as title */
glutSetWindowTitle(fpsString);
}

frames = 0;
glutTimerFunc(250, timer, 1);
}

/*
* createCube():
* Setup the cube.
*/
void createCube(void) {
const Vertex vertices[8] = {
{{-0.5f, -0.5f, 0.5f, 1}, {0, 0, 1, 1}},
{{-0.5f, 0.5f, 0.5f, 1}, {1, 0, 0, 1}},
{{0.5f, 0.5f, 0.5f, 1}, {0, 1, 0, 1}},
{{0.5f, -0.5f, 0.5f, 1}, {1, 1, 0, 1}},
{{-0.5f, -0.5f, -0.5f, 1}, {1, 1, 1, 1}},
{{-0.5f, 0.5f, -0.5f, 1}, {1, 0, 0, 1}},
{{0.5f, 0.5f, -0.5f, 1}, {1, 0, 1, 1}},
{{0.5f, -0.5f, -0.5f, 1}, {0, 0, 1, 1}}
};
const GLuint indices[36] = {
0, 2, 1, 0, 3, 2, 4, 3, 0, 4, 7, 3,
4, 1, 5, 4, 0, 1, 3, 6, 2, 3, 7, 6,
1, 6, 5, 1, 2, 6, 7, 5, 6, 7, 4, 5
};

/* Generate the shader program */
shaderIds[SHADER_PROGRAM] = glCreateProgram();
ExitOnGLError("ERROR: Could not create shader program");

/* Load and attach shaders */
shaderIds[SHADER_FRAGMENT] = LoadShader("fragment.glsl", GL_FRAGMENT_SHADER);
shaderIds[SHADER_VERTEX] = LoadShader("vertex.glsl", GL_VERTEX_SHADER);
glAttachShader(shaderIds[SHADER_PROGRAM], shaderIds[SHADER_FRAGMENT]);
glAttachShader(shaderIds[SHADER_PROGRAM], shaderIds[SHADER_VERTEX]);

/* Link shader program */
glLinkProgram(shaderIds[SHADER_PROGRAM]);
ExitOnGLError("ERROR: Could not link shader program");

/* Retrive shader uniforms */
modelMatUniLoc = glGetUniformLocation(shaderIds[SHADER_PROGRAM], "modelMatrix");
viewMatUniLoc = glGetUniformLocation(shaderIds[SHADER_PROGRAM], "viewMatrix");
projMatUniLoc = glGetUniformLocation(shaderIds[SHADER_PROGRAM], "projectionMatrix");
ExitOnGLError("ERROR: Could not get shader uniform locations");

/* Generate and bind our vertex array object */
glGenVertexArrays(1, &bufferIds[BUFFER_VAO]);
ExitOnGLError("ERROR: Could not generate VAO");
glBindVertexArray(bufferIds[BUFFER_VAO]);
ExitOnGLError("ERROR: Could not bind VAO");
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
ExitOnGLError("ERROR: Could not enable vertex attributes");

/* Bind VBO and send data */
glGenBuffers(2, &bufferIds[BUFFER_VBO]);
glBindBuffer(GL_ARRAY_BUFFER, bufferIds[BUFFER_VBO]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind VBO to VAO");

/* Set the attributes for our VAO */
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (GLvoid *)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(vertices[0]),
(GLvoid *)sizeof(vertices[0].Position));
ExitOnGLError("ERROR: Could not set VAO attributes");

/* Bind IBO and send data */
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferIds[BUFFER_IBO]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind IBO to VAO");
glBindVertexArray(0);
}

/*
* destroyCube():
* Destroy out little cubie.
*/
void destroyCube(void) {
/* Detach and delete shaders */
glDetachShader(shaderIds[SHADER_PROGRAM], shaderIds[SHADER_FRAGMENT]);
glDetachShader(shaderIds[SHADER_PROGRAM], shaderIds[SHADER_VERTEX]);
glDeleteShader(shaderIds[SHADER_FRAGMENT]);
glDeleteShader(shaderIds[SHADER_VERTEX]);
glDeleteProgram(shaderIds[SHADER_PROGRAM]);
ExitOnGLError("ERROR: Could not destroy shaders");

/* Delete and destroy buffers */
glDeleteBuffers(2, &bufferIds[BUFFER_VBO]);
glDeleteVertexArrays(1, &bufferIds[BUFFER_VAO]);
ExitOnGLError("ERROR: Could not destroy buffer objects");
}

/*
* drawCube():
* Draw the cube.
*/
void drawCube(void) {
float cubeAngle;
clock_t now;

/* Current time */
now = clock();
if (lastTime == 0)
lastTime = now;

/* Adjust rotation */
cubeRotation += 45.0f * ((float)(now - lastTime) / CLOCKS_PER_SEC);
cubeAngle = DegreesToRadians(cubeRotation);
lastTime = now;

/* Rotate the matrix */
modelMatrix = IDENTITY_MATRIX;
RotateAboutY(&modelMatrix, cubeAngle);
RotateAboutX(&modelMatrix, cubeAngle);

/* Use the shader program so we can manipluate */
glUseProgram(shaderIds[SHADER_PROGRAM]);

/* Adjust matrix uniform locations */
glUniformMatrix4fv(modelMatUniLoc, 1, GL_FALSE, modelMatrix.m);
glUniformMatrix4fv(viewMatUniLoc, 1, GL_FALSE, viewMatrix.m);
ExitOnGLError("ERROR: Could not set the shader uniforms");

/* Bind and draw the VAO */
glBindVertexArray(bufferIds[BUFFER_VAO]);
ExitOnGLError("ERROR: Could not bind VAO for drawing");
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (GLvoid *)0);
ExitOnGLError("ERROR: Could not draw the cube");

/* Done manipulating */
glBindVertexArray(0);
glUseProgram(0);
}

[/code]


Their code
[code]

/* Copyright (C) 2011 by Eddy Luten

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "Utils.h"
#define WINDOW_TITLE_PREFIX "Chapter 4"

int CurrentWidth = 800,
CurrentHeight = 600,
WindowHandle = 0;

unsigned FrameCount = 0;

GLuint
ProjectionMatrixUniformLocation,
ViewMatrixUniformLocation,
ModelMatrixUniformLocation,
BufferIds[3] = { 0 },
ShaderIds[3] = { 0 };

Matrix
ProjectionMatrix,
ViewMatrix,
ModelMatrix;

float CubeRotation = 0;
clock_t LastTime = 0;

void Initialize(int, char*[]);
void InitWindow(int, char*[]);
void ResizeFunction(int, int);
void RenderFunction(void);
void TimerFunction(int);
void IdleFunction(void);
void CreateCube(void);
void DestroyCube(void);
void DrawCube(void);

int main(int argc, char* argv[])
{
Initialize(argc, argv);

glutMainLoop();

exit(EXIT_SUCCESS);
}

void Initialize(int argc, char* argv[])
{
GLenum GlewInitResult;

InitWindow(argc, argv);

glewExperimental = GL_TRUE;
GlewInitResult = glewInit();

if (GLEW_OK != GlewInitResult) {
fprintf(
stderr,
"ERROR: %s\n",
glewGetErrorString(GlewInitResult)
);
exit(EXIT_FAILURE);
}

fprintf(
stdout,
"INFO: OpenGL Version: %s\n",
glGetString(GL_VERSION)
);

glGetError();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
ExitOnGLError("ERROR: Could not set OpenGL depth testing options");

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
ExitOnGLError("ERROR: Could not set OpenGL culling options");

ModelMatrix = IDENTITY_MATRIX;
ProjectionMatrix = IDENTITY_MATRIX;
ViewMatrix = IDENTITY_MATRIX;
TranslateMatrix(&ViewMatrix, 0, 0, -2);

CreateCube();
}

void InitWindow(int argc, char* argv[])
{
glutInit(&argc, argv);

glutInitContextVersion(4, 0);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);

glutSetOption(
GLUT_ACTION_ON_WINDOW_CLOSE,
GLUT_ACTION_GLUTMAINLOOP_RETURNS
);

glutInitWindowSize(CurrentWidth, CurrentHeight);

glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);

if(WindowHandle < 1) {
fprintf(
stderr,
"ERROR: Could not create a new rendering window.\n"
);
exit(EXIT_FAILURE);
}

glutReshapeFunc(ResizeFunction);
glutDisplayFunc(RenderFunction);
glutIdleFunc(IdleFunction);
glutTimerFunc(0, TimerFunction, 0);
glutCloseFunc(DestroyCube);
}

void ResizeFunction(int Width, int Height)
{
CurrentWidth = Width;
CurrentHeight = Height;
glViewport(0, 0, CurrentWidth, CurrentHeight);
ProjectionMatrix =
CreateProjectionMatrix(
60,
(float)CurrentWidth / CurrentHeight,
1.0f,
100.0f
);

glUseProgram(ShaderIds[0]);
glUniformMatrix4fv(ProjectionMatrixUniformLocation, 1, GL_FALSE, ProjectionMatrix.m);
glUseProgram(0);
}

void RenderFunction(void)
{
++FrameCount;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

DrawCube();

glutSwapBuffers();
glutPostRedisplay();
}

void IdleFunction(void)
{
glutPostRedisplay();
}

void TimerFunction(int Value)
{
if (0 != Value) {
char* TempString = (char*)
malloc(512 + strlen(WINDOW_TITLE_PREFIX));

sprintf(
TempString,
"%s: %d Frames Per Second @ %d x %d",
WINDOW_TITLE_PREFIX,
FrameCount * 4,
CurrentWidth,
CurrentHeight
);

glutSetWindowTitle(TempString);
free(TempString);
}

FrameCount = 0;
glutTimerFunc(250, TimerFunction, 1);
}

void CreateCube()
{
const Vertex VERTICES[8] =
{
{ { -.5f, -.5f, .5f, 1 }, { 0, 0, 1, 1 } },
{ { -.5f, .5f, .5f, 1 }, { 1, 0, 0, 1 } },
{ { .5f, .5f, .5f, 1 }, { 0, 1, 0, 1 } },
{ { .5f, -.5f, .5f, 1 }, { 1, 1, 0, 1 } },
{ { -.5f, -.5f, -.5f, 1 }, { 1, 1, 1, 1 } },
{ { -.5f, .5f, -.5f, 1 }, { 1, 0, 0, 1 } },
{ { .5f, .5f, -.5f, 1 }, { 1, 0, 1, 1 } },
{ { .5f, -.5f, -.5f, 1 }, { 0, 0, 1, 1 } }
};

const GLuint INDICES[36] =
{
0,2,1, 0,3,2,
4,3,0, 4,7,3,
4,1,5, 4,0,1,
3,6,2, 3,7,6,
1,6,5, 1,2,6,
7,5,6, 7,4,5
};

ShaderIds[0] = glCreateProgram();
ExitOnGLError("ERROR: Could not create the shader program");
{
ShaderIds[1] = LoadShader("SimpleShader.fragment.glsl", GL_FRAGMENT_SHADER);
ShaderIds[2] = LoadShader("SimpleShader.vertex.glsl", GL_VERTEX_SHADER);
glAttachShader(ShaderIds[0], ShaderIds[1]);
glAttachShader(ShaderIds[0], ShaderIds[2]);
}
glLinkProgram(ShaderIds[0]);
ExitOnGLError("ERROR: Could not link the shader program");

ModelMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ModelMatrix");
ViewMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ViewMatrix");
ProjectionMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ProjectionMatrix");
ExitOnGLError("ERROR: Could not get shader uniform locations");

glGenVertexArrays(1, &BufferIds[0]);
ExitOnGLError("ERROR: Could not generate the VAO");
glBindVertexArray(BufferIds[0]);
ExitOnGLError("ERROR: Could not bind the VAO");

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
ExitOnGLError("ERROR: Could not enable vertex attributes");

glGenBuffers(2, &BufferIds[1]);
ExitOnGLError("ERROR: Could not generate the buffer objects");

glBindBuffer(GL_ARRAY_BUFFER, BufferIds[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind the VBO to the VAO");

glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)sizeof(VERTICES[0].Position));
ExitOnGLError("ERROR: Could not set VAO attributes");

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferIds[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(INDICES), INDICES, GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind the IBO to the VAO");

glBindVertexArray(0);
}

void DestroyCube()
{
glDetachShader(ShaderIds[0], ShaderIds[1]);
glDetachShader(ShaderIds[0], ShaderIds[2]);
glDeleteShader(ShaderIds[1]);
glDeleteShader(ShaderIds[2]);
glDeleteProgram(ShaderIds[0]);
ExitOnGLError("ERROR: Could not destroy the shaders");

glDeleteBuffers(2, &BufferIds[1]);
glDeleteVertexArrays(1, &BufferIds[0]);
ExitOnGLError("ERROR: Could not destroy the buffer objects");
}

void DrawCube(void)
{
float CubeAngle;
clock_t Now = clock();

if (LastTime == 0)
LastTime = Now;

CubeRotation += 45.0f * ((float)(Now - LastTime) / CLOCKS_PER_SEC);
CubeAngle = DegreesToRadians(CubeRotation);
LastTime = Now;

ModelMatrix = IDENTITY_MATRIX;
RotateAboutY(&ModelMatrix, CubeAngle);
RotateAboutX(&ModelMatrix, CubeAngle);

glUseProgram(ShaderIds[0]);
ExitOnGLError("ERROR: Could not use the shader program");

glUniformMatrix4fv(ModelMatrixUniformLocation, 1, GL_FALSE, ModelMatrix.m);
glUniformMatrix4fv(ViewMatrixUniformLocation, 1, GL_FALSE, ViewMatrix.m);
ExitOnGLError("ERROR: Could not set the shader uniforms");

glBindVertexArray(BufferIds[0]);
ExitOnGLError("ERROR: Could not bind the VAO for drawing purposes");

glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (GLvoid*)0);
ExitOnGLError("ERROR: Could not draw the cube");

glBindVertexArray(0);
glUseProgram(0);
}[/code]

Share this post


Link to post
Share on other sites
Paradoxia    116
Hi Zomgbie,

What exactly doesn't work? You could provide some more specific information besides simply pasting the code from the samples:

Do you get any errors, black screens, can you tell me anything about the output? What platform are you on? What hardware are you using? Are you checking glGetError along the way?



The more info you can provide, the better.


Eddy

[quote name='Zomgbie' timestamp='1313669345' post='4850721']
Hello fellows!

So, i've been updating my OpenGL knowledge and managed to get stuck. I've followed the guides over at [url="http://openglbook.com"]OpenGLBook.com[/url] and all was going well, untill i hit [url="http://openglbook.com/the-book/chapter-4-entering-the-third-dimension/"]Chapter 4[/url].
The code i've written is next to identical to their sample code, yet their code works and mine doesn't. I have even copy pasted my code into theirs, replacing every function with mine, and that still works. But my code alone is not up to the task sadly. There must be something i'm missing. Please, help me out!

My code
[code]

/*
* Free Glut test.
*/
#include "utils.h"

/* === DEFINITIONS === */
#define WINDOW_TITLE "Chapter 4: 3D"
#define SHADER_PROGRAM 0
#define SHADER_FRAGMENT 1
#define SHADER_VERTEX 2
#define BUFFER_VAO 0
#define BUFFER_VBO 1
#define BUFFER_IBO 2

/* === GLOBAL VARIABLES === */
GLuint projMatUniLoc, viewMatUniLoc, modelMatUniLoc;
GLuint bufferIds[3] = {0}, shaderIds[3] = {0};
int windowWidth, windowHeight, windowHandle;
float cubeRotation;
clock_t lastTime;
unsigned frames;

/* === MATRIXES === */
Matrix projectionMatrix, viewMatrix, modelMatrix;

/* === FUNCTION PROTOTYPES === */
void init(int, char **);
void initWindow(int, char **);
void resize(int, int);
void render(void);
void timer(int);
void idle(void);
void createCube(void);
void destroyCube(void);
void drawCube(void);

/*
* main():
* Main entry point of the application.
*/
int main(int argc, char **argv) {
/* Initialize everything we need */
init(argc, argv);

/* Start the glut loop */
glutMainLoop();

exit(EXIT_SUCCESS);
}

/*
* init(argc, argv):
* Initializes everything we need to start running.
*/
void init(int argc, char **argv) {
GLenum ret;
/* Create our openGL window */
windowWidth = 1024;
windowHeight = 768;
initWindow(argc, argv);

/* Initialize glew */
ret = glewInit();
if (GLEW_OK != ret) {
fprintf(stderr, "ERROR: %s\n", glewGetErrorString(ret));
exit(EXIT_FAILURE);
}

/* Get the running openGL version */
fprintf(stdout, "INFO: OpenGL Version: %s\n", glGetString(GL_VERSION));

/* Empty potential errors */
glGetError();

/* Clear the screen */
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

/* Enable depth test */
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
ExitOnGLError("ERROR: Could not set OpenGL depth testing");

/* Enable culling of faces */
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
ExitOnGLError("Error: Could not set OpenGL culling");

/* Setup out default matrices */
modelMatrix = IDENTITY_MATRIX;
projectionMatrix = IDENTITY_MATRIX;
viewMatrix = IDENTITY_MATRIX;
TranslateMatrix(&viewMatrix, 0, 0, -2);

/* Create our initial cube */
createCube();
}

/*
* initWindow(argc, argv):
* Create the openGL window for us.
*/
void initWindow(int argc, char **argv) {
/* Initialize glut */
glutInit(&argc, argv);
glutInitContextVersion(4, 0);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glutInitWindowSize(windowWidth, windowHeight);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

/* Create the window */
windowHandle = glutCreateWindow(WINDOW_TITLE);
if (windowHandle < 1) {
fprintf(stderr, "ERROR: Could not create window.\n");
exit(EXIT_FAILURE);
}

/* Set glut callback functions */
glutReshapeFunc(resize);
glutDisplayFunc(render);
glutIdleFunc(idle);
glutTimerFunc(0, timer, 0);
glutCloseFunc(destroyCube);
}

/*
* rezie(width, height):
* Resize the openGL window.
*/
void resize(int width, int height) {
/* Save dimensions */
windowWidth = width;
windowHeight = height;

/* Set viewport */
glViewport(0, 0, width, height);

/* Change our projection matrix */
projectionMatrix = CreateProjectionMatrix(60,
(float)windowWidth / windowHeight, 1.0f, 100.0f);
glUseProgram(shaderIds[0]);
glUniformMatrix4fv(projMatUniLoc, 1, GL_FALSE, projectionMatrix.m);
glUseProgram(0);
}

/*
* render()
* Render our openGL scene.
*/
void render(void) {
++frames;

/* Clear screen */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* Draw our cube */
drawCube();

/* Swap buffers and update display */
glutSwapBuffers();
glutPostRedisplay();
}

/*
* idle():
* Logic function.
*/
void idle(void) {
glutPostRedisplay();
}

/*
* timer(val):
* Timer to count FPS.
*/
void timer(int val) {
char fpsString[512];

if (val != 0) {
/* Allocate memory and copy string */
sprintf(fpsString, "%s: %d FPS @ %d x %d", WINDOW_TITLE,
frames * 4, windowWidth, windowHeight);

/* Set it as title */
glutSetWindowTitle(fpsString);
}

frames = 0;
glutTimerFunc(250, timer, 1);
}

/*
* createCube():
* Setup the cube.
*/
void createCube(void) {
const Vertex vertices[8] = {
{{-0.5f, -0.5f, 0.5f, 1}, {0, 0, 1, 1}},
{{-0.5f, 0.5f, 0.5f, 1}, {1, 0, 0, 1}},
{{0.5f, 0.5f, 0.5f, 1}, {0, 1, 0, 1}},
{{0.5f, -0.5f, 0.5f, 1}, {1, 1, 0, 1}},
{{-0.5f, -0.5f, -0.5f, 1}, {1, 1, 1, 1}},
{{-0.5f, 0.5f, -0.5f, 1}, {1, 0, 0, 1}},
{{0.5f, 0.5f, -0.5f, 1}, {1, 0, 1, 1}},
{{0.5f, -0.5f, -0.5f, 1}, {0, 0, 1, 1}}
};
const GLuint indices[36] = {
0, 2, 1, 0, 3, 2, 4, 3, 0, 4, 7, 3,
4, 1, 5, 4, 0, 1, 3, 6, 2, 3, 7, 6,
1, 6, 5, 1, 2, 6, 7, 5, 6, 7, 4, 5
};

/* Generate the shader program */
shaderIds[SHADER_PROGRAM] = glCreateProgram();
ExitOnGLError("ERROR: Could not create shader program");

/* Load and attach shaders */
shaderIds[SHADER_FRAGMENT] = LoadShader("fragment.glsl", GL_FRAGMENT_SHADER);
shaderIds[SHADER_VERTEX] = LoadShader("vertex.glsl", GL_VERTEX_SHADER);
glAttachShader(shaderIds[SHADER_PROGRAM], shaderIds[SHADER_FRAGMENT]);
glAttachShader(shaderIds[SHADER_PROGRAM], shaderIds[SHADER_VERTEX]);

/* Link shader program */
glLinkProgram(shaderIds[SHADER_PROGRAM]);
ExitOnGLError("ERROR: Could not link shader program");

/* Retrive shader uniforms */
modelMatUniLoc = glGetUniformLocation(shaderIds[SHADER_PROGRAM], "modelMatrix");
viewMatUniLoc = glGetUniformLocation(shaderIds[SHADER_PROGRAM], "viewMatrix");
projMatUniLoc = glGetUniformLocation(shaderIds[SHADER_PROGRAM], "projectionMatrix");
ExitOnGLError("ERROR: Could not get shader uniform locations");

/* Generate and bind our vertex array object */
glGenVertexArrays(1, &bufferIds[BUFFER_VAO]);
ExitOnGLError("ERROR: Could not generate VAO");
glBindVertexArray(bufferIds[BUFFER_VAO]);
ExitOnGLError("ERROR: Could not bind VAO");
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
ExitOnGLError("ERROR: Could not enable vertex attributes");

/* Bind VBO and send data */
glGenBuffers(2, &bufferIds[BUFFER_VBO]);
glBindBuffer(GL_ARRAY_BUFFER, bufferIds[BUFFER_VBO]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind VBO to VAO");

/* Set the attributes for our VAO */
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (GLvoid *)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(vertices[0]),
(GLvoid *)sizeof(vertices[0].Position));
ExitOnGLError("ERROR: Could not set VAO attributes");

/* Bind IBO and send data */
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferIds[BUFFER_IBO]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind IBO to VAO");
glBindVertexArray(0);
}

/*
* destroyCube():
* Destroy out little cubie.
*/
void destroyCube(void) {
/* Detach and delete shaders */
glDetachShader(shaderIds[SHADER_PROGRAM], shaderIds[SHADER_FRAGMENT]);
glDetachShader(shaderIds[SHADER_PROGRAM], shaderIds[SHADER_VERTEX]);
glDeleteShader(shaderIds[SHADER_FRAGMENT]);
glDeleteShader(shaderIds[SHADER_VERTEX]);
glDeleteProgram(shaderIds[SHADER_PROGRAM]);
ExitOnGLError("ERROR: Could not destroy shaders");

/* Delete and destroy buffers */
glDeleteBuffers(2, &bufferIds[BUFFER_VBO]);
glDeleteVertexArrays(1, &bufferIds[BUFFER_VAO]);
ExitOnGLError("ERROR: Could not destroy buffer objects");
}

/*
* drawCube():
* Draw the cube.
*/
void drawCube(void) {
float cubeAngle;
clock_t now;

/* Current time */
now = clock();
if (lastTime == 0)
lastTime = now;

/* Adjust rotation */
cubeRotation += 45.0f * ((float)(now - lastTime) / CLOCKS_PER_SEC);
cubeAngle = DegreesToRadians(cubeRotation);
lastTime = now;

/* Rotate the matrix */
modelMatrix = IDENTITY_MATRIX;
RotateAboutY(&modelMatrix, cubeAngle);
RotateAboutX(&modelMatrix, cubeAngle);

/* Use the shader program so we can manipluate */
glUseProgram(shaderIds[SHADER_PROGRAM]);

/* Adjust matrix uniform locations */
glUniformMatrix4fv(modelMatUniLoc, 1, GL_FALSE, modelMatrix.m);
glUniformMatrix4fv(viewMatUniLoc, 1, GL_FALSE, viewMatrix.m);
ExitOnGLError("ERROR: Could not set the shader uniforms");

/* Bind and draw the VAO */
glBindVertexArray(bufferIds[BUFFER_VAO]);
ExitOnGLError("ERROR: Could not bind VAO for drawing");
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (GLvoid *)0);
ExitOnGLError("ERROR: Could not draw the cube");

/* Done manipulating */
glBindVertexArray(0);
glUseProgram(0);
}

[/code]


Their code
[code]

/* Copyright (C) 2011 by Eddy Luten

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "Utils.h"
#define WINDOW_TITLE_PREFIX "Chapter 4"

int CurrentWidth = 800,
CurrentHeight = 600,
WindowHandle = 0;

unsigned FrameCount = 0;

GLuint
ProjectionMatrixUniformLocation,
ViewMatrixUniformLocation,
ModelMatrixUniformLocation,
BufferIds[3] = { 0 },
ShaderIds[3] = { 0 };

Matrix
ProjectionMatrix,
ViewMatrix,
ModelMatrix;

float CubeRotation = 0;
clock_t LastTime = 0;

void Initialize(int, char*[]);
void InitWindow(int, char*[]);
void ResizeFunction(int, int);
void RenderFunction(void);
void TimerFunction(int);
void IdleFunction(void);
void CreateCube(void);
void DestroyCube(void);
void DrawCube(void);

int main(int argc, char* argv[])
{
Initialize(argc, argv);

glutMainLoop();

exit(EXIT_SUCCESS);
}

void Initialize(int argc, char* argv[])
{
GLenum GlewInitResult;

InitWindow(argc, argv);

glewExperimental = GL_TRUE;
GlewInitResult = glewInit();

if (GLEW_OK != GlewInitResult) {
fprintf(
stderr,
"ERROR: %s\n",
glewGetErrorString(GlewInitResult)
);
exit(EXIT_FAILURE);
}

fprintf(
stdout,
"INFO: OpenGL Version: %s\n",
glGetString(GL_VERSION)
);

glGetError();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
ExitOnGLError("ERROR: Could not set OpenGL depth testing options");

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
ExitOnGLError("ERROR: Could not set OpenGL culling options");

ModelMatrix = IDENTITY_MATRIX;
ProjectionMatrix = IDENTITY_MATRIX;
ViewMatrix = IDENTITY_MATRIX;
TranslateMatrix(&ViewMatrix, 0, 0, -2);

CreateCube();
}

void InitWindow(int argc, char* argv[])
{
glutInit(&argc, argv);

glutInitContextVersion(4, 0);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);

glutSetOption(
GLUT_ACTION_ON_WINDOW_CLOSE,
GLUT_ACTION_GLUTMAINLOOP_RETURNS
);

glutInitWindowSize(CurrentWidth, CurrentHeight);

glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);

if(WindowHandle < 1) {
fprintf(
stderr,
"ERROR: Could not create a new rendering window.\n"
);
exit(EXIT_FAILURE);
}

glutReshapeFunc(ResizeFunction);
glutDisplayFunc(RenderFunction);
glutIdleFunc(IdleFunction);
glutTimerFunc(0, TimerFunction, 0);
glutCloseFunc(DestroyCube);
}

void ResizeFunction(int Width, int Height)
{
CurrentWidth = Width;
CurrentHeight = Height;
glViewport(0, 0, CurrentWidth, CurrentHeight);
ProjectionMatrix =
CreateProjectionMatrix(
60,
(float)CurrentWidth / CurrentHeight,
1.0f,
100.0f
);

glUseProgram(ShaderIds[0]);
glUniformMatrix4fv(ProjectionMatrixUniformLocation, 1, GL_FALSE, ProjectionMatrix.m);
glUseProgram(0);
}

void RenderFunction(void)
{
++FrameCount;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

DrawCube();

glutSwapBuffers();
glutPostRedisplay();
}

void IdleFunction(void)
{
glutPostRedisplay();
}

void TimerFunction(int Value)
{
if (0 != Value) {
char* TempString = (char*)
malloc(512 + strlen(WINDOW_TITLE_PREFIX));

sprintf(
TempString,
"%s: %d Frames Per Second @ %d x %d",
WINDOW_TITLE_PREFIX,
FrameCount * 4,
CurrentWidth,
CurrentHeight
);

glutSetWindowTitle(TempString);
free(TempString);
}

FrameCount = 0;
glutTimerFunc(250, TimerFunction, 1);
}

void CreateCube()
{
const Vertex VERTICES[8] =
{
{ { -.5f, -.5f, .5f, 1 }, { 0, 0, 1, 1 } },
{ { -.5f, .5f, .5f, 1 }, { 1, 0, 0, 1 } },
{ { .5f, .5f, .5f, 1 }, { 0, 1, 0, 1 } },
{ { .5f, -.5f, .5f, 1 }, { 1, 1, 0, 1 } },
{ { -.5f, -.5f, -.5f, 1 }, { 1, 1, 1, 1 } },
{ { -.5f, .5f, -.5f, 1 }, { 1, 0, 0, 1 } },
{ { .5f, .5f, -.5f, 1 }, { 1, 0, 1, 1 } },
{ { .5f, -.5f, -.5f, 1 }, { 0, 0, 1, 1 } }
};

const GLuint INDICES[36] =
{
0,2,1, 0,3,2,
4,3,0, 4,7,3,
4,1,5, 4,0,1,
3,6,2, 3,7,6,
1,6,5, 1,2,6,
7,5,6, 7,4,5
};

ShaderIds[0] = glCreateProgram();
ExitOnGLError("ERROR: Could not create the shader program");
{
ShaderIds[1] = LoadShader("SimpleShader.fragment.glsl", GL_FRAGMENT_SHADER);
ShaderIds[2] = LoadShader("SimpleShader.vertex.glsl", GL_VERTEX_SHADER);
glAttachShader(ShaderIds[0], ShaderIds[1]);
glAttachShader(ShaderIds[0], ShaderIds[2]);
}
glLinkProgram(ShaderIds[0]);
ExitOnGLError("ERROR: Could not link the shader program");

ModelMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ModelMatrix");
ViewMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ViewMatrix");
ProjectionMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ProjectionMatrix");
ExitOnGLError("ERROR: Could not get shader uniform locations");

glGenVertexArrays(1, &BufferIds[0]);
ExitOnGLError("ERROR: Could not generate the VAO");
glBindVertexArray(BufferIds[0]);
ExitOnGLError("ERROR: Could not bind the VAO");

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
ExitOnGLError("ERROR: Could not enable vertex attributes");

glGenBuffers(2, &BufferIds[1]);
ExitOnGLError("ERROR: Could not generate the buffer objects");

glBindBuffer(GL_ARRAY_BUFFER, BufferIds[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind the VBO to the VAO");

glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)sizeof(VERTICES[0].Position));
ExitOnGLError("ERROR: Could not set VAO attributes");

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferIds[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(INDICES), INDICES, GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind the IBO to the VAO");

glBindVertexArray(0);
}

void DestroyCube()
{
glDetachShader(ShaderIds[0], ShaderIds[1]);
glDetachShader(ShaderIds[0], ShaderIds[2]);
glDeleteShader(ShaderIds[1]);
glDeleteShader(ShaderIds[2]);
glDeleteProgram(ShaderIds[0]);
ExitOnGLError("ERROR: Could not destroy the shaders");

glDeleteBuffers(2, &BufferIds[1]);
glDeleteVertexArrays(1, &BufferIds[0]);
ExitOnGLError("ERROR: Could not destroy the buffer objects");
}

void DrawCube(void)
{
float CubeAngle;
clock_t Now = clock();

if (LastTime == 0)
LastTime = Now;

CubeRotation += 45.0f * ((float)(Now - LastTime) / CLOCKS_PER_SEC);
CubeAngle = DegreesToRadians(CubeRotation);
LastTime = Now;

ModelMatrix = IDENTITY_MATRIX;
RotateAboutY(&ModelMatrix, CubeAngle);
RotateAboutX(&ModelMatrix, CubeAngle);

glUseProgram(ShaderIds[0]);
ExitOnGLError("ERROR: Could not use the shader program");

glUniformMatrix4fv(ModelMatrixUniformLocation, 1, GL_FALSE, ModelMatrix.m);
glUniformMatrix4fv(ViewMatrixUniformLocation, 1, GL_FALSE, ViewMatrix.m);
ExitOnGLError("ERROR: Could not set the shader uniforms");

glBindVertexArray(BufferIds[0]);
ExitOnGLError("ERROR: Could not bind the VAO for drawing purposes");

glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (GLvoid*)0);
ExitOnGLError("ERROR: Could not draw the cube");

glBindVertexArray(0);
glUseProgram(0);
}[/code]
[/quote]

Share this post


Link to post
Share on other sites
Zomgbie    238
Hello again folks, thank you for the fast answers!

I'm not getting any error outputs at all. The thing that "doesn't work" is that the screen is entierly black. The nice little cube is missing.

Share this post


Link to post
Share on other sites
karwosts    840
Can you post your shaders as well? For one thing its not good to use hardcoded indexes (0/1) for your attribs. You should query the attribute indexes for your shader inputs via glGetAttribLocation, then use the returned value to index it.

I'm not sure you can just assume you can always assign 0 to vertex position unless you're using deprecated shader builtin variables, which I don't know if you are or not.

Share this post


Link to post
Share on other sites
Zomgbie    238
I'm using the same shaders as the example that works:

Vertex:
[code]

#version 400

layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
out vec4 ex_Color;

uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;

void main(void)
{
gl_Position = (ProjectionMatrix * ViewMatrix * ModelMatrix) * in_Position;
ex_Color = in_Color;
}
[/code]

Fragment:
[code]

#version 400

in vec4 ex_Color;
out vec4 out_Color;

void main(void)
{
out_Color = ex_Color;
}
[/code]

Share this post


Link to post
Share on other sites
Paradoxia    116
You changed the casing in your call to glGetUniformLocation to lower camel case ([color=#660066][font=CourierNew, monospace][size=2]modelMatrix[/size][/font][/color]), while the names of the uniforms in the shaders remain upper camel case ([color=#660066][font=CourierNew, monospace][size=2]ModelMatrix[/size][/font][/color]), that may be why it's not working. Let me know.

Share this post


Link to post
Share on other sites
BytePtr    106
I even didn't know that such website exists.
Will there be more than 4 chapters?

Or that's all?
Big .com website for only 4 chapters is pretty expensive.


But if you're going to expand it, then it's good.

Share this post


Link to post
Share on other sites
Zomgbie    238
The OpenGLBook.com website is awesome. I've gotten a much better understanding of shaders in general thanks to the help i've gotten from it.

I'd love to see more chapters! :)

Share this post


Link to post
Share on other sites
Paradoxia    116
[font="arial, verdana, tahoma, sans-serif"][size="2"]More chapters are under way, just not sure when I'll be releasing them yet. I've been busy with other projects lately and haven't had much time for this or other hobbies. I'm sorry to have kept you and others waiting, I hope to be getting back on track one day soon.

[quote name='BytePtr'][color="#1C2837"][size="2"]Big .com website for only 4 chapters is pretty expensive.[/quote][/size][/color]
[color="#1C2837"][size="2"]"Expensive" is a relative term, and I would like to point out that IMHO the length and breadth of the chapters is a bit more than your regular OpenGL"triangle" tutorial. I could split the 4 chapters up into 8 or more smaller tutorials if that justifies the existence of the site more in your eyes. ;)[/size][/color][/size][/font][size="2"][color="#1c2837"]
[/color][/size]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now