Jump to content

  • Log In with Google      Sign In   
  • Create Account


[RESOLVED]OpenGL simple triangle -> Black screen but no errors


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 rXpSwiss   Members   -  Reputation: 167

Like
0Likes
Like

Posted 16 July 2014 - 01:58 PM

Hello,

 

I'm learning OpenGL at the moment and I setup everything and it works (I can get the version code etc from my GC).

Now I'm trying to make a simple triangle with the help of the 8th edition of the red book but I get a black screen and no error.

 

I understand what I do but I might have done something in the wrong order or ...

 

This is the complete code :

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <GL\glew.h>
#include <SDL/SDL.h>
#include <windows.h>
#define PROGRAM_NAME "SDL2 OpenGL4 Example"

GLuint vboHandles[2];
GLuint vaoHandle;
GLuint programHandle;

//this enables the nvidia GC on optimus computer
//dont forget to import windows.h
extern "C" {
	_declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
}

GLint createBasicShader();
GLint createFragShader();
void detVersion();
GLint glVerifyStatus(GLint,char*);

const char* loadShaderAsString(const char* file)
{
	std::ifstream shader_file(file, std::ifstream::in);
	std::string str((std::istreambuf_iterator<char>(shader_file)), std::istreambuf_iterator<char>());
	GLchar *data = (GLchar*)malloc((1+str.size())*sizeof(GLchar));
	strcpy_s(data, str.size()+1, str.c_str());
	return data;
}

void draw();

void init();

int main(int argc, char** argv)
{
	SDL_Window* sdl2_window = 0;
	SDL_GLContext opengl3_context;

	SDL_Init(SDL_INIT_VIDEO);
	// turn on double buffering set the depth buffer to 24 bits
	// 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);\

	// create the sdl2 window
	sdl2_window = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED,
		SDL_WINDOWPOS_CENTERED, 512, 512,
		SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);

	// create the opengl3 context
	opengl3_context = SDL_GL_CreateContext(sdl2_window);
	SDL_GL_MakeCurrent(sdl2_window, opengl3_context);

	// set the opengl context version
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 44);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);

	//glewExperimental = GL_TRUE;
	GLenum status = glewInit();
	if (status != GLEW_OK)
	{
		std::cerr << "GLEW Error: " << glewGetErrorString(status) << "\n";
		exit(1);
	}

	// sync buffer swap with monitor's vertical refresh rate
	SDL_GL_SetSwapInterval(1);

	init();

	draw();

	while (true)
	{
		int status = 0;

		glClear(GL_COLOR_BUFFER_BIT);

		SDL_GL_SwapWindow(sdl2_window);

		SDL_Event event;

		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
			case SDL_KEYDOWN:
				if (event.key.keysym.sym == SDLK_ESCAPE)
					status = 1;
				break;
			case SDL_KEYUP:
				if (event.key.keysym.sym == SDLK_w)
					detVersion();
				break;
			case SDL_QUIT:
				status = 1;
				break;
			}
		}

		if (status == 1) // if received instruction to quit
			break;
	}
	// delete opengl3 context, destroy sdl2 window, and shut down sdl subsystems
	SDL_GL_DeleteContext(opengl3_context);
	SDL_DestroyWindow(sdl2_window);
	SDL_Quit();

	return 0;
}

void init(){
	//---------------------------- SHADERS
	GLint vertShader = createBasicShader();
	GLint fragShader = createFragShader();

	programHandle = glCreateProgram();
	if (0 == programHandle)
	{
		fprintf(stderr, "Error creating program object.\n");
		exit(1);
	}
	glAttachShader(programHandle, vertShader);
	glAttachShader(programHandle, fragShader);

	glLinkProgram(programHandle);
	if (!glVerifyStatus(programHandle, "link program"))
		glUseProgram(programHandle);

	//---------------------------- VERTEX

	// A single triangle
	static const GLfloat vertexPositions[] =
	{
		0.0f, 0.5f, 0.0f,
		0.5f, -0.5f, 0.0f,
		-0.5f, -0.5f, 0.0f
	};

	// Color for each vertex
	static const GLfloat vertexColors[] =
	{
		1.0f, 0.0f, 0.0f, 
		0.0f, 1.0f, 0.0f,
		0.0f, 0.0f, 1.0f
	};

	//setup element array buffer
	glGenBuffers(2, vboHandles);
	glBindBuffer(GL_ARRAY_BUFFER, vboHandles[0]);
	glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), vertexPositions, GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, vboHandles[1]);
	glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), vertexColors, GL_STATIC_DRAW);

	glGenVertexArrays(1, &vaoHandle);
	glBindVertexArray(vaoHandle);

	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);

	glBindBuffer(GL_ARRAY_BUFFER, vboHandles[0]);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);

	glBindBuffer(GL_ARRAY_BUFFER, vboHandles[1]);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
}

void draw(){
	// Set up for a glDrawElements call
	glClear(GL_COLOR_BUFFER_BIT);
	glBindVertexArray(vaoHandle);
	glDrawArrays(GL_TRIANGLES, 0, 3);
	glFlush();
}

GLint createBasicShader(){
	GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
	if (0 == vertShader)
	{
		fprintf(stderr, "Error creating vertex shader.\n");
		exit(1);
	}
	const GLchar * shaderCode = loadShaderAsString("shaders/basic.vert");
	const GLchar* codeArray[] = { shaderCode };
	GLint size = strlen(shaderCode);
	glShaderSource(vertShader, 1, codeArray, &size);
	glCompileShader(vertShader);
	if (glVerifyStatus(vertShader, "make vertex shader"))
	{
		//do something
	}
	return vertShader;
}

GLint createFragShader(){
	GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
	if (0 == fragShader)
	{
		fprintf(stderr, "Error creating vertex shader.\n");
		exit(1);
	}
	const GLchar * shaderCode = loadShaderAsString("shaders/basic.frag");
	const GLchar* codeArray[] = { shaderCode };
	GLint size = strlen(shaderCode);
	glShaderSource(fragShader, 1, codeArray, &size);
	glCompileShader(fragShader);
	if (glVerifyStatus(fragShader, "make fragment shader"))
	{
		//do something
	}
	return fragShader;

}

GLint glVerifyStatus(GLint toVerify, char* what){
	GLint status;
	glGetProgramiv(toVerify, GL_LINK_STATUS, &status);
	if (GL_FALSE == status) {
		std::cerr << "Failed to :" << "\"" << what << "\"" << std::endl;
		GLint logLen;
		glGetProgramiv(toVerify, GL_INFO_LOG_LENGTH,
			&logLen);
		if (logLen > 0)
		{
			char * log = (char *)malloc(logLen);
			GLsizei written;
			glGetProgramInfoLog(toVerify, logLen,
				&written, log);
			std::cerr << "Log :" << log << std::endl;
			free(log);
		}
	}
	return status;
}

void detVersion(){
	const GLubyte *renderer = glGetString(GL_RENDERER);
	const GLubyte *vendor = glGetString(GL_VENDOR);
	const GLubyte *version = glGetString(GL_VERSION);
	const GLubyte *glslVersion =
		glGetString(GL_SHADING_LANGUAGE_VERSION);
	GLint major, minor;
	glGetIntegerv(GL_MAJOR_VERSION, &major);
	glGetIntegerv(GL_MINOR_VERSION, &minor);
	printf("GL Vendor : %s\n", vendor);
	printf("GL Renderer : %s\n", renderer);
	printf("GL Version (string) : %s\n", version);
	printf("GL Version (integer) : %d.%d\n", major, minor);
	printf("GLSL Version : %s\n", glslVersion);
}

basic.vert

#version 430 core
layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec3 VertexColor;
out vec3 Color;
void main()
{
	Color = VertexColor;
	gl_Position = vec4( VertexPosition, 1.0 );
} 

basic.frag

#version 430 core
layout(location = 0) in vec3 Color;
out vec4 FragColor;
void main() {
	FragColor = vec4(Color, 1.0);
}

Can anyone help me ?

 

rXp

 

 

---------------------------------------------------------------------------

Answer : My draw was not in the main loop and I had a clear in the main loop so....


Edited by rXpSwiss, 16 July 2014 - 02:32 PM.


Sponsor:

#2 slicer4ever   Crossbones+   -  Reputation: 3703

Like
3Likes
Like

Posted 16 July 2014 - 02:30 PM


int main(int argc, char** argv)
{

        ...
	draw();//this is outside the loop.

	while (true)
	{
		int status = 0;

                /*
		glClear(GL_COLOR_BUFFER_BIT);*/
                draw(); //now it's inside the loop!

		SDL_GL_SwapWindow(sdl2_window);

		SDL_Event event;

		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
			case SDL_KEYDOWN:
				if (event.key.keysym.sym == SDLK_ESCAPE)
					status = 1;
				break;
			case SDL_KEYUP:
				if (event.key.keysym.sym == SDLK_w)
					detVersion();
				break;
			case SDL_QUIT:
				status = 1;
				break;
			}
		}

		if (status == 1) // if received instruction to quit
			break;
	}
	// delete opengl3 context, destroy sdl2 window, and shut down sdl subsystems
	SDL_GL_DeleteContext(opengl3_context);
	SDL_DestroyWindow(sdl2_window);
	SDL_Quit();

	return 0;
}
move draw from outside your while loop, into your while loop. right now you draw the triangle once, clear the screen, then swap the buffers. this effectly means you never draw anything.

Edited by slicer4ever, 16 July 2014 - 02:30 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#3 rXpSwiss   Members   -  Reputation: 167

Like
0Likes
Like

Posted 16 July 2014 - 02:32 PM

Thank you slicer, I must be tired. You saved me a lot of code-gazing






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS